diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java index 13ce175e6b7..de691a77394 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/BatchLogRecordProcessor.java @@ -42,9 +42,6 @@ */ public final class BatchLogRecordProcessor implements LogRecordProcessor { - private static final ComponentId COMPONENT_ID = - ComponentId.generateLazy("batching_log_processor"); - private static final String WORKER_THREAD_NAME = BatchLogRecordProcessor.class.getSimpleName() + "_WorkerThread"; @@ -176,7 +173,8 @@ private Worker( this.queue = queue; this.signal = new ArrayBlockingQueue<>(1); logProcessorInstrumentation = - LogRecordProcessorInstrumentation.get(telemetryVersion, COMPONENT_ID, meterProvider); + LogRecordProcessorInstrumentation.get( + telemetryVersion, ComponentId.generateLazy("batching_log_processor"), meterProvider); this.maxQueueSize = maxQueueSize; this.batch = new ArrayList<>(this.maxExportBatchSize); diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/SimpleLogRecordProcessor.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/SimpleLogRecordProcessor.java index 7c1b1f8aec6..b961dc1ed4f 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/SimpleLogRecordProcessor.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/export/SimpleLogRecordProcessor.java @@ -39,8 +39,6 @@ */ public final class SimpleLogRecordProcessor implements LogRecordProcessor { - private static final ComponentId COMPONENT_ID = ComponentId.generateLazy("simple_log_processor"); - private static final Logger logger = Logger.getLogger(SimpleLogRecordProcessor.class.getName()); private final LogRecordExporter logRecordExporter; @@ -81,7 +79,9 @@ public static SimpleLogRecordProcessorBuilder builder(LogRecordExporter exporter this.logRecordExporter = requireNonNull(logRecordExporter, "logRecordExporter"); logProcessorInstrumentation = LogRecordProcessorInstrumentation.get( - InternalTelemetryVersion.LATEST, COMPONENT_ID, meterProvider); + InternalTelemetryVersion.LATEST, + ComponentId.generateLazy("simple_log_processor"), + meterProvider); } @Override diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderMetricsTest.java b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderMetricsTest.java index 345e69dbf5c..1d2e6d11c35 100644 --- a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderMetricsTest.java +++ b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/SdkLoggerProviderMetricsTest.java @@ -9,11 +9,12 @@ import static io.opentelemetry.sdk.common.internal.SemConvConstants.OTEL_COMPONENT_NAME; import static io.opentelemetry.sdk.common.internal.SemConvConstants.OTEL_COMPONENT_TYPE; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; -import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.LoggerProvider; import io.opentelemetry.api.metrics.MeterProvider; @@ -25,12 +26,15 @@ import io.opentelemetry.sdk.logs.export.LogRecordExporter; import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import java.time.Duration; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -39,6 +43,42 @@ class SdkLoggerProviderMetricsTest { @Mock private LogRecordExporter mockExporter; + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void componentNamesAreUnique(boolean batching) { + InMemoryMetricReader reader = InMemoryMetricReader.create(); + try (SdkMeterProvider meterProvider = + SdkMeterProvider.builder().registerMetricReader(reader).build()) { + SdkLoggerProviderBuilder builder = SdkLoggerProvider.builder(); + for (int i = 0; i < 2; i++) { + builder.addLogRecordProcessor( + batching + ? BatchLogRecordProcessor.builder(InMemoryLogRecordExporter.create()) + .setMeterProvider(() -> meterProvider) + .setInternalTelemetryVersion(InternalTelemetryVersion.LATEST) + .build() + : SimpleLogRecordProcessor.builder(InMemoryLogRecordExporter.create()) + .setMeterProvider(() -> meterProvider) + .build()); + } + try (SdkLoggerProvider provider = builder.build()) { + provider.get("test").logRecordBuilder().emit(); + assertThat(provider.forceFlush().join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); + MetricData metric = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals("otel.sdk.processor.log.processed")) + .findFirst() + .get(); + assertThat(metric.getLongSumData().getPoints()) + .hasSize(2) + .allSatisfy(point -> assertThat(point.getValue()).isEqualTo(1)) + .extracting(point -> point.getAttributes().get(OTEL_COMPONENT_NAME)) + .doesNotContainNull() + .doesNotHaveDuplicates(); + } + } + } + @Test void simple() { InMemoryMetricReader metricReader = InMemoryMetricReader.create(); @@ -70,12 +110,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_log_processor/0", - OTEL_COMPONENT_TYPE, - "simple_log_processor"))))); + name -> name.matches("simple_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_log_processor"))))); logger.logRecordBuilder().emit(); assertThat(metricReader.collectAllMetrics()) @@ -91,12 +130,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_log_processor/0", - OTEL_COMPONENT_TYPE, - "simple_log_processor"))))); + name -> name.matches("simple_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_log_processor"))))); // Logs rejected after the call to shutdown, regardless of completion so no join. processor.shutdown(); @@ -116,22 +154,19 @@ void simple() { s.hasPointsSatisfying( p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_log_processor/0", - OTEL_COMPONENT_TYPE, - "simple_log_processor")), + name -> name.matches("simple_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_log_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_log_processor/0", - OTEL_COMPONENT_TYPE, - "simple_log_processor", - ERROR_TYPE, - "already_shutdown"))))); + name -> name.matches("simple_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_log_processor"), + equalTo(ERROR_TYPE, "already_shutdown"))))); } @Test @@ -178,24 +213,22 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.log.queue.size") .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.log.processed") @@ -203,22 +236,19 @@ void batch() throws Exception { s.hasPointsSatisfying( p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor", - ERROR_TYPE, - "queue_full")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor"), + equalTo(ERROR_TYPE, "queue_full")))), m -> assertThat(m) .hasName("otel.sdk.log.created") @@ -237,24 +267,22 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.log.queue.size") .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(0) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.log.processed") @@ -262,22 +290,19 @@ void batch() throws Exception { s.hasPointsSatisfying( p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor", - ERROR_TYPE, - "queue_full")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor"), + equalTo(ERROR_TYPE, "queue_full")))), m -> assertThat(m) .hasName("otel.sdk.log.created") @@ -297,24 +322,22 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.log.queue.size") .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(0) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.log.processed") @@ -322,32 +345,27 @@ void batch() throws Exception { s.hasPointsSatisfying( p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor")), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor", - ERROR_TYPE, - "already_shutdown")), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor"), + equalTo(ERROR_TYPE, "already_shutdown")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_log_processor/0", - OTEL_COMPONENT_TYPE, - "batching_log_processor", - ERROR_TYPE, - "queue_full")))), + name -> name.matches("batching_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_log_processor"), + equalTo(ERROR_TYPE, "queue_full")))), m -> assertThat(m) .hasName("otel.sdk.log.created") @@ -384,12 +402,11 @@ void simpleExportError() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_log_processor/0", - OTEL_COMPONENT_TYPE, - "simple_log_processor")))), + name -> name.matches("simple_log_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_log_processor")))), m -> assertThat(m) .hasName("otel.sdk.log.created") diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/export/PeriodicMetricReader.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/export/PeriodicMetricReader.java index eb8668db984..ef96c0280b2 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/export/PeriodicMetricReader.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/export/PeriodicMetricReader.java @@ -43,8 +43,7 @@ public final class PeriodicMetricReader implements MetricReader { private static final Clock CLOCK = Clock.getDefault(); - private static final ComponentId COMPONENT_ID = - ComponentId.generateLazy("periodic_metric_reader"); + private final ComponentId componentId = ComponentId.generateLazy("periodic_metric_reader"); private final MetricExporter exporter; private final long intervalNanos; @@ -71,7 +70,7 @@ public final class PeriodicMetricReader implements MetricReader { @Nullable private volatile ScheduledFuture scheduledFuture; private volatile MetricReaderInstrumentation instrumentation = - new MetricReaderInstrumentation(COMPONENT_ID, MeterProvider.noop()); + new MetricReaderInstrumentation(componentId, MeterProvider.noop()); /** * Returns a new {@link PeriodicMetricReader} which exports to the {@code exporter} once every @@ -202,7 +201,7 @@ public void register(CollectionRegistration collectionRegistration) { @SuppressWarnings("UnusedMethod") private void setMeterProvider(MeterProvider meterProvider) { if (internalTelemetryVersion != InternalTelemetryVersion.LEGACY) { - instrumentation = new MetricReaderInstrumentation(COMPONENT_ID, meterProvider); + instrumentation = new MetricReaderInstrumentation(componentId, meterProvider); } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderMetricsTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderMetricsTest.java index f7f4efb32e7..588249669e8 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderMetricsTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderMetricsTest.java @@ -7,6 +7,7 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.Meter; @@ -16,6 +17,7 @@ import io.opentelemetry.sdk.metrics.export.MetricProducer; import io.opentelemetry.sdk.metrics.export.PeriodicMetricReader; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricExporter; +import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -23,6 +25,30 @@ import org.junit.jupiter.api.Test; class SdkMeterProviderMetricsTest { + @Test + void componentNamesAreUnique() { + InMemoryMetricReader observer = InMemoryMetricReader.create(); + try (SdkMeterProvider provider = + SdkMeterProvider.builder() + .registerMetricReader(PeriodicMetricReader.create(InMemoryMetricExporter.create())) + .registerMetricReader(PeriodicMetricReader.create(InMemoryMetricExporter.create())) + .registerMetricReader(observer) + .build()) { + assertThat(provider.forceFlush().join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); + MetricData metric = + observer.collectAllMetrics().stream() + .filter(m -> m.getName().equals("otel.sdk.metric_reader.collection.duration")) + .findFirst() + .get(); + assertThat(metric.getHistogramData().getPoints()) + .hasSize(2) + .allSatisfy(point -> assertThat(point.getCount()).isEqualTo(1)) + .extracting(point -> point.getAttributes().get(SemConvConstants.OTEL_COMPONENT_NAME)) + .doesNotContainNull() + .doesNotHaveDuplicates(); + } + } + @Test void simple() { InMemoryMetricExporter metricExporter = InMemoryMetricExporter.create(); @@ -51,13 +77,13 @@ void simple() { .hasHistogramSatisfying(h -> h.hasPointsSatisfying(p -> p.hasCount(1) - .hasAttributesSatisfying( + .hasAttributesSatisfyingExactly( equalTo( SemConvConstants.OTEL_COMPONENT_TYPE, "periodic_metric_reader"), - equalTo( + satisfies( SemConvConstants.OTEL_COMPONENT_NAME, - "periodic_metric_reader/0")))); + name -> name.matches("periodic_metric_reader/[0-9]+"))))); }); } } diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java index 422c9a576c1..0480cb09bbf 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/BatchSpanProcessor.java @@ -44,9 +44,6 @@ */ public final class BatchSpanProcessor implements SpanProcessor { - private static final ComponentId COMPONENT_ID = - ComponentId.generateLazy("batching_span_processor"); - private static final Logger logger = Logger.getLogger(BatchSpanProcessor.class.getName()); private static final String WORKER_THREAD_NAME = @@ -219,7 +216,8 @@ private Worker( this.signal = new ArrayBlockingQueue<>(1); spanProcessorInstrumentation = - SpanProcessorInstrumentation.get(telemetryVersion, COMPONENT_ID, meterProvider); + SpanProcessorInstrumentation.get( + telemetryVersion, ComponentId.generateLazy("batching_span_processor"), meterProvider); this.maxQueueSize = maxQueueSize; this.batch = new ArrayList<>(this.maxExportBatchSize); diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java index 80a14438d11..b9df2d8c979 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/export/SimpleSpanProcessor.java @@ -38,8 +38,6 @@ */ public final class SimpleSpanProcessor implements SpanProcessor { - private static final ComponentId COMPONENT_ID = ComponentId.generateLazy("simple_span_processor"); - private static final Logger logger = Logger.getLogger(SimpleSpanProcessor.class.getName()); private final SpanExporter spanExporter; @@ -84,7 +82,9 @@ public static SimpleSpanProcessorBuilder builder(SpanExporter exporter) { this.exportUnsampledSpans = exportUnsampledSpans; spanProcessorInstrumentation = SpanProcessorInstrumentation.get( - InternalTelemetryVersion.LATEST, COMPONENT_ID, meterProvider); + InternalTelemetryVersion.LATEST, + ComponentId.generateLazy("simple_span_processor"), + meterProvider); } @Override diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderMetricsTest.java b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderMetricsTest.java index dc5e34c2ef0..41f503ea17b 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderMetricsTest.java +++ b/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkTracerProviderMetricsTest.java @@ -11,6 +11,8 @@ import static io.opentelemetry.sdk.common.internal.SemConvConstants.OTEL_SPAN_PARENT_ORIGIN; import static io.opentelemetry.sdk.common.internal.SemConvConstants.OTEL_SPAN_SAMPLING_RESULT; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.lenient; import static org.mockito.Mockito.when; @@ -30,6 +32,7 @@ import io.opentelemetry.sdk.common.CompletableResultCode; import io.opentelemetry.sdk.common.InternalTelemetryVersion; import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; import io.opentelemetry.sdk.trace.export.BatchSpanProcessor; @@ -42,6 +45,8 @@ import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -51,6 +56,42 @@ class SdkTracerProviderMetricsTest { @Mock private Sampler sampler; @Mock private SpanExporter mockExporter; + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void componentNamesAreUnique(boolean batching) { + InMemoryMetricReader reader = InMemoryMetricReader.create(); + try (SdkMeterProvider meterProvider = + SdkMeterProvider.builder().registerMetricReader(reader).build()) { + SdkTracerProviderBuilder builder = SdkTracerProvider.builder(); + for (int i = 0; i < 2; i++) { + builder.addSpanProcessor( + batching + ? BatchSpanProcessor.builder(InMemorySpanExporter.create()) + .setMeterProvider(() -> meterProvider) + .setInternalTelemetryVersion(InternalTelemetryVersion.LATEST) + .build() + : SimpleSpanProcessor.builder(InMemorySpanExporter.create()) + .setMeterProvider(() -> meterProvider) + .build()); + } + try (SdkTracerProvider provider = builder.build()) { + provider.get("test").spanBuilder("span").startSpan().end(); + assertThat(provider.forceFlush().join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); + MetricData metric = + reader.collectAllMetrics().stream() + .filter(m -> m.getName().equals("otel.sdk.processor.span.processed")) + .findFirst() + .get(); + assertThat(metric.getLongSumData().getPoints()) + .hasSize(2) + .allSatisfy(point -> assertThat(point.getValue()).isEqualTo(1)) + .extracting(point -> point.getAttributes().get(OTEL_COMPONENT_NAME)) + .doesNotContainNull() + .doesNotHaveDuplicates(); + } + } + } + @Test void simple() { InMemoryMetricReader metricReader = InMemoryMetricReader.create(); @@ -104,12 +145,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -142,12 +182,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -192,12 +231,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -244,12 +282,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -302,12 +339,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -373,12 +409,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -439,12 +474,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -519,12 +553,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -593,12 +626,11 @@ void simple() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -672,22 +704,19 @@ void simple() { s.hasPointsSatisfying( p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor", - ERROR_TYPE, - "already_shutdown")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor"), + equalTo(ERROR_TYPE, "already_shutdown")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -796,12 +825,11 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.span.queue.size") @@ -809,12 +837,11 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.span.processed") @@ -823,22 +850,19 @@ void batch() throws Exception { s.hasPointsSatisfying( p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor", - ERROR_TYPE, - "queue_full")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor"), + equalTo(ERROR_TYPE, "queue_full")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -873,24 +897,22 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.span.queue.size") .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(0) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.span.processed") @@ -898,22 +920,19 @@ void batch() throws Exception { s.hasPointsSatisfying( p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor", - ERROR_TYPE, - "queue_full")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor"), + equalTo(ERROR_TYPE, "queue_full")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -949,24 +968,22 @@ void batch() throws Exception { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.span.queue.size") .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(0) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.processor.span.processed") @@ -974,32 +991,27 @@ void batch() throws Exception { s.hasPointsSatisfying( p -> p.hasValue(2) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor")), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor", - ERROR_TYPE, - "already_shutdown")), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor"), + equalTo(ERROR_TYPE, "already_shutdown")), p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "batching_span_processor/0", - OTEL_COMPONENT_TYPE, - "batching_span_processor", - ERROR_TYPE, - "queue_full")))), + name -> name.matches("batching_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "batching_span_processor"), + equalTo(ERROR_TYPE, "queue_full")))), m -> assertThat(m) .hasName("otel.sdk.span.started") @@ -1053,12 +1065,11 @@ void simpleExportError() { .hasLongSumSatisfying(s -> s.hasPointsSatisfying(p -> p.hasValue(1) - .hasAttributes( - Attributes.of( + .hasAttributesSatisfyingExactly( + satisfies( OTEL_COMPONENT_NAME, - "simple_span_processor/0", - OTEL_COMPONENT_TYPE, - "simple_span_processor")))), + name -> name.matches("simple_span_processor/[0-9]+")), + equalTo(OTEL_COMPONENT_TYPE, "simple_span_processor")))), m -> assertThat(m) .hasName("otel.sdk.span.started")