From 3baa22ac48805020aaaed7142c830b6b191464f3 Mon Sep 17 00:00:00 2001 From: Afzal Ansari Date: Mon, 10 Jul 2023 15:48:55 +0000 Subject: [PATCH] minor fixes and adds otel baggage syntax Signed-off-by: Afzal Ansari --- examples/hotrod/pkg/tracing/http.go | 9 +++++++-- examples/hotrod/pkg/tracing/mux.go | 7 ++++--- examples/hotrod/services/customer/client.go | 2 +- examples/hotrod/services/frontend/best_eta.go | 7 +++++++ examples/hotrod/services/route/client.go | 2 +- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/examples/hotrod/pkg/tracing/http.go b/examples/hotrod/pkg/tracing/http.go index a1bb1b19048..dbea5c95ec8 100644 --- a/examples/hotrod/pkg/tracing/http.go +++ b/examples/hotrod/pkg/tracing/http.go @@ -35,13 +35,18 @@ type HTTPClient struct { func NewHTTPClient(tp trace.TracerProvider) *HTTPClient { return &HTTPClient{ TracerProvider: tp, - Client: &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport, otelhttp.WithTracerProvider(tp))}, + Client: &http.Client{ + Transport: otelhttp.NewTransport( + http.DefaultTransport, + otelhttp.WithTracerProvider(tp), + ), + }, } } // GetJSON executes HTTP GET against specified url and tried to parse // the response into out object. -func (c *HTTPClient) GetJSON(ctx context.Context, url string, out interface{}) error { +func (c *HTTPClient) GetJSON(ctx context.Context, endpoint string, url string, out interface{}) error { req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return err diff --git a/examples/hotrod/pkg/tracing/mux.go b/examples/hotrod/pkg/tracing/mux.go index 7aaafdeb749..fe8d197a8d9 100644 --- a/examples/hotrod/pkg/tracing/mux.go +++ b/examples/hotrod/pkg/tracing/mux.go @@ -47,10 +47,11 @@ type TracedServeMux struct { func (tm *TracedServeMux) Handle(pattern string, handler http.Handler) { tm.logger.Bg().Debug("registering traced handler", zap.String("endpoint", pattern)) - handler = otelhttp.WithRouteTag(pattern, handler) - - middleware := otelhttp.NewHandler(handler, pattern, + middleware := otelhttp.NewHandler( + otelhttp.WithRouteTag(pattern, handler), + pattern, otelhttp.WithTracerProvider(tm.tracer)) + tm.mux.Handle(pattern, middleware) } diff --git a/examples/hotrod/services/customer/client.go b/examples/hotrod/services/customer/client.go index 8514d0f140f..59787771519 100644 --- a/examples/hotrod/services/customer/client.go +++ b/examples/hotrod/services/customer/client.go @@ -48,7 +48,7 @@ func (c *Client) Get(ctx context.Context, customerID string) (*Customer, error) url := fmt.Sprintf("http://"+c.hostPort+"/customer?customer=%s", customerID) var customer Customer - if err := c.client.GetJSON(ctx, url, &customer); err != nil { + if err := c.client.GetJSON(ctx, "/customer", url, &customer); err != nil { return nil, err } return &customer, nil diff --git a/examples/hotrod/services/frontend/best_eta.go b/examples/hotrod/services/frontend/best_eta.go index 27924b6810c..8161d450795 100644 --- a/examples/hotrod/services/frontend/best_eta.go +++ b/examples/hotrod/services/frontend/best_eta.go @@ -22,6 +22,8 @@ import ( "sync" "time" + "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/trace" "go.uber.org/zap" @@ -76,6 +78,11 @@ func (eta *bestETA) Get(ctx context.Context, customerID string) (*Response, erro } eta.logger.For(ctx).Info("Found customer", zap.Any("customer", customer)) + span := trace.SpanFromContext(ctx) + bag := baggage.FromContext(ctx) + m := bag.Member(customer.Name) + span.SetAttributes(attribute.Key(m.Key()).String(m.Value())) + drivers, err := eta.driver.FindNearest(ctx, customer.Location) if err != nil { return nil, err diff --git a/examples/hotrod/services/route/client.go b/examples/hotrod/services/route/client.go index 9448c21cfe6..7c58f76e1dc 100644 --- a/examples/hotrod/services/route/client.go +++ b/examples/hotrod/services/route/client.go @@ -51,7 +51,7 @@ func (c *Client) FindRoute(ctx context.Context, pickup, dropoff string) (*Route, v.Set("dropoff", dropoff) url := "http://" + c.hostPort + "/route?" + v.Encode() var route Route - if err := c.client.GetJSON(ctx, url, &route); err != nil { + if err := c.client.GetJSON(ctx, "/route", url, &route); err != nil { c.logger.For(ctx).Error("Error getting route", zap.Error(err)) return nil, err }