From 1c3e4ea9fa624fc19e3f950b0902d5e1316f5393 Mon Sep 17 00:00:00 2001 From: Mike Goldsmth Date: Wed, 24 Apr 2024 16:10:08 +0100 Subject: [PATCH] Add baggage span processor --- .github/component_owners.yml | 2 + baggage-processor/README.md | 19 ++++++++++ baggage-processor/build.gradle.kts | 16 ++++++++ baggage-processor/gradle.properties | 2 + .../processor/BaggageSpanProcessor.java | 37 +++++++++++++++++++ .../processor/BaggageSpanProcessorTest.java | 30 +++++++++++++++ settings.gradle.kts | 1 + 7 files changed, 107 insertions(+) create mode 100644 baggage-processor/README.md create mode 100644 baggage-processor/build.gradle.kts create mode 100644 baggage-processor/gradle.properties create mode 100644 baggage-processor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessor.java create mode 100644 baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorTest.java diff --git a/.github/component_owners.yml b/.github/component_owners.yml index 6c8223cd9..b052ee74c 100644 --- a/.github/component_owners.yml +++ b/.github/component_owners.yml @@ -19,6 +19,8 @@ components: aws-xray-propagator: - wangzlei - srprash + baggage-procesor: + - mikegoldsmith compressors: - jack-berg consistent-sampling: diff --git a/baggage-processor/README.md b/baggage-processor/README.md new file mode 100644 index 000000000..5f809da05 --- /dev/null +++ b/baggage-processor/README.md @@ -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). diff --git a/baggage-processor/build.gradle.kts b/baggage-processor/build.gradle.kts new file mode 100644 index 000000000..780322906 --- /dev/null +++ b/baggage-processor/build.gradle.kts @@ -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") +} diff --git a/baggage-processor/gradle.properties b/baggage-processor/gradle.properties new file mode 100644 index 000000000..a0402e1e2 --- /dev/null +++ b/baggage-processor/gradle.properties @@ -0,0 +1,2 @@ +# TODO: uncomment when ready to mark as stable +# otel.stable=true diff --git a/baggage-processor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessor.java b/baggage-processor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessor.java new file mode 100644 index 000000000..978183541 --- /dev/null +++ b/baggage-processor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessor.java @@ -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; + } +} diff --git a/baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorTest.java b/baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorTest.java new file mode 100644 index 000000000..f93dcb5ab --- /dev/null +++ b/baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorTest.java @@ -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"); + } + } + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 8720226a1..5d80a7d81 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -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")