Skip to content

Commit 7baf19f

Browse files
committed
enhance productcatalogservice tracing support: add attributes to existing spans (open-telemetry#143)
1 parent 2fc136f commit 7baf19f

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/productcatalogservice/server.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ import (
3535
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
3636

3737
"go.opentelemetry.io/otel"
38+
"go.opentelemetry.io/otel/attribute"
39+
otelcodes "go.opentelemetry.io/otel/codes"
3840
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
3941
"go.opentelemetry.io/otel/propagation"
4042
sdktrace "go.opentelemetry.io/otel/sdk/trace"
43+
"go.opentelemetry.io/otel/trace"
4144

4245
"google.golang.org/protobuf/encoding/protojson"
4346

@@ -200,26 +203,50 @@ func (p *productCatalog) Watch(req *healthpb.HealthCheckRequest, ws healthpb.Hea
200203
return status.Errorf(codes.Unimplemented, "health check via Watch not implemented")
201204
}
202205

203-
func (p *productCatalog) ListProducts(context.Context, *pb.Empty) (*pb.ListProductsResponse, error) {
206+
func (p *productCatalog) ListProducts(ctx context.Context, req *pb.Empty) (*pb.ListProductsResponse, error) {
207+
span := trace.SpanFromContext(ctx)
208+
204209
time.Sleep(extraLatency)
205-
return &pb.ListProductsResponse{Products: parseCatalog()}, nil
210+
var ps []*pb.Product
211+
ps = parseCatalog()
212+
213+
span.SetAttributes(
214+
attribute.Int("app.products.count", len(ps)),
215+
)
216+
return &pb.ListProductsResponse{Products: ps}, nil
206217
}
207218

208219
func (p *productCatalog) GetProduct(ctx context.Context, req *pb.GetProductRequest) (*pb.Product, error) {
220+
span := trace.SpanFromContext(ctx)
221+
span.SetAttributes(
222+
attribute.String("app.product.id", req.Id),
223+
)
224+
209225
time.Sleep(extraLatency)
210226
var found *pb.Product
211227
for i := 0; i < len(parseCatalog()); i++ {
212228
if req.Id == parseCatalog()[i].Id {
213229
found = parseCatalog()[i]
214230
}
215231
}
232+
216233
if found == nil {
217-
return nil, status.Errorf(codes.NotFound, "no product with ID %s", req.Id)
234+
msg := fmt.Sprintf("no product with ID %s", req.Id)
235+
span.SetStatus(otelcodes.Error, msg)
236+
span.AddEvent(msg)
237+
return nil, status.Errorf(codes.NotFound, msg)
238+
} else {
239+
msg := fmt.Sprintf("found product with ID %s, name %s", req.Id, found.Name)
240+
span.AddEvent(msg)
241+
span.SetAttributes(
242+
attribute.String("app.product.name", found.Name),
243+
)
218244
}
219245
return found, nil
220246
}
221247

222248
func (p *productCatalog) SearchProducts(ctx context.Context, req *pb.SearchProductsRequest) (*pb.SearchProductsResponse, error) {
249+
span := trace.SpanFromContext(ctx)
223250
time.Sleep(extraLatency)
224251
// Intepret query as a substring match in name or description.
225252
var ps []*pb.Product
@@ -229,5 +256,8 @@ func (p *productCatalog) SearchProducts(ctx context.Context, req *pb.SearchProdu
229256
ps = append(ps, p)
230257
}
231258
}
259+
span.SetAttributes(
260+
attribute.Int("app.products.count", len(ps)),
261+
)
232262
return &pb.SearchProductsResponse{Results: ps}, nil
233263
}

0 commit comments

Comments
 (0)