Skip to content

Commit

Permalink
refactors server mux
Browse files Browse the repository at this point in the history
Signed-off-by: Afzal Ansari <[email protected]>
  • Loading branch information
afzal442 committed Jul 2, 2023
1 parent d84bf83 commit c3849c9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/hotrod/cmd/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var routeCmd = &cobra.Command{
logger := log.NewFactory(zapLogger)
server := route.NewServer(
net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort)),
tracing.Init("route", otelExporter, metricsFactory, logger),
tracing.InitOTEL("route", otelExporter, metricsFactory, logger),
logger,
)
return logError(zapLogger, server.Run())
Expand Down
33 changes: 6 additions & 27 deletions examples/hotrod/pkg/tracing/mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ package tracing
import (
"net/http"

"github.com/opentracing-contrib/go-stdlib/nethttp"
"github.com/opentracing/opentracing-go"
"go.opentelemetry.io/otel/baggage"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/examples/hotrod/pkg/log"
)

// NewServeMux creates a new TracedServeMux.
func NewServeMux(copyBaggage bool, tracer opentracing.Tracer, logger log.Factory) *TracedServeMux {
func NewServeMux(copyBaggage bool, tracer trace.TracerProvider, logger log.Factory) *TracedServeMux {
return &TracedServeMux{
mux: http.NewServeMux(),
copyBaggage: copyBaggage,
Expand All @@ -41,36 +40,16 @@ func NewServeMux(copyBaggage bool, tracer opentracing.Tracer, logger log.Factory
type TracedServeMux struct {
mux *http.ServeMux
copyBaggage bool
tracer opentracing.Tracer
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))

middleware := nethttp.Middleware(
tm.tracer,
handler,
nethttp.OperationNameFunc(func(r *http.Request) string {
return "HTTP " + r.Method + " " + pattern
}),
// Jaeger SDK was able to accept `jaeger-baggage` header even for requests without am active trace.
// OTEL Bridge does not support that, so we use Baggage propagator to manually extract the baggage
// into Context (in otelBaggageExtractor handler below), and once the Bridge creates a Span,
// we use this SpanObserver to copy OTEL baggage from Context into the Span.
nethttp.MWSpanObserver(func(span opentracing.Span, r *http.Request) {
if !tm.copyBaggage {
return
}
bag := baggage.FromContext(r.Context())
for _, m := range bag.Members() {
if b := span.BaggageItem(m.Key()); b == "" {
span.SetBaggageItem(m.Key(), m.Value())
}
}
}),
)
middleware := otelhttp.NewHandler(tm.mux, pattern,
otelhttp.WithTracerProvider(tm.tracer))
tm.mux.Handle(pattern, otelBaggageExtractor(middleware))
}

Expand Down
9 changes: 4 additions & 5 deletions examples/hotrod/services/customer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
// Server implements Customer service
type Server struct {
hostPort string
mux http.ServeMux
tracer trace.TracerProvider
logger log.Factory
database *database
Expand All @@ -55,13 +54,13 @@ func NewServer(hostPort string, otelExporter string, metricsFactory metrics.Fact
func (s *Server) Run() error {
mux := s.createServeMux()
s.logger.Bg().Info("Starting", zap.String("address", "http://"+s.hostPort))
return http.ListenAndServe(s.hostPort, otelhttp.NewHandler(mux, "/customer",
otelhttp.WithTracerProvider(s.tracer)))
return http.ListenAndServe(s.hostPort, mux)
}

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

func (s *Server) customer(w http.ResponseWriter, r *http.Request) {
Expand Down
15 changes: 7 additions & 8 deletions examples/hotrod/services/frontend/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/jaegertracing/jaeger/examples/hotrod/pkg/httperr"
"github.com/jaegertracing/jaeger/examples/hotrod/pkg/log"
"github.com/jaegertracing/jaeger/examples/hotrod/pkg/tracing"
"github.com/jaegertracing/jaeger/pkg/httpfs"
)

Expand All @@ -36,7 +37,6 @@ var assetFS embed.FS
// Server implements jaeger-demo-frontend service
type Server struct {
hostPort string
mux http.ServeMux
tracer trace.TracerProvider
logger log.Factory
bestETA *bestETA
Expand Down Expand Up @@ -73,17 +73,16 @@ func NewServer(options ConfigOptions, tracer trace.TracerProvider, logger log.Fa
func (s *Server) Run() error {
mux := s.createServeMux()
s.logger.Bg().Info("Starting", zap.String("address", "http://"+path.Join(s.hostPort, s.basepath)))
return http.ListenAndServe(s.hostPort, otelhttp.NewHandler(mux, s.basepath,
otelhttp.WithTracerProvider(s.tracer)))
return http.ListenAndServe(s.hostPort, mux)
}

func (s *Server) createServeMux() http.Handler {
// mux := tracing.NewServeMux(true, s.tracer, s.logger)
mux := tracing.NewServeMux(true, s.tracer, s.logger)
p := path.Join("/", s.basepath)
s.mux.Handle(p, otelhttp.WithRouteTag(p, http.StripPrefix(p, http.FileServer(s.assetFS))))
s.mux.Handle(path.Join(p, "/dispatch"), otelhttp.WithRouteTag(path.Join(p, "/dispatch"), http.HandlerFunc(s.dispatch)))
s.mux.Handle(path.Join(p, "/config"), otelhttp.WithRouteTag(path.Join(p, "/config"), http.HandlerFunc(s.config)))
return &s.mux
mux.Handle(p, otelhttp.WithRouteTag(p, http.StripPrefix(p, http.FileServer(s.assetFS))))
mux.Handle(path.Join(p, "/dispatch"), otelhttp.WithRouteTag(path.Join(p, "/dispatch"), http.HandlerFunc(s.dispatch)))
mux.Handle(path.Join(p, "/config"), otelhttp.WithRouteTag(path.Join(p, "/config"), http.HandlerFunc(s.config)))
return mux
}

func (s *Server) config(w http.ResponseWriter, r *http.Request) {
Expand Down
6 changes: 3 additions & 3 deletions examples/hotrod/services/route/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"net/http"
"time"

"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/examples/hotrod/pkg/delay"
Expand All @@ -38,12 +38,12 @@ import (
// Server implements Route service
type Server struct {
hostPort string
tracer opentracing.Tracer
tracer trace.TracerProvider
logger log.Factory
}

// NewServer creates a new route.Server
func NewServer(hostPort string, tracer opentracing.Tracer, logger log.Factory) *Server {
func NewServer(hostPort string, tracer trace.TracerProvider, logger log.Factory) *Server {
return &Server{
hostPort: hostPort,
tracer: tracer,
Expand Down

0 comments on commit c3849c9

Please sign in to comment.