Skip to content

Commit

Permalink
Merge branch 'main' into no-alias-import
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jun 27, 2024
2 parents 13758a5 + edcddd7 commit 4273bf3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion processors/baggage/baggagetrace/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()))
}
}
Expand Down
27 changes: 27 additions & 0 deletions processors/baggage/baggagetrace/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 := otelbaggage.NewMember("key", "val")
require.NoError(t, err)
b, err := otelbaggage.New(m)
require.NoError(t, err)

ctx := otelbaggage.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) {}

0 comments on commit 4273bf3

Please sign in to comment.