diff --git a/sdk-extensions/autoconfigure/build.gradle.kts b/sdk-extensions/autoconfigure/build.gradle.kts index 7f0077178e5..df01e3d34d7 100644 --- a/sdk-extensions/autoconfigure/build.gradle.kts +++ b/sdk-extensions/autoconfigure/build.gradle.kts @@ -78,7 +78,6 @@ testing { environment("OTEL_PROPAGATORS", "tracecontext,baggage,b3,b3multi,jaeger,ottrace,test") environment("OTEL_EXPORTER_OTLP_HEADERS", "cat=meow,dog=bark") environment("OTEL_EXPORTER_OTLP_TIMEOUT", "5000") - environment("OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT", "2") environment("OTEL_TEST_CONFIGURED", "true") environment("OTEL_TEST_WRAPPED", "1") } diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/LogRecordExporterConfiguration.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/LogRecordExporterConfiguration.java index ff70fa4d114..db0589ee183 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/LogRecordExporterConfiguration.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/LogRecordExporterConfiguration.java @@ -48,13 +48,7 @@ static Map configureLogRecordExporters( throw new ConfigurationException( "otel.logs.exporter contains " + EXPORTER_NONE + " along with other exporters"); } - LogRecordExporter noop = LogRecordExporter.composite(); - LogRecordExporter customized = logRecordExporterCustomizer.apply(noop, config); - if (customized == noop) { - return Collections.emptyMap(); - } - closeables.add(customized); - return Collections.singletonMap(EXPORTER_NONE, customized); + return Collections.emptyMap(); } if (exporterNames.isEmpty()) { diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/SpanExporterConfiguration.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/SpanExporterConfiguration.java index 581ce575b81..74e6457125c 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/SpanExporterConfiguration.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/SpanExporterConfiguration.java @@ -48,13 +48,7 @@ static Map configureSpanExporters( throw new ConfigurationException( "otel.traces.exporter contains " + EXPORTER_NONE + " along with other exporters"); } - SpanExporter noop = SpanExporter.composite(); - SpanExporter customized = spanExporterCustomizer.apply(noop, config); - if (customized == noop) { - return Collections.emptyMap(); - } - closeables.add(customized); - return Collections.singletonMap(EXPORTER_NONE, customized); + return Collections.emptyMap(); } if (exporterNames.isEmpty()) { diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java index b42c1936130..ed2c94a7108 100644 --- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java +++ b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java @@ -11,7 +11,6 @@ import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -19,7 +18,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; @@ -54,14 +52,10 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; import io.opentelemetry.sdk.trace.IdGenerator; -import io.opentelemetry.sdk.trace.ReadWriteSpan; -import io.opentelemetry.sdk.trace.ReadableSpan; import io.opentelemetry.sdk.trace.SdkTracerProvider; import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder; -import io.opentelemetry.sdk.trace.SpanProcessor; import io.opentelemetry.sdk.trace.data.SpanData; import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; -import io.opentelemetry.sdk.trace.export.SpanExporter; import io.opentelemetry.sdk.trace.samplers.Sampler; import java.io.IOException; import java.math.BigDecimal; @@ -98,8 +92,6 @@ class AutoConfiguredOpenTelemetrySdkTest { @Mock private TextMapGetter> getter; @Mock private Sampler sampler1; @Mock private Sampler sampler2; - @Mock private SpanExporter spanExporter1; - @Mock private SpanExporter spanExporter2; @Mock private MetricReader metricReader; @Mock private LogRecordProcessor logRecordProcessor; @@ -247,76 +239,6 @@ void builder_addSamplerCustomizer() { .isEqualTo(sampler2); } - @Test - void builder_addSpanExporterCustomizer() { - Mockito.lenient().when(spanExporter2.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); - - SdkTracerProvider sdkTracerProvider = - builder - .addSpanExporterCustomizer( - (previous, config) -> { - assertThat(previous).isSameAs(SpanExporter.composite()); - return spanExporter1; - }) - .addSpanExporterCustomizer( - (previous, config) -> { - assertThat(previous).isSameAs(spanExporter1); - return spanExporter2; - }) - .build() - .getOpenTelemetrySdk() - .getSdkTracerProvider(); - - assertThat(sdkTracerProvider) - .extracting("sharedState") - .extracting("activeSpanProcessor") - .extracting("worker") - .extracting("spanExporter") - .isEqualTo(spanExporter2); - } - - @Test - void builder_addSpanProcessorCustomizer() { - SpanProcessor mockProcessor1 = Mockito.mock(SpanProcessor.class); - SpanProcessor mockProcessor2 = Mockito.mock(SpanProcessor.class); - when(mockProcessor2.isStartRequired()).thenReturn(true); - when(mockProcessor2.isEndRequired()).thenReturn(true); - Mockito.lenient().doReturn(CompletableResultCode.ofSuccess()).when(mockProcessor2).shutdown(); - Mockito.lenient().when(spanExporter1.shutdown()).thenReturn(CompletableResultCode.ofSuccess()); - - SdkTracerProvider sdkTracerProvider = - builder - .addSpanExporterCustomizer((prev, config) -> spanExporter1) - .addSpanProcessorCustomizer( - (previous, config) -> { - assertThat(previous).isNotSameAs(mockProcessor2); - return mockProcessor1; - }) - .addSpanProcessorCustomizer( - (previous, config) -> { - assertThat(previous).isSameAs(mockProcessor1); - return mockProcessor2; - }) - .build() - .getOpenTelemetrySdk() - .getSdkTracerProvider(); - - assertThat(sdkTracerProvider) - .extracting("sharedState") - .extracting("activeSpanProcessor") - .isSameAs(mockProcessor2); - - Span span = sdkTracerProvider.get("dummy-scope").spanBuilder("dummy-span").startSpan(); - - verify(mockProcessor2).onStart(any(), same((ReadWriteSpan) span)); - - span.end(); - verify(mockProcessor2).onEnd(same((ReadableSpan) span)); - - verifyNoInteractions(mockProcessor1); - verifyNoInteractions(spanExporter1); - } - @Test void builder_addAutoConfigurationCustomizerProviderUsingComponentLoader() { AutoConfigurationCustomizerProvider customizerProvider = @@ -420,8 +342,6 @@ void builder_addMeterProviderCustomizer() { verify(metricReader).forceFlush(); } - // TODO: add test for addMetricExporterCustomizer once OTLP export is enabled by default - @Test void builder_addLoggerProviderCustomizer() { Mockito.lenient() @@ -442,10 +362,6 @@ void builder_addLoggerProviderCustomizer() { verify(logRecordProcessor).forceFlush(); } - // TODO: add test for addLogRecordExporterCustomizer once OTLP export is enabled by default - - // TODO: add test for addLogRecordProcessorCustomizer once OTLP export is enabled by default - @Test void builder_setResultAsGlobalFalse() { GlobalOpenTelemetry.set(OpenTelemetry.noop()); diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java index e4ac7963ea8..96fd7e3eb61 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/FullConfigTest.java @@ -53,7 +53,7 @@ import org.junit.jupiter.api.extension.RegisterExtension; @SuppressWarnings("InterruptedExceptionSwallowed") -class FullConfigTest { +public class FullConfigTest { private static final BlockingQueue otlpTraceRequests = new LinkedBlockingDeque<>(); @@ -193,7 +193,6 @@ void configures() throws Exception { .spanBuilder("test") .startSpan() .setAttribute("cat", "meow") - .setAttribute("dog", "bark") .end(); Meter meter = GlobalOpenTelemetry.get().getMeter("test"); @@ -209,7 +208,6 @@ void configures() throws Exception { EventLogger eventLogger = GlobalEventLoggerProvider.get().eventLoggerBuilder("test").build(); eventLogger.builder("namespace.test-name").put("cow", "moo").emit(); - ; openTelemetrySdk.getSdkTracerProvider().forceFlush().join(10, TimeUnit.SECONDS); openTelemetrySdk.getSdkLoggerProvider().forceFlush().join(10, TimeUnit.SECONDS); @@ -218,37 +216,19 @@ void configures() throws Exception { await().untilAsserted(() -> assertThat(otlpTraceRequests).hasSize(1)); ExportTraceServiceRequest traceRequest = otlpTraceRequests.take(); - assertThat(traceRequest.getResourceSpans(0).getResource().getAttributesList()) - .contains( - KeyValue.newBuilder() - .setKey("service.name") - .setValue(AnyValue.newBuilder().setStringValue("test").build()) - .build(), - KeyValue.newBuilder() - .setKey("cat") - .setValue(AnyValue.newBuilder().setStringValue("meow").build()) - .build()); + List spanResourceAttributes = + traceRequest.getResourceSpans(0).getResource().getAttributesList(); + assertHasKeyValue(spanResourceAttributes, "service.name", "test"); + assertHasKeyValue(spanResourceAttributes, "cat", "meow"); io.opentelemetry.proto.trace.v1.Span span = traceRequest.getResourceSpans(0).getScopeSpans(0).getSpans(0); - // Dog dropped by attribute limit. - assertThat(span.getAttributesList()) - .containsExactlyInAnyOrder( - KeyValue.newBuilder() - .setKey("configured") - .setValue(AnyValue.newBuilder().setBoolValue(true).build()) - .build(), - KeyValue.newBuilder() - .setKey("wrapped") - .setValue(AnyValue.newBuilder().setIntValue(1).build()) - .build(), - KeyValue.newBuilder() - .setKey("cat") - .setValue(AnyValue.newBuilder().setStringValue("meow").build()) - .build()); + assertHasKeyValue(span.getAttributesList(), "configured", true); + assertHasKeyValue(span.getAttributesList(), "wrapped", 1); + assertHasKeyValue(span.getAttributesList(), "cat", "meow"); + assertHasKeyValue(span.getAttributesList(), "extra-key", "extra-value"); // await on assertions since metrics may come in different order for BatchSpanProcessor, - // exporter, or the ones we - // created in the test. + // exporter, or the ones we created in the test. await() .untilAsserted( () -> { @@ -257,16 +237,10 @@ void configures() throws Exception { assertThat(metricRequest.getResourceMetricsList()) .satisfiesExactly( resourceMetrics -> { - assertThat(resourceMetrics.getResource().getAttributesList()) - .contains( - KeyValue.newBuilder() - .setKey("service.name") - .setValue(AnyValue.newBuilder().setStringValue("test").build()) - .build(), - KeyValue.newBuilder() - .setKey("cat") - .setValue(AnyValue.newBuilder().setStringValue("meow").build()) - .build()); + List metricResourceAttributes = + resourceMetrics.getResource().getAttributesList(); + assertHasKeyValue(metricResourceAttributes, "service.name", "test"); + assertHasKeyValue(metricResourceAttributes, "cat", "meow"); assertThat(resourceMetrics.getScopeMetricsList()) .anySatisfy( scopeMetrics -> { @@ -277,18 +251,10 @@ void configures() throws Exception { // SPI was loaded assertThat(metric.getName()).isEqualTo("my-metric"); // TestMeterProviderConfigurer configures a view that - // only passes on attribute - // named allowed + // only passes an attribute named "allowed" // configured-test - assertThat(getFirstDataPointLabels(metric)) - .contains( - KeyValue.newBuilder() - .setKey("allowed") - .setValue( - AnyValue.newBuilder() - .setStringValue("bear") - .build()) - .build()); + assertHasKeyValue( + getFirstDataPointLabels(metric), "allowed", "bear"); }); }) // This verifies that AutoConfigureListener was invoked and the OTLP @@ -312,21 +278,20 @@ void configures() throws Exception { await().untilAsserted(() -> assertThat(otlpLogsRequests).hasSize(1)); ExportLogsServiceRequest logRequest = otlpLogsRequests.take(); - assertThat(logRequest.getResourceLogs(0).getResource().getAttributesList()) - .contains( - KeyValue.newBuilder() - .setKey("service.name") - .setValue(AnyValue.newBuilder().setStringValue("test").build()) - .build(), - KeyValue.newBuilder() - .setKey("cat") - .setValue(AnyValue.newBuilder().setStringValue("meow").build()) - .build()); + List logResourceAttributes = + logRequest.getResourceLogs(0).getResource().getAttributesList(); + assertHasKeyValue(logResourceAttributes, "service.name", "test"); + assertHasKeyValue(logResourceAttributes, "cat", "meow"); assertThat(logRequest.getResourceLogs(0).getScopeLogs(0).getLogRecordsList()) + // LogRecordCustomizer customizes BatchLogProcessor to add an extra attribute on every log + // record + .allSatisfy( + logRecord -> + assertHasKeyValue(logRecord.getAttributesList(), "extra-key", "extra-value")) .satisfiesExactlyInAnyOrder( logRecord -> { - // LogRecordExporterCustomizer filters logs not whose level is less than Severity.INFO + // LogRecordCustomizer filters logs not whose level is less than Severity.INFO assertThat(logRecord.getBody().getStringValue()).isEqualTo("info log message"); assertThat(logRecord.getSeverityNumberValue()) .isEqualTo(Severity.INFO.getSeverityNumber()); @@ -340,16 +305,37 @@ void configures() throws Exception { .build()); assertThat(logRecord.getSeverityNumber()) .isEqualTo(SeverityNumber.SEVERITY_NUMBER_INFO); - assertThat(logRecord.getAttributesList()) - .containsExactlyInAnyOrder( - KeyValue.newBuilder() - .setKey("event.name") - .setValue( - AnyValue.newBuilder().setStringValue("namespace.test-name").build()) - .build()); + assertHasKeyValue(logRecord.getAttributesList(), "event.name", "namespace.test-name"); }); } + private static void assertHasKeyValue(List keyValues, String key, boolean value) { + assertThat(keyValues) + .contains( + KeyValue.newBuilder() + .setKey(key) + .setValue(AnyValue.newBuilder().setBoolValue(value)) + .build()); + } + + private static void assertHasKeyValue(List keyValues, String key, long value) { + assertThat(keyValues) + .contains( + KeyValue.newBuilder() + .setKey(key) + .setValue(AnyValue.newBuilder().setIntValue(value)) + .build()); + } + + private static void assertHasKeyValue(List keyValues, String key, String value) { + assertThat(keyValues) + .contains( + KeyValue.newBuilder() + .setKey(key) + .setValue(AnyValue.newBuilder().setStringValue(value)) + .build()); + } + private static List getFirstDataPointLabels(Metric metric) { switch (metric.getDataCase()) { case GAUGE: diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/LogRecordCustomizer.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/LogRecordCustomizer.java new file mode 100644 index 00000000000..78c21104ff2 --- /dev/null +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/LogRecordCustomizer.java @@ -0,0 +1,78 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure.provider; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.logs.Severity; +import io.opentelemetry.context.Context; +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.common.CompletableResultCode; +import io.opentelemetry.sdk.logs.LogRecordProcessor; +import io.opentelemetry.sdk.logs.ReadWriteLogRecord; +import io.opentelemetry.sdk.logs.data.LogRecordData; +import io.opentelemetry.sdk.logs.export.LogRecordExporter; +import java.util.Collection; +import java.util.stream.Collectors; + +/** Behavior asserted in {@link io.opentelemetry.sdk.autoconfigure.FullConfigTest}. */ +public class LogRecordCustomizer implements AutoConfigurationCustomizerProvider { + @Override + public void customize(AutoConfigurationCustomizer autoConfiguration) { + autoConfiguration.addLogRecordProcessorCustomizer(LogRecordCustomizer::processorCustomizer); + autoConfiguration.addLogRecordExporterCustomizer(LogRecordCustomizer::exporterCustomizer); + } + + private static LogRecordProcessor processorCustomizer( + LogRecordProcessor delegate, ConfigProperties config) { + return new LogRecordProcessor() { + @Override + public void onEmit(Context context, ReadWriteLogRecord logRecord) { + logRecord.setAttribute(AttributeKey.stringKey("extra-key"), "extra-value"); + delegate.onEmit(context, logRecord); + } + + @Override + public CompletableResultCode shutdown() { + return delegate.shutdown(); + } + + @Override + public CompletableResultCode forceFlush() { + return delegate.forceFlush(); + } + }; + } + + private static LogRecordExporter exporterCustomizer( + LogRecordExporter delegate, ConfigProperties config) { + return new LogRecordExporter() { + @Override + public CompletableResultCode export(Collection logs) { + Collection filtered = + logs.stream() + .filter( + log -> + log.getSeverity() == Severity.UNDEFINED_SEVERITY_NUMBER + || log.getSeverity().getSeverityNumber() + >= Severity.INFO.getSeverityNumber()) + .collect(Collectors.toList()); + return delegate.export(filtered); + } + + @Override + public CompletableResultCode flush() { + return delegate.flush(); + } + + @Override + public CompletableResultCode shutdown() { + return delegate.shutdown(); + } + }; + } +} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/LogRecordExporterCustomizer.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/LogRecordExporterCustomizer.java deleted file mode 100644 index fe6c2930b7e..00000000000 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/LogRecordExporterCustomizer.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.autoconfigure.provider; - -import io.opentelemetry.api.logs.Severity; -import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; -import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; -import io.opentelemetry.sdk.common.CompletableResultCode; -import io.opentelemetry.sdk.logs.data.LogRecordData; -import io.opentelemetry.sdk.logs.export.LogRecordExporter; -import java.util.Collection; -import java.util.stream.Collectors; - -public class LogRecordExporterCustomizer implements AutoConfigurationCustomizerProvider { - @Override - public void customize(AutoConfigurationCustomizer autoConfiguration) { - autoConfiguration.addLogRecordExporterCustomizer( - (delegate, config) -> - new LogRecordExporter() { - @Override - public CompletableResultCode export(Collection logs) { - Collection filtered = - logs.stream() - .filter( - log -> - log.getSeverity() == Severity.UNDEFINED_SEVERITY_NUMBER - || log.getSeverity().getSeverityNumber() - >= Severity.INFO.getSeverityNumber()) - .collect(Collectors.toList()); - return delegate.export(filtered); - } - - @Override - public CompletableResultCode flush() { - return delegate.flush(); - } - - @Override - public CompletableResultCode shutdown() { - return delegate.shutdown(); - } - }); - } -} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/MetricCustomizer.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/MetricCustomizer.java index d4a34a57d82..c65434cf908 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/MetricCustomizer.java +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/MetricCustomizer.java @@ -19,6 +19,7 @@ import java.util.Collection; import java.util.stream.Collectors; +/** Behavior asserted in {@link io.opentelemetry.sdk.autoconfigure.FullConfigTest}. */ public class MetricCustomizer implements AutoConfigurationCustomizerProvider { @Override public void customize(AutoConfigurationCustomizer autoConfiguration) { diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/SpanCustomizer.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/SpanCustomizer.java new file mode 100644 index 00000000000..5fa8c41b3ca --- /dev/null +++ b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/SpanCustomizer.java @@ -0,0 +1,102 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure.provider; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.context.Context; +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.common.CompletableResultCode; +import io.opentelemetry.sdk.trace.ReadWriteSpan; +import io.opentelemetry.sdk.trace.ReadableSpan; +import io.opentelemetry.sdk.trace.SpanProcessor; +import io.opentelemetry.sdk.trace.data.DelegatingSpanData; +import io.opentelemetry.sdk.trace.data.SpanData; +import io.opentelemetry.sdk.trace.export.SpanExporter; +import java.util.Collection; +import java.util.stream.Collectors; + +/** Behavior asserted in {@link io.opentelemetry.sdk.autoconfigure.FullConfigTest}. */ +public class SpanCustomizer implements AutoConfigurationCustomizerProvider { + @Override + public void customize(AutoConfigurationCustomizer autoConfiguration) { + autoConfiguration.addSpanProcessorCustomizer(SpanCustomizer::processorCustomizer); + autoConfiguration.addSpanExporterCustomizer(SpanCustomizer::exporterCustomizer); + } + + private static SpanProcessor processorCustomizer( + SpanProcessor delegate, ConfigProperties config) { + return new SpanProcessor() { + @Override + public void onStart(Context parentContext, ReadWriteSpan span) { + span.setAttribute(AttributeKey.stringKey("extra-key"), "extra-value"); + if (delegate.isStartRequired()) { + delegate.onStart(parentContext, span); + } + } + + @Override + public boolean isStartRequired() { + return true; + } + + @Override + public void onEnd(ReadableSpan span) { + if (delegate.isEndRequired()) { + delegate.onEnd(span); + } + } + + @Override + public boolean isEndRequired() { + return delegate.isEndRequired(); + } + + @Override + public CompletableResultCode shutdown() { + return delegate.shutdown(); + } + + @Override + public CompletableResultCode forceFlush() { + return delegate.forceFlush(); + } + }; + } + + private static SpanExporter exporterCustomizer(SpanExporter delegate, ConfigProperties config) { + return new SpanExporter() { + @Override + public CompletableResultCode export(Collection spans) { + return delegate.export( + spans.stream() + .map( + span -> + new DelegatingSpanData(span) { + @Override + public Attributes getAttributes() { + return span.getAttributes().toBuilder() + .put("wrapped", config.getInt("otel.test.wrapped")) + .build(); + } + }) + .collect(Collectors.toList())); + } + + @Override + public CompletableResultCode flush() { + return delegate.flush(); + } + + @Override + public CompletableResultCode shutdown() { + return delegate.shutdown(); + } + }; + } +} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/SpanExporterCustomizer.java b/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/SpanExporterCustomizer.java deleted file mode 100644 index 50f8210ecde..00000000000 --- a/sdk-extensions/autoconfigure/src/testFullConfig/java/io/opentelemetry/sdk/autoconfigure/provider/SpanExporterCustomizer.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.autoconfigure.provider; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; -import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; -import io.opentelemetry.sdk.common.CompletableResultCode; -import io.opentelemetry.sdk.trace.data.DelegatingSpanData; -import io.opentelemetry.sdk.trace.data.SpanData; -import io.opentelemetry.sdk.trace.export.SpanExporter; -import java.util.Collection; -import java.util.stream.Collectors; - -public class SpanExporterCustomizer implements AutoConfigurationCustomizerProvider { - @Override - public void customize(AutoConfigurationCustomizer autoConfiguration) { - autoConfiguration.addSpanExporterCustomizer( - (delegate, config) -> - new SpanExporter() { - @Override - public CompletableResultCode export(Collection spans) { - return delegate.export( - spans.stream() - .map( - span -> - new DelegatingSpanData(span) { - @Override - public Attributes getAttributes() { - return span.getAttributes().toBuilder() - .put("wrapped", config.getInt("otel.test.wrapped")) - .build(); - } - }) - .collect(Collectors.toList())); - } - - @Override - public CompletableResultCode flush() { - return delegate.flush(); - } - - @Override - public CompletableResultCode shutdown() { - return delegate.shutdown(); - } - }); - } -} diff --git a/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider b/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider index 3103a136000..38b64bf91d3 100644 --- a/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider +++ b/sdk-extensions/autoconfigure/src/testFullConfig/resources/META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider @@ -1,3 +1,3 @@ -io.opentelemetry.sdk.autoconfigure.provider.SpanExporterCustomizer +io.opentelemetry.sdk.autoconfigure.provider.SpanCustomizer io.opentelemetry.sdk.autoconfigure.provider.MetricCustomizer -io.opentelemetry.sdk.autoconfigure.provider.LogRecordExporterCustomizer \ No newline at end of file +io.opentelemetry.sdk.autoconfigure.provider.LogRecordCustomizer