diff --git a/CHANGELOG.md b/CHANGELOG.md index 39a0371d096d..263a66310853 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ - `couchdbreceiver`: Fix issue where the receiver would not respect custom metric settings (#9598) - `nginxreceiver`: Include nginxreceiver in components (#9572) - `pkg/translator/prometheusremotewrite`: Fix data race when used with other exporters (#9736) +- `examples/demo`: fix baggage not work in trace demo app. (#9418) ## v0.50.0 diff --git a/examples/demo/client/main.go b/examples/demo/client/main.go index 0c14d64b2a9d..2ccbfb4ba5de 100644 --- a/examples/demo/client/main.go +++ b/examples/demo/client/main.go @@ -101,7 +101,7 @@ func initProvider() func() { ) // set global propagator to tracecontext (the default is no-op). - otel.SetTextMapPropagator(propagation.TraceContext{}) + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) otel.SetTracerProvider(tracerProvider) return func() { diff --git a/examples/demo/server/main.go b/examples/demo/server/main.go index 7b913e022690..81d00682950a 100644 --- a/examples/demo/server/main.go +++ b/examples/demo/server/main.go @@ -27,6 +27,7 @@ import ( "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" + "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric" "go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc" "go.opentelemetry.io/otel/exporters/otlp/otlptrace" @@ -102,7 +103,7 @@ func initProvider() func() { ) // set global propagator to tracecontext (the default is no-op). - otel.SetTextMapPropagator(propagation.TraceContext{}) + otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{})) otel.SetTracerProvider(tracerProvider) return func() { @@ -156,7 +157,15 @@ func main() { ctx := req.Context() requestCount.Add(ctx, 1, commonLabels...) span := trace.SpanFromContext(ctx) - span.SetAttributes(serverAttribute) + bag := baggage.FromContext(ctx) + + baggageAttributes := []attribute.KeyValue{} + baggageAttributes = append(baggageAttributes, serverAttribute) + for _, member := range bag.Members() { + baggageAttributes = append(baggageAttributes, attribute.String("baggage key:"+member.Key(), member.Value())) + } + span.SetAttributes(baggageAttributes...) + w.Write([]byte("Hello World")) }) wrappedHandler := otelhttp.NewHandler(handler, "/hello")