Skip to content

Commit

Permalink
Add demo baggage span processor
Browse files Browse the repository at this point in the history
* this shows how to inject baggage attributes to a span

Co-authored-by: MarianaSequeira <mfs98@live.com.pt>
  • Loading branch information
2 people authored and Rui Venâncio committed Nov 24, 2023
1 parent 7c3a412 commit 533568c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ For more information, see the `extendedAgent` task in [build.gradle](build.gradl
[DemoPropagator]: src/main/java/com/example/javaagent/DemoPropagator.java
[DemoSampler]: src/main/java/com/example/javaagent/DemoSampler.java
[DemoSpanProcessor]: src/main/java/com/example/javaagent/DemoSpanProcessor.java
[DemoBaggageSpanProcessor]: src/main/java/com/example/javaagent/DemoBaggageSpanProcessor.java
[DemoSpanExporter]: src/main/java/com/example/javaagent/DemoSpanExporter.java
[DemoServlet3InstrumentationModule]: src/main/java/com/example/javaagent/instrumentation/DemoServlet3InstrumentationModule.java

Expand All @@ -47,6 +48,7 @@ For more information, see the `extendedAgent` task in [build.gradle](build.gradl
- Custom `TextMapPropagator`: [DemoPropagator][DemoPropagator]
- Custom `Sampler`: [DemoSampler][DemoSampler]
- Custom `SpanProcessor`: [DemoSpanProcessor][DemoSpanProcessor]
- Custom `SpanProcessor`: [DemoBaggageSpanProcessor][DemoBaggageSpanProcessor]
- Custom `SpanExporter`: [DemoSpanExporter][DemoSpanExporter]
- Additional instrumentation: [DemoServlet3InstrumentationModule][DemoServlet3InstrumentationModule]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private SdkTracerProviderBuilder configureSdkTracerProvider(
.setIdGenerator(new DemoIdGenerator())
.setSpanLimits(SpanLimits.builder().setMaxNumberOfAttributes(1024).build())
.addSpanProcessor(new DemoSpanProcessor())
.addSpanProcessor(new DemoBaggageSpanProcessor())
.addSpanProcessor(SimpleSpanProcessor.create(new DemoSpanExporter()));
}

Expand Down
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;
}
}

0 comments on commit 533568c

Please sign in to comment.