-
Notifications
You must be signed in to change notification settings - Fork 869
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
Optionally add baggage to span attributes #8023
Closed
adamleantech
wants to merge
3
commits into
open-telemetry:main
from
adamleantech:add-baggage-to-spans
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
18 changes: 18 additions & 0 deletions
18
...ent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/AddBaggageSpanProcessor.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,18 @@ | ||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
|
||
public class AddBaggageSpanProcessor implements OnStartSpanProcessor { | ||
@Override | ||
public void onStart(Context context, ReadWriteSpan span) { | ||
Baggage baggage = Baggage.fromContext(context); | ||
baggage.forEach( | ||
(key, value) -> | ||
span.setAttribute( | ||
// add prefix to key to not override existing attributes | ||
"baggage." + key, | ||
value.getValue())); | ||
} | ||
} |
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 | ||||
---|---|---|---|---|---|---|
|
@@ -20,6 +20,7 @@ | |||||
@AutoService(AutoConfigurationCustomizerProvider.class) | ||||||
public class AgentTracerProviderConfigurer implements AutoConfigurationCustomizerProvider { | ||||||
private static final String ADD_THREAD_DETAILS = "otel.javaagent.add-thread-details"; | ||||||
private static final String ADD_BAGGAGE = "otel.javaagent.span.add-baggage"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would correspond with, e.g.
Suggested change
|
||||||
|
||||||
@Override | ||||||
public void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) { | ||||||
|
@@ -38,6 +39,11 @@ private static SdkTracerProviderBuilder configure( | |||||
sdkTracerProviderBuilder.addSpanProcessor(new AddThreadDetailsSpanProcessor()); | ||||||
} | ||||||
|
||||||
// Register baggage adding span processor if config value is set | ||||||
if (config.getBoolean(ADD_BAGGAGE, false)) { | ||||||
sdkTracerProviderBuilder.addSpanProcessor(new AddBaggageSpanProcessor()); | ||||||
} | ||||||
|
||||||
maybeEnableLoggingExporter(sdkTracerProviderBuilder, config); | ||||||
|
||||||
return sdkTracerProviderBuilder; | ||||||
|
30 changes: 30 additions & 0 deletions
30
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/OnStartSpanProcessor.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,30 @@ | ||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
|
||
public interface OnStartSpanProcessor extends SpanProcessor { | ||
@Override | ||
default boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
default void onEnd(ReadableSpan span) {} | ||
|
||
@Override | ||
default boolean isEndRequired() { | ||
return false; | ||
} | ||
|
||
@Override | ||
default CompletableResultCode shutdown() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
default CompletableResultCode forceFlush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...tooling/src/test/java/io/opentelemetry/javaagent/tooling/AddBaggageSpanProcessorTest.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,26 @@ | ||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import static org.mockito.Mockito.verify; | ||
|
||
import io.opentelemetry.api.baggage.Baggage; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AddBaggageSpanProcessorTest { | ||
@Mock ReadWriteSpan readWriteSpan; | ||
|
||
@Test | ||
void shouldAddBaggageToSpanWithPrefixedKeysWhenBaggageIsPopulated() { | ||
try (Scope unused = Baggage.builder().put("someKey", "someValue").build().makeCurrent()) { | ||
new AddBaggageSpanProcessor().onStart(Context.current(), readWriteSpan); | ||
} | ||
|
||
verify(readWriteSpan).setAttribute("baggage.someKey", "someValue"); | ||
} | ||
} |
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.
Given the lack of spec around this topic, it would be good to keep the attribute naming flexible and have the
baggage
namespace prefix come in through the constructor as an optional parameter. The suggested whitelisting of baggage keys to attach would provide the necessary security to prevent unwanted overrides.