forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
processors/baggage: add baggage span processor
Fixes open-telemetry#5397 Signed-off-by: Alex Boten <223565+codeboten@users.noreply.github.com>
- Loading branch information
Showing
8 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package baggage implements the baggage span processor which duplicates | ||
// onto a span the attributes found in Baggage in the parent context at | ||
// the moment the span is started. | ||
package baggage // import "go.opentelemetry.io/contrib/processors/baggage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module go.opentelemetry.io/contrib/processors/baggage | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/stretchr/testify v1.9.0 | ||
go.opentelemetry.io/otel v1.25.0 | ||
go.opentelemetry.io/otel/sdk v1.25.0 | ||
) | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/go-logr/logr v1.4.1 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
go.opentelemetry.io/otel/metric v1.25.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.25.0 // indirect | ||
golang.org/x/sys v0.18.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= | ||
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= | ||
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= | ||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= | ||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
go.opentelemetry.io/otel v1.25.0 h1:gldB5FfhRl7OJQbUHt/8s0a7cE8fbsPAtdpRaApKy4k= | ||
go.opentelemetry.io/otel v1.25.0/go.mod h1:Wa2ds5NOXEMkCmUou1WA7ZBfLTHWIsp034OVD7AO+Vg= | ||
go.opentelemetry.io/otel/metric v1.25.0 h1:LUKbS7ArpFL/I2jJHdJcqMGxkRdxpPHE0VU/D4NuEwA= | ||
go.opentelemetry.io/otel/metric v1.25.0/go.mod h1:rkDLUSd2lC5lq2dFNrX9LGAbINP5B7WBkC78RXCpH5s= | ||
go.opentelemetry.io/otel/sdk v1.25.0 h1:PDryEJPC8YJZQSyLY5eqLeafHtG+X7FWnf3aXMtxbqo= | ||
go.opentelemetry.io/otel/sdk v1.25.0/go.mod h1:oFgzCM2zdsxKzz6zwpTZYLLQsFwc+K0daArPdIhuxkw= | ||
go.opentelemetry.io/otel/trace v1.25.0 h1:tqukZGLwQYRIFtSQM2u2+yfMVTgGVeqRLPUYx1Dq6RM= | ||
go.opentelemetry.io/otel/trace v1.25.0/go.mod h1:hCCs70XM/ljO+BeQkyFnbK28SBIJ/Emuha+ccrCRT7I= | ||
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= | ||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package baggage | ||
|
||
import ( | ||
"context" | ||
|
||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/baggage" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
type baggageSpanProcessor struct{} | ||
|
||
var _ trace.SpanProcessor = (*baggageSpanProcessor)(nil) | ||
|
||
// Returns a new baggageSpanProcessor. | ||
// | ||
// The Baggage span processor duplicates onto a span the attributes found | ||
// in Baggage in the parent context at the moment the span is started. | ||
func NewBaggageSpanProcessor() trace.SpanProcessor { | ||
return &baggageSpanProcessor{} | ||
} | ||
|
||
func (processor baggageSpanProcessor) OnStart(ctx context.Context, span trace.ReadWriteSpan) { | ||
baggage := baggage.FromContext(ctx) | ||
for _, entry := range baggage.Members() { | ||
span.SetAttributes(attribute.String(entry.Key(), entry.Value())) | ||
} | ||
} | ||
|
||
func (processor baggageSpanProcessor) OnEnd(s trace.ReadOnlySpan) {} | ||
func (processor baggageSpanProcessor) Shutdown(context.Context) error { return nil } | ||
func (processor baggageSpanProcessor) ForceFlush(context.Context) error { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package baggage | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/baggage" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
var _ trace.SpanExporter = &testExporter{} | ||
|
||
type testExporter struct { | ||
spans []trace.ReadOnlySpan | ||
} | ||
|
||
func (e *testExporter) Start(ctx context.Context) error { return nil } | ||
func (e *testExporter) Shutdown(ctx context.Context) error { return nil } | ||
|
||
func (e *testExporter) ExportSpans(ctx context.Context, ss []trace.ReadOnlySpan) error { | ||
e.spans = append(e.spans, ss...) | ||
return nil | ||
} | ||
|
||
func NewTestExporter() *testExporter { | ||
return &testExporter{} | ||
} | ||
|
||
func TestBaggageSpanProcessorAppendsBaggageAttributes(t *testing.T) { | ||
// create ctx with some baggage | ||
ctx := context.Background() | ||
suitcase := baggage.FromContext(ctx) | ||
packingCube, _ := baggage.NewMember("baggage.test", url.PathEscape("baggage value")) | ||
suitcase, _ = suitcase.SetMember(packingCube) | ||
ctx = baggage.ContextWithBaggage(ctx, suitcase) | ||
|
||
// create trace provider with baggage processor and test exporter | ||
exporter := NewTestExporter() | ||
tp := trace.NewTracerProvider( | ||
trace.WithSpanProcessor(NewBaggageSpanProcessor()), | ||
trace.WithSpanProcessor(trace.NewSimpleSpanProcessor(exporter)), | ||
) | ||
|
||
// create tracer and start/end span | ||
tracer := tp.Tracer("test") | ||
_, span := tracer.Start(ctx, "test") | ||
span.End() | ||
|
||
assert.Equal(t, 1, len(exporter.spans)) | ||
assert.Equal(t, 1, len(exporter.spans[0].Attributes())) | ||
|
||
for _, attr := range exporter.spans[0].Attributes() { | ||
assert.Equal(t, attribute.Key("baggage.test"), attr.Key) | ||
assert.Equal(t, "baggage value", attr.Value.AsString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters