diff --git a/CHANGELOG.md b/CHANGELOG.md index f4d25b1f6a4..9b66ef0eb4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Use `c.FullPath()` method to set `http.route` attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#5734) - The double setup in `go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace/example` that caused duplicate traces. (#5564) - Out-of-bounds panic in case of invalid span ID in `go.opentelemetry.io/contrib/propagators/b3`. (#5754) +- Do not panic if a zero-value `SpanProcessor` is used from `go.opentelemetry.io/contrib/processors/baggage/baggagetrace`. (#5811) ### Deprecated diff --git a/processors/baggage/baggagetrace/processor.go b/processors/baggage/baggagetrace/processor.go index 0542907df93..c899e7bda6e 100644 --- a/processors/baggage/baggagetrace/processor.go +++ b/processors/baggage/baggagetrace/processor.go @@ -31,6 +31,8 @@ var _ trace.SpanProcessor = (*SpanProcessor)(nil) // The Baggage span processor duplicates onto a span the attributes found // in Baggage in the parent context at the moment the span is started. // The passed filter determines which baggage members are added to the span. +// +// If filter is nil, all baggage members will be added. func New(filter Filter) *SpanProcessor { return &SpanProcessor{ filter: filter, @@ -39,8 +41,13 @@ func New(filter Filter) *SpanProcessor { // OnStart is called when a span is started and adds span attributes for baggage contents. func (processor SpanProcessor) OnStart(ctx context.Context, span trace.ReadWriteSpan) { + filter := processor.filter + if filter == nil { + filter = AllowAllBaggageKeys + } + for _, entry := range baggage.FromContext(ctx).Members() { - if processor.filter(entry.Key()) { + if filter(entry.Key()) { span.SetAttributes(attribute.String(entry.Key(), entry.Value())) } } diff --git a/processors/baggage/baggagetrace/processor_test.go b/processors/baggage/baggagetrace/processor_test.go index f16feb7dcac..f39e6d4f771 100644 --- a/processors/baggage/baggagetrace/processor_test.go +++ b/processors/baggage/baggagetrace/processor_test.go @@ -9,11 +9,13 @@ import ( "strings" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/baggage" "go.opentelemetry.io/otel/sdk/trace" + "go.opentelemetry.io/otel/sdk/trace/tracetest" ) var _ trace.SpanExporter = &testExporter{} @@ -151,3 +153,28 @@ func addEntryToBaggage(t *testing.T, b baggage.Baggage, key, value string) bagga require.NoError(t, err) return b } + +func TestZeroSpanProcessorNoPanic(t *testing.T) { + sp := new(SpanProcessor) + + m, err := baggage.NewMember("key", "val") + require.NoError(t, err) + b, err := baggage.New(m) + require.NoError(t, err) + + ctx := baggage.ContextWithBaggage(context.Background(), b) + roS := (tracetest.SpanStub{}).Snapshot() + rwS := rwSpan{} + assert.NotPanics(t, func() { + sp.OnStart(ctx, rwS) + sp.OnEnd(roS) + _ = sp.ForceFlush(ctx) + _ = sp.Shutdown(ctx) + }) +} + +type rwSpan struct { + trace.ReadWriteSpan +} + +func (s rwSpan) SetAttributes(kv ...attribute.KeyValue) {}