Skip to content

add config support for BaggageSpanProcessor #1330

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

Merged
merged 7 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ components:
- srprash
baggage-procesor:
- mikegoldsmith
- zeitlinger
compressors:
- jack-berg
consistent-sampling:
Expand Down
10 changes: 10 additions & 0 deletions baggage-processor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,18 @@ Pattern pattern = Pattern.compile("^key.+");
new BaggageSpanProcessor(baggageKey -> pattern.matcher(baggageKey).matches())
```

## Usage with SDK auto-configuration

If you are using the OpenTelemetry SDK auto-configuration, you can add the span processor this
library to configure the span processor.

| Property | Description |
|------------------------------------------------------------------|---------------------------------------------------------------------------------------------------|
| otel.java.experimental.span-attributes.copy-from-baggage.include | Add baggage entries as span attributes, e.g. `key1,key2` or `*` to add all baggage items as keys. |

## Component owners

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

Learn more about component owners in [component_owners.yml](../.github/component_owners.yml).
5 changes: 5 additions & 0 deletions baggage-processor/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ otelJava.moduleName.set("io.opentelemetry.contrib.baggage.processor")
dependencies {
api("io.opentelemetry:opentelemetry-api")
api("io.opentelemetry:opentelemetry-sdk")
implementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure-spi")

testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
testImplementation("org.mockito:mockito-inline")
testImplementation("com.google.guava:guava")
testImplementation("org.awaitility:awaitility")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
public class BaggageSpanProcessor implements SpanProcessor {
private final Predicate<String> baggageKeyPredicate;

/** A {@link Predicate} that returns true for all baggage keys. */
public static final Predicate<String> allowAllBaggageKeys = baggageKey -> true;
/** use {@link #allowAllBaggageKeys()} instead */
@Deprecated public static final Predicate<String> allowAllBaggageKeys = baggageKey -> true;

/**
* Creates a new {@link BaggageSpanProcessor} that copies only baggage entries with keys that pass
Expand All @@ -30,6 +30,14 @@ public BaggageSpanProcessor(Predicate<String> baggageKeyPredicate) {
this.baggageKeyPredicate = baggageKeyPredicate;
}

/**
* Creates a new {@link BaggageSpanProcessor} that copies all baggage entries into the newly
* created {@link io.opentelemetry.api.trace.Span}.
*/
public static BaggageSpanProcessor allowAllBaggageKeys() {
return new BaggageSpanProcessor(baggageKey -> true);
}

@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
Baggage.fromContext(parentContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.baggage.processor;

import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
import java.util.List;

public class BaggageSpanProcessorCustomizer implements AutoConfigurationCustomizerProvider {
@Override
public void customize(AutoConfigurationCustomizer autoConfigurationCustomizer) {
autoConfigurationCustomizer.addTracerProviderCustomizer(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a moment I thought this wouldn't work because this customizers are applied after the batch exporter is registered, but then I realized that order of registration doesn't matter for span processors which want to take advantage of modifying the span on start.

We would need to make changes in the SDK if this was a BaggageLogRecordProcessor: Either providing a hook to customize the logger provider before batch processor is added, or by extending SdkLoggerProviderBuilder with a method to insert a processor at the front of the queue.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either providing a hook to customize the logger provider before batch processor is added

I think this is a great idea - would make sense for all signals

BTW, I've made the test end-to-end to cover that "onStast" is actually called

(sdkTracerProviderBuilder, config) -> {
addSpanProcessor(sdkTracerProviderBuilder, config);
return sdkTracerProviderBuilder;
});
}

private static void addSpanProcessor(
SdkTracerProviderBuilder sdkTracerProviderBuilder, ConfigProperties config) {
List<String> keys =
config.getList("otel.java.experimental.span-attributes.copy-from-baggage.include");

if (keys.isEmpty()) {
return;
}

sdkTracerProviderBuilder.addSpanProcessor(createProcessor(keys));
}

static BaggageSpanProcessor createProcessor(List<String> keys) {
if (keys.size() == 1 && keys.get(0).equals("*")) {
return BaggageSpanProcessor.allowAllBaggageKeys();
}
return new BaggageSpanProcessor(keys::contains);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.contrib.baggage.processor.BaggageSpanProcessorCustomizer
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.contrib.baggage.processor;

import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.verify;

import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil;
import io.opentelemetry.sdk.autoconfigure.internal.ComponentLoader;
import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider;
import io.opentelemetry.sdk.testing.assertj.SpanDataAssert;
import io.opentelemetry.sdk.testing.assertj.TracesAssert;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
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)
class BaggageSpanProcessorCustomizerTest {

private static final String MEMORY_EXPORTER = "memory";

@Test
void test_customizer() {
assertCustomizer(Collections.emptyMap(), span -> span.hasTotalAttributeCount(0));
assertCustomizer(
Collections.singletonMap(
"otel.java.experimental.span-attributes.copy-from-baggage.include", "key"),
span -> span.hasAttribute(AttributeKey.stringKey("key"), "value"));
}

private static void assertCustomizer(
Map<String, String> properties, Consumer<SpanDataAssert> spanDataAssertConsumer) {

InMemorySpanExporter spanExporter = InMemorySpanExporter.create();

OpenTelemetrySdk sdk = getOpenTelemetrySdk(properties, spanExporter);
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
sdk.getTracer("test").spanBuilder("test").startSpan().end();
}
await()
.atMost(Duration.ofSeconds(1))
.untilAsserted(
() ->
TracesAssert.assertThat(spanExporter.getFinishedSpanItems())
.hasTracesSatisfyingExactly(
trace -> trace.hasSpansSatisfyingExactly(spanDataAssertConsumer)));
}

private static OpenTelemetrySdk getOpenTelemetrySdk(
Map<String, String> properties, InMemorySpanExporter spanExporter) {
SpiHelper spiHelper =
SpiHelper.create(BaggageSpanProcessorCustomizerTest.class.getClassLoader());

AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder =
AutoConfiguredOpenTelemetrySdk.builder()
.addPropertiesSupplier(
() ->
ImmutableMap.of(
// We set the export interval of the spans to 100 ms. The default value is 5
// seconds.
"otel.bsp.schedule.delay",
"10",
"otel.traces.exporter",
MEMORY_EXPORTER,
"otel.metrics.exporter",
"none",
"otel.logs.exporter",
"none"))
.addPropertiesSupplier(() -> properties);
AutoConfigureUtil.setComponentLoader(
sdkBuilder,
new ComponentLoader() {
@SuppressWarnings("unchecked")
@Override
public <T> List<T> load(Class<T> spiClass) {
if (spiClass == ConfigurableSpanExporterProvider.class) {
return Collections.singletonList(
(T)
new ConfigurableSpanExporterProvider() {
@Override
public SpanExporter createExporter(ConfigProperties configProperties) {
return spanExporter;
}

@Override
public String getName() {
return MEMORY_EXPORTER;
}
});
}
return spiHelper.load(spiClass);
}
});
return sdkBuilder.build().getOpenTelemetrySdk();
}

@Test
public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) {
try (BaggageSpanProcessor processor =
BaggageSpanProcessorCustomizer.createProcessor(Collections.singletonList("*"))) {
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
processor.onStart(Context.current(), span);
verify(span).setAttribute("key", "value");
}
}
}

@Test
public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_matches(
@Mock ReadWriteSpan span) {
try (BaggageSpanProcessor processor =
BaggageSpanProcessorCustomizer.createProcessor(Collections.singletonList("key"))) {
try (Scope ignore =
Baggage.current().toBuilder()
.put("key", "value")
.put("other", "value")
.build()
.makeCurrent()) {
processor.onStart(Context.current(), span);
verify(span).setAttribute("key", "value");
verify(span, Mockito.never()).setAttribute("other", "value");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class BaggageSpanProcessorTest {

@Test
public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) {
try (BaggageSpanProcessor processor =
new BaggageSpanProcessor(BaggageSpanProcessor.allowAllBaggageKeys)) {
try (BaggageSpanProcessor processor = BaggageSpanProcessor.allowAllBaggageKeys()) {
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
processor.onStart(Context.current(), span);
Mockito.verify(span).setAttribute("key", "value");
Expand Down
Loading