Skip to content

Commit

Permalink
minor fixes and adds otel baggage syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Afzal Ansari <afzal442@gmail.com>
  • Loading branch information
afzal442 committed Jul 10, 2023
1 parent 37357a5 commit 3baa22a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
9 changes: 7 additions & 2 deletions examples/hotrod/pkg/tracing/http.go
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions examples/hotrod/pkg/tracing/mux.go
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/services/customer/client.go
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions examples/hotrod/services/frontend/best_eta.go
Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/services/route/client.go
Expand Up @@ -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
}
Expand Down

0 comments on commit 3baa22a

Please sign in to comment.