Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix baggage not work in trace demo app. #9418

Merged
merged 11 commits into from
May 5, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion examples/demo/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 11 additions & 2 deletions examples/demo/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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")
Expand Down