-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* this shows how to inject baggage attributes to a span Co-authored-by: MarianaSequeira <mfs98@live.com.pt>
- Loading branch information
1 parent
7c3a412
commit 1f0afe4
Showing
3 changed files
with
49 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
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
46 changes: 46 additions & 0 deletions
46
examples/extension/src/main/java/com/example/javaagent/DemoBaggageSpanProcessor.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,46 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.example.javaagent; | ||
|
||
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; | ||
|
||
/** | ||
* Custom Span Processor to inject Baggage attributes to a Span. | ||
* This allows to use the Baggage to hold some custom values and inject them automatically into the child Spans. | ||
* <p> | ||
* See <a | ||
* href="https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/sdk.md#span-processor"> | ||
* OpenTelemetry Specification</a> for more information about {@link SpanProcessor}. | ||
* | ||
* @see DemoAutoConfigurationCustomizerProvider | ||
*/ | ||
public class DemoBaggageSpanProcessor implements SpanProcessor { | ||
|
||
@Override | ||
public void onStart(Context parentContext, ReadWriteSpan span) { | ||
Baggage.fromContext(parentContext) | ||
.forEach((s, baggageEntry) -> span.setAttribute(s, baggageEntry.getValue())); | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) { | ||
// Do nothing on span end | ||
} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return false; | ||
} | ||
} |