-
Notifications
You must be signed in to change notification settings - Fork 171
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
Changes from all commits
f398623
53bf330
f5b6c6e
a4dca36
cc98df6
456b734
cc65560
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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( | ||
zeitlinger marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.