Skip to content

Commit

Permalink
cleanup and put the http client in order
Browse files Browse the repository at this point in the history
Signed-off-by: Afzal Ansari <[email protected]>
  • Loading branch information
afzalbin64 authored and afzal442 committed Jul 10, 2023
1 parent a32768e commit ef1eb6b
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 21 deletions.
6 changes: 3 additions & 3 deletions examples/hotrod/pkg/tracing/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ type HTTPClient struct {
Client *http.Client
}

func NewHTTPClient(tp trace.TracerProvider) HTTPClient {
return HTTPClient{
func NewHTTPClient(tp trace.TracerProvider) *HTTPClient {
return &HTTPClient{
TracerProvider: tp,
Client: &http.Client{Transport: &otelhttp.Transport{}},
Client: &http.Client{Transport: otelhttp.NewTransport(http.DefaultTransport, otelhttp.WithTracerProvider(tp))},
}
}

Expand Down
18 changes: 11 additions & 7 deletions examples/hotrod/pkg/tracing/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,29 @@ import (
)

// NewServeMux creates a new TracedServeMux.
func NewServeMux(tracer trace.TracerProvider, logger log.Factory) *TracedServeMux {
func NewServeMux(copyBaggage bool, tracer trace.TracerProvider, logger log.Factory) *TracedServeMux {
return &TracedServeMux{
mux: http.NewServeMux(),
tracer: tracer,
logger: logger,
mux: http.NewServeMux(),
copyBaggage: copyBaggage,
tracer: tracer,
logger: logger,
}
}

// TracedServeMux is a wrapper around http.ServeMux that instruments handlers for tracing.
type TracedServeMux struct {
mux *http.ServeMux
tracer trace.TracerProvider
logger log.Factory
mux *http.ServeMux
copyBaggage bool
tracer trace.TracerProvider
logger log.Factory
}

// Handle implements http.ServeMux#Handle, which is used to register new handler.
func (tm *TracedServeMux) Handle(pattern string, handler http.Handler) {
tm.logger.Bg().Debug("registering traced handler", zap.String("endpoint", pattern))

otelhttp.WithRouteTag(pattern, handler)

middleware := otelhttp.NewHandler(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
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// Client is a remote client that implements customer.Interface
type Client struct {
logger log.Factory
client tracing.HTTPClient
client *tracing.HTTPClient
hostPort string
}

Expand Down
5 changes: 2 additions & 3 deletions examples/hotrod/services/customer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"net/http"

"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand Down Expand Up @@ -58,8 +57,8 @@ func (s *Server) Run() error {
}

func (s *Server) createServeMux() http.Handler {
mux := tracing.NewServeMux(s.tracer, s.logger)
mux.Handle("/customer", otelhttp.WithRouteTag("/customer", http.HandlerFunc(s.customer)))
mux := tracing.NewServeMux(false, s.tracer, s.logger)
mux.Handle("/customer", http.HandlerFunc(s.customer))
return mux
}

Expand Down
4 changes: 0 additions & 4 deletions examples/hotrod/services/frontend/best_eta.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sync"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

Expand Down Expand Up @@ -77,9 +76,6 @@ 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)
span.AddEvent("customer", trace.WithAttributes(attribute.Key(customer.Name).String(customer.Location)))

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/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *Server) Run() error {
}

func (s *Server) createServeMux() http.Handler {
mux := tracing.NewServeMux(s.tracer, s.logger)
mux := tracing.NewServeMux(true, s.tracer, s.logger)
p := path.Join("/", s.basepath)
mux.Handle(p, http.StripPrefix(p, http.FileServer(s.assetFS)))
mux.Handle(path.Join(p, "/dispatch"), http.HandlerFunc(s.dispatch))
Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/services/route/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
// Client is a remote client that implements route.Interface
type Client struct {
logger log.Factory
client tracing.HTTPClient
client *tracing.HTTPClient
hostPort string
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hotrod/services/route/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (s *Server) Run() error {
}

func (s *Server) createServeMux() http.Handler {
mux := tracing.NewServeMux(s.tracer, s.logger)
mux := tracing.NewServeMux(false, s.tracer, s.logger)
mux.Handle("/route", http.HandlerFunc(s.route))
mux.Handle("/debug/vars", expvar.Handler()) // expvar
mux.Handle("/metrics", promhttp.Handler()) // Prometheus
Expand Down

0 comments on commit ef1eb6b

Please sign in to comment.