Skip to content

Commit

Permalink
Add baggage span processor
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeGoldsmith committed Apr 24, 2024
1 parent 9d1f243 commit 1c3e4ea
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ components:
aws-xray-propagator:
- wangzlei
- srprash
baggage-procesor:
- mikegoldsmith
compressors:
- jack-berg
consistent-sampling:
Expand Down
19 changes: 19 additions & 0 deletions baggage-processor/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# OpenTelemetry Baggage Span Processor

The BaggageActivityProcessor reads entries stored in Baggage from the parent context
and adds the baggage keys and values to the `Activity` as tags(attributes) on start.

Add this activity 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.

## Component owners

- [Mike Golsmith](https://github.com/MikeGoldsmith), Honeycomb

Learn more about component owners in [component_owners.yml](../.github/component_owners.yml).
16 changes: 16 additions & 0 deletions baggage-processor/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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")
api("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")

testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
}
2 changes: 2 additions & 0 deletions baggage-processor/gradle.properties
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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;

/**
* This span processor copies attributes stored in {@link Baggage} into each newly created {@link
* io.opentelemetry.api.trace.Span}.
*/
public class BaggageSpanProcessor 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) {}

@Override
public boolean isEndRequired() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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 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()) {
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
processor.onStart(Context.current(), span);
Mockito.verify(span).setAttribute("key", "value");
}
}
}
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ include(":all")
include(":aws-resources")
include(":aws-xray")
include(":aws-xray-propagator")
include(":baggage-processor")
include(":compressors:compressor-zstd")
include(":consistent-sampling")
include(":dependencyManagement")
Expand Down

0 comments on commit 1c3e4ea

Please sign in to comment.