diff --git a/pkg/server/middleware/logging.go b/pkg/server/middleware/logging.go index 2828d778..d5c5e197 100644 --- a/pkg/server/middleware/logging.go +++ b/pkg/server/middleware/logging.go @@ -23,6 +23,7 @@ import ( "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/propagation" sdktrace "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.18.0" @@ -125,18 +126,18 @@ func (*LoggingSpanProcessor) ForceFlush(ctx context.Context) error { // Logger attaches logging context to the request. func Logger() func(next http.Handler) http.Handler { - // TODO: this needs an implmenetation of https://www.w3.org/TR/trace-context/. - // Like everything here, OpenTelemetry is very good at doing nothing by default. - propagator := otel.GetTextMapPropagator() - return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // Extract the tracing information from the HTTP headers. See above - // for what this entails. - ctx := propagator.Extract(r.Context(), propagation.HeaderCarrier(r.Header)) - var attributes []attribute.KeyValue + // Extract the tracing information from the HTTP headers. + ctx := otel.GetTextMapPropagator().Extract(r.Context(), propagation.HeaderCarrier(r.Header)) + baggage := baggage.FromContext(ctx) + + for _, member := range baggage.Members() { + attributes = append(attributes, attribute.Key("baggage."+member.Key()).String(member.Value())) + } + // Add in service information. attributes = append(attributes, semconv.ServiceName(constants.Application)) attributes = append(attributes, semconv.ServiceVersion(constants.Version)) diff --git a/pkg/server/server.go b/pkg/server/server.go index a81ae08d..cccaaba6 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/pflag" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp" + "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/sdk/trace" "github.com/unikorn-cloud/unikorn/pkg/server/generated" @@ -68,6 +69,9 @@ func (s *Server) SetupLogging() { func (s *Server) SetupOpenTelemetry(ctx context.Context) error { otel.SetLogger(log.Log) + // TODO: use a full w3c trace context. + otel.SetTextMapPropagator(propagation.Baggage{}) + opts := []trace.TracerProviderOption{ trace.WithSpanProcessor(&middleware.LoggingSpanProcessor{}), }