|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.baggage.processor; |
| 7 | + |
| 8 | +import static org.awaitility.Awaitility.await; |
| 9 | +import static org.mockito.Mockito.verify; |
| 10 | + |
| 11 | +import com.google.common.collect.ImmutableMap; |
| 12 | +import io.opentelemetry.api.baggage.Baggage; |
| 13 | +import io.opentelemetry.api.common.AttributeKey; |
| 14 | +import io.opentelemetry.context.Context; |
| 15 | +import io.opentelemetry.context.Scope; |
| 16 | +import io.opentelemetry.sdk.OpenTelemetrySdk; |
| 17 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; |
| 18 | +import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdkBuilder; |
| 19 | +import io.opentelemetry.sdk.autoconfigure.internal.AutoConfigureUtil; |
| 20 | +import io.opentelemetry.sdk.autoconfigure.internal.ComponentLoader; |
| 21 | +import io.opentelemetry.sdk.autoconfigure.internal.SpiHelper; |
| 22 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 23 | +import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider; |
| 24 | +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; |
| 25 | +import io.opentelemetry.sdk.testing.assertj.TracesAssert; |
| 26 | +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; |
| 27 | +import io.opentelemetry.sdk.trace.ReadWriteSpan; |
| 28 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 29 | +import java.time.Duration; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.function.Consumer; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 36 | +import org.mockito.Mock; |
| 37 | +import org.mockito.Mockito; |
| 38 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 39 | + |
| 40 | +@ExtendWith(MockitoExtension.class) |
| 41 | +class BaggageSpanProcessorCustomizerTest { |
| 42 | + |
| 43 | + private static final String MEMORY_EXPORTER = "memory"; |
| 44 | + |
| 45 | + @Test |
| 46 | + void test_customizer() { |
| 47 | + assertCustomizer(Collections.emptyMap(), span -> span.hasTotalAttributeCount(0)); |
| 48 | + assertCustomizer( |
| 49 | + Collections.singletonMap( |
| 50 | + "otel.java.experimental.span-attributes.copy-from-baggage.include", "key"), |
| 51 | + span -> span.hasAttribute(AttributeKey.stringKey("key"), "value")); |
| 52 | + } |
| 53 | + |
| 54 | + private static void assertCustomizer( |
| 55 | + Map<String, String> properties, Consumer<SpanDataAssert> spanDataAssertConsumer) { |
| 56 | + |
| 57 | + InMemorySpanExporter spanExporter = InMemorySpanExporter.create(); |
| 58 | + |
| 59 | + OpenTelemetrySdk sdk = getOpenTelemetrySdk(properties, spanExporter); |
| 60 | + try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) { |
| 61 | + sdk.getTracer("test").spanBuilder("test").startSpan().end(); |
| 62 | + } |
| 63 | + await() |
| 64 | + .atMost(Duration.ofSeconds(1)) |
| 65 | + .untilAsserted( |
| 66 | + () -> |
| 67 | + TracesAssert.assertThat(spanExporter.getFinishedSpanItems()) |
| 68 | + .hasTracesSatisfyingExactly( |
| 69 | + trace -> trace.hasSpansSatisfyingExactly(spanDataAssertConsumer))); |
| 70 | + } |
| 71 | + |
| 72 | + private static OpenTelemetrySdk getOpenTelemetrySdk( |
| 73 | + Map<String, String> properties, InMemorySpanExporter spanExporter) { |
| 74 | + SpiHelper spiHelper = |
| 75 | + SpiHelper.create(BaggageSpanProcessorCustomizerTest.class.getClassLoader()); |
| 76 | + |
| 77 | + AutoConfiguredOpenTelemetrySdkBuilder sdkBuilder = |
| 78 | + AutoConfiguredOpenTelemetrySdk.builder() |
| 79 | + .addPropertiesSupplier( |
| 80 | + () -> |
| 81 | + ImmutableMap.of( |
| 82 | + // We set the export interval of the spans to 100 ms. The default value is 5 |
| 83 | + // seconds. |
| 84 | + "otel.bsp.schedule.delay", |
| 85 | + "10", |
| 86 | + "otel.traces.exporter", |
| 87 | + MEMORY_EXPORTER, |
| 88 | + "otel.metrics.exporter", |
| 89 | + "none", |
| 90 | + "otel.logs.exporter", |
| 91 | + "none")) |
| 92 | + .addPropertiesSupplier(() -> properties); |
| 93 | + AutoConfigureUtil.setComponentLoader( |
| 94 | + sdkBuilder, |
| 95 | + new ComponentLoader() { |
| 96 | + @SuppressWarnings("unchecked") |
| 97 | + @Override |
| 98 | + public <T> List<T> load(Class<T> spiClass) { |
| 99 | + if (spiClass == ConfigurableSpanExporterProvider.class) { |
| 100 | + return Collections.singletonList( |
| 101 | + (T) |
| 102 | + new ConfigurableSpanExporterProvider() { |
| 103 | + @Override |
| 104 | + public SpanExporter createExporter(ConfigProperties configProperties) { |
| 105 | + return spanExporter; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String getName() { |
| 110 | + return MEMORY_EXPORTER; |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + return spiHelper.load(spiClass); |
| 115 | + } |
| 116 | + }); |
| 117 | + return sdkBuilder.build().getOpenTelemetrySdk(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) { |
| 122 | + try (BaggageSpanProcessor processor = |
| 123 | + BaggageSpanProcessorCustomizer.createProcessor(Collections.singletonList("*"))) { |
| 124 | + try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) { |
| 125 | + processor.onStart(Context.current(), span); |
| 126 | + verify(span).setAttribute("key", "value"); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_matches( |
| 133 | + @Mock ReadWriteSpan span) { |
| 134 | + try (BaggageSpanProcessor processor = |
| 135 | + BaggageSpanProcessorCustomizer.createProcessor(Collections.singletonList("key"))) { |
| 136 | + try (Scope ignore = |
| 137 | + Baggage.current().toBuilder() |
| 138 | + .put("key", "value") |
| 139 | + .put("other", "value") |
| 140 | + .build() |
| 141 | + .makeCurrent()) { |
| 142 | + processor.onStart(Context.current(), span); |
| 143 | + verify(span).setAttribute("key", "value"); |
| 144 | + verify(span, Mockito.never()).setAttribute("other", "value"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments