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.
baggagetrace: Add baggage key predicate (open-telemetry#5619)
## Which problem is this PR solving? - Closes open-telemetry#5437 ## Short description of the changes - Add `BaggageKeyPredicate` type and `AllowAllBaggageKeys` implementation that allows all keys - Add baggage key predicate as constructor parameter to processor - Add unit tests to verify behaviour for prefix and regex - Update README with examples of setting a filter --------- Co-authored-by: Alex Boten <223565+codeboten@users.noreply.github.com>
- Loading branch information
1 parent
0430e80
commit 85969a3
Showing
5 changed files
with
189 additions
and
18 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
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 |
---|---|---|
@@ -1,5 +1,27 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package baggagetrace implements a baggage span processor. | ||
// Package baggagetrace is an OpenTelemetry [Span Processor] that reads key/values | ||
// stored in [Baggage] in the starting span's parent context and adds them as | ||
// attributes to the span. | ||
// | ||
// Keys and values added to Baggage will appear on all subsequent child spans for | ||
// a trace within this service and will be propagated to external services via | ||
// propagation headers. | ||
// If the external services also have a Baggage span processor, the keys and | ||
// values will appear in those child spans as well. | ||
// | ||
// Do not put sensitive information in Baggage. | ||
// | ||
// # Usage | ||
// | ||
// Add the span processor when configuring the tracer provider. | ||
// | ||
// The convenience function [AllowAllBaggageKeys] is provided to | ||
// allow all baggage keys to be copied to the span. Alternatively, you can | ||
// provide a custom baggage key predicate to select which baggage keys you want | ||
// to copy. | ||
// | ||
// [Span Processor]: https://opentelemetry.io/docs/specs/otel/trace/sdk/#span-processor | ||
// [Baggage]: https://opentelemetry.io/docs/specs/otel/api/baggage | ||
package baggagetrace // import "go.opentelemetry.io/contrib/processors/baggage/baggagetrace" |
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,43 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package baggagetrace_test | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"go.opentelemetry.io/contrib/processors/baggage/baggagetrace" | ||
"go.opentelemetry.io/otel/sdk/trace" | ||
) | ||
|
||
func ExampleNew_allKeys() { | ||
trace.NewTracerProvider( | ||
trace.WithSpanProcessor(baggagetrace.New(baggagetrace.AllowAllBaggageKeys)), | ||
) | ||
} | ||
|
||
func ExampleNew_keysWithPrefix() { | ||
trace.NewTracerProvider( | ||
trace.WithSpanProcessor( | ||
baggagetrace.New( | ||
func(baggageKey string) bool { | ||
return strings.HasPrefix(baggageKey, "my-key") | ||
}, | ||
), | ||
), | ||
) | ||
} | ||
|
||
func ExampleNew_keysMatchingRegex() { | ||
expr := regexp.MustCompile(`^key.+`) | ||
trace.NewTracerProvider( | ||
trace.WithSpanProcessor( | ||
baggagetrace.New( | ||
func(baggageKey string) bool { | ||
return expr.MatchString(baggageKey) | ||
}, | ||
), | ||
), | ||
) | ||
} |
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