Skip to content

Commit

Permalink
fix baggage not work in trace demo app. (open-telemetry#9418)
Browse files Browse the repository at this point in the history
* fix Baggage not work in demo app.

Signed-off-by: jian.tan <[email protected]>

Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
JaredTan95 and Alex Boten committed May 5, 2022
1 parent 910f9c4 commit 65bfcd0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
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

0 comments on commit 65bfcd0

Please sign in to comment.