-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add baggage span processor component #1290
Merged
trask
merged 1 commit into
open-telemetry:main
from
MikeGoldsmith:mike/baggage-processor
May 28, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# OpenTelemetry Baggage Span Processor | ||
|
||
The BaggageSpanProcessor reads entries stored in Baggage from the parent context | ||
and adds the baggage keys and values to the `Span` as attributes on start. | ||
|
||
Add this span processor to a tracer provider. | ||
|
||
Warning! | ||
|
||
To repeat: a consequence of adding data to Baggage is that the keys and values | ||
will appear in all outgoing trace context headers from the application. | ||
|
||
Do not put sensitive information in Baggage. | ||
|
||
## Usage | ||
|
||
Add the span processor when configuring the tracer provider. | ||
|
||
To configure the span processor to copy all baggage entries during configuration: | ||
|
||
```java | ||
import io.opentelemetry.contrib.baggage.processor; | ||
// ... | ||
|
||
Tracer tracer = SdkTracerProvider.builder() | ||
.addSpanProcessor(new BaggageSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys)) | ||
.build() | ||
``` | ||
|
||
Alternatively, you can provide a custom baggage key predicate to select which baggage keys you want to copy. | ||
|
||
For example, to only copy baggage entries that start with `my-key`: | ||
|
||
```java | ||
new BaggageSpanProcessor(baggageKey -> baggageKey.startsWith("my-key")) | ||
``` | ||
|
||
For example, to only copy baggage entries that match the regex `^key.+`: | ||
|
||
```java | ||
Pattern pattern = Pattern.compile("^key.+"); | ||
new BaggageSpanProcessor(baggageKey -> pattern.matcher(baggageKey).matches()) | ||
``` | ||
|
||
## Component owners | ||
|
||
- [Mike Golsmith](https://github.com/MikeGoldsmith), Honeycomb | ||
|
||
Learn more about component owners in [component_owners.yml](../.github/component_owners.yml). |
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,15 @@ | ||
plugins { | ||
id("otel.java-conventions") | ||
|
||
id("otel.publish-conventions") | ||
} | ||
|
||
description = "OpenTelemetry Baggage Span Processor" | ||
otelJava.moduleName.set("io.opentelemetry.contrib.baggage.processor") | ||
|
||
dependencies { | ||
api("io.opentelemetry:opentelemetry-api") | ||
api("io.opentelemetry:opentelemetry-sdk") | ||
|
||
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure") | ||
} |
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,2 @@ | ||
# TODO: uncomment when ready to mark as stable | ||
# otel.stable=true |
56 changes: 56 additions & 0 deletions
56
...cessor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessor.java
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,56 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.baggage.processor; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* This span processor copies attributes stored in {@link Baggage} into each newly created {@link | ||
* io.opentelemetry.api.trace.Span}. | ||
*/ | ||
public class BaggageSpanProcessor implements SpanProcessor { | ||
private final Predicate<String> baggageKeyPredicate; | ||
|
||
/** A {@link Predicate} that returns true for all baggage keys. */ | ||
public static final Predicate<String> allowAllBaggageKeys = baggageKey -> true; | ||
|
||
/** | ||
* Creates a new {@link BaggageSpanProcessor} that copies only baggage entries with keys that pass | ||
* the provided filter into the newly created {@link io.opentelemetry.api.trace.Span}. | ||
*/ | ||
public BaggageSpanProcessor(Predicate<String> baggageKeyPredicate) { | ||
this.baggageKeyPredicate = baggageKeyPredicate; | ||
} | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) { | ||
Baggage.fromContext(parentContext) | ||
.forEach( | ||
(s, baggageEntry) -> { | ||
if (baggageKeyPredicate.test(s)) { | ||
span.setAttribute(s, baggageEntry.getValue()); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) {} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return false; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...or/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorTest.java
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,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.baggage.processor; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import java.util.regex.Pattern; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class BaggageSpanProcessorTest { | ||
|
||
@Test | ||
public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) { | ||
try (BaggageSpanProcessor processor = | ||
new BaggageSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys)) { | ||
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) { | ||
processor.onStart(Context.current(), span); | ||
Mockito.verify(span).setAttribute("key", "value"); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_matches( | ||
@Mock ReadWriteSpan span) { | ||
try (BaggageSpanProcessor processor = new BaggageSpanProcessor(key -> key.startsWith("k"))) { | ||
try (Scope ignore = | ||
Baggage.current().toBuilder() | ||
.put("key", "value") | ||
.put("other", "value") | ||
.build() | ||
.makeCurrent()) { | ||
processor.onStart(Context.current(), span); | ||
Mockito.verify(span).setAttribute("key", "value"); | ||
Mockito.verify(span, Mockito.never()).setAttribute("other", "value"); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_matches_regex( | ||
@Mock ReadWriteSpan span) { | ||
Pattern pattern = Pattern.compile("k.*"); | ||
try (BaggageSpanProcessor processor = | ||
new BaggageSpanProcessor(key -> pattern.matcher(key).matches())) { | ||
try (Scope ignore = | ||
Baggage.current().toBuilder() | ||
.put("key", "value") | ||
.put("other", "value") | ||
.build() | ||
.makeCurrent()) { | ||
processor.onStart(Context.current(), span); | ||
Mockito.verify(span).setAttribute("key", "value"); | ||
Mockito.verify(span, Mockito.never()).setAttribute("other", "value"); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+💯