From 8475095bf7dd9fe4e5a53ae80aee6878ba449378 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 2 Aug 2024 15:19:19 +0200 Subject: [PATCH 01/11] don't throw class cast exception when we have a noop tracer --- api/all/build.gradle.kts | 1 + .../api/trace/DefaultTracer.java | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/api/all/build.gradle.kts b/api/all/build.gradle.kts index 4998fd9ddd3..73d2ebf69e8 100644 --- a/api/all/build.gradle.kts +++ b/api/all/build.gradle.kts @@ -12,6 +12,7 @@ base.archivesName.set("opentelemetry-api") dependencies { api(project(":context")) + implementation(project(":api:incubator")) annotationProcessor("com.google.auto.value:auto-value") diff --git a/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java b/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java index a7b02e9363f..2f3d54358fe 100644 --- a/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java +++ b/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java @@ -7,15 +7,23 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators; +import io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder; +import io.opentelemetry.api.incubator.trace.ExtendedTracer; +import io.opentelemetry.api.incubator.trace.SpanCallable; +import io.opentelemetry.api.incubator.trace.SpanRunnable; import io.opentelemetry.api.internal.ApiUsageLogger; import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.ContextPropagators; +import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; /** No-op implementation of {@link Tracer}. */ @ThreadSafe -final class DefaultTracer implements Tracer { +final class DefaultTracer implements ExtendedTracer { private static final Tracer INSTANCE = new DefaultTracer(); @@ -31,7 +39,7 @@ public SpanBuilder spanBuilder(String spanName) { private DefaultTracer() {} // Noop implementation of Span.Builder. - private static final class NoopSpanBuilder implements SpanBuilder { + private static final class NoopSpanBuilder implements ExtendedSpanBuilder { static NoopSpanBuilder create() { return new NoopSpanBuilder(); } @@ -57,6 +65,13 @@ public NoopSpanBuilder setParent(Context context) { return this; } + @Override + public NoopSpanBuilder setParentFrom( + ContextPropagators propagators, Map carrier) { + setParent(ExtendedContextPropagators.extractTextMapPropagationContext(carrier, propagators)); + return this; + } + @Override public NoopSpanBuilder setNoParent() { spanContext = SpanContext.getInvalid(); @@ -113,6 +128,28 @@ public NoopSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { return this; } + @Override + public T startAndCall(SpanCallable spanCallable) throws E { + return spanCallable.callInSpan(); + } + + @Override + public T startAndCall( + SpanCallable spanCallable, BiConsumer handleException) throws E { + return spanCallable.callInSpan(); + } + + @Override + public void startAndRun(SpanRunnable runnable) throws E { + runnable.runInSpan(); + } + + @Override + public void startAndRun( + SpanRunnable runnable, BiConsumer handleException) throws E { + runnable.runInSpan(); + } + private NoopSpanBuilder() {} } } From 96fea4502c9c7194009f4698180af08c818614aa Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 9 Aug 2024 10:36:57 +0200 Subject: [PATCH 02/11] use reflection to load incubating noop apis if available --- api/all/build.gradle.kts | 1 - .../api/DefaultOpenTelemetry.java | 6 +- .../api/internal/IncubatingUtil.java | 29 ++ .../api/logs/LoggerProvider.java | 5 +- .../api/metrics/DefaultMeterProvider.java | 7 +- .../api/trace/DefaultTracer.java | 41 +- .../api/trace/DefaultTracerProvider.java | 6 +- .../ExtendedDefaultOpenTelemetry.java | 58 +++ .../incubator/logs/ExtendedDefaultLogger.java | 89 ++++ .../logs/ExtendedDefaultLoggerProvider.java | 45 ++ .../metrics/ExtendedDefaultMeter.java | 454 ++++++++++++++++++ .../metrics/ExtendedDefaultMeterProvider.java | 45 ++ .../trace/ExtendedDefaultTracer.java | 156 ++++++ .../trace/ExtendedDefaultTracerBuilder.java | 32 ++ .../trace/ExtendedDefaultTracerProvider.java | 32 ++ .../opentelemetry-api/reflect-config.json | 38 ++ .../logs/ExtendedLogsBridgeApiUsageTest.java | 10 + .../metrics/ExtendedMetricsApiUsageTest.java | 45 ++ .../trace/ExtendedTraceApiUsageTest.java | 11 + integration-tests/graal/build.gradle.kts | 1 + .../graal/IncubationApiTests.java | 26 + 21 files changed, 1093 insertions(+), 44 deletions(-) create mode 100644 api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java create mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java create mode 100644 api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json create mode 100644 integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java diff --git a/api/all/build.gradle.kts b/api/all/build.gradle.kts index 73d2ebf69e8..4998fd9ddd3 100644 --- a/api/all/build.gradle.kts +++ b/api/all/build.gradle.kts @@ -12,7 +12,6 @@ base.archivesName.set("opentelemetry-api") dependencies { api(project(":context")) - implementation(project(":api:incubator")) annotationProcessor("com.google.auto.value:auto-value") diff --git a/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java b/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java index 137a805b73e..c11c40d2ae6 100644 --- a/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java +++ b/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java @@ -5,6 +5,7 @@ package io.opentelemetry.api; +import io.opentelemetry.api.internal.IncubatingUtil; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.context.propagation.ContextPropagators; @@ -16,7 +17,10 @@ */ @ThreadSafe final class DefaultOpenTelemetry implements OpenTelemetry { - private static final OpenTelemetry NO_OP = new DefaultOpenTelemetry(ContextPropagators.noop()); + private static final OpenTelemetry NO_OP = + IncubatingUtil.incubatingApiIfAvailable( + new DefaultOpenTelemetry(ContextPropagators.noop()), + "io.opentelemetry.api.incubator.ExtendedDefaultOpenTelemetry"); static OpenTelemetry getNoop() { return NO_OP; diff --git a/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java b/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java new file mode 100644 index 00000000000..31c0f0d0d4d --- /dev/null +++ b/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.internal; + +import java.lang.reflect.Method; + +/** + * Incubating utilities. + * + *

This class is internal and is hence not for public use. Its APIs are unstable and can change + * at any time. + */ +public class IncubatingUtil { + private IncubatingUtil() {} + + @SuppressWarnings("unchecked") + public static T incubatingApiIfAvailable(T stableApi, String incubatingClassName) { + try { + Class incubatingClass = Class.forName(incubatingClassName); + Method getInstance = incubatingClass.getDeclaredMethod("getInstance"); + return (T) getInstance.invoke(null); + } catch (Exception e) { + return stableApi; + } + } +} diff --git a/api/all/src/main/java/io/opentelemetry/api/logs/LoggerProvider.java b/api/all/src/main/java/io/opentelemetry/api/logs/LoggerProvider.java index d00cb310ffc..5bad7eeee51 100644 --- a/api/all/src/main/java/io/opentelemetry/api/logs/LoggerProvider.java +++ b/api/all/src/main/java/io/opentelemetry/api/logs/LoggerProvider.java @@ -5,6 +5,7 @@ package io.opentelemetry.api.logs; +import io.opentelemetry.api.internal.IncubatingUtil; import javax.annotation.concurrent.ThreadSafe; /** @@ -43,6 +44,8 @@ default Logger get(String instrumentationScopeName) { /** Returns a no-op {@link LoggerProvider} which provides Loggers which do not record or emit. */ static LoggerProvider noop() { - return DefaultLoggerProvider.getInstance(); + return IncubatingUtil.incubatingApiIfAvailable( + DefaultLoggerProvider.getInstance(), + "io.opentelemetry.api.incubator.logs.ExtendedDefaultLoggerProvider"); } } diff --git a/api/all/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java b/api/all/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java index 6d1a6de3d48..3ea78ec2d34 100644 --- a/api/all/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java +++ b/api/all/src/main/java/io/opentelemetry/api/metrics/DefaultMeterProvider.java @@ -5,6 +5,8 @@ package io.opentelemetry.api.metrics; +import io.opentelemetry.api.internal.IncubatingUtil; + /** A {@link MeterProvider} that does nothing. */ class DefaultMeterProvider implements MeterProvider { @Override @@ -12,7 +14,10 @@ public MeterBuilder meterBuilder(String instrumentationScopeName) { return BUILDER_INSTANCE; } - private static final DefaultMeterProvider INSTANCE = new DefaultMeterProvider(); + private static final MeterProvider INSTANCE = + IncubatingUtil.incubatingApiIfAvailable( + new DefaultMeterProvider(), + "io.opentelemetry.api.incubator.metrics.ExtendedDefaultMeterProvider"); private static final MeterBuilder BUILDER_INSTANCE = new NoopMeterBuilder(); static MeterProvider getInstance() { diff --git a/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java b/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java index 2f3d54358fe..a7b02e9363f 100644 --- a/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java +++ b/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracer.java @@ -7,23 +7,15 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators; -import io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder; -import io.opentelemetry.api.incubator.trace.ExtendedTracer; -import io.opentelemetry.api.incubator.trace.SpanCallable; -import io.opentelemetry.api.incubator.trace.SpanRunnable; import io.opentelemetry.api.internal.ApiUsageLogger; import io.opentelemetry.context.Context; -import io.opentelemetry.context.propagation.ContextPropagators; -import java.util.Map; import java.util.concurrent.TimeUnit; -import java.util.function.BiConsumer; import javax.annotation.Nullable; import javax.annotation.concurrent.ThreadSafe; /** No-op implementation of {@link Tracer}. */ @ThreadSafe -final class DefaultTracer implements ExtendedTracer { +final class DefaultTracer implements Tracer { private static final Tracer INSTANCE = new DefaultTracer(); @@ -39,7 +31,7 @@ public SpanBuilder spanBuilder(String spanName) { private DefaultTracer() {} // Noop implementation of Span.Builder. - private static final class NoopSpanBuilder implements ExtendedSpanBuilder { + private static final class NoopSpanBuilder implements SpanBuilder { static NoopSpanBuilder create() { return new NoopSpanBuilder(); } @@ -65,13 +57,6 @@ public NoopSpanBuilder setParent(Context context) { return this; } - @Override - public NoopSpanBuilder setParentFrom( - ContextPropagators propagators, Map carrier) { - setParent(ExtendedContextPropagators.extractTextMapPropagationContext(carrier, propagators)); - return this; - } - @Override public NoopSpanBuilder setNoParent() { spanContext = SpanContext.getInvalid(); @@ -128,28 +113,6 @@ public NoopSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { return this; } - @Override - public T startAndCall(SpanCallable spanCallable) throws E { - return spanCallable.callInSpan(); - } - - @Override - public T startAndCall( - SpanCallable spanCallable, BiConsumer handleException) throws E { - return spanCallable.callInSpan(); - } - - @Override - public void startAndRun(SpanRunnable runnable) throws E { - runnable.runInSpan(); - } - - @Override - public void startAndRun( - SpanRunnable runnable, BiConsumer handleException) throws E { - runnable.runInSpan(); - } - private NoopSpanBuilder() {} } } diff --git a/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracerProvider.java b/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracerProvider.java index 97ddbe0c7b8..9bef6cf9928 100644 --- a/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracerProvider.java +++ b/api/all/src/main/java/io/opentelemetry/api/trace/DefaultTracerProvider.java @@ -5,12 +5,16 @@ package io.opentelemetry.api.trace; +import io.opentelemetry.api.internal.IncubatingUtil; import javax.annotation.concurrent.ThreadSafe; @ThreadSafe class DefaultTracerProvider implements TracerProvider { - private static final TracerProvider INSTANCE = new DefaultTracerProvider(); + private static final TracerProvider INSTANCE = + IncubatingUtil.incubatingApiIfAvailable( + new DefaultTracerProvider(), + "io.opentelemetry.api.incubator.trace.ExtendedDefaultTracerProvider"); static TracerProvider getInstance() { return INSTANCE; diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java new file mode 100644 index 00000000000..0ab41d37be6 --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java @@ -0,0 +1,58 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.incubator.metrics.ExtendedDefaultMeterProvider; +import io.opentelemetry.api.incubator.trace.ExtendedDefaultTracerProvider; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.context.propagation.ContextPropagators; +import javax.annotation.concurrent.ThreadSafe; + +/** + * The default OpenTelemetry API, which tries to find API implementations via SPI or otherwise falls + * back to no-op default implementations. + */ +@ThreadSafe +final class ExtendedDefaultOpenTelemetry implements OpenTelemetry { + private static final OpenTelemetry NO_OP = + new ExtendedDefaultOpenTelemetry(ContextPropagators.noop()); + + static OpenTelemetry getNoop() { + return NO_OP; + } + + static OpenTelemetry getPropagating(ContextPropagators propagators) { + return new ExtendedDefaultOpenTelemetry(propagators); + } + + private final ContextPropagators propagators; + + ExtendedDefaultOpenTelemetry(ContextPropagators propagators) { + this.propagators = propagators; + } + + @Override + public TracerProvider getTracerProvider() { + return ExtendedDefaultTracerProvider.getInstance(); + } + + @Override + public MeterProvider getMeterProvider() { + return ExtendedDefaultMeterProvider.getInstance(); + } + + @Override + public ContextPropagators getPropagators() { + return propagators; + } + + @Override + public String toString() { + return "DefaultOpenTelemetry{" + "propagators=" + propagators + "}"; + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java new file mode 100644 index 00000000000..c1e80c52e7e --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java @@ -0,0 +1,89 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.logs; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.logs.LogRecordBuilder; +import io.opentelemetry.api.logs.Logger; +import io.opentelemetry.api.logs.Severity; +import io.opentelemetry.context.Context; +import java.time.Instant; +import java.util.concurrent.TimeUnit; + +class ExtendedDefaultLogger implements ExtendedLogger { + + private static final Logger INSTANCE = new ExtendedDefaultLogger(); + private static final LogRecordBuilder NOOP_LOG_RECORD_BUILDER = new NoopLogRecordBuilder(); + + private ExtendedDefaultLogger() {} + + static Logger getInstance() { + return INSTANCE; + } + + @Override + public LogRecordBuilder logRecordBuilder() { + return NOOP_LOG_RECORD_BUILDER; + } + + private static final class NoopLogRecordBuilder implements ExtendedLogRecordBuilder { + + private NoopLogRecordBuilder() {} + + @Override + public LogRecordBuilder setTimestamp(long timestamp, TimeUnit unit) { + return this; + } + + @Override + public LogRecordBuilder setTimestamp(Instant instant) { + return this; + } + + @Override + public LogRecordBuilder setObservedTimestamp(long timestamp, TimeUnit unit) { + return this; + } + + @Override + public LogRecordBuilder setObservedTimestamp(Instant instant) { + return this; + } + + @Override + public LogRecordBuilder setContext(Context context) { + return this; + } + + @Override + public LogRecordBuilder setSeverity(Severity severity) { + return this; + } + + @Override + public LogRecordBuilder setSeverityText(String severityText) { + return this; + } + + @Override + public LogRecordBuilder setBody(String body) { + return this; + } + + @Override + public LogRecordBuilder setBody(AnyValue body) { + return this; + } + + @Override + public LogRecordBuilder setAttribute(AttributeKey key, T value) { + return this; + } + + @Override + public void emit() {} + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java new file mode 100644 index 00000000000..b5155261005 --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java @@ -0,0 +1,45 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.logs; + +import io.opentelemetry.api.logs.Logger; +import io.opentelemetry.api.logs.LoggerBuilder; +import io.opentelemetry.api.logs.LoggerProvider; + +public class ExtendedDefaultLoggerProvider implements LoggerProvider { + + private static final LoggerProvider INSTANCE = new ExtendedDefaultLoggerProvider(); + private static final LoggerBuilder NOOP_BUILDER = new NoopLoggerBuilder(); + + private ExtendedDefaultLoggerProvider() {} + + public static LoggerProvider getInstance() { + return INSTANCE; + } + + @Override + public LoggerBuilder loggerBuilder(String instrumentationScopeName) { + return NOOP_BUILDER; + } + + private static class NoopLoggerBuilder implements LoggerBuilder { + + @Override + public LoggerBuilder setSchemaUrl(String schemaUrl) { + return this; + } + + @Override + public LoggerBuilder setInstrumentationVersion(String instrumentationVersion) { + return this; + } + + @Override + public Logger build() { + return ExtendedDefaultLogger.getInstance(); + } + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java new file mode 100644 index 00000000000..2c6f0cdeaae --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java @@ -0,0 +1,454 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.metrics; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.metrics.BatchCallback; +import io.opentelemetry.api.metrics.DoubleCounter; +import io.opentelemetry.api.metrics.DoubleCounterBuilder; +import io.opentelemetry.api.metrics.DoubleGauge; +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; +import io.opentelemetry.api.metrics.DoubleHistogram; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.DoubleUpDownCounter; +import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; +import io.opentelemetry.api.metrics.LongCounter; +import io.opentelemetry.api.metrics.LongCounterBuilder; +import io.opentelemetry.api.metrics.LongGauge; +import io.opentelemetry.api.metrics.LongGaugeBuilder; +import io.opentelemetry.api.metrics.LongHistogram; +import io.opentelemetry.api.metrics.LongHistogramBuilder; +import io.opentelemetry.api.metrics.LongUpDownCounter; +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.ObservableDoubleCounter; +import io.opentelemetry.api.metrics.ObservableDoubleGauge; +import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; +import io.opentelemetry.api.metrics.ObservableDoubleUpDownCounter; +import io.opentelemetry.api.metrics.ObservableLongCounter; +import io.opentelemetry.api.metrics.ObservableLongGauge; +import io.opentelemetry.api.metrics.ObservableLongMeasurement; +import io.opentelemetry.api.metrics.ObservableLongUpDownCounter; +import io.opentelemetry.api.metrics.ObservableMeasurement; +import io.opentelemetry.context.Context; +import java.util.function.Consumer; +import javax.annotation.concurrent.ThreadSafe; + +/** + * No-op implementation of {@link Meter}. + * + *

This implementation should induce as close to zero overhead as possible. + */ +@ThreadSafe +class ExtendedDefaultMeter implements Meter { + + private static final Meter INSTANCE = new ExtendedDefaultMeter(); + + private static final LongCounterBuilder NOOP_LONG_COUNTER_BUILDER = new NoopLongCounterBuilder(); + private static final LongUpDownCounterBuilder NOOP_LONG_UP_DOWN_COUNTER_BUILDER = + new NoopLongUpDownCounterBuilder(); + private static final DoubleHistogramBuilder NOOP_DOUBLE_HISTOGRAM_BUILDER = + new NoopDoubleHistogramBuilder(); + private static final DoubleGaugeBuilder NOOP_DOUBLE_GAUGE_BUILDER = new NoopDoubleGaugeBuilder(); + private static final BatchCallback NOOP_BATCH_CALLBACK = new BatchCallback() {}; + private static final ObservableDoubleMeasurement NOOP_OBSERVABLE_DOUBLE_MEASUREMENT = + new NoopObservableDoubleMeasurement(); + private static final ObservableLongMeasurement NOOP_OBSERVABLE_LONG_MEASUREMENT = + new NoopObservableLongMeasurement(); + + static Meter getInstance() { + return INSTANCE; + } + + @Override + public LongCounterBuilder counterBuilder(String name) { + return NOOP_LONG_COUNTER_BUILDER; + } + + @Override + public LongUpDownCounterBuilder upDownCounterBuilder(String name) { + return NOOP_LONG_UP_DOWN_COUNTER_BUILDER; + } + + @Override + public DoubleHistogramBuilder histogramBuilder(String name) { + return NOOP_DOUBLE_HISTOGRAM_BUILDER; + } + + @Override + public DoubleGaugeBuilder gaugeBuilder(String name) { + return NOOP_DOUBLE_GAUGE_BUILDER; + } + + @Override + public BatchCallback batchCallback( + Runnable callback, + ObservableMeasurement observableMeasurement, + ObservableMeasurement... additionalMeasurements) { + return NOOP_BATCH_CALLBACK; + } + + private ExtendedDefaultMeter() {} + + private static class NoopLongCounter implements ExtendedLongCounter { + @Override + public void add(long value, Attributes attributes, Context context) {} + + @Override + public void add(long value, Attributes attributes) {} + + @Override + public void add(long value) {} + } + + private static class NoopDoubleCounter implements ExtendedDoubleCounter { + @Override + public void add(double value, Attributes attributes, Context context) {} + + @Override + public void add(double value, Attributes attributes) {} + + @Override + public void add(double value) {} + } + + private static class NoopLongCounterBuilder implements ExtendedLongCounterBuilder { + private static final LongCounter NOOP_COUNTER = new NoopLongCounter(); + private static final ObservableLongCounter NOOP_OBSERVABLE_COUNTER = + new ObservableLongCounter() {}; + private static final DoubleCounterBuilder NOOP_DOUBLE_COUNTER_BUILDER = + new NoopDoubleCounterBuilder(); + + @Override + public LongCounterBuilder setDescription(String description) { + return this; + } + + @Override + public LongCounterBuilder setUnit(String unit) { + return this; + } + + @Override + public DoubleCounterBuilder ofDoubles() { + return NOOP_DOUBLE_COUNTER_BUILDER; + } + + @Override + public LongCounter build() { + return NOOP_COUNTER; + } + + @Override + public ObservableLongCounter buildWithCallback(Consumer callback) { + return NOOP_OBSERVABLE_COUNTER; + } + + @Override + public ObservableLongMeasurement buildObserver() { + return NOOP_OBSERVABLE_LONG_MEASUREMENT; + } + } + + private static class NoopDoubleCounterBuilder implements ExtendedDoubleCounterBuilder { + private static final DoubleCounter NOOP_COUNTER = new NoopDoubleCounter(); + private static final ObservableDoubleCounter NOOP_OBSERVABLE_COUNTER = + new ObservableDoubleCounter() {}; + + @Override + public DoubleCounterBuilder setDescription(String description) { + return this; + } + + @Override + public DoubleCounterBuilder setUnit(String unit) { + return this; + } + + @Override + public DoubleCounter build() { + return NOOP_COUNTER; + } + + @Override + public ObservableDoubleCounter buildWithCallback( + Consumer callback) { + return NOOP_OBSERVABLE_COUNTER; + } + + @Override + public ObservableDoubleMeasurement buildObserver() { + return NOOP_OBSERVABLE_DOUBLE_MEASUREMENT; + } + } + + private static class NoopLongUpDownCounter implements ExtendedLongUpDownCounter { + @Override + public void add(long value, Attributes attributes, Context context) {} + + @Override + public void add(long value, Attributes attributes) {} + + @Override + public void add(long value) {} + } + + private static class NoopDoubleUpDownCounter implements ExtendedDoubleUpDownCounter { + @Override + public void add(double value, Attributes attributes, Context context) {} + + @Override + public void add(double value, Attributes attributes) {} + + @Override + public void add(double value) {} + } + + private static class NoopLongUpDownCounterBuilder implements ExtendedLongUpDownCounterBuilder { + private static final LongUpDownCounter NOOP_UP_DOWN_COUNTER = new NoopLongUpDownCounter() {}; + private static final ObservableLongUpDownCounter NOOP_OBSERVABLE_UP_DOWN_COUNTER = + new ObservableLongUpDownCounter() {}; + private static final DoubleUpDownCounterBuilder NOOP_DOUBLE_UP_DOWN_COUNTER_BUILDER = + new NoopDoubleUpDownCounterBuilder(); + + @Override + public LongUpDownCounterBuilder setDescription(String description) { + return this; + } + + @Override + public LongUpDownCounterBuilder setUnit(String unit) { + return this; + } + + @Override + public DoubleUpDownCounterBuilder ofDoubles() { + return NOOP_DOUBLE_UP_DOWN_COUNTER_BUILDER; + } + + @Override + public LongUpDownCounter build() { + return NOOP_UP_DOWN_COUNTER; + } + + @Override + public ObservableLongUpDownCounter buildWithCallback( + Consumer callback) { + return NOOP_OBSERVABLE_UP_DOWN_COUNTER; + } + + @Override + public ObservableLongMeasurement buildObserver() { + return NOOP_OBSERVABLE_LONG_MEASUREMENT; + } + } + + private static class NoopDoubleUpDownCounterBuilder + implements ExtendedDoubleUpDownCounterBuilder { + private static final DoubleUpDownCounter NOOP_UP_DOWN_COUNTER = + new NoopDoubleUpDownCounter() {}; + private static final ObservableDoubleUpDownCounter NOOP_OBSERVABLE_UP_DOWN_COUNTER = + new ObservableDoubleUpDownCounter() {}; + + @Override + public DoubleUpDownCounterBuilder setDescription(String description) { + return this; + } + + @Override + public DoubleUpDownCounterBuilder setUnit(String unit) { + return this; + } + + @Override + public DoubleUpDownCounter build() { + return NOOP_UP_DOWN_COUNTER; + } + + @Override + public ObservableDoubleUpDownCounter buildWithCallback( + Consumer callback) { + return NOOP_OBSERVABLE_UP_DOWN_COUNTER; + } + + @Override + public ObservableDoubleMeasurement buildObserver() { + return NOOP_OBSERVABLE_DOUBLE_MEASUREMENT; + } + } + + private static class NoopDoubleHistogram implements ExtendedDoubleHistogram { + @Override + public void record(double value, Attributes attributes, Context context) {} + + @Override + public void record(double value, Attributes attributes) {} + + @Override + public void record(double value) {} + } + + private static class NoopLongHistogram implements ExtendedLongHistogram { + @Override + public void record(long value, Attributes attributes, Context context) {} + + @Override + public void record(long value, Attributes attributes) {} + + @Override + public void record(long value) {} + } + + private static class NoopDoubleHistogramBuilder implements ExtendedDoubleHistogramBuilder { + private static final DoubleHistogram NOOP = new NoopDoubleHistogram(); + private static final LongHistogramBuilder NOOP_LONG_HISTOGRAM_BUILDER = + new NoopLongHistogramBuilder(); + + @Override + public DoubleHistogramBuilder setDescription(String description) { + return this; + } + + @Override + public DoubleHistogramBuilder setUnit(String unit) { + return this; + } + + @Override + public LongHistogramBuilder ofLongs() { + return NOOP_LONG_HISTOGRAM_BUILDER; + } + + @Override + public DoubleHistogram build() { + return NOOP; + } + } + + private static class NoopLongHistogramBuilder implements ExtendedLongHistogramBuilder { + private static final LongHistogram NOOP = new NoopLongHistogram(); + + @Override + public LongHistogramBuilder setDescription(String description) { + return this; + } + + @Override + public LongHistogramBuilder setUnit(String unit) { + return this; + } + + @Override + public LongHistogram build() { + return NOOP; + } + } + + private static class NoopDoubleGaugeBuilder implements ExtendedDoubleGaugeBuilder { + private static final ObservableDoubleGauge NOOP_OBSERVABLE_GAUGE = + new ObservableDoubleGauge() {}; + private static final LongGaugeBuilder NOOP_LONG_GAUGE_BUILDER = new NoopLongGaugeBuilder(); + private static final NoopDoubleGauge NOOP_GAUGE = new NoopDoubleGauge(); + + @Override + public DoubleGaugeBuilder setDescription(String description) { + return this; + } + + @Override + public DoubleGaugeBuilder setUnit(String unit) { + return this; + } + + @Override + public LongGaugeBuilder ofLongs() { + return NOOP_LONG_GAUGE_BUILDER; + } + + @Override + public ObservableDoubleGauge buildWithCallback(Consumer callback) { + return NOOP_OBSERVABLE_GAUGE; + } + + @Override + public ObservableDoubleMeasurement buildObserver() { + return NOOP_OBSERVABLE_DOUBLE_MEASUREMENT; + } + + @Override + public DoubleGauge build() { + return NOOP_GAUGE; + } + } + + private static class NoopDoubleGauge implements ExtendedDoubleGauge { + @Override + public void set(double value) {} + + @Override + public void set(double value, Attributes attributes) {} + + @Override + public void set(double value, Attributes attributes, Context context) {} + } + + private static class NoopLongGaugeBuilder implements ExtendedLongGaugeBuilder { + private static final ObservableLongGauge NOOP_OBSERVABLE_GAUGE = new ObservableLongGauge() {}; + private static final NoopLongGauge NOOP_GAUGE = new NoopLongGauge(); + + @Override + public LongGaugeBuilder setDescription(String description) { + return this; + } + + @Override + public LongGaugeBuilder setUnit(String unit) { + return this; + } + + @Override + public ObservableLongGauge buildWithCallback(Consumer callback) { + return NOOP_OBSERVABLE_GAUGE; + } + + @Override + public ObservableLongMeasurement buildObserver() { + return NOOP_OBSERVABLE_LONG_MEASUREMENT; + } + + @Override + public LongGauge build() { + return NOOP_GAUGE; + } + } + + private static class NoopLongGauge implements ExtendedLongGauge { + @Override + public void set(long value) {} + + @Override + public void set(long value, Attributes attributes) {} + + @Override + public void set(long value, Attributes attributes, Context context) {} + } + + private static class NoopObservableDoubleMeasurement implements ObservableDoubleMeasurement { + @Override + public void record(double value) {} + + @Override + public void record(double value, Attributes attributes) {} + } + + private static class NoopObservableLongMeasurement implements ObservableLongMeasurement { + @Override + public void record(long value) {} + + @Override + public void record(long value, Attributes attributes) {} + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java new file mode 100644 index 00000000000..2851706009d --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java @@ -0,0 +1,45 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.metrics; + +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterBuilder; +import io.opentelemetry.api.metrics.MeterProvider; + +/** A {@link MeterProvider} that does nothing. */ +public class ExtendedDefaultMeterProvider implements MeterProvider { + @Override + public MeterBuilder meterBuilder(String instrumentationScopeName) { + return BUILDER_INSTANCE; + } + + private static final ExtendedDefaultMeterProvider INSTANCE = new ExtendedDefaultMeterProvider(); + private static final MeterBuilder BUILDER_INSTANCE = new NoopMeterBuilder(); + + public static MeterProvider getInstance() { + return INSTANCE; + } + + private ExtendedDefaultMeterProvider() {} + + private static class NoopMeterBuilder implements MeterBuilder { + + @Override + public MeterBuilder setSchemaUrl(String schemaUrl) { + return this; + } + + @Override + public MeterBuilder setInstrumentationVersion(String instrumentationScopeVersion) { + return this; + } + + @Override + public Meter build() { + return ExtendedDefaultMeter.getInstance(); + } + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java new file mode 100644 index 00000000000..64a0dc4f2eb --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java @@ -0,0 +1,156 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.trace; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators; +import io.opentelemetry.api.internal.ApiUsageLogger; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanBuilder; +import io.opentelemetry.api.trace.SpanContext; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.propagation.ContextPropagators; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; +import javax.annotation.Nullable; +import javax.annotation.concurrent.ThreadSafe; + +/** No-op implementation of {@link ExtendedTracer}. */ +@ThreadSafe +final class ExtendedDefaultTracer implements ExtendedTracer { + + private static final Tracer INSTANCE = new ExtendedDefaultTracer(); + + static Tracer getInstance() { + return INSTANCE; + } + + @Override + public SpanBuilder spanBuilder(String spanName) { + return NoopSpanBuilder.create(); + } + + private ExtendedDefaultTracer() {} + + // Noop implementation of Span.Builder. + private static final class NoopSpanBuilder implements ExtendedSpanBuilder { + static NoopSpanBuilder create() { + return new NoopSpanBuilder(); + } + + @Nullable private SpanContext spanContext; + + @Override + public Span startSpan() { + if (spanContext == null) { + spanContext = Span.current().getSpanContext(); + } + + return Span.wrap(spanContext); + } + + @Override + public NoopSpanBuilder setParent(Context context) { + if (context == null) { + ApiUsageLogger.log("context is null"); + return this; + } + spanContext = Span.fromContext(context).getSpanContext(); + return this; + } + + @Override + public NoopSpanBuilder setParentFrom( + ContextPropagators propagators, Map carrier) { + setParent(ExtendedContextPropagators.extractTextMapPropagationContext(carrier, propagators)); + return this; + } + + @Override + public NoopSpanBuilder setNoParent() { + spanContext = SpanContext.getInvalid(); + return this; + } + + @Override + public NoopSpanBuilder addLink(SpanContext spanContext) { + return this; + } + + @Override + public NoopSpanBuilder addLink(SpanContext spanContext, Attributes attributes) { + return this; + } + + @Override + public NoopSpanBuilder setAttribute(String key, String value) { + return this; + } + + @Override + public NoopSpanBuilder setAttribute(String key, long value) { + return this; + } + + @Override + public NoopSpanBuilder setAttribute(String key, double value) { + return this; + } + + @Override + public NoopSpanBuilder setAttribute(String key, boolean value) { + return this; + } + + @Override + public NoopSpanBuilder setAttribute(AttributeKey key, T value) { + return this; + } + + @Override + public NoopSpanBuilder setAllAttributes(Attributes attributes) { + return this; + } + + @Override + public NoopSpanBuilder setSpanKind(SpanKind spanKind) { + return this; + } + + @Override + public NoopSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { + return this; + } + + @Override + public T startAndCall(SpanCallable spanCallable) throws E { + return spanCallable.callInSpan(); + } + + @Override + public T startAndCall( + SpanCallable spanCallable, BiConsumer handleException) throws E { + return spanCallable.callInSpan(); + } + + @Override + public void startAndRun(SpanRunnable runnable) throws E { + runnable.runInSpan(); + } + + @Override + public void startAndRun( + SpanRunnable runnable, BiConsumer handleException) throws E { + runnable.runInSpan(); + } + + private NoopSpanBuilder() {} + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java new file mode 100644 index 00000000000..9f8000a429a --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java @@ -0,0 +1,32 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.trace; + +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerBuilder; + +class ExtendedDefaultTracerBuilder implements TracerBuilder { + private static final ExtendedDefaultTracerBuilder INSTANCE = new ExtendedDefaultTracerBuilder(); + + static TracerBuilder getInstance() { + return INSTANCE; + } + + @Override + public TracerBuilder setSchemaUrl(String schemaUrl) { + return this; + } + + @Override + public TracerBuilder setInstrumentationVersion(String instrumentationScopeVersion) { + return this; + } + + @Override + public Tracer build() { + return ExtendedDefaultTracer.getInstance(); + } +} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java new file mode 100644 index 00000000000..82020962cd2 --- /dev/null +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java @@ -0,0 +1,32 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.trace; + +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import javax.annotation.concurrent.ThreadSafe; + +@ThreadSafe +public class ExtendedDefaultTracerProvider implements TracerProvider { + + private static final TracerProvider INSTANCE = new ExtendedDefaultTracerProvider(); + + public static TracerProvider getInstance() { + return INSTANCE; + } + + @Override + public Tracer get(String instrumentationScopeName) { + return ExtendedDefaultTracer.getInstance(); + } + + @Override + public Tracer get(String instrumentationScopeName, String instrumentationScopeVersion) { + return ExtendedDefaultTracer.getInstance(); + } + + private ExtendedDefaultTracerProvider() {} +} diff --git a/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json b/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json new file mode 100644 index 00000000000..c1e8e6414b8 --- /dev/null +++ b/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json @@ -0,0 +1,38 @@ +[ + { + "methods": [ + { + "name": "getInstance", + "parameterTypes": [] + } + ], + "name": "io.opentelemetry.api.incubator.logs.ExtendedDefaultLoggerProvider" + }, + { + "methods": [ + { + "name": "getInstance", + "parameterTypes": [] + } + ], + "name": "io.opentelemetry.api.incubator.metrics.ExtendedDefaultMeterProvider" + }, + { + "methods": [ + { + "name": "getInstance", + "parameterTypes": [] + } + ], + "name": "io.opentelemetry.api.incubator.trace.ExtendedDefaultTracerProvider" + }, + { + "methods": [ + { + "name": "getInstance", + "parameterTypes": [] + } + ], + "name": "io.opentelemetry.api.incubator.ExtendedDefaultOpenTelemetry" + } +] diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java index 7a3d7c82e17..b92bb4548a2 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java @@ -12,6 +12,7 @@ import com.google.common.collect.ImmutableMap; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.logs.Logger; +import io.opentelemetry.api.logs.LoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; @@ -21,11 +22,20 @@ import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; import java.util.Random; import java.util.concurrent.TimeUnit; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; /** Demonstrating usage of extended Logs Bridge API. */ class ExtendedLogsBridgeApiUsageTest { + @Test + void incubatingApiIsLoaded() { + Logger logger = LoggerProvider.noop().get("test"); + + Assertions.assertThat(logger).isInstanceOf(ExtendedLogger.class); + Assertions.assertThat(logger.logRecordBuilder()).isInstanceOf(ExtendedLogRecordBuilder.class); + } + @Test void loggerEnabled() { // Setup SdkLoggerProvider diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java index 09cda254e18..c503f1f88fb 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java @@ -11,10 +11,12 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.sdk.metrics.InstrumentSelector; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; @@ -23,11 +25,54 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import java.util.Random; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; /** Demonstrating usage of extended Metrics API. */ class ExtendedMetricsApiUsageTest { + @Test + void incubatingApiIsLoaded() { + Meter meter = MeterProvider.noop().get("test"); + assertThat(meter).isSameAs(OpenTelemetry.noop().getMeter("test")); + + Assertions.assertThat(meter.gaugeBuilder("test").ofLongs()) + .isInstanceOf(ExtendedLongGaugeBuilder.class); + Assertions.assertThat(meter.gaugeBuilder("test").ofLongs().build()) + .isInstanceOf(ExtendedLongGauge.class); + Assertions.assertThat(meter.gaugeBuilder("test")) + .isInstanceOf(ExtendedDoubleGaugeBuilder.class); + Assertions.assertThat(meter.gaugeBuilder("test").build()) + .isInstanceOf(ExtendedDoubleGauge.class); + + Assertions.assertThat(meter.histogramBuilder("test").ofLongs()) + .isInstanceOf(ExtendedLongHistogramBuilder.class); + Assertions.assertThat(meter.histogramBuilder("test").ofLongs().build()) + .isInstanceOf(ExtendedLongHistogram.class); + Assertions.assertThat(meter.histogramBuilder("test")) + .isInstanceOf(ExtendedDoubleHistogramBuilder.class); + Assertions.assertThat(meter.histogramBuilder("test").build()) + .isInstanceOf(ExtendedDoubleHistogram.class); + + Assertions.assertThat(meter.counterBuilder("test")) + .isInstanceOf(ExtendedLongCounterBuilder.class); + Assertions.assertThat(meter.counterBuilder("test").build()) + .isInstanceOf(ExtendedLongCounter.class); + Assertions.assertThat(meter.counterBuilder("test").ofDoubles()) + .isInstanceOf(ExtendedDoubleCounterBuilder.class); + Assertions.assertThat(meter.counterBuilder("test").ofDoubles().build()) + .isInstanceOf(ExtendedDoubleCounter.class); + + Assertions.assertThat(meter.upDownCounterBuilder("test")) + .isInstanceOf(ExtendedLongUpDownCounterBuilder.class); + Assertions.assertThat(meter.upDownCounterBuilder("test").build()) + .isInstanceOf(ExtendedLongUpDownCounter.class); + Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles()) + .isInstanceOf(ExtendedDoubleUpDownCounterBuilder.class); + Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles().build()) + .isInstanceOf(ExtendedDoubleUpDownCounter.class); + } + @Test void meterEnabled() { // Setup SdkMeterProvider diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java index 40bf10454de..3650f6978d0 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java @@ -9,6 +9,7 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static io.opentelemetry.sdk.trace.internal.TracerConfig.disabled; +import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; @@ -16,6 +17,7 @@ import io.opentelemetry.api.trace.TraceFlags; import io.opentelemetry.api.trace.TraceState; import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.ContextPropagators; @@ -37,6 +39,15 @@ /** Demonstrating usage of extended Trace API. */ class ExtendedTraceApiUsageTest { + @Test + void incubatingApiIsLoaded() { + Tracer tracer = TracerProvider.noop().get("test"); + assertThat(tracer).isSameAs(OpenTelemetry.noop().getTracer("test")); + + assertThat(tracer).isInstanceOf(ExtendedTracer.class); + assertThat(tracer.spanBuilder("test")).isInstanceOf(ExtendedSpanBuilder.class); + } + @Test void tracerEnabled() { // Setup SdkTracerProvider diff --git a/integration-tests/graal/build.gradle.kts b/integration-tests/graal/build.gradle.kts index 090be76a2f5..ae575ca1677 100644 --- a/integration-tests/graal/build.gradle.kts +++ b/integration-tests/graal/build.gradle.kts @@ -19,6 +19,7 @@ dependencies { implementation(project(":sdk:all")) implementation(project(":sdk:trace-shaded-deps")) implementation(project(":exporters:otlp:all")) + implementation(project(":api:incubator")) } // org.graalvm.buildtools.native pluging requires java 11+ as of version 0.9.26 diff --git a/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java b/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java new file mode 100644 index 00000000000..642e2abd984 --- /dev/null +++ b/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.integrationtests.graal; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.incubator.logs.ExtendedLogger; +import io.opentelemetry.api.incubator.metrics.ExtendedLongCounterBuilder; +import io.opentelemetry.api.incubator.trace.ExtendedTracer; +import io.opentelemetry.api.logs.LoggerProvider; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.api.trace.TracerProvider; +import org.junit.jupiter.api.Test; + +public class IncubationApiTests { + @Test + void incubatingApiIsLoadedViaReflection() { + assertThat(LoggerProvider.noop().get("test")).isInstanceOf(ExtendedLogger.class); + assertThat(TracerProvider.noop().get("test")).isInstanceOf(ExtendedTracer.class); + assertThat(MeterProvider.noop().get("test").counterBuilder("test")) + .isInstanceOf(ExtendedLongCounterBuilder.class); + } +} From 807157c86099ce3a3405b68f2e1ad2354f334032 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 9 Aug 2024 13:29:19 +0200 Subject: [PATCH 03/11] add tests --- api/all/build.gradle.kts | 5 + .../api/internal/IncubatingUtil.java | 2 +- .../opentelemetry/api/OpenTelemetryTest.java | 97 +------- .../api/logs/DefaultLoggerProviderTest.java | 33 --- .../api/logs/DefaultLoggerTest.java | 35 +-- .../api/metrics/DefaultMeterProviderTest.java | 29 --- .../api/metrics/DefaultMeterTest.java | 195 +-------------- .../api/trace/DefaultTracerProviderTest.java | 19 -- .../api/trace/DefaultTracerTest.java | 88 +------ .../api/AbstractOpenTelemetryTest.java | 111 +++++++++ .../api/logs/AbstractDefaultLoggerTest.java | 62 +++++ .../api/metrics/AbstractDefaultMeterTest.java | 226 ++++++++++++++++++ .../api/trace/AbstractDefaultTracerTest.java | 111 +++++++++ api/incubator/build.gradle.kts | 1 + .../ExtendedDefaultOpenTelemetry.java | 4 +- .../incubator/logs/ExtendedDefaultLogger.java | 2 +- .../logs/ExtendedDefaultLoggerProvider.java | 4 +- .../metrics/ExtendedDefaultMeter.java | 2 +- .../metrics/ExtendedDefaultMeterProvider.java | 4 +- .../trace/ExtendedDefaultTracer.java | 2 +- .../trace/ExtendedDefaultTracerBuilder.java | 2 +- .../trace/ExtendedDefaultTracerProvider.java | 6 +- .../incubator/ExtendedOpenTelemetryTest.java | 52 ++++ .../logs/ExtendedDefaultLoggerTest.java | 33 +++ .../logs/ExtendedLogsBridgeApiUsageTest.java | 10 - .../metrics/ExtendedDefaultMeterTest.java | 72 ++++++ .../metrics/ExtendedMetricsApiUsageTest.java | 45 ---- .../trace/ExtendedDefaultTracerTest.java | 36 +++ .../trace/ExtendedTraceApiUsageTest.java | 11 - 29 files changed, 758 insertions(+), 541 deletions(-) delete mode 100644 api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerProviderTest.java delete mode 100644 api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterProviderTest.java delete mode 100644 api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerProviderTest.java create mode 100644 api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java create mode 100644 api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java create mode 100644 api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java create mode 100644 api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java create mode 100644 api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java create mode 100644 api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java create mode 100644 api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java create mode 100644 api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java diff --git a/api/all/build.gradle.kts b/api/all/build.gradle.kts index 4998fd9ddd3..6ade72dfd88 100644 --- a/api/all/build.gradle.kts +++ b/api/all/build.gradle.kts @@ -1,6 +1,7 @@ plugins { id("otel.java-conventions") id("otel.publish-conventions") + id("java-test-fixtures") id("otel.jmh-conventions") id("otel.animalsniffer-conventions") @@ -17,6 +18,10 @@ dependencies { testImplementation("edu.berkeley.cs.jqf:jqf-fuzz") testImplementation("com.google.guava:guava-testlib") + testFixturesApi(project(":testing-internal")) + testFixturesApi("junit:junit") + testFixturesApi("org.assertj:assertj-core") + testFixturesApi("org.mockito:mockito-core") } tasks.test { diff --git a/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java b/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java index 31c0f0d0d4d..1ef82d373f2 100644 --- a/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java +++ b/api/all/src/main/java/io/opentelemetry/api/internal/IncubatingUtil.java @@ -20,7 +20,7 @@ private IncubatingUtil() {} public static T incubatingApiIfAvailable(T stableApi, String incubatingClassName) { try { Class incubatingClass = Class.forName(incubatingClassName); - Method getInstance = incubatingClass.getDeclaredMethod("getInstance"); + Method getInstance = incubatingClass.getDeclaredMethod("getNoop"); return (T) getInstance.invoke(null); } catch (Exception e) { return stableApi; diff --git a/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java b/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java index 42a4cd31476..e9038b3bc67 100644 --- a/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java @@ -5,100 +5,29 @@ package io.opentelemetry.api; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.mockito.Mockito.mock; - import io.opentelemetry.api.logs.LoggerProvider; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.api.trace.TracerProvider; -import io.opentelemetry.context.propagation.ContextPropagators; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -class OpenTelemetryTest { - - @BeforeAll - static void beforeClass() { - GlobalOpenTelemetry.resetForTest(); - } - - @AfterEach - void after() { - GlobalOpenTelemetry.resetForTest(); - } - - @Test - void testDefault() { - assertThat(OpenTelemetry.noop().getTracerProvider()).isSameAs(TracerProvider.noop()); - assertThat(OpenTelemetry.noop().getPropagators()).isSameAs(ContextPropagators.noop()); - assertThat(OpenTelemetry.noop().getMeterProvider()).isSameAs(MeterProvider.noop()); - assertThat(OpenTelemetry.noop().getLogsBridge()).isSameAs(LoggerProvider.noop()); - } - - @Test - void propagating() { - ContextPropagators contextPropagators = mock(ContextPropagators.class); - OpenTelemetry openTelemetry = OpenTelemetry.propagating(contextPropagators); - assertThat(openTelemetry.getTracerProvider()).isSameAs(TracerProvider.noop()); - assertThat(openTelemetry.getMeterProvider()).isSameAs(MeterProvider.noop()); - assertThat(openTelemetry.getLogsBridge()).isSameAs(LoggerProvider.noop()); - assertThat(openTelemetry.getPropagators()).isSameAs(contextPropagators); - } - - @Test - void testGlobalBeforeSet() { - assertThat(GlobalOpenTelemetry.getTracerProvider()).isSameAs(TracerProvider.noop()); - assertThat(GlobalOpenTelemetry.getTracerProvider()) - .isSameAs(GlobalOpenTelemetry.getTracerProvider()); - assertThat(GlobalOpenTelemetry.getPropagators()).isSameAs(GlobalOpenTelemetry.getPropagators()); - } - - @Test - void independentNonGlobalPropagators() { - ContextPropagators propagators1 = mock(ContextPropagators.class); - OpenTelemetry otel1 = OpenTelemetry.propagating(propagators1); - ContextPropagators propagators2 = mock(ContextPropagators.class); - OpenTelemetry otel2 = OpenTelemetry.propagating(propagators2); - - assertThat(otel1.getPropagators()).isSameAs(propagators1); - assertThat(otel2.getPropagators()).isSameAs(propagators2); - } - - @Test - void setThenSet() { - setOpenTelemetry(); - assertThatThrownBy(() -> GlobalOpenTelemetry.set(OpenTelemetry.noop())) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("GlobalOpenTelemetry.set has already been called") - .hasStackTraceContaining("setOpenTelemetry"); - } +class OpenTelemetryTest extends AbstractOpenTelemetryTest { - @Test - void getThenSet() { - assertThat(getOpenTelemetry()).isInstanceOf(DefaultOpenTelemetry.class); - assertThatThrownBy(() -> GlobalOpenTelemetry.set(OpenTelemetry.noop())) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("GlobalOpenTelemetry.set has already been called") - .hasStackTraceContaining("getOpenTelemetry"); + @Override + protected TracerProvider getTracerProvider() { + return TracerProvider.noop(); } - @Test - void toString_noop_Valid() { - assertThat(OpenTelemetry.noop().toString()) - .isEqualTo( - "DefaultOpenTelemetry{" - + "propagators=DefaultContextPropagators{textMapPropagator=NoopTextMapPropagator}" - + "}"); + @Override + protected OpenTelemetry getOpenTelemetry() { + return OpenTelemetry.noop(); } - private static void setOpenTelemetry() { - GlobalOpenTelemetry.set(OpenTelemetry.noop()); + @Override + protected MeterProvider getMeterProvider() { + return MeterProvider.noop(); } - private static OpenTelemetry getOpenTelemetry() { - return GlobalOpenTelemetry.get(); + @Override + protected LoggerProvider getLoggerProvider() { + return LoggerProvider.noop(); } } diff --git a/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerProviderTest.java b/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerProviderTest.java deleted file mode 100644 index 81a8bec1f84..00000000000 --- a/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerProviderTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.logs; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; - -import org.junit.jupiter.api.Test; - -class DefaultLoggerProviderTest { - - @Test - void noopLoggerProvider_doesNotThrow() { - LoggerProvider provider = LoggerProvider.noop(); - - assertThat(provider).isSameAs(DefaultLoggerProvider.getInstance()); - assertThatCode(() -> provider.get("scope-name")).doesNotThrowAnyException(); - assertThatCode( - () -> - provider - .loggerBuilder("scope-name") - .setInstrumentationVersion("1.0") - .setSchemaUrl("http://schema.com") - .build()) - .doesNotThrowAnyException(); - - assertThatCode(() -> provider.loggerBuilder("scope-name").build().logRecordBuilder()) - .doesNotThrowAnyException(); - } -} diff --git a/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerTest.java b/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerTest.java index 9f43ab22b87..436baee524c 100644 --- a/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/logs/DefaultLoggerTest.java @@ -5,34 +5,15 @@ package io.opentelemetry.api.logs; -import static org.assertj.core.api.Assertions.assertThatCode; +class DefaultLoggerTest extends AbstractDefaultLoggerTest { -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.context.Context; -import java.time.Instant; -import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.Test; - -class DefaultLoggerTest { + @Override + protected LoggerProvider getLoggerProvider() { + return DefaultLoggerProvider.getInstance(); + } - @Test - void buildAndEmit() { - assertThatCode( - () -> - DefaultLogger.getInstance() - .logRecordBuilder() - .setTimestamp(100, TimeUnit.SECONDS) - .setTimestamp(Instant.now()) - .setObservedTimestamp(100, TimeUnit.SECONDS) - .setObservedTimestamp(Instant.now()) - .setContext(Context.root()) - .setSeverity(Severity.DEBUG) - .setSeverityText("debug") - .setBody("body") - .setAttribute(AttributeKey.stringKey("key1"), "value1") - .setAllAttributes(Attributes.builder().put("key2", "value2").build()) - .emit()) - .doesNotThrowAnyException(); + @Override + protected Logger getLogger() { + return DefaultLogger.getInstance(); } } diff --git a/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterProviderTest.java b/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterProviderTest.java deleted file mode 100644 index 786d0a68a6e..00000000000 --- a/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterProviderTest.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.metrics; - -import org.junit.jupiter.api.Test; - -public class DefaultMeterProviderTest { - @Test - void noopMeterProvider_getDoesNotThrow() { - MeterProvider provider = MeterProvider.noop(); - provider.get("user-instrumentation"); - } - - @Test - void noopMeterProvider_builderDoesNotThrow() { - MeterProvider provider = MeterProvider.noop(); - provider.meterBuilder("user-instrumentation").build(); - provider.meterBuilder("advanced-instrumetnation").setInstrumentationVersion("1.0").build(); - provider.meterBuilder("schema-instrumentation").setSchemaUrl("myschema://url").build(); - provider - .meterBuilder("schema-instrumentation") - .setInstrumentationVersion("1.0") - .setSchemaUrl("myschema://url") - .build(); - } -} diff --git a/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java b/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java index fd9884bdad7..8136ffe529d 100644 --- a/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java @@ -5,199 +5,18 @@ package io.opentelemetry.api.metrics; -import static io.opentelemetry.api.common.AttributeKey.stringKey; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.context.Context; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; -import org.junit.jupiter.api.Test; @SuppressLogger() -public class DefaultMeterTest { - private static final Meter METER = DefaultMeter.getInstance(); - - @Test - void noopLongCounter_doesNotThrow() { - LongCounter counter = - METER.counterBuilder("size").setDescription("The size I'm measuring").setUnit("1").build(); - counter.add(1); - counter.add(1, Attributes.of(stringKey("thing"), "car")); - counter.add(1, Attributes.of(stringKey("thing"), "car"), Context.current()); - } - - @Test - void noopDoubleCounter_doesNotThrow() { - DoubleCounter counter = - METER - .counterBuilder("size") - .ofDoubles() - .setDescription("The size I'm measuring") - .setUnit("1") - .build(); - counter.add(1.2); - counter.add(2.5, Attributes.of(stringKey("thing"), "car")); - counter.add(2.5, Attributes.of(stringKey("thing"), "car"), Context.current()); - } - - @Test - void noopLongUpDownCounter_doesNotThrow() { - LongUpDownCounter counter = - METER - .upDownCounterBuilder("size") - .setDescription("The size I'm measuring") - .setUnit("1") - .build(); - counter.add(-1); - counter.add(1, Attributes.of(stringKey("thing"), "car")); - counter.add(1, Attributes.of(stringKey("thing"), "car"), Context.current()); - } - - @Test - void noopDoubleUpDownCounter_doesNotThrow() { - DoubleUpDownCounter counter = - METER - .upDownCounterBuilder("size") - .ofDoubles() - .setDescription("The size I'm measuring") - .setUnit("1") - .build(); - counter.add(-2e4); - counter.add(1.0e-1, Attributes.of(stringKey("thing"), "car")); - counter.add(1.0e-1, Attributes.of(stringKey("thing"), "car"), Context.current()); - } - - @Test - void noopLongHistogram_doesNotThrow() { - LongHistogram histogram = - METER - .histogramBuilder("size") - .ofLongs() - .setDescription("The size I'm measuring") - .setUnit("1") - .build(); - histogram.record(-1); - histogram.record(1, Attributes.of(stringKey("thing"), "car")); - histogram.record(1, Attributes.of(stringKey("thing"), "car"), Context.current()); - } - - @Test - void noopDoubleHistogram_doesNotThrow() { - DoubleHistogram histogram = - METER - .histogramBuilder("size") - .setDescription("The size I'm measuring") - .setUnit("1") - .build(); - histogram.record(-2e4); - histogram.record(1.0e-1, Attributes.of(stringKey("thing"), "car")); - histogram.record(1.0e-1, Attributes.of(stringKey("thing"), "car"), Context.current()); - } - - @Test - void noopLongGauage_doesNotThrow() { - LongGauge gauge = - METER - .gaugeBuilder("temperature") - .ofLongs() - .setDescription("The current temperature") - .setUnit("C") - .build(); - gauge.set(1); - gauge.set(2, Attributes.of(stringKey("thing"), "engine")); - gauge.set(2, Attributes.of(stringKey("thing"), "engine"), Context.current()); - } - - @Test - void noopObservableLongGauage_doesNotThrow() { - METER - .gaugeBuilder("temperature") - .ofLongs() - .setDescription("The current temperature") - .setUnit("C") - .buildWithCallback( - m -> { - m.record(1); - m.record(2, Attributes.of(stringKey("thing"), "engine")); - }); - } - - @Test - void noopDoubleGauage_doesNotThrow() { - DoubleGauge gauge = - METER - .gaugeBuilder("temperature") - .setDescription("The current temperature") - .setUnit("C") - .build(); - gauge.set(1); - gauge.set(2, Attributes.of(stringKey("thing"), "engine")); - gauge.set(2, Attributes.of(stringKey("thing"), "engine"), Context.current()); - } - - @Test - void noopObservableDoubleGauage_doesNotThrow() { - METER - .gaugeBuilder("temperature") - .setDescription("The current temperature") - .setUnit("C") - .buildWithCallback( - m -> { - m.record(1.0e1); - m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); - }); - } - - @Test - void noopObservableLongCounter_doesNotThrow() { - METER - .counterBuilder("temperature") - .setDescription("The current temperature") - .setUnit("C") - .buildWithCallback( - m -> { - m.record(1); - m.record(2, Attributes.of(stringKey("thing"), "engine")); - }); - } - - @Test - void noopObservableDoubleCounter_doesNotThrow() { - METER - .counterBuilder("temperature") - .ofDoubles() - .setDescription("The current temperature") - .setUnit("C") - .buildWithCallback( - m -> { - m.record(1.0e1); - m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); - }); - } +public class DefaultMeterTest extends AbstractDefaultMeterTest { - @Test - void noopObservableLongUpDownCounter_doesNotThrow() { - METER - .upDownCounterBuilder("temperature") - .setDescription("The current temperature") - .setUnit("C") - .buildWithCallback( - m -> { - m.record(1); - m.record(2, Attributes.of(stringKey("thing"), "engine")); - }); + @Override + protected Meter getMeter() { + return DefaultMeter.getInstance(); } - @Test - void noopObservableDoubleUpDownCounter_doesNotThrow() { - METER - .upDownCounterBuilder("temperature") - .ofDoubles() - .setDescription("The current temperature") - .setUnit("C") - .buildWithCallback( - m -> { - m.record(1.0e1); - m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); - }); + @Override + protected MeterProvider getMeterProvider() { + return DefaultMeterProvider.getInstance(); } } diff --git a/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerProviderTest.java b/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerProviderTest.java deleted file mode 100644 index 72a98f29f59..00000000000 --- a/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerProviderTest.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.trace; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -class DefaultTracerProviderTest { - - @Test - void returnsDefaultTracer() { - assertThat(TracerProvider.noop().get("test")).isInstanceOf(DefaultTracer.class); - assertThat(TracerProvider.noop().get("test", "1.0")).isInstanceOf(DefaultTracer.class); - } -} diff --git a/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java b/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java index dc364e96457..791050f8c4c 100644 --- a/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java @@ -5,89 +5,15 @@ package io.opentelemetry.api.trace; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; +public class DefaultTracerTest extends AbstractDefaultTracerTest { -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.context.Context; -import org.junit.jupiter.api.Test; - -/** Unit tests for {@link DefaultTracer}. */ -// Need to suppress warnings for MustBeClosed because Android 14 does not support -// try-with-resources. -@SuppressWarnings("MustBeClosedChecker") -class DefaultTracerTest { - private static final Tracer defaultTracer = DefaultTracer.getInstance(); - private static final String SPAN_NAME = "MySpanName"; - private static final SpanContext spanContext = - SpanContext.create( - "00000000000000000000000000000061", - "0000000000000061", - TraceFlags.getDefault(), - TraceState.getDefault()); - - @Test - void defaultSpanBuilderWithName() { - assertThat(defaultTracer.spanBuilder(SPAN_NAME).startSpan().getSpanContext().isValid()) - .isFalse(); - } - - @Test - void testSpanContextPropagationExplicitParent() { - Span span = - defaultTracer - .spanBuilder(SPAN_NAME) - .setParent(Context.root().with(Span.wrap(spanContext))) - .startSpan(); - assertThat(span.getSpanContext()).isSameAs(spanContext); - } - - @Test - void testSpanContextPropagation() { - Span parent = Span.wrap(spanContext); - - Span span = - defaultTracer.spanBuilder(SPAN_NAME).setParent(Context.root().with(parent)).startSpan(); - assertThat(span.getSpanContext()).isSameAs(spanContext); - } - - @Test - void noSpanContextMakesInvalidSpans() { - Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); - assertThat(span.getSpanContext()).isSameAs(SpanContext.getInvalid()); - } - - @Test - void testSpanContextPropagation_fromContext() { - Context context = Context.current().with(Span.wrap(spanContext)); - - Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); - assertThat(span.getSpanContext()).isSameAs(spanContext); - } - - @Test - void testSpanContextPropagation_fromContextAfterNoParent() { - Context context = Context.current().with(Span.wrap(spanContext)); - - Span span = defaultTracer.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan(); - assertThat(span.getSpanContext()).isSameAs(spanContext); - } - - @Test - void testSpanContextPropagation_fromContextThenNoParent() { - Context context = Context.current().with(Span.wrap(spanContext)); - - Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).setNoParent().startSpan(); - assertThat(span.getSpanContext()).isEqualTo(SpanContext.getInvalid()); + @Override + public Tracer getTracer() { + return DefaultTracer.getInstance(); } - @Test - void addLink() { - Span span = Span.fromContext(Context.root()); - assertThatCode(() -> span.addLink(null)).doesNotThrowAnyException(); - assertThatCode(() -> span.addLink(SpanContext.getInvalid())).doesNotThrowAnyException(); - assertThatCode(() -> span.addLink(null, null)).doesNotThrowAnyException(); - assertThatCode(() -> span.addLink(SpanContext.getInvalid(), Attributes.empty())) - .doesNotThrowAnyException(); + @Override + public TracerProvider getTracerProvider() { + return DefaultTracerProvider.getInstance(); } } diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java new file mode 100644 index 00000000000..7dae2b70ef0 --- /dev/null +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java @@ -0,0 +1,111 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import io.opentelemetry.api.logs.LoggerProvider; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.context.propagation.ContextPropagators; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +public abstract class AbstractOpenTelemetryTest { + @BeforeAll + public static void beforeClass() { + GlobalOpenTelemetry.resetForTest(); + } + + private void setOpenTelemetry() { + GlobalOpenTelemetry.set(getOpenTelemetry()); + } + + private static OpenTelemetry getGlobalOpenTelemetry() { + return GlobalOpenTelemetry.get(); + } + + @AfterEach + public void after() { + GlobalOpenTelemetry.resetForTest(); + } + + @Test + void testDefault() { + assertThat(getOpenTelemetry().getTracerProvider()).isSameAs(getTracerProvider()); + assertThat(getOpenTelemetry().getPropagators()).isSameAs(ContextPropagators.noop()); + assertThat(getOpenTelemetry().getMeterProvider()).isSameAs(getMeterProvider()); + assertThat(getOpenTelemetry().getLogsBridge()).isSameAs(getLoggerProvider()); + } + + protected abstract TracerProvider getTracerProvider(); + + protected abstract OpenTelemetry getOpenTelemetry(); + + protected abstract MeterProvider getMeterProvider(); + + protected abstract LoggerProvider getLoggerProvider(); + + @Test + void propagating() { + ContextPropagators contextPropagators = Mockito.mock(ContextPropagators.class); + OpenTelemetry openTelemetry = OpenTelemetry.propagating(contextPropagators); + + assertThat(openTelemetry.getTracerProvider()).isSameAs(getTracerProvider()); + assertThat(openTelemetry.getMeterProvider()).isSameAs(getMeterProvider()); + assertThat(openTelemetry.getLogsBridge()).isSameAs(getLoggerProvider()); + assertThat(openTelemetry.getPropagators()).isSameAs(contextPropagators); + } + + @Test + void testGlobalBeforeSet() { + assertThat(GlobalOpenTelemetry.getTracerProvider()).isSameAs(getTracerProvider()); + assertThat(GlobalOpenTelemetry.getTracerProvider()) + .isSameAs(GlobalOpenTelemetry.getTracerProvider()); + assertThat(GlobalOpenTelemetry.getPropagators()).isSameAs(GlobalOpenTelemetry.getPropagators()); + } + + @Test + void independentNonGlobalPropagators() { + ContextPropagators propagators1 = Mockito.mock(ContextPropagators.class); + OpenTelemetry otel1 = OpenTelemetry.propagating(propagators1); + ContextPropagators propagators2 = Mockito.mock(ContextPropagators.class); + OpenTelemetry otel2 = OpenTelemetry.propagating(propagators2); + + assertThat(otel1.getPropagators()).isSameAs(propagators1); + assertThat(otel2.getPropagators()).isSameAs(propagators2); + } + + @Test + void setThenSet() { + setOpenTelemetry(); + assertThatThrownBy(() -> GlobalOpenTelemetry.set(getOpenTelemetry())) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("GlobalOpenTelemetry.set has already been called") + .hasStackTraceContaining("setOpenTelemetry"); + } + + @Test + void getThenSet() { + assertThat(getGlobalOpenTelemetry()).isInstanceOf(DefaultOpenTelemetry.class); + assertThatThrownBy(() -> GlobalOpenTelemetry.set(getOpenTelemetry())) + .isInstanceOf(IllegalStateException.class) + .hasMessageContaining("GlobalOpenTelemetry.set has already been called") + .hasStackTraceContaining("getGlobalOpenTelemetry"); + } + + @Test + void toString_noop_Valid() { + assertThat(getOpenTelemetry().toString()) + .isEqualTo( + "DefaultOpenTelemetry{" + + "propagators=DefaultContextPropagators{textMapPropagator=NoopTextMapPropagator}" + + "}"); + } +} diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java new file mode 100644 index 00000000000..9f2669e5788 --- /dev/null +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java @@ -0,0 +1,62 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.logs; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.context.Context; +import java.time.Instant; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; + +public abstract class AbstractDefaultLoggerTest { + + protected abstract LoggerProvider getLoggerProvider(); + + protected abstract Logger getLogger(); + + @Test + void noopLoggerProvider_doesNotThrow() { + LoggerProvider provider = LoggerProvider.noop(); + + assertThat(provider).isSameAs(getLoggerProvider()); + assertThatCode(() -> provider.get("scope-name")).doesNotThrowAnyException(); + assertThatCode( + () -> + provider + .loggerBuilder("scope-name") + .setInstrumentationVersion("1.0") + .setSchemaUrl("http://schema.com") + .build()) + .doesNotThrowAnyException(); + + assertThatCode(() -> provider.loggerBuilder("scope-name").build().logRecordBuilder()) + .doesNotThrowAnyException(); + } + + @Test + void buildAndEmit() { + assertThatCode( + () -> + getLogger() + .logRecordBuilder() + .setTimestamp(100, TimeUnit.SECONDS) + .setTimestamp(Instant.now()) + .setObservedTimestamp(100, TimeUnit.SECONDS) + .setObservedTimestamp(Instant.now()) + .setContext(Context.root()) + .setSeverity(Severity.DEBUG) + .setSeverityText("debug") + .setBody("body") + .setAttribute(AttributeKey.stringKey("key1"), "value1") + .setAllAttributes(Attributes.builder().put("key2", "value2").build()) + .emit()) + .doesNotThrowAnyException(); + } +} diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java new file mode 100644 index 00000000000..ebc9c37f336 --- /dev/null +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java @@ -0,0 +1,226 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.metrics; + +import static io.opentelemetry.api.common.AttributeKey.stringKey; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.context.Context; +import io.opentelemetry.internal.testing.slf4j.SuppressLogger; +import org.junit.Test; + +@SuppressLogger() +public abstract class AbstractDefaultMeterTest { + private final Meter meter = getMeter(); + + protected abstract Meter getMeter(); + + protected abstract MeterProvider getMeterProvider(); + + @Test + public void noopMeterProvider_getDoesNotThrow() { + MeterProvider provider = getMeterProvider(); + provider.get("user-instrumentation"); + } + + @Test + public void noopMeterProvider_builderDoesNotThrow() { + MeterProvider provider = getMeterProvider(); + provider.meterBuilder("user-instrumentation").build(); + provider.meterBuilder("advanced-instrumetnation").setInstrumentationVersion("1.0").build(); + provider.meterBuilder("schema-instrumentation").setSchemaUrl("myschema://url").build(); + provider + .meterBuilder("schema-instrumentation") + .setInstrumentationVersion("1.0") + .setSchemaUrl("myschema://url") + .build(); + } + + @Test + public void noopLongCounter_doesNotThrow() { + LongCounter counter = + meter.counterBuilder("size").setDescription("The size I'm measuring").setUnit("1").build(); + counter.add(1); + counter.add(1, Attributes.of(stringKey("thing"), "car")); + counter.add(1, Attributes.of(stringKey("thing"), "car"), Context.current()); + } + + @Test + public void noopDoubleCounter_doesNotThrow() { + DoubleCounter counter = + meter + .counterBuilder("size") + .ofDoubles() + .setDescription("The size I'm measuring") + .setUnit("1") + .build(); + counter.add(1.2); + counter.add(2.5, Attributes.of(stringKey("thing"), "car")); + counter.add(2.5, Attributes.of(stringKey("thing"), "car"), Context.current()); + } + + @Test + public void noopLongUpDownCounter_doesNotThrow() { + LongUpDownCounter counter = + meter + .upDownCounterBuilder("size") + .setDescription("The size I'm measuring") + .setUnit("1") + .build(); + counter.add(-1); + counter.add(1, Attributes.of(stringKey("thing"), "car")); + counter.add(1, Attributes.of(stringKey("thing"), "car"), Context.current()); + } + + @Test + public void noopDoubleUpDownCounter_doesNotThrow() { + DoubleUpDownCounter counter = + meter + .upDownCounterBuilder("size") + .ofDoubles() + .setDescription("The size I'm measuring") + .setUnit("1") + .build(); + counter.add(-2e4); + counter.add(1.0e-1, Attributes.of(stringKey("thing"), "car")); + counter.add(1.0e-1, Attributes.of(stringKey("thing"), "car"), Context.current()); + } + + @Test + public void noopLongHistogram_doesNotThrow() { + LongHistogram histogram = + meter + .histogramBuilder("size") + .ofLongs() + .setDescription("The size I'm measuring") + .setUnit("1") + .build(); + histogram.record(-1); + histogram.record(1, Attributes.of(stringKey("thing"), "car")); + histogram.record(1, Attributes.of(stringKey("thing"), "car"), Context.current()); + } + + @Test + public void noopDoubleHistogram_doesNotThrow() { + DoubleHistogram histogram = + meter + .histogramBuilder("size") + .setDescription("The size I'm measuring") + .setUnit("1") + .build(); + histogram.record(-2e4); + histogram.record(1.0e-1, Attributes.of(stringKey("thing"), "car")); + histogram.record(1.0e-1, Attributes.of(stringKey("thing"), "car"), Context.current()); + } + + @Test + public void noopLongGauage_doesNotThrow() { + LongGauge gauge = + meter + .gaugeBuilder("temperature") + .ofLongs() + .setDescription("The current temperature") + .setUnit("C") + .build(); + gauge.set(1); + gauge.set(2, Attributes.of(stringKey("thing"), "engine")); + gauge.set(2, Attributes.of(stringKey("thing"), "engine"), Context.current()); + } + + @Test + public void noopObservableLongGauage_doesNotThrow() { + meter + .gaugeBuilder("temperature") + .ofLongs() + .setDescription("The current temperature") + .setUnit("C") + .buildWithCallback( + m -> { + m.record(1); + m.record(2, Attributes.of(stringKey("thing"), "engine")); + }); + } + + @Test + void noopDoubleGauage_doesNotThrow() { + DoubleGauge gauge = + meter + .gaugeBuilder("temperature") + .setDescription("The current temperature") + .setUnit("C") + .build(); + gauge.set(1); + gauge.set(2, Attributes.of(stringKey("thing"), "engine")); + gauge.set(2, Attributes.of(stringKey("thing"), "engine"), Context.current()); + } + + @Test + public void noopObservableDoubleGauage_doesNotThrow() { + meter + .gaugeBuilder("temperature") + .setDescription("The current temperature") + .setUnit("C") + .buildWithCallback( + m -> { + m.record(1.0e1); + m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); + }); + } + + @Test + public void noopObservableLongCounter_doesNotThrow() { + meter + .counterBuilder("temperature") + .setDescription("The current temperature") + .setUnit("C") + .buildWithCallback( + m -> { + m.record(1); + m.record(2, Attributes.of(stringKey("thing"), "engine")); + }); + } + + @Test + public void noopObservableDoubleCounter_doesNotThrow() { + meter + .counterBuilder("temperature") + .ofDoubles() + .setDescription("The current temperature") + .setUnit("C") + .buildWithCallback( + m -> { + m.record(1.0e1); + m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); + }); + } + + @Test + public void noopObservableLongUpDownCounter_doesNotThrow() { + meter + .upDownCounterBuilder("temperature") + .setDescription("The current temperature") + .setUnit("C") + .buildWithCallback( + m -> { + m.record(1); + m.record(2, Attributes.of(stringKey("thing"), "engine")); + }); + } + + @Test + public void noopObservableDoubleUpDownCounter_doesNotThrow() { + meter + .upDownCounterBuilder("temperature") + .ofDoubles() + .setDescription("The current temperature") + .setUnit("C") + .buildWithCallback( + m -> { + m.record(1.0e1); + m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); + }); + } +} diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java new file mode 100644 index 00000000000..c75fc6d8a93 --- /dev/null +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java @@ -0,0 +1,111 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.trace; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatCode; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.context.Context; +import org.junit.Test; + +/** Unit tests for {@link DefaultTracer}. */ +// Need to suppress warnings for MustBeClosed because Android 14 does not support +// try-with-resources. +@SuppressWarnings("MustBeClosedChecker") +public abstract class AbstractDefaultTracerTest { + private final Tracer defaultTracer = getTracer(); + private static final String SPAN_NAME = "MySpanName"; + private static final SpanContext spanContext = + SpanContext.create( + "00000000000000000000000000000061", + "0000000000000061", + TraceFlags.getDefault(), + TraceState.getDefault()); + + public abstract Tracer getTracer(); + + public abstract TracerProvider getTracerProvider(); + + @Test + public void returnsDefaultTracer() { + TracerProvider tracerProvider = getTracerProvider(); + assertThat( + tracerProvider + .tracerBuilder("test") + .setSchemaUrl("schema") + .setInstrumentationVersion("1") + .build()) + .isInstanceOf(DefaultTracer.class); + assertThat(tracerProvider.get("test")).isInstanceOf(DefaultTracer.class); + assertThat(tracerProvider.get("test", "1.0")).isInstanceOf(DefaultTracer.class); + } + + @Test + public void defaultSpanBuilderWithName() { + assertThat(defaultTracer.spanBuilder(SPAN_NAME).startSpan().getSpanContext().isValid()) + .isFalse(); + } + + @Test + public void spanContextPropagationExplicitParent() { + Span span = + defaultTracer + .spanBuilder(SPAN_NAME) + .setParent(Context.root().with(Span.wrap(spanContext))) + .startSpan(); + assertThat(span.getSpanContext()).isSameAs(spanContext); + } + + @Test + public void spanContextPropagation() { + Span parent = Span.wrap(spanContext); + + Span span = + defaultTracer.spanBuilder(SPAN_NAME).setParent(Context.root().with(parent)).startSpan(); + assertThat(span.getSpanContext()).isSameAs(spanContext); + } + + @Test + public void noSpanContextMakesInvalidSpans() { + Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); + assertThat(span.getSpanContext()).isSameAs(SpanContext.getInvalid()); + } + + @Test + public void spanContextPropagation_fromContext() { + Context context = Context.current().with(Span.wrap(spanContext)); + + Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); + assertThat(span.getSpanContext()).isSameAs(spanContext); + } + + @Test + public void spanContextPropagation_fromContextAfterNoParent() { + Context context = Context.current().with(Span.wrap(spanContext)); + + Span span = defaultTracer.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan(); + assertThat(span.getSpanContext()).isSameAs(spanContext); + } + + @Test + public void spanContextPropagation_fromContextThenNoParent() { + Context context = Context.current().with(Span.wrap(spanContext)); + + Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).setNoParent().startSpan(); + assertThat(span.getSpanContext()).isEqualTo(SpanContext.getInvalid()); + } + + @Test + public void addLink() { + Span span = Span.fromContext(Context.root()); + assertThatCode(() -> span.addLink(null)).doesNotThrowAnyException(); + assertThatCode(() -> span.addLink(SpanContext.getInvalid())).doesNotThrowAnyException(); + assertThatCode(() -> span.addLink(null, null)).doesNotThrowAnyException(); + assertThatCode(() -> span.addLink(SpanContext.getInvalid(), Attributes.empty())) + .doesNotThrowAnyException(); + } +} diff --git a/api/incubator/build.gradle.kts b/api/incubator/build.gradle.kts index 3dfa0e79a3c..b28295de6d0 100644 --- a/api/incubator/build.gradle.kts +++ b/api/incubator/build.gradle.kts @@ -15,6 +15,7 @@ dependencies { annotationProcessor("com.google.auto.value:auto-value") testImplementation(project(":sdk:testing")) + testImplementation(testFixtures(project(":api:all"))) testImplementation("io.opentelemetry.semconv:opentelemetry-semconv-incubating") diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java index 0ab41d37be6..f18e52a0953 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java @@ -38,12 +38,12 @@ static OpenTelemetry getPropagating(ContextPropagators propagators) { @Override public TracerProvider getTracerProvider() { - return ExtendedDefaultTracerProvider.getInstance(); + return ExtendedDefaultTracerProvider.getNoop(); } @Override public MeterProvider getMeterProvider() { - return ExtendedDefaultMeterProvider.getInstance(); + return ExtendedDefaultMeterProvider.getNoop(); } @Override diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java index c1e80c52e7e..b76b2087065 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java @@ -20,7 +20,7 @@ class ExtendedDefaultLogger implements ExtendedLogger { private ExtendedDefaultLogger() {} - static Logger getInstance() { + static Logger getNoop() { return INSTANCE; } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java index b5155261005..6cf93296689 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerProvider.java @@ -16,7 +16,7 @@ public class ExtendedDefaultLoggerProvider implements LoggerProvider { private ExtendedDefaultLoggerProvider() {} - public static LoggerProvider getInstance() { + public static LoggerProvider getNoop() { return INSTANCE; } @@ -39,7 +39,7 @@ public LoggerBuilder setInstrumentationVersion(String instrumentationVersion) { @Override public Logger build() { - return ExtendedDefaultLogger.getInstance(); + return ExtendedDefaultLogger.getNoop(); } } } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java index 2c6f0cdeaae..de1ec1fdefc 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeter.java @@ -59,7 +59,7 @@ class ExtendedDefaultMeter implements Meter { private static final ObservableLongMeasurement NOOP_OBSERVABLE_LONG_MEASUREMENT = new NoopObservableLongMeasurement(); - static Meter getInstance() { + static Meter getNoop() { return INSTANCE; } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java index 2851706009d..3eeca2081f8 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterProvider.java @@ -19,7 +19,7 @@ public MeterBuilder meterBuilder(String instrumentationScopeName) { private static final ExtendedDefaultMeterProvider INSTANCE = new ExtendedDefaultMeterProvider(); private static final MeterBuilder BUILDER_INSTANCE = new NoopMeterBuilder(); - public static MeterProvider getInstance() { + public static MeterProvider getNoop() { return INSTANCE; } @@ -39,7 +39,7 @@ public MeterBuilder setInstrumentationVersion(String instrumentationScopeVersion @Override public Meter build() { - return ExtendedDefaultMeter.getInstance(); + return ExtendedDefaultMeter.getNoop(); } } } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java index 64a0dc4f2eb..948f17e9996 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracer.java @@ -28,7 +28,7 @@ final class ExtendedDefaultTracer implements ExtendedTracer { private static final Tracer INSTANCE = new ExtendedDefaultTracer(); - static Tracer getInstance() { + static Tracer getNoop() { return INSTANCE; } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java index 9f8000a429a..fba4782c1e9 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java @@ -27,6 +27,6 @@ public TracerBuilder setInstrumentationVersion(String instrumentationScopeVersio @Override public Tracer build() { - return ExtendedDefaultTracer.getInstance(); + return ExtendedDefaultTracer.getNoop(); } } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java index 82020962cd2..04618956446 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java @@ -14,18 +14,18 @@ public class ExtendedDefaultTracerProvider implements TracerProvider { private static final TracerProvider INSTANCE = new ExtendedDefaultTracerProvider(); - public static TracerProvider getInstance() { + public static TracerProvider getNoop() { return INSTANCE; } @Override public Tracer get(String instrumentationScopeName) { - return ExtendedDefaultTracer.getInstance(); + return ExtendedDefaultTracer.getNoop(); } @Override public Tracer get(String instrumentationScopeName, String instrumentationScopeVersion) { - return ExtendedDefaultTracer.getInstance(); + return ExtendedDefaultTracer.getNoop(); } private ExtendedDefaultTracerProvider() {} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java new file mode 100644 index 00000000000..9cb6147b255 --- /dev/null +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java @@ -0,0 +1,52 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.AbstractOpenTelemetryTest; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.incubator.logs.ExtendedDefaultLoggerProvider; +import io.opentelemetry.api.incubator.logs.ExtendedLogger; +import io.opentelemetry.api.incubator.metrics.ExtendedDefaultMeterProvider; +import io.opentelemetry.api.incubator.metrics.ExtendedLongCounterBuilder; +import io.opentelemetry.api.incubator.trace.ExtendedDefaultTracerProvider; +import io.opentelemetry.api.incubator.trace.ExtendedTracer; +import io.opentelemetry.api.logs.LoggerProvider; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.api.trace.TracerProvider; +import org.junit.jupiter.api.Test; + +class ExtendedOpenTelemetryTest extends AbstractOpenTelemetryTest { + + @Override + protected TracerProvider getTracerProvider() { + return ExtendedDefaultTracerProvider.getNoop(); + } + + @Override + protected OpenTelemetry getOpenTelemetry() { + return ExtendedDefaultOpenTelemetry.getNoop(); + } + + @Override + protected MeterProvider getMeterProvider() { + return ExtendedDefaultMeterProvider.getNoop(); + } + + @Override + protected LoggerProvider getLoggerProvider() { + return ExtendedDefaultLoggerProvider.getNoop(); + } + + @Test + void incubatingApiIsLoaded() { + assertThat(OpenTelemetry.noop().getMeter("test").counterBuilder("test")) + .isInstanceOf(ExtendedLongCounterBuilder.class); + assertThat(OpenTelemetry.noop().getLogsBridge().get("test")).isInstanceOf(ExtendedLogger.class); + assertThat(OpenTelemetry.noop().getTracer("test")).isInstanceOf(ExtendedTracer.class); + } +} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java new file mode 100644 index 00000000000..9bf6147aa25 --- /dev/null +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java @@ -0,0 +1,33 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.logs; + +import io.opentelemetry.api.logs.AbstractDefaultLoggerTest; +import io.opentelemetry.api.logs.Logger; +import io.opentelemetry.api.logs.LoggerProvider; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +class ExtendedDefaultLoggerTest extends AbstractDefaultLoggerTest { + + @Override + protected LoggerProvider getLoggerProvider() { + return ExtendedDefaultLoggerProvider.getNoop(); + } + + @Override + protected Logger getLogger() { + return ExtendedDefaultLogger.getNoop(); + } + + @Test + void incubatingApiIsLoaded() { + Logger logger = LoggerProvider.noop().get("test"); + + Assertions.assertThat(logger).isInstanceOf(ExtendedLogger.class); + Assertions.assertThat(logger.logRecordBuilder()).isInstanceOf(ExtendedLogRecordBuilder.class); + } +} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java index b92bb4548a2..7a3d7c82e17 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java @@ -12,7 +12,6 @@ import com.google.common.collect.ImmutableMap; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.logs.Logger; -import io.opentelemetry.api.logs.LoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; @@ -22,20 +21,11 @@ import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; import java.util.Random; import java.util.concurrent.TimeUnit; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; /** Demonstrating usage of extended Logs Bridge API. */ class ExtendedLogsBridgeApiUsageTest { - @Test - void incubatingApiIsLoaded() { - Logger logger = LoggerProvider.noop().get("test"); - - Assertions.assertThat(logger).isInstanceOf(ExtendedLogger.class); - Assertions.assertThat(logger.logRecordBuilder()).isInstanceOf(ExtendedLogRecordBuilder.class); - } - @Test void loggerEnabled() { // Setup SdkLoggerProvider diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java new file mode 100644 index 00000000000..ac24978fcac --- /dev/null +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java @@ -0,0 +1,72 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.metrics; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.metrics.AbstractDefaultMeterTest; +import io.opentelemetry.api.metrics.Meter; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.internal.testing.slf4j.SuppressLogger; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +@SuppressLogger() +public class ExtendedDefaultMeterTest extends AbstractDefaultMeterTest { + + @Override + protected Meter getMeter() { + return ExtendedDefaultMeter.getNoop(); + } + + @Override + protected MeterProvider getMeterProvider() { + return ExtendedDefaultMeterProvider.getNoop(); + } + + @Test + void incubatingApiIsLoaded() { + Meter meter = MeterProvider.noop().get("test"); + assertThat(meter).isSameAs(OpenTelemetry.noop().getMeter("test")); + + Assertions.assertThat(meter.gaugeBuilder("test").ofLongs()) + .isInstanceOf(ExtendedLongGaugeBuilder.class); + Assertions.assertThat(meter.gaugeBuilder("test").ofLongs().build()) + .isInstanceOf(ExtendedLongGauge.class); + Assertions.assertThat(meter.gaugeBuilder("test")) + .isInstanceOf(ExtendedDoubleGaugeBuilder.class); + Assertions.assertThat(meter.gaugeBuilder("test").build()) + .isInstanceOf(ExtendedDoubleGauge.class); + + Assertions.assertThat(meter.histogramBuilder("test").ofLongs()) + .isInstanceOf(ExtendedLongHistogramBuilder.class); + Assertions.assertThat(meter.histogramBuilder("test").ofLongs().build()) + .isInstanceOf(ExtendedLongHistogram.class); + Assertions.assertThat(meter.histogramBuilder("test")) + .isInstanceOf(ExtendedDoubleHistogramBuilder.class); + Assertions.assertThat(meter.histogramBuilder("test").build()) + .isInstanceOf(ExtendedDoubleHistogram.class); + + Assertions.assertThat(meter.counterBuilder("test")) + .isInstanceOf(ExtendedLongCounterBuilder.class); + Assertions.assertThat(meter.counterBuilder("test").build()) + .isInstanceOf(ExtendedLongCounter.class); + Assertions.assertThat(meter.counterBuilder("test").ofDoubles()) + .isInstanceOf(ExtendedDoubleCounterBuilder.class); + Assertions.assertThat(meter.counterBuilder("test").ofDoubles().build()) + .isInstanceOf(ExtendedDoubleCounter.class); + + Assertions.assertThat(meter.upDownCounterBuilder("test")) + .isInstanceOf(ExtendedLongUpDownCounterBuilder.class); + Assertions.assertThat(meter.upDownCounterBuilder("test").build()) + .isInstanceOf(ExtendedLongUpDownCounter.class); + Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles()) + .isInstanceOf(ExtendedDoubleUpDownCounterBuilder.class); + Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles().build()) + .isInstanceOf(ExtendedDoubleUpDownCounter.class); + } +} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java index c503f1f88fb..09cda254e18 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedMetricsApiUsageTest.java @@ -11,12 +11,10 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.sdk.metrics.InstrumentSelector; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; @@ -25,54 +23,11 @@ import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; import java.util.Random; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; /** Demonstrating usage of extended Metrics API. */ class ExtendedMetricsApiUsageTest { - @Test - void incubatingApiIsLoaded() { - Meter meter = MeterProvider.noop().get("test"); - assertThat(meter).isSameAs(OpenTelemetry.noop().getMeter("test")); - - Assertions.assertThat(meter.gaugeBuilder("test").ofLongs()) - .isInstanceOf(ExtendedLongGaugeBuilder.class); - Assertions.assertThat(meter.gaugeBuilder("test").ofLongs().build()) - .isInstanceOf(ExtendedLongGauge.class); - Assertions.assertThat(meter.gaugeBuilder("test")) - .isInstanceOf(ExtendedDoubleGaugeBuilder.class); - Assertions.assertThat(meter.gaugeBuilder("test").build()) - .isInstanceOf(ExtendedDoubleGauge.class); - - Assertions.assertThat(meter.histogramBuilder("test").ofLongs()) - .isInstanceOf(ExtendedLongHistogramBuilder.class); - Assertions.assertThat(meter.histogramBuilder("test").ofLongs().build()) - .isInstanceOf(ExtendedLongHistogram.class); - Assertions.assertThat(meter.histogramBuilder("test")) - .isInstanceOf(ExtendedDoubleHistogramBuilder.class); - Assertions.assertThat(meter.histogramBuilder("test").build()) - .isInstanceOf(ExtendedDoubleHistogram.class); - - Assertions.assertThat(meter.counterBuilder("test")) - .isInstanceOf(ExtendedLongCounterBuilder.class); - Assertions.assertThat(meter.counterBuilder("test").build()) - .isInstanceOf(ExtendedLongCounter.class); - Assertions.assertThat(meter.counterBuilder("test").ofDoubles()) - .isInstanceOf(ExtendedDoubleCounterBuilder.class); - Assertions.assertThat(meter.counterBuilder("test").ofDoubles().build()) - .isInstanceOf(ExtendedDoubleCounter.class); - - Assertions.assertThat(meter.upDownCounterBuilder("test")) - .isInstanceOf(ExtendedLongUpDownCounterBuilder.class); - Assertions.assertThat(meter.upDownCounterBuilder("test").build()) - .isInstanceOf(ExtendedLongUpDownCounter.class); - Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles()) - .isInstanceOf(ExtendedDoubleUpDownCounterBuilder.class); - Assertions.assertThat(meter.upDownCounterBuilder("test").ofDoubles().build()) - .isInstanceOf(ExtendedDoubleUpDownCounter.class); - } - @Test void meterEnabled() { // Setup SdkMeterProvider diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java new file mode 100644 index 00000000000..db659bf725b --- /dev/null +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java @@ -0,0 +1,36 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.api.incubator.trace; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.trace.AbstractDefaultTracerTest; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import org.junit.jupiter.api.Test; + +public class ExtendedDefaultTracerTest extends AbstractDefaultTracerTest { + + @Override + public Tracer getTracer() { + return ExtendedDefaultTracer.getNoop(); + } + + @Override + public TracerProvider getTracerProvider() { + return ExtendedDefaultTracerProvider.getNoop(); + } + + @Test + void incubatingApiIsLoaded() { + Tracer tracer = TracerProvider.noop().get("test"); + assertThat(tracer).isSameAs(OpenTelemetry.noop().getTracer("test")); + + assertThat(tracer).isInstanceOf(ExtendedTracer.class); + assertThat(tracer.spanBuilder("test")).isInstanceOf(ExtendedSpanBuilder.class); + } +} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java index 3650f6978d0..40bf10454de 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedTraceApiUsageTest.java @@ -9,7 +9,6 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static io.opentelemetry.sdk.trace.internal.TracerConfig.disabled; -import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; @@ -17,7 +16,6 @@ import io.opentelemetry.api.trace.TraceFlags; import io.opentelemetry.api.trace.TraceState; import io.opentelemetry.api.trace.Tracer; -import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator; import io.opentelemetry.context.Context; import io.opentelemetry.context.propagation.ContextPropagators; @@ -39,15 +37,6 @@ /** Demonstrating usage of extended Trace API. */ class ExtendedTraceApiUsageTest { - @Test - void incubatingApiIsLoaded() { - Tracer tracer = TracerProvider.noop().get("test"); - assertThat(tracer).isSameAs(OpenTelemetry.noop().getTracer("test")); - - assertThat(tracer).isInstanceOf(ExtendedTracer.class); - assertThat(tracer.spanBuilder("test")).isInstanceOf(ExtendedSpanBuilder.class); - } - @Test void tracerEnabled() { // Setup SdkTracerProvider From 46ac7669669aed97326f796839c5af421d345c37 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 9 Aug 2024 13:44:22 +0200 Subject: [PATCH 04/11] use reflection to load incubating noop apis if available --- .../opentelemetry-api/reflect-config.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json b/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json index c1e8e6414b8..d9abd56c422 100644 --- a/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json +++ b/api/incubator/src/main/resources/META-INF/native-image/io.opentelemetry/opentelemetry-api/reflect-config.json @@ -2,7 +2,7 @@ { "methods": [ { - "name": "getInstance", + "name": "getNoop", "parameterTypes": [] } ], @@ -11,7 +11,7 @@ { "methods": [ { - "name": "getInstance", + "name": "getNoop", "parameterTypes": [] } ], @@ -20,7 +20,7 @@ { "methods": [ { - "name": "getInstance", + "name": "getNoop", "parameterTypes": [] } ], @@ -29,7 +29,7 @@ { "methods": [ { - "name": "getInstance", + "name": "getNoop", "parameterTypes": [] } ], From a44c930af7e8ad04a24c1d6db75bb58bcd0b8ce1 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 9 Aug 2024 16:20:29 +0200 Subject: [PATCH 05/11] use reflection to load incubating noop apis if available --- .../opentelemetry/api/OpenTelemetryTest.java | 5 -- .../api/metrics/DefaultMeterTest.java | 3 - .../api/trace/SpanBuilderTest.java | 65 ---------------- .../api/AbstractOpenTelemetryTest.java | 4 +- .../api/metrics/AbstractDefaultMeterTest.java | 26 ++++++- .../api/trace/AbstractDefaultTracerTest.java | 74 +++++++++++++++---- .../ExtendedDefaultOpenTelemetry.java | 58 --------------- .../trace/ExtendedDefaultTracerBuilder.java | 2 +- .../trace/ExtendedDefaultTracerProvider.java | 6 ++ .../incubator/ExtendedOpenTelemetryTest.java | 17 +++-- .../logs/ExtendedDefaultLoggerTest.java | 9 ++- .../metrics/ExtendedDefaultMeterTest.java | 4 +- .../trace/ExtendedDefaultTracerTest.java | 31 +++++++- 13 files changed, 139 insertions(+), 165 deletions(-) delete mode 100644 api/all/src/test/java/io/opentelemetry/api/trace/SpanBuilderTest.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java diff --git a/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java b/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java index e9038b3bc67..c3754a6df3b 100644 --- a/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/OpenTelemetryTest.java @@ -16,11 +16,6 @@ protected TracerProvider getTracerProvider() { return TracerProvider.noop(); } - @Override - protected OpenTelemetry getOpenTelemetry() { - return OpenTelemetry.noop(); - } - @Override protected MeterProvider getMeterProvider() { return MeterProvider.noop(); diff --git a/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java b/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java index 8136ffe529d..1b012be1d55 100644 --- a/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/metrics/DefaultMeterTest.java @@ -5,9 +5,6 @@ package io.opentelemetry.api.metrics; -import io.opentelemetry.internal.testing.slf4j.SuppressLogger; - -@SuppressLogger() public class DefaultMeterTest extends AbstractDefaultMeterTest { @Override diff --git a/api/all/src/test/java/io/opentelemetry/api/trace/SpanBuilderTest.java b/api/all/src/test/java/io/opentelemetry/api/trace/SpanBuilderTest.java deleted file mode 100644 index a7ed2dc27cb..00000000000 --- a/api/all/src/test/java/io/opentelemetry/api/trace/SpanBuilderTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.trace; - -import static io.opentelemetry.api.common.AttributeKey.stringKey; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatCode; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.context.Context; -import java.time.Instant; -import java.util.concurrent.TimeUnit; -import org.junit.jupiter.api.Test; - -/** Unit tests for {@link SpanBuilder}. */ -class SpanBuilderTest { - private final Tracer tracer = DefaultTracer.getInstance(); - - @Test - void doNotCrash_NoopImplementation() { - assertThatCode( - () -> { - SpanBuilder spanBuilder = tracer.spanBuilder(null); - spanBuilder.setSpanKind(null); - spanBuilder.setParent(null); - spanBuilder.setNoParent(); - spanBuilder.addLink(null); - spanBuilder.addLink(null, Attributes.empty()); - spanBuilder.addLink(SpanContext.getInvalid(), null); - spanBuilder.setAttribute((String) null, "foo"); - spanBuilder.setAttribute("foo", null); - spanBuilder.setAttribute(null, 0L); - spanBuilder.setAttribute(null, 0.0); - spanBuilder.setAttribute(null, false); - spanBuilder.setAttribute((AttributeKey) null, "foo"); - spanBuilder.setAttribute(stringKey(null), "foo"); - spanBuilder.setAttribute(stringKey(""), "foo"); - spanBuilder.setAttribute(stringKey("foo"), null); - spanBuilder.setStartTimestamp(-1, TimeUnit.MILLISECONDS); - spanBuilder.setStartTimestamp(1, null); - spanBuilder.setParent(Context.root().with(Span.wrap(null))); - spanBuilder.setParent(Context.root()); - spanBuilder.setNoParent(); - spanBuilder.addLink(Span.getInvalid().getSpanContext()); - spanBuilder.addLink(Span.getInvalid().getSpanContext(), Attributes.empty()); - spanBuilder.setAttribute("key", "value"); - spanBuilder.setAttribute("key", 12345L); - spanBuilder.setAttribute("key", .12345); - spanBuilder.setAttribute("key", true); - spanBuilder.setAttribute(stringKey("key"), "value"); - spanBuilder.setAllAttributes(Attributes.of(stringKey("key"), "value")); - spanBuilder.setAllAttributes(Attributes.empty()); - spanBuilder.setAllAttributes(null); - spanBuilder.setStartTimestamp(12345L, TimeUnit.NANOSECONDS); - spanBuilder.setStartTimestamp(Instant.EPOCH); - spanBuilder.setStartTimestamp(null); - assertThat(spanBuilder.startSpan().getSpanContext().isValid()).isFalse(); - }) - .doesNotThrowAnyException(); - } -} diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java index 7dae2b70ef0..a55a4bb5131 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java @@ -46,7 +46,9 @@ void testDefault() { protected abstract TracerProvider getTracerProvider(); - protected abstract OpenTelemetry getOpenTelemetry(); + protected OpenTelemetry getOpenTelemetry() { + return OpenTelemetry.noop(); + } protected abstract MeterProvider getMeterProvider(); diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java index ebc9c37f336..43142c0954c 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java @@ -10,7 +10,7 @@ import io.opentelemetry.api.common.Attributes; import io.opentelemetry.context.Context; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; -import org.junit.Test; +import org.junit.jupiter.api.Test; @SuppressLogger() public abstract class AbstractDefaultMeterTest { @@ -128,6 +128,16 @@ public void noopLongGauage_doesNotThrow() { gauge.set(1); gauge.set(2, Attributes.of(stringKey("thing"), "engine")); gauge.set(2, Attributes.of(stringKey("thing"), "engine"), Context.current()); + + ObservableLongMeasurement measurement = + meter + .gaugeBuilder("temperature") + .ofLongs() + .setDescription("The current temperature") + .setUnit("C") + .buildObserver(); + measurement.record(1); + measurement.record(1, Attributes.of(stringKey("thing"), "engine")); } @Test @@ -155,6 +165,15 @@ void noopDoubleGauage_doesNotThrow() { gauge.set(1); gauge.set(2, Attributes.of(stringKey("thing"), "engine")); gauge.set(2, Attributes.of(stringKey("thing"), "engine"), Context.current()); + + ObservableDoubleMeasurement measurement = + meter + .gaugeBuilder("temperature") + .setDescription("The current temperature") + .setUnit("C") + .buildObserver(); + measurement.record(1.0); + measurement.record(1.0, Attributes.of(stringKey("thing"), "engine")); } @Test @@ -223,4 +242,9 @@ public void noopObservableDoubleUpDownCounter_doesNotThrow() { m.record(-27.4, Attributes.of(stringKey("thing"), "engine")); }); } + + @Test + public void noopBatchCallback_doesNotThrow() { + meter.batchCallback(() -> {}, null); + } } diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java index c75fc6d8a93..c9f707262b4 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java @@ -5,12 +5,16 @@ package io.opentelemetry.api.trace; +import static io.opentelemetry.api.common.AttributeKey.stringKey; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; +import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.context.Context; -import org.junit.Test; +import java.time.Instant; +import java.util.concurrent.TimeUnit; +import org.junit.jupiter.api.Test; /** Unit tests for {@link DefaultTracer}. */ // Need to suppress warnings for MustBeClosed because Android 14 does not support @@ -33,15 +37,16 @@ public abstract class AbstractDefaultTracerTest { @Test public void returnsDefaultTracer() { TracerProvider tracerProvider = getTracerProvider(); + Class want = defaultTracer.getClass(); assertThat( tracerProvider .tracerBuilder("test") .setSchemaUrl("schema") .setInstrumentationVersion("1") .build()) - .isInstanceOf(DefaultTracer.class); - assertThat(tracerProvider.get("test")).isInstanceOf(DefaultTracer.class); - assertThat(tracerProvider.get("test", "1.0")).isInstanceOf(DefaultTracer.class); + .isInstanceOf(want); + assertThat(tracerProvider.get("test")).isInstanceOf(want); + assertThat(tracerProvider.get("test", "1.0")).isInstanceOf(want); } @Test @@ -52,12 +57,16 @@ public void defaultSpanBuilderWithName() { @Test public void spanContextPropagationExplicitParent() { - Span span = - defaultTracer - .spanBuilder(SPAN_NAME) - .setParent(Context.root().with(Span.wrap(spanContext))) - .startSpan(); - assertThat(span.getSpanContext()).isSameAs(spanContext); + assertThat( + defaultTracer + .spanBuilder(SPAN_NAME) + .setParent(Context.root().with(Span.wrap(spanContext))) + .startSpan() + .getSpanContext()) + .isSameAs(spanContext); + + SpanBuilder builder = defaultTracer.spanBuilder(SPAN_NAME); + assertThat(builder.setParent(null)).isSameAs(builder); } @Test @@ -100,12 +109,45 @@ public void spanContextPropagation_fromContextThenNoParent() { } @Test - public void addLink() { - Span span = Span.fromContext(Context.root()); - assertThatCode(() -> span.addLink(null)).doesNotThrowAnyException(); - assertThatCode(() -> span.addLink(SpanContext.getInvalid())).doesNotThrowAnyException(); - assertThatCode(() -> span.addLink(null, null)).doesNotThrowAnyException(); - assertThatCode(() -> span.addLink(SpanContext.getInvalid(), Attributes.empty())) + public void doNotCrash_NoopImplementation() { + assertThatCode( + () -> { + SpanBuilder spanBuilder = defaultTracer.spanBuilder(null); + spanBuilder.setSpanKind(null); + spanBuilder.setParent(null); + spanBuilder.setNoParent(); + spanBuilder.addLink(null); + spanBuilder.addLink(null, Attributes.empty()); + spanBuilder.addLink(SpanContext.getInvalid(), null); + spanBuilder.setAttribute((String) null, "foo"); + spanBuilder.setAttribute("foo", null); + spanBuilder.setAttribute(null, 0L); + spanBuilder.setAttribute(null, 0.0); + spanBuilder.setAttribute(null, false); + spanBuilder.setAttribute((AttributeKey) null, "foo"); + spanBuilder.setAttribute(stringKey(null), "foo"); + spanBuilder.setAttribute(stringKey(""), "foo"); + spanBuilder.setAttribute(stringKey("foo"), null); + spanBuilder.setStartTimestamp(-1, TimeUnit.MILLISECONDS); + spanBuilder.setStartTimestamp(1, null); + spanBuilder.setParent(Context.root().with(Span.wrap(null))); + spanBuilder.setParent(Context.root()); + spanBuilder.setNoParent(); + spanBuilder.addLink(Span.getInvalid().getSpanContext()); + spanBuilder.addLink(Span.getInvalid().getSpanContext(), Attributes.empty()); + spanBuilder.setAttribute("key", "value"); + spanBuilder.setAttribute("key", 12345L); + spanBuilder.setAttribute("key", .12345); + spanBuilder.setAttribute("key", true); + spanBuilder.setAttribute(stringKey("key"), "value"); + spanBuilder.setAllAttributes(Attributes.of(stringKey("key"), "value")); + spanBuilder.setAllAttributes(Attributes.empty()); + spanBuilder.setAllAttributes(null); + spanBuilder.setStartTimestamp(12345L, TimeUnit.NANOSECONDS); + spanBuilder.setStartTimestamp(Instant.EPOCH); + spanBuilder.setStartTimestamp(null); + assertThat(spanBuilder.startSpan().getSpanContext().isValid()).isFalse(); + }) .doesNotThrowAnyException(); } } diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java deleted file mode 100644 index f18e52a0953..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/ExtendedDefaultOpenTelemetry.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator; - -import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.api.incubator.metrics.ExtendedDefaultMeterProvider; -import io.opentelemetry.api.incubator.trace.ExtendedDefaultTracerProvider; -import io.opentelemetry.api.metrics.MeterProvider; -import io.opentelemetry.api.trace.TracerProvider; -import io.opentelemetry.context.propagation.ContextPropagators; -import javax.annotation.concurrent.ThreadSafe; - -/** - * The default OpenTelemetry API, which tries to find API implementations via SPI or otherwise falls - * back to no-op default implementations. - */ -@ThreadSafe -final class ExtendedDefaultOpenTelemetry implements OpenTelemetry { - private static final OpenTelemetry NO_OP = - new ExtendedDefaultOpenTelemetry(ContextPropagators.noop()); - - static OpenTelemetry getNoop() { - return NO_OP; - } - - static OpenTelemetry getPropagating(ContextPropagators propagators) { - return new ExtendedDefaultOpenTelemetry(propagators); - } - - private final ContextPropagators propagators; - - ExtendedDefaultOpenTelemetry(ContextPropagators propagators) { - this.propagators = propagators; - } - - @Override - public TracerProvider getTracerProvider() { - return ExtendedDefaultTracerProvider.getNoop(); - } - - @Override - public MeterProvider getMeterProvider() { - return ExtendedDefaultMeterProvider.getNoop(); - } - - @Override - public ContextPropagators getPropagators() { - return propagators; - } - - @Override - public String toString() { - return "DefaultOpenTelemetry{" + "propagators=" + propagators + "}"; - } -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java index fba4782c1e9..20469674ae5 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerBuilder.java @@ -8,7 +8,7 @@ import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.api.trace.TracerBuilder; -class ExtendedDefaultTracerBuilder implements TracerBuilder { +final class ExtendedDefaultTracerBuilder implements TracerBuilder { private static final ExtendedDefaultTracerBuilder INSTANCE = new ExtendedDefaultTracerBuilder(); static TracerBuilder getInstance() { diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java index 04618956446..b7bd2133ad7 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerProvider.java @@ -6,6 +6,7 @@ package io.opentelemetry.api.incubator.trace; import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerBuilder; import io.opentelemetry.api.trace.TracerProvider; import javax.annotation.concurrent.ThreadSafe; @@ -28,5 +29,10 @@ public Tracer get(String instrumentationScopeName, String instrumentationScopeVe return ExtendedDefaultTracer.getNoop(); } + @Override + public TracerBuilder tracerBuilder(String instrumentationScopeName) { + return ExtendedDefaultTracerBuilder.getInstance(); + } + private ExtendedDefaultTracerProvider() {} } diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java index 9cb6147b255..b33c29c8194 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/ExtendedOpenTelemetryTest.java @@ -18,6 +18,7 @@ import io.opentelemetry.api.logs.LoggerProvider; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.context.propagation.ContextPropagators; import org.junit.jupiter.api.Test; class ExtendedOpenTelemetryTest extends AbstractOpenTelemetryTest { @@ -27,11 +28,6 @@ protected TracerProvider getTracerProvider() { return ExtendedDefaultTracerProvider.getNoop(); } - @Override - protected OpenTelemetry getOpenTelemetry() { - return ExtendedDefaultOpenTelemetry.getNoop(); - } - @Override protected MeterProvider getMeterProvider() { return ExtendedDefaultMeterProvider.getNoop(); @@ -44,9 +40,14 @@ protected LoggerProvider getLoggerProvider() { @Test void incubatingApiIsLoaded() { - assertThat(OpenTelemetry.noop().getMeter("test").counterBuilder("test")) + assertIsExtended(OpenTelemetry.noop()); + assertIsExtended(OpenTelemetry.propagating(ContextPropagators.noop())); + } + + private static void assertIsExtended(OpenTelemetry openTelemetry) { + assertThat(openTelemetry.getMeter("test").counterBuilder("test")) .isInstanceOf(ExtendedLongCounterBuilder.class); - assertThat(OpenTelemetry.noop().getLogsBridge().get("test")).isInstanceOf(ExtendedLogger.class); - assertThat(OpenTelemetry.noop().getTracer("test")).isInstanceOf(ExtendedTracer.class); + assertThat(openTelemetry.getLogsBridge().get("test")).isInstanceOf(ExtendedLogger.class); + assertThat(openTelemetry.getTracer("test")).isInstanceOf(ExtendedTracer.class); } } diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java index 9bf6147aa25..1e85c85295a 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java @@ -5,10 +5,11 @@ package io.opentelemetry.api.incubator.logs; +import static org.assertj.core.api.Assertions.assertThat; + import io.opentelemetry.api.logs.AbstractDefaultLoggerTest; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.LoggerProvider; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; class ExtendedDefaultLoggerTest extends AbstractDefaultLoggerTest { @@ -27,7 +28,9 @@ protected Logger getLogger() { void incubatingApiIsLoaded() { Logger logger = LoggerProvider.noop().get("test"); - Assertions.assertThat(logger).isInstanceOf(ExtendedLogger.class); - Assertions.assertThat(logger.logRecordBuilder()).isInstanceOf(ExtendedLogRecordBuilder.class); + assertThat(logger).isInstanceOf(ExtendedLogger.class); + ExtendedLogRecordBuilder builder = (ExtendedLogRecordBuilder) logger.logRecordBuilder(); + assertThat(builder).isInstanceOf(ExtendedLogRecordBuilder.class); + assertThat(builder.setBody(AnyValue.of(0))).isSameAs(builder); } } diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java index ac24978fcac..b6e14cf52b0 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java @@ -11,11 +11,9 @@ import io.opentelemetry.api.metrics.AbstractDefaultMeterTest; import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterProvider; -import io.opentelemetry.internal.testing.slf4j.SuppressLogger; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; -@SuppressLogger() public class ExtendedDefaultMeterTest extends AbstractDefaultMeterTest { @Override @@ -29,7 +27,7 @@ protected MeterProvider getMeterProvider() { } @Test - void incubatingApiIsLoaded() { + public void incubatingApiIsLoaded() { Meter meter = MeterProvider.noop().get("test"); assertThat(meter).isSameAs(OpenTelemetry.noop().getMeter("test")); diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java index db659bf725b..543cfee1ed7 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java @@ -12,6 +12,7 @@ import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.api.trace.TracerProvider; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; public class ExtendedDefaultTracerTest extends AbstractDefaultTracerTest { @@ -26,11 +27,39 @@ public TracerProvider getTracerProvider() { } @Test - void incubatingApiIsLoaded() { + public void incubatingApiIsLoaded() { Tracer tracer = TracerProvider.noop().get("test"); assertThat(tracer).isSameAs(OpenTelemetry.noop().getTracer("test")); assertThat(tracer).isInstanceOf(ExtendedTracer.class); assertThat(tracer.spanBuilder("test")).isInstanceOf(ExtendedSpanBuilder.class); } + + @SuppressWarnings("unchecked") + @Test + public void incubatingApi() { + ExtendedSpanBuilder spanBuilder = + (ExtendedSpanBuilder) ExtendedDefaultTracer.getNoop().spanBuilder("test"); + assertThat(spanBuilder.setParentFrom(null, null)).isSameAs(spanBuilder); + + SpanRunnable spanRunnable = Mockito.mock(SpanRunnable.class); + + spanBuilder.startAndRun(spanRunnable); + Mockito.verify(spanRunnable).runInSpan(); + Mockito.reset(spanRunnable); + + spanBuilder.startAndRun(spanRunnable, null); + Mockito.verify(spanRunnable).runInSpan(); + Mockito.reset(spanRunnable); + + SpanCallable spanCallable = Mockito.mock(SpanCallable.class); + + spanBuilder.startAndCall(spanCallable); + Mockito.verify(spanCallable).callInSpan(); + Mockito.reset(spanCallable); + + spanBuilder.startAndCall(spanCallable, null); + Mockito.verify(spanCallable).callInSpan(); + Mockito.reset(spanCallable); + } } From c39c70efacb7cca5260fdf6eace5852828ed6b31 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 27 Aug 2024 10:14:27 +0200 Subject: [PATCH 06/11] test graal with and without incubating --- .../graal-incubating/build.gradle.kts | 48 +++++++++++++++++++ .../graal/IncubatingApiTests.java} | 2 +- integration-tests/graal/build.gradle.kts | 1 - .../graal/IncubatingNotFoundApiTests.java | 26 ++++++++++ settings.gradle.kts | 1 + 5 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 integration-tests/graal-incubating/build.gradle.kts rename integration-tests/{graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java => graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java} (96%) create mode 100644 integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java diff --git a/integration-tests/graal-incubating/build.gradle.kts b/integration-tests/graal-incubating/build.gradle.kts new file mode 100644 index 00000000000..d050e01dee8 --- /dev/null +++ b/integration-tests/graal-incubating/build.gradle.kts @@ -0,0 +1,48 @@ +plugins { + id("otel.java-conventions") + id("org.graalvm.buildtools.native") +} + +description = "OpenTelemetry Graal Integration Tests (Incubating)" +otelJava.moduleName.set("io.opentelemetry.graal.integration.tests.incubating") + +sourceSets { + main { + // We need to ensure that we have the shadowed classes on the classpath, without this + // we will get the <:sdk:trace-shaded-deps> classes only, without the shadowed ones + val traceShadedDeps = project(":sdk:trace-shaded-deps") + output.dir(traceShadedDeps.file("build/extracted/shadow"), "builtBy" to ":sdk:trace-shaded-deps:extractShadowJar") + } +} + +dependencies { + implementation(project(":sdk:all")) + implementation(project(":sdk:trace-shaded-deps")) + implementation(project(":exporters:otlp:all")) + implementation(project(":api:incubator")) +} + +// org.graalvm.buildtools.native pluging requires java 11+ as of version 0.9.26 +// https://github.com/graalvm/native-build-tools/blob/master/docs/src/docs/asciidoc/index.adoc +tasks { + withType().configureEach { + sourceCompatibility = "11" + targetCompatibility = "11" + options.release.set(11) + } + withType().configureEach { + val testJavaVersion: String? by project + enabled = !testJavaVersion.equals("8") + } +} + +graalvmNative { + binaries { + named("test") { + // Required as of junit 5.10.0: https://junit.org/junit5/docs/5.10.0/release-notes/#deprecations-and-breaking-changes + buildArgs.add("--initialize-at-build-time=org.junit.platform.launcher.core.LauncherConfig") + buildArgs.add("--initialize-at-build-time=org.junit.jupiter.engine.config.InstantiatingConfigurationParameterConverter") + } + } + toolchainDetection.set(false) +} diff --git a/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java b/integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java similarity index 96% rename from integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java rename to integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java index 642e2abd984..318630b5c00 100644 --- a/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubationApiTests.java +++ b/integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java @@ -15,7 +15,7 @@ import io.opentelemetry.api.trace.TracerProvider; import org.junit.jupiter.api.Test; -public class IncubationApiTests { +public class IncubatingApiTests { @Test void incubatingApiIsLoadedViaReflection() { assertThat(LoggerProvider.noop().get("test")).isInstanceOf(ExtendedLogger.class); diff --git a/integration-tests/graal/build.gradle.kts b/integration-tests/graal/build.gradle.kts index ae575ca1677..090be76a2f5 100644 --- a/integration-tests/graal/build.gradle.kts +++ b/integration-tests/graal/build.gradle.kts @@ -19,7 +19,6 @@ dependencies { implementation(project(":sdk:all")) implementation(project(":sdk:trace-shaded-deps")) implementation(project(":exporters:otlp:all")) - implementation(project(":api:incubator")) } // org.graalvm.buildtools.native pluging requires java 11+ as of version 0.9.26 diff --git a/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java b/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java new file mode 100644 index 00000000000..4d98a46196a --- /dev/null +++ b/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.integrationtests.graal; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.logs.Logger; +import io.opentelemetry.api.logs.LoggerProvider; +import io.opentelemetry.api.metrics.LongCounterBuilder; +import io.opentelemetry.api.metrics.MeterProvider; +import io.opentelemetry.api.trace.Tracer; +import io.opentelemetry.api.trace.TracerProvider; +import org.junit.jupiter.api.Test; + +public class IncubatingNotFoundApiTests { + @Test + void incubatingApiIsNotFoundViaReflection() { + assertThat(LoggerProvider.noop().get("test")).isInstanceOf(Logger.class); + assertThat(TracerProvider.noop().get("test")).isInstanceOf(Tracer.class); + assertThat(MeterProvider.noop().get("test").counterBuilder("test")) + .isInstanceOf(LongCounterBuilder.class); + } +} diff --git a/settings.gradle.kts b/settings.gradle.kts index 1ad4058a096..4bf7ca86538 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -48,6 +48,7 @@ include(":integration-tests") include(":integration-tests:otlp") include(":integration-tests:tracecontext") include(":integration-tests:graal") +include(":integration-tests:graal-incubating") include(":opencensus-shim") include(":opentracing-shim") include(":perf-harness") From 0289719eb7528a8a462d6b6ab45ee2f4fd1c4b9b Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 27 Aug 2024 10:29:21 +0200 Subject: [PATCH 07/11] don't load class that doesn't exist --- .../java/io/opentelemetry/api/DefaultOpenTelemetry.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java b/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java index c11c40d2ae6..137a805b73e 100644 --- a/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java +++ b/api/all/src/main/java/io/opentelemetry/api/DefaultOpenTelemetry.java @@ -5,7 +5,6 @@ package io.opentelemetry.api; -import io.opentelemetry.api.internal.IncubatingUtil; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.api.trace.TracerProvider; import io.opentelemetry.context.propagation.ContextPropagators; @@ -17,10 +16,7 @@ */ @ThreadSafe final class DefaultOpenTelemetry implements OpenTelemetry { - private static final OpenTelemetry NO_OP = - IncubatingUtil.incubatingApiIfAvailable( - new DefaultOpenTelemetry(ContextPropagators.noop()), - "io.opentelemetry.api.incubator.ExtendedDefaultOpenTelemetry"); + private static final OpenTelemetry NO_OP = new DefaultOpenTelemetry(ContextPropagators.noop()); static OpenTelemetry getNoop() { return NO_OP; From a46f3e439e8906acefa27f4e9d4bdb9a7e5ce42d Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 27 Aug 2024 10:43:27 +0200 Subject: [PATCH 08/11] javadoc --- .../java/io/opentelemetry/api/AbstractOpenTelemetryTest.java | 1 + .../io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java | 1 + .../io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java | 1 + 3 files changed, 3 insertions(+) diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java index a55a4bb5131..6ba27d89760 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/AbstractOpenTelemetryTest.java @@ -17,6 +17,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; +/** Unit tests for {@link OpenTelemetry}. */ public abstract class AbstractOpenTelemetryTest { @BeforeAll public static void beforeClass() { diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java index 9f2669e5788..c70cb68f3b4 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/logs/AbstractDefaultLoggerTest.java @@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; +/** Unit tests for {@link DefaultLogger}. */ public abstract class AbstractDefaultLoggerTest { protected abstract LoggerProvider getLoggerProvider(); diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java index 43142c0954c..239db09b024 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java @@ -12,6 +12,7 @@ import io.opentelemetry.internal.testing.slf4j.SuppressLogger; import org.junit.jupiter.api.Test; +/** Unit tests for {@link DefaultMeter}. */ @SuppressLogger() public abstract class AbstractDefaultMeterTest { private final Meter meter = getMeter(); From 516d92fbb20305fe04dd347dc0856d9e59a3b57f Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 27 Aug 2024 11:45:36 +0200 Subject: [PATCH 09/11] reduce visibility --- .../api/trace/DefaultTracerTest.java | 2 +- .../api/metrics/AbstractDefaultMeterTest.java | 32 +++++++++---------- .../api/trace/AbstractDefaultTracerTest.java | 18 +++++------ .../metrics/ExtendedDefaultMeterTest.java | 2 +- .../trace/ExtendedDefaultTracerTest.java | 2 +- .../graal/IncubatingApiTests.java | 2 +- .../graal/IncubatingNotFoundApiTests.java | 2 +- 7 files changed, 30 insertions(+), 30 deletions(-) diff --git a/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java b/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java index 791050f8c4c..f0f577d9946 100644 --- a/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java +++ b/api/all/src/test/java/io/opentelemetry/api/trace/DefaultTracerTest.java @@ -5,7 +5,7 @@ package io.opentelemetry.api.trace; -public class DefaultTracerTest extends AbstractDefaultTracerTest { +class DefaultTracerTest extends AbstractDefaultTracerTest { @Override public Tracer getTracer() { diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java index 239db09b024..d81bc4f8810 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java @@ -22,13 +22,13 @@ public abstract class AbstractDefaultMeterTest { protected abstract MeterProvider getMeterProvider(); @Test - public void noopMeterProvider_getDoesNotThrow() { + void noopMeterProvider_getDoesNotThrow() { MeterProvider provider = getMeterProvider(); provider.get("user-instrumentation"); } @Test - public void noopMeterProvider_builderDoesNotThrow() { + void noopMeterProvider_builderDoesNotThrow() { MeterProvider provider = getMeterProvider(); provider.meterBuilder("user-instrumentation").build(); provider.meterBuilder("advanced-instrumetnation").setInstrumentationVersion("1.0").build(); @@ -41,7 +41,7 @@ public void noopMeterProvider_builderDoesNotThrow() { } @Test - public void noopLongCounter_doesNotThrow() { + void noopLongCounter_doesNotThrow() { LongCounter counter = meter.counterBuilder("size").setDescription("The size I'm measuring").setUnit("1").build(); counter.add(1); @@ -50,7 +50,7 @@ public void noopLongCounter_doesNotThrow() { } @Test - public void noopDoubleCounter_doesNotThrow() { + void noopDoubleCounter_doesNotThrow() { DoubleCounter counter = meter .counterBuilder("size") @@ -64,7 +64,7 @@ public void noopDoubleCounter_doesNotThrow() { } @Test - public void noopLongUpDownCounter_doesNotThrow() { + void noopLongUpDownCounter_doesNotThrow() { LongUpDownCounter counter = meter .upDownCounterBuilder("size") @@ -77,7 +77,7 @@ public void noopLongUpDownCounter_doesNotThrow() { } @Test - public void noopDoubleUpDownCounter_doesNotThrow() { + void noopDoubleUpDownCounter_doesNotThrow() { DoubleUpDownCounter counter = meter .upDownCounterBuilder("size") @@ -91,7 +91,7 @@ public void noopDoubleUpDownCounter_doesNotThrow() { } @Test - public void noopLongHistogram_doesNotThrow() { + void noopLongHistogram_doesNotThrow() { LongHistogram histogram = meter .histogramBuilder("size") @@ -105,7 +105,7 @@ public void noopLongHistogram_doesNotThrow() { } @Test - public void noopDoubleHistogram_doesNotThrow() { + void noopDoubleHistogram_doesNotThrow() { DoubleHistogram histogram = meter .histogramBuilder("size") @@ -118,7 +118,7 @@ public void noopDoubleHistogram_doesNotThrow() { } @Test - public void noopLongGauage_doesNotThrow() { + void noopLongGauage_doesNotThrow() { LongGauge gauge = meter .gaugeBuilder("temperature") @@ -142,7 +142,7 @@ public void noopLongGauage_doesNotThrow() { } @Test - public void noopObservableLongGauage_doesNotThrow() { + void noopObservableLongGauage_doesNotThrow() { meter .gaugeBuilder("temperature") .ofLongs() @@ -178,7 +178,7 @@ void noopDoubleGauage_doesNotThrow() { } @Test - public void noopObservableDoubleGauage_doesNotThrow() { + void noopObservableDoubleGauage_doesNotThrow() { meter .gaugeBuilder("temperature") .setDescription("The current temperature") @@ -191,7 +191,7 @@ public void noopObservableDoubleGauage_doesNotThrow() { } @Test - public void noopObservableLongCounter_doesNotThrow() { + void noopObservableLongCounter_doesNotThrow() { meter .counterBuilder("temperature") .setDescription("The current temperature") @@ -204,7 +204,7 @@ public void noopObservableLongCounter_doesNotThrow() { } @Test - public void noopObservableDoubleCounter_doesNotThrow() { + void noopObservableDoubleCounter_doesNotThrow() { meter .counterBuilder("temperature") .ofDoubles() @@ -218,7 +218,7 @@ public void noopObservableDoubleCounter_doesNotThrow() { } @Test - public void noopObservableLongUpDownCounter_doesNotThrow() { + void noopObservableLongUpDownCounter_doesNotThrow() { meter .upDownCounterBuilder("temperature") .setDescription("The current temperature") @@ -231,7 +231,7 @@ public void noopObservableLongUpDownCounter_doesNotThrow() { } @Test - public void noopObservableDoubleUpDownCounter_doesNotThrow() { + void noopObservableDoubleUpDownCounter_doesNotThrow() { meter .upDownCounterBuilder("temperature") .ofDoubles() @@ -245,7 +245,7 @@ public void noopObservableDoubleUpDownCounter_doesNotThrow() { } @Test - public void noopBatchCallback_doesNotThrow() { + void noopBatchCallback_doesNotThrow() { meter.batchCallback(() -> {}, null); } } diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java index c9f707262b4..4f075547bf9 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java @@ -35,7 +35,7 @@ public abstract class AbstractDefaultTracerTest { public abstract TracerProvider getTracerProvider(); @Test - public void returnsDefaultTracer() { + void returnsDefaultTracer() { TracerProvider tracerProvider = getTracerProvider(); Class want = defaultTracer.getClass(); assertThat( @@ -50,13 +50,13 @@ public void returnsDefaultTracer() { } @Test - public void defaultSpanBuilderWithName() { + void defaultSpanBuilderWithName() { assertThat(defaultTracer.spanBuilder(SPAN_NAME).startSpan().getSpanContext().isValid()) .isFalse(); } @Test - public void spanContextPropagationExplicitParent() { + void spanContextPropagationExplicitParent() { assertThat( defaultTracer .spanBuilder(SPAN_NAME) @@ -70,7 +70,7 @@ public void spanContextPropagationExplicitParent() { } @Test - public void spanContextPropagation() { + void spanContextPropagation() { Span parent = Span.wrap(spanContext); Span span = @@ -79,13 +79,13 @@ public void spanContextPropagation() { } @Test - public void noSpanContextMakesInvalidSpans() { + void noSpanContextMakesInvalidSpans() { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getSpanContext()).isSameAs(SpanContext.getInvalid()); } @Test - public void spanContextPropagation_fromContext() { + void spanContextPropagation_fromContext() { Context context = Context.current().with(Span.wrap(spanContext)); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); @@ -93,7 +93,7 @@ public void spanContextPropagation_fromContext() { } @Test - public void spanContextPropagation_fromContextAfterNoParent() { + void spanContextPropagation_fromContextAfterNoParent() { Context context = Context.current().with(Span.wrap(spanContext)); Span span = defaultTracer.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan(); @@ -101,7 +101,7 @@ public void spanContextPropagation_fromContextAfterNoParent() { } @Test - public void spanContextPropagation_fromContextThenNoParent() { + void spanContextPropagation_fromContextThenNoParent() { Context context = Context.current().with(Span.wrap(spanContext)); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).setNoParent().startSpan(); @@ -109,7 +109,7 @@ public void spanContextPropagation_fromContextThenNoParent() { } @Test - public void doNotCrash_NoopImplementation() { + void doNotCrash_NoopImplementation() { assertThatCode( () -> { SpanBuilder spanBuilder = defaultTracer.spanBuilder(null); diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java index b6e14cf52b0..3d342a33661 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/metrics/ExtendedDefaultMeterTest.java @@ -14,7 +14,7 @@ import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; -public class ExtendedDefaultMeterTest extends AbstractDefaultMeterTest { +class ExtendedDefaultMeterTest extends AbstractDefaultMeterTest { @Override protected Meter getMeter() { diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java index 543cfee1ed7..1550796d42d 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/trace/ExtendedDefaultTracerTest.java @@ -14,7 +14,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; -public class ExtendedDefaultTracerTest extends AbstractDefaultTracerTest { +class ExtendedDefaultTracerTest extends AbstractDefaultTracerTest { @Override public Tracer getTracer() { diff --git a/integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java b/integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java index 318630b5c00..8d705419452 100644 --- a/integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java +++ b/integration-tests/graal-incubating/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingApiTests.java @@ -15,7 +15,7 @@ import io.opentelemetry.api.trace.TracerProvider; import org.junit.jupiter.api.Test; -public class IncubatingApiTests { +class IncubatingApiTests { @Test void incubatingApiIsLoadedViaReflection() { assertThat(LoggerProvider.noop().get("test")).isInstanceOf(ExtendedLogger.class); diff --git a/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java b/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java index 4d98a46196a..ba2ba02a2c3 100644 --- a/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java +++ b/integration-tests/graal/src/test/java/io/opentelemetry/integrationtests/graal/IncubatingNotFoundApiTests.java @@ -15,7 +15,7 @@ import io.opentelemetry.api.trace.TracerProvider; import org.junit.jupiter.api.Test; -public class IncubatingNotFoundApiTests { +class IncubatingNotFoundApiTests { @Test void incubatingApiIsNotFoundViaReflection() { assertThat(LoggerProvider.noop().get("test")).isInstanceOf(Logger.class); From a95e6ff99a72d5849212212475d26361dee26a5f Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Tue, 27 Aug 2024 11:46:14 +0200 Subject: [PATCH 10/11] format --- .../api/metrics/AbstractDefaultMeterTest.java | 4 ++-- .../api/trace/AbstractDefaultTracerTest.java | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java index d81bc4f8810..b64c0929803 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/metrics/AbstractDefaultMeterTest.java @@ -231,7 +231,7 @@ void noopObservableLongUpDownCounter_doesNotThrow() { } @Test - void noopObservableDoubleUpDownCounter_doesNotThrow() { + void noopObservableDoubleUpDownCounter_doesNotThrow() { meter .upDownCounterBuilder("temperature") .ofDoubles() @@ -245,7 +245,7 @@ void noopObservableDoubleUpDownCounter_doesNotThrow() { } @Test - void noopBatchCallback_doesNotThrow() { + void noopBatchCallback_doesNotThrow() { meter.batchCallback(() -> {}, null); } } diff --git a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java index 4f075547bf9..1933e289d5b 100644 --- a/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java +++ b/api/all/src/testFixtures/java/io/opentelemetry/api/trace/AbstractDefaultTracerTest.java @@ -35,7 +35,7 @@ public abstract class AbstractDefaultTracerTest { public abstract TracerProvider getTracerProvider(); @Test - void returnsDefaultTracer() { + void returnsDefaultTracer() { TracerProvider tracerProvider = getTracerProvider(); Class want = defaultTracer.getClass(); assertThat( @@ -50,13 +50,13 @@ void returnsDefaultTracer() { } @Test - void defaultSpanBuilderWithName() { + void defaultSpanBuilderWithName() { assertThat(defaultTracer.spanBuilder(SPAN_NAME).startSpan().getSpanContext().isValid()) .isFalse(); } @Test - void spanContextPropagationExplicitParent() { + void spanContextPropagationExplicitParent() { assertThat( defaultTracer .spanBuilder(SPAN_NAME) @@ -70,7 +70,7 @@ void spanContextPropagationExplicitParent() { } @Test - void spanContextPropagation() { + void spanContextPropagation() { Span parent = Span.wrap(spanContext); Span span = @@ -79,13 +79,13 @@ void spanContextPropagation() { } @Test - void noSpanContextMakesInvalidSpans() { + void noSpanContextMakesInvalidSpans() { Span span = defaultTracer.spanBuilder(SPAN_NAME).startSpan(); assertThat(span.getSpanContext()).isSameAs(SpanContext.getInvalid()); } @Test - void spanContextPropagation_fromContext() { + void spanContextPropagation_fromContext() { Context context = Context.current().with(Span.wrap(spanContext)); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).startSpan(); @@ -93,7 +93,7 @@ void spanContextPropagation_fromContext() { } @Test - void spanContextPropagation_fromContextAfterNoParent() { + void spanContextPropagation_fromContextAfterNoParent() { Context context = Context.current().with(Span.wrap(spanContext)); Span span = defaultTracer.spanBuilder(SPAN_NAME).setNoParent().setParent(context).startSpan(); @@ -101,7 +101,7 @@ void spanContextPropagation_fromContextAfterNoParent() { } @Test - void spanContextPropagation_fromContextThenNoParent() { + void spanContextPropagation_fromContextThenNoParent() { Context context = Context.current().with(Span.wrap(spanContext)); Span span = defaultTracer.spanBuilder(SPAN_NAME).setParent(context).setNoParent().startSpan(); @@ -109,7 +109,7 @@ void spanContextPropagation_fromContextThenNoParent() { } @Test - void doNotCrash_NoopImplementation() { + void doNotCrash_NoopImplementation() { assertThatCode( () -> { SpanBuilder spanBuilder = defaultTracer.spanBuilder(null); From e7d71142417bbe435c0b3f462dbbc84d3cf11f52 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 5 Sep 2024 15:45:19 -0500 Subject: [PATCH 11/11] fix build --- .../api/incubator/logs/ExtendedDefaultLogger.java | 3 ++- .../api/incubator/logs/ExtendedDefaultLoggerTest.java | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java index b76b2087065..3e8dce08e74 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java @@ -6,6 +6,7 @@ package io.opentelemetry.api.incubator.logs; import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Value; import io.opentelemetry.api.logs.LogRecordBuilder; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.Severity; @@ -74,7 +75,7 @@ public LogRecordBuilder setBody(String body) { } @Override - public LogRecordBuilder setBody(AnyValue body) { + public LogRecordBuilder setBody(Value body) { return this; } diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java index 1e85c85295a..e558b53fcf1 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLoggerTest.java @@ -7,6 +7,7 @@ import static org.assertj.core.api.Assertions.assertThat; +import io.opentelemetry.api.common.Value; import io.opentelemetry.api.logs.AbstractDefaultLoggerTest; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.LoggerProvider; @@ -31,6 +32,6 @@ void incubatingApiIsLoaded() { assertThat(logger).isInstanceOf(ExtendedLogger.class); ExtendedLogRecordBuilder builder = (ExtendedLogRecordBuilder) logger.logRecordBuilder(); assertThat(builder).isInstanceOf(ExtendedLogRecordBuilder.class); - assertThat(builder.setBody(AnyValue.of(0))).isSameAs(builder); + assertThat(builder.setBody(Value.of(0))).isSameAs(builder); } }