From d9261e410db82c54c61ff4630bb7024b96b60018 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Wed, 11 Dec 2024 08:52:08 -0600 Subject: [PATCH 1/7] wip --- sdk/metrics/build.gradle.kts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/metrics/build.gradle.kts b/sdk/metrics/build.gradle.kts index 0fffd03457e..5c174b8e4d6 100644 --- a/sdk/metrics/build.gradle.kts +++ b/sdk/metrics/build.gradle.kts @@ -15,7 +15,7 @@ otelJava.moduleName.set("io.opentelemetry.sdk.metrics") dependencies { api(project(":api:all")) api(project(":sdk:common")) - implementation(project(":api:incubator")) + compileOnly(project(":api:incubator")) compileOnly("org.codehaus.mojo:animal-sniffer-annotations") @@ -23,6 +23,7 @@ dependencies { testAnnotationProcessor("com.google.auto.value:auto-value") + testImplementation(project(":api:incubator")) testImplementation(project(":sdk:testing")) testImplementation("com.google.guava:guava") testImplementation("com.google.guava:guava-testlib") From 6361036a497730dfe40e0ddab8a6c1482492c3b9 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Wed, 11 Dec 2024 12:25:20 -0600 Subject: [PATCH 2/7] Remove alpha artifacts from runtime classpath of stable artifacts --- .../kotlin/otel.java-conventions.gradle.kts | 4 +- exporters/otlp/common/build.gradle.kts | 2 +- sdk-extensions/autoconfigure/build.gradle.kts | 7 +- ...AutoConfiguredOpenTelemetrySdkBuilder.java | 10 +- .../sdk/autoconfigure/IncubatingUtil.java | 19 +++ .../AutoConfiguredOpenTelemetrySdkTest.java | 8 - ...onfigureGlobalEventLoggerProviderTest.java | 62 +++++++ sdk/logs/build.gradle.kts | 20 ++- .../sdk/logs/ExtendedSdkLogRecordBuilder.java | 86 ++++++++++ .../sdk/logs/ExtendedSdkLogger.java | 29 ++++ .../sdk/logs/IncubatingUtil.java | 26 +++ .../sdk/logs/SdkLogRecordBuilder.java | 3 +- .../io/opentelemetry/sdk/logs/SdkLogger.java | 29 +++- .../sdk/logs/SdkLoggerProvider.java | 2 +- .../opentelemetry/sdk/logs/ValueBodyTest.java | 16 +- .../sdk/logs/LoggerConfigTest.java | 0 .../logs/internal/SdkEventBuilderTest.java | 0 .../internal/SdkEventLoggerProviderTest.java | 0 sdk/metrics/build.gradle.kts | 8 +- .../sdk/metrics/ExtendedSdkDoubleCounter.java | 51 ++++++ .../sdk/metrics/ExtendedSdkDoubleGauge.java | 50 ++++++ .../metrics/ExtendedSdkDoubleHistogram.java | 52 ++++++ .../ExtendedSdkDoubleUpDownCounter.java | 53 ++++++ .../sdk/metrics/ExtendedSdkLongCounter.java | 51 ++++++ .../sdk/metrics/ExtendedSdkLongGauge.java | 51 ++++++ .../sdk/metrics/ExtendedSdkLongHistogram.java | 51 ++++++ .../metrics/ExtendedSdkLongUpDownCounter.java | 53 ++++++ .../sdk/metrics/IncubatingUtil.java | 34 ++++ .../sdk/metrics/SdkDoubleCounter.java | 28 +-- .../sdk/metrics/SdkDoubleGauge.java | 29 +--- .../sdk/metrics/SdkDoubleHistogram.java | 30 +--- .../sdk/metrics/SdkDoubleUpDownCounter.java | 29 +--- .../sdk/metrics/SdkLongCounter.java | 28 +-- .../sdk/metrics/SdkLongGauge.java | 30 +--- .../sdk/metrics/SdkLongHistogram.java | 30 +--- .../sdk/metrics/SdkLongUpDownCounter.java | 27 +-- .../opentelemetry/sdk/metrics/SdkMeter.java | 48 ++++-- .../sdk/metrics/IdentityTest.java | 9 +- .../sdk/metrics/AttributesAdviceTest.java | 0 .../sdk/metrics/MeterConfigTest.java | 0 sdk/testing/build.gradle.kts | 3 +- sdk/trace/build.gradle.kts | 20 ++- .../sdk/trace/ExtendedSdkSpanBuilder.java | 159 ++++++++++++++++++ .../sdk/trace/ExtendedSdkTracer.java | 30 ++++ .../sdk/trace/IncubatingUtil.java | 30 ++++ .../sdk/trace/SdkSpanBuilder.java | 77 ++------- .../io/opentelemetry/sdk/trace/SdkTracer.java | 31 +++- .../sdk/trace/SdkTracerProvider.java | 2 +- .../sdk/trace/TracerConfigTest.java | 9 +- 49 files changed, 1115 insertions(+), 311 deletions(-) create mode 100644 sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java create mode 100644 sdk-extensions/autoconfigure/src/testIncubating/java/io/opentelemetry/sdk/autoconfigure/AutoconfigureGlobalEventLoggerProviderTest.java create mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java create mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogger.java create mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java rename sdk/logs/src/{test => testIncubating}/java/io/opentelemetry/sdk/logs/LoggerConfigTest.java (100%) rename sdk/logs/src/{test => testIncubating}/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilderTest.java (100%) rename sdk/logs/src/{test => testIncubating}/java/io/opentelemetry/sdk/logs/internal/SdkEventLoggerProviderTest.java (100%) create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleCounter.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleGauge.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleHistogram.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleUpDownCounter.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongCounter.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongGauge.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongHistogram.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongUpDownCounter.java create mode 100644 sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java rename sdk/metrics/src/{test => testIncubating}/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java (100%) rename sdk/metrics/src/{test => testIncubating}/java/io/opentelemetry/sdk/metrics/MeterConfigTest.java (100%) create mode 100644 sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkSpanBuilder.java create mode 100644 sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkTracer.java create mode 100644 sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java rename sdk/trace/src/{test => testIncubating}/java/io/opentelemetry/sdk/trace/TracerConfigTest.java (95%) diff --git a/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts index 26a845f5c8a..c3b713a682f 100644 --- a/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts @@ -86,9 +86,7 @@ tasks { // https://groups.google.com/forum/#!topic/bazel-discuss/_R3A9TJSoPM "-Xlint:-processing", // We suppress the "options" warning because it prevents compilation on modern JDKs - "-Xlint:-options", - // Fail build on any warning - "-Werror", + "-Xlint:-options" ), ) } diff --git a/exporters/otlp/common/build.gradle.kts b/exporters/otlp/common/build.gradle.kts index 99b054f3d5e..0e74e8829a7 100644 --- a/exporters/otlp/common/build.gradle.kts +++ b/exporters/otlp/common/build.gradle.kts @@ -16,7 +16,6 @@ dependencies { protoSource("io.opentelemetry.proto:opentelemetry-proto:${versions["io.opentelemetry.proto"]}") api(project(":exporters:common")) - implementation(project(":api:incubator")) compileOnly(project(":sdk:metrics")) compileOnly(project(":sdk:trace")) @@ -32,6 +31,7 @@ dependencies { testImplementation("com.google.guava:guava") testImplementation("io.opentelemetry.proto:opentelemetry-proto") + jmhImplementation(project(":api:incubator")) jmhImplementation(project(":sdk:testing")) jmhImplementation("com.fasterxml.jackson.core:jackson-core") jmhImplementation("io.opentelemetry.proto:opentelemetry-proto") diff --git a/sdk-extensions/autoconfigure/build.gradle.kts b/sdk-extensions/autoconfigure/build.gradle.kts index fb73bdaa6b6..7f0077178e5 100644 --- a/sdk-extensions/autoconfigure/build.gradle.kts +++ b/sdk-extensions/autoconfigure/build.gradle.kts @@ -10,7 +10,7 @@ dependencies { api(project(":sdk:all")) api(project(":sdk-extensions:autoconfigure-spi")) - implementation(project(":api:incubator")) + compileOnly(project(":api:incubator")) annotationProcessor("com.google.auto.value:auto-value") @@ -23,6 +23,11 @@ dependencies { testing { suites { + register("testIncubating") { + dependencies { + implementation(project(":api:incubator")) + } + } register("testAutoConfigureOrder") { targets { all { diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java index 1c293a002cd..74796d3ed0e 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java @@ -8,7 +8,6 @@ import static java.util.Objects.requireNonNull; import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider; import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.context.propagation.TextMapPropagator; import io.opentelemetry.sdk.OpenTelemetrySdk; @@ -26,7 +25,6 @@ import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; import io.opentelemetry.sdk.logs.export.LogRecordExporter; -import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; import io.opentelemetry.sdk.metrics.export.MetricExporter; @@ -592,8 +590,12 @@ private void maybeSetAsGlobal(OpenTelemetrySdk openTelemetrySdk) { return; } GlobalOpenTelemetry.set(openTelemetrySdk); - GlobalEventLoggerProvider.set( - SdkEventLoggerProvider.create(openTelemetrySdk.getSdkLoggerProvider())); + try { + Class.forName("io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider"); + IncubatingUtil.setGlobalEventLoggerProvider(openTelemetrySdk.getSdkLoggerProvider()); + } catch (Exception e) { + // do nothing + } logger.log( Level.FINE, "Global OpenTelemetry set to {0} by autoconfiguration", openTelemetrySdk); } diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java new file mode 100644 index 00000000000..2526cda38ed --- /dev/null +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java @@ -0,0 +1,19 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure; + +import io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider; +import io.opentelemetry.sdk.logs.SdkLoggerProvider; +import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider; + +final class IncubatingUtil { + + private IncubatingUtil() {} + + static void setGlobalEventLoggerProvider(SdkLoggerProvider sdkLoggerProvider) { + GlobalEventLoggerProvider.set(SdkEventLoggerProvider.create(sdkLoggerProvider)); + } +} diff --git a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java index f74b248c04d..b42c1936130 100644 --- a/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java +++ b/sdk-extensions/autoconfigure/src/test/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkTest.java @@ -26,7 +26,6 @@ import io.github.netmikey.logunit.api.LogCapturer; import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.api.OpenTelemetry; -import io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanId; import io.opentelemetry.api.trace.TraceId; @@ -49,7 +48,6 @@ import io.opentelemetry.sdk.logs.LogRecordProcessor; import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; -import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider; import io.opentelemetry.sdk.metrics.SdkMeterProvider; import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; import io.opentelemetry.sdk.metrics.export.MetricReader; @@ -156,7 +154,6 @@ public SdkLoggerProviderBuilder apply( @BeforeEach void resetGlobal() { GlobalOpenTelemetry.resetForTest(); - GlobalEventLoggerProvider.resetForTest(); builder = AutoConfiguredOpenTelemetrySdk.builder() .addPropertiesSupplier(disableExportPropertySupplier()); @@ -456,7 +453,6 @@ void builder_setResultAsGlobalFalse() { OpenTelemetrySdk openTelemetry = builder.build().getOpenTelemetrySdk(); assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isNotSameAs(openTelemetry); - assertThat(GlobalEventLoggerProvider.get()).isNotSameAs(openTelemetry.getSdkLoggerProvider()); } @Test @@ -464,10 +460,6 @@ void builder_setResultAsGlobalTrue() { OpenTelemetrySdk openTelemetry = builder.setResultAsGlobal().build().getOpenTelemetrySdk(); assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isSameAs(openTelemetry); - assertThat(GlobalEventLoggerProvider.get()) - .isInstanceOf(SdkEventLoggerProvider.class) - .extracting("delegateLoggerProvider") - .isSameAs(openTelemetry.getSdkLoggerProvider()); } @Test diff --git a/sdk-extensions/autoconfigure/src/testIncubating/java/io/opentelemetry/sdk/autoconfigure/AutoconfigureGlobalEventLoggerProviderTest.java b/sdk-extensions/autoconfigure/src/testIncubating/java/io/opentelemetry/sdk/autoconfigure/AutoconfigureGlobalEventLoggerProviderTest.java new file mode 100644 index 00000000000..8ca85372ada --- /dev/null +++ b/sdk-extensions/autoconfigure/src/testIncubating/java/io/opentelemetry/sdk/autoconfigure/AutoconfigureGlobalEventLoggerProviderTest.java @@ -0,0 +1,62 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.autoconfigure; + +import static org.assertj.core.api.Assertions.assertThat; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider; +import io.opentelemetry.sdk.OpenTelemetrySdk; +import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class AutoconfigureGlobalEventLoggerProviderTest { + + private AutoConfiguredOpenTelemetrySdkBuilder builder; + + @BeforeEach + void resetGlobal() { + GlobalOpenTelemetry.resetForTest(); + GlobalEventLoggerProvider.resetForTest(); + builder = + AutoConfiguredOpenTelemetrySdk.builder() + .addPropertiesSupplier(disableExportPropertySupplier()); + } + + @Test + void builder_setResultAsGlobalFalse() { + GlobalOpenTelemetry.set(OpenTelemetry.noop()); + + OpenTelemetrySdk openTelemetry = builder.build().getOpenTelemetrySdk(); + + assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isNotSameAs(openTelemetry); + assertThat(GlobalEventLoggerProvider.get()).isNotSameAs(openTelemetry.getSdkLoggerProvider()); + } + + @Test + void builder_setResultAsGlobalTrue() { + OpenTelemetrySdk openTelemetry = builder.setResultAsGlobal().build().getOpenTelemetrySdk(); + + assertThat(GlobalOpenTelemetry.get()).extracting("delegate").isSameAs(openTelemetry); + assertThat(GlobalEventLoggerProvider.get()) + .isInstanceOf(SdkEventLoggerProvider.class) + .extracting("delegateLoggerProvider") + .isSameAs(openTelemetry.getSdkLoggerProvider()); + } + + private static Supplier> disableExportPropertySupplier() { + Map props = new HashMap<>(); + props.put("otel.metrics.exporter", "none"); + props.put("otel.traces.exporter", "none"); + props.put("otel.logs.exporter", "none"); + return () -> props; + } +} diff --git a/sdk/logs/build.gradle.kts b/sdk/logs/build.gradle.kts index 7c8c020d6a2..c0c740d3743 100644 --- a/sdk/logs/build.gradle.kts +++ b/sdk/logs/build.gradle.kts @@ -12,7 +12,7 @@ otelJava.moduleName.set("io.opentelemetry.sdk.logs") dependencies { api(project(":api:all")) api(project(":sdk:common")) - implementation(project(":api:incubator")) + compileOnly(project(":api:incubator")) annotationProcessor("com.google.auto.value:auto-value") @@ -21,3 +21,21 @@ dependencies { testImplementation("org.awaitility:awaitility") testImplementation("com.google.guava:guava") } + +testing { + suites { + register("testIncubating") { + dependencies { + implementation(project(":sdk:testing")) + implementation(project(":api:incubator")) + implementation("com.google.guava:guava") + } + } + } +} + +tasks { + check { + dependsOn(testing.suites) + } +} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java new file mode 100644 index 00000000000..ecaab256377 --- /dev/null +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java @@ -0,0 +1,86 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.logs; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.common.Value; +import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; +import io.opentelemetry.api.logs.LogRecordBuilder; +import io.opentelemetry.api.logs.Severity; +import io.opentelemetry.context.Context; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import java.time.Instant; +import java.util.concurrent.TimeUnit; + +/** SDK implementation of {@link ExtendedLogRecordBuilder}. */ +final class ExtendedSdkLogRecordBuilder extends SdkLogRecordBuilder + implements ExtendedLogRecordBuilder { + + ExtendedSdkLogRecordBuilder( + LoggerSharedState loggerSharedState, InstrumentationScopeInfo instrumentationScopeInfo) { + super(loggerSharedState, instrumentationScopeInfo); + } + + @Override + public ExtendedSdkLogRecordBuilder setTimestamp(long timestamp, TimeUnit unit) { + super.setTimestamp(timestamp, unit); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setTimestamp(Instant instant) { + super.setTimestamp(instant); + return this; + } + + @Override + public LogRecordBuilder setObservedTimestamp(long timestamp, TimeUnit unit) { + super.setObservedTimestamp(timestamp, unit); + return this; + } + + @Override + public LogRecordBuilder setObservedTimestamp(Instant instant) { + super.setObservedTimestamp(instant); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setContext(Context context) { + super.setContext(context); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setSeverity(Severity severity) { + super.setSeverity(severity); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setSeverityText(String severityText) { + super.setSeverityText(severityText); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setBody(String body) { + super.setBody(body); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setBody(Value value) { + super.setBody(value); + return this; + } + + @Override + public ExtendedSdkLogRecordBuilder setAttribute(AttributeKey key, T value) { + super.setAttribute(key, value); + return this; + } +} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogger.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogger.java new file mode 100644 index 00000000000..67813bb933d --- /dev/null +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogger.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.logs; + +import io.opentelemetry.api.incubator.logs.ExtendedLogger; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.logs.internal.LoggerConfig; + +/** SDK implementation of {@link ExtendedLogger}. */ +final class ExtendedSdkLogger extends SdkLogger implements ExtendedLogger { + + private final boolean loggerEnabled; + + ExtendedSdkLogger( + LoggerSharedState loggerSharedState, + InstrumentationScopeInfo instrumentationScopeInfo, + LoggerConfig loggerConfig) { + super(loggerSharedState, instrumentationScopeInfo, loggerConfig); + this.loggerEnabled = loggerConfig.isEnabled(); + } + + @Override + public boolean isEnabled() { + return loggerEnabled; + } +} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java new file mode 100644 index 00000000000..70ad8bb30e3 --- /dev/null +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.logs; + +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.logs.internal.LoggerConfig; + +final class IncubatingUtil { + + private IncubatingUtil() {} + + static SdkLogger createIncubatingLogger( + LoggerSharedState sharedState, + InstrumentationScopeInfo instrumentationScopeInfo, + LoggerConfig tracerConfig) { + return new ExtendedSdkLogger(sharedState, instrumentationScopeInfo, tracerConfig); + } + + static SdkLogRecordBuilder createIncubatingLogRecordBuilder( + LoggerSharedState loggerSharedState, InstrumentationScopeInfo instrumentationScopeInfo) { + return new ExtendedSdkLogRecordBuilder(loggerSharedState, instrumentationScopeInfo); + } +} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java index ce77d076730..ce43bc8d2c6 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java @@ -7,7 +7,6 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; import io.opentelemetry.api.logs.LogRecordBuilder; import io.opentelemetry.api.logs.Severity; import io.opentelemetry.api.trace.Span; @@ -19,7 +18,7 @@ import javax.annotation.Nullable; /** SDK implementation of {@link LogRecordBuilder}. */ -final class SdkLogRecordBuilder implements ExtendedLogRecordBuilder { +class SdkLogRecordBuilder implements LogRecordBuilder { private final LoggerSharedState loggerSharedState; private final LogLimits logLimits; diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java index 72fb9f0b356..1832ae9dad5 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java @@ -5,7 +5,6 @@ package io.opentelemetry.sdk.logs; -import io.opentelemetry.api.incubator.logs.ExtendedLogger; import io.opentelemetry.api.logs.LogRecordBuilder; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.LoggerProvider; @@ -13,7 +12,7 @@ import io.opentelemetry.sdk.logs.internal.LoggerConfig; /** SDK implementation of {@link Logger}. */ -final class SdkLogger implements ExtendedLogger { +class SdkLogger implements Logger { private static final Logger NOOP_LOGGER = LoggerProvider.noop().get("noop"); @@ -30,10 +29,29 @@ final class SdkLogger implements ExtendedLogger { this.loggerEnabled = loggerConfig.isEnabled(); } + static SdkLogger create( + LoggerSharedState sharedState, + InstrumentationScopeInfo instrumentationScopeInfo, + LoggerConfig loggerConfig) { + try { + Class.forName("io.opentelemetry.api.incubator.logs.ExtendedLogger"); + return IncubatingUtil.createIncubatingLogger( + sharedState, instrumentationScopeInfo, loggerConfig); + } catch (Exception e) { + return new SdkLogger(sharedState, instrumentationScopeInfo, loggerConfig); + } + } + @Override public LogRecordBuilder logRecordBuilder() { if (loggerEnabled) { - return new SdkLogRecordBuilder(loggerSharedState, instrumentationScopeInfo); + try { + Class.forName("io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder"); + return IncubatingUtil.createIncubatingLogRecordBuilder( + loggerSharedState, instrumentationScopeInfo); + } catch (Exception e) { + return new SdkLogRecordBuilder(loggerSharedState, instrumentationScopeInfo); + } } return NOOP_LOGGER.logRecordBuilder(); } @@ -42,9 +60,4 @@ public LogRecordBuilder logRecordBuilder() { InstrumentationScopeInfo getInstrumentationScopeInfo() { return instrumentationScopeInfo; } - - @Override - public boolean isEnabled() { - return loggerEnabled; - } } diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProvider.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProvider.java index 2d7b87e6b47..ea68a6c2a5c 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProvider.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLoggerProvider.java @@ -60,7 +60,7 @@ public static SdkLoggerProviderBuilder builder() { this.loggerComponentRegistry = new ComponentRegistry<>( instrumentationScopeInfo -> - new SdkLogger( + SdkLogger.create( sharedState, instrumentationScopeInfo, getLoggerConfig(instrumentationScopeInfo))); diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ValueBodyTest.java b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ValueBodyTest.java index cd5820bb974..954ba6bdb63 100644 --- a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ValueBodyTest.java +++ b/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/ValueBodyTest.java @@ -10,7 +10,6 @@ import io.opentelemetry.api.common.KeyValue; import io.opentelemetry.api.common.Value; import io.opentelemetry.api.common.ValueType; -import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; @@ -32,7 +31,7 @@ void valueBody() { Logger logger = provider.get(ValueBodyTest.class.getName()); // Value can be a primitive type, like a string, long, double, boolean - extendedLogRecordBuilder(logger).setBody(Value.of(1)).emit(); + logger.logRecordBuilder().setBody(Value.of(1)).emit(); assertThat(exporter.getFinishedLogRecordItems()) .hasSize(1) .satisfiesExactly( @@ -48,7 +47,8 @@ void valueBody() { exporter.reset(); // ...or a byte array of raw data - extendedLogRecordBuilder(logger) + logger + .logRecordBuilder() .setBody(Value.of("hello world".getBytes(StandardCharsets.UTF_8))) .emit(); assertThat(exporter.getFinishedLogRecordItems()) @@ -68,7 +68,8 @@ void valueBody() { exporter.reset(); // But most commonly it will be used to represent complex structured like a map - extendedLogRecordBuilder(logger) + logger + .logRecordBuilder() .setBody( // The protocol data structure uses a repeated KeyValue to represent a map: // https://github.com/open-telemetry/opentelemetry-proto/blob/ac3242b03157295e4ee9e616af53b81517b06559/opentelemetry/proto/common/v1/common.proto#L59 @@ -145,7 +146,8 @@ void valueBody() { exporter.reset(); // ..or an array (optionally with heterogeneous types) - extendedLogRecordBuilder(logger) + logger + .logRecordBuilder() .setBody(Value.of(Value.of("entry1"), Value.of("entry2"), Value.of(3))) .emit(); assertThat(exporter.getFinishedLogRecordItems()) @@ -164,8 +166,4 @@ void valueBody() { }); exporter.reset(); } - - ExtendedLogRecordBuilder extendedLogRecordBuilder(Logger logger) { - return (ExtendedLogRecordBuilder) logger.logRecordBuilder(); - } } diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/LoggerConfigTest.java b/sdk/logs/src/testIncubating/java/io/opentelemetry/sdk/logs/LoggerConfigTest.java similarity index 100% rename from sdk/logs/src/test/java/io/opentelemetry/sdk/logs/LoggerConfigTest.java rename to sdk/logs/src/testIncubating/java/io/opentelemetry/sdk/logs/LoggerConfigTest.java diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilderTest.java b/sdk/logs/src/testIncubating/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilderTest.java similarity index 100% rename from sdk/logs/src/test/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilderTest.java rename to sdk/logs/src/testIncubating/java/io/opentelemetry/sdk/logs/internal/SdkEventBuilderTest.java diff --git a/sdk/logs/src/test/java/io/opentelemetry/sdk/logs/internal/SdkEventLoggerProviderTest.java b/sdk/logs/src/testIncubating/java/io/opentelemetry/sdk/logs/internal/SdkEventLoggerProviderTest.java similarity index 100% rename from sdk/logs/src/test/java/io/opentelemetry/sdk/logs/internal/SdkEventLoggerProviderTest.java rename to sdk/logs/src/testIncubating/java/io/opentelemetry/sdk/logs/internal/SdkEventLoggerProviderTest.java diff --git a/sdk/metrics/build.gradle.kts b/sdk/metrics/build.gradle.kts index 5c174b8e4d6..a0b667943c8 100644 --- a/sdk/metrics/build.gradle.kts +++ b/sdk/metrics/build.gradle.kts @@ -23,7 +23,6 @@ dependencies { testAnnotationProcessor("com.google.auto.value:auto-value") - testImplementation(project(":api:incubator")) testImplementation(project(":sdk:testing")) testImplementation("com.google.guava:guava") testImplementation("com.google.guava:guava-testlib") @@ -38,6 +37,13 @@ dependencyCheck { testing { suites { + register("testIncubating") { + dependencies { + implementation(project(":sdk:testing")) + implementation(project(":api:incubator")) + implementation("com.google.guava:guava") + } + } register("debugEnabledTest") { targets { all { diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleCounter.java new file mode 100644 index 00000000000..050429f8f52 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleCounter.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleCounter; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleCounterBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkDoubleCounter extends SdkDoubleCounter implements ExtendedDoubleCounter { + + private ExtendedSdkDoubleCounter( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkDoubleCounterBuilder extends SdkDoubleCounterBuilder + implements ExtendedDoubleCounterBuilder { + + ExtendedSdkDoubleCounterBuilder( + SdkMeter sdkMeter, + String name, + String description, + String unit, + Advice.AdviceBuilder adviceBuilder) { + super(sdkMeter, name, description, unit, adviceBuilder); + } + + @Override + public ExtendedSdkDoubleCounter build() { + return builder.buildSynchronousInstrument(ExtendedSdkDoubleCounter::new); + } + + @Override + public ExtendedDoubleCounterBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleGauge.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleGauge.java new file mode 100644 index 00000000000..def79d8bb65 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleGauge.java @@ -0,0 +1,50 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleGauge; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleGaugeBuilder; +import io.opentelemetry.api.incubator.metrics.ExtendedLongGaugeBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkDoubleGauge extends SdkDoubleGauge implements ExtendedDoubleGauge { + + private ExtendedSdkDoubleGauge( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkDoubleGaugeBuilder extends SdkDoubleGaugeBuilder + implements ExtendedDoubleGaugeBuilder { + ExtendedSdkDoubleGaugeBuilder(SdkMeter sdkMeter, String name) { + super(sdkMeter, name); + } + + @Override + public ExtendedSdkDoubleGauge build() { + return builder.buildSynchronousInstrument(ExtendedSdkDoubleGauge::new); + } + + @Override + public ExtendedDoubleGaugeBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + + @Override + public ExtendedLongGaugeBuilder ofLongs() { + return builder.swapBuilder(ExtendedSdkLongGauge.ExtendedSdkLongGaugeBuilder::new); + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleHistogram.java new file mode 100644 index 00000000000..76afb84214f --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleHistogram.java @@ -0,0 +1,52 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogram; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogramBuilder; +import io.opentelemetry.api.incubator.metrics.ExtendedLongHistogramBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkDoubleHistogram extends SdkDoubleHistogram + implements ExtendedDoubleHistogram { + + ExtendedSdkDoubleHistogram( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkDoubleHistogramBuilder extends SdkDoubleHistogramBuilder + implements ExtendedDoubleHistogramBuilder { + + ExtendedSdkDoubleHistogramBuilder(SdkMeter sdkMeter, String name) { + super(sdkMeter, name); + } + + @Override + public ExtendedSdkDoubleHistogram build() { + return builder.buildSynchronousInstrument(ExtendedSdkDoubleHistogram::new); + } + + @Override + public ExtendedLongHistogramBuilder ofLongs() { + return builder.swapBuilder(ExtendedSdkLongHistogram.ExtendedSdkLongHistogramBuilder::new); + } + + @Override + public ExtendedDoubleHistogramBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleUpDownCounter.java new file mode 100644 index 00000000000..3e7ae4bdb11 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkDoubleUpDownCounter.java @@ -0,0 +1,53 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleUpDownCounter; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleUpDownCounterBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkDoubleUpDownCounter extends SdkDoubleUpDownCounter + implements ExtendedDoubleUpDownCounter { + + private ExtendedSdkDoubleUpDownCounter( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkDoubleUpDownCounterBuilder extends SdkDoubleUpDownCounterBuilder + implements ExtendedDoubleUpDownCounterBuilder { + + ExtendedSdkDoubleUpDownCounterBuilder( + SdkMeter sdkMeter, + String name, + String description, + String unit, + Advice.AdviceBuilder adviceBuilder) { + super(sdkMeter, name, description, unit, adviceBuilder); + } + + @Override + public ExtendedDoubleUpDownCounter build() { + return builder.buildSynchronousInstrument(ExtendedSdkDoubleUpDownCounter::new); + } + + @Override + public ExtendedDoubleUpDownCounterBuilder setAttributesAdvice( + List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongCounter.java new file mode 100644 index 00000000000..f87e3407184 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongCounter.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleCounterBuilder; +import io.opentelemetry.api.incubator.metrics.ExtendedLongCounter; +import io.opentelemetry.api.incubator.metrics.ExtendedLongCounterBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkLongCounter extends SdkLongCounter implements ExtendedLongCounter { + + private ExtendedSdkLongCounter( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkLongCounterBuilder extends SdkLongCounterBuilder + implements ExtendedLongCounterBuilder { + + ExtendedSdkLongCounterBuilder(SdkMeter sdkMeter, String name) { + super(sdkMeter, name); + } + + @Override + public ExtendedSdkLongCounter build() { + return builder.buildSynchronousInstrument(ExtendedSdkLongCounter::new); + } + + @Override + public ExtendedDoubleCounterBuilder ofDoubles() { + return builder.swapBuilder(ExtendedSdkDoubleCounter.ExtendedSdkDoubleCounterBuilder::new); + } + + @Override + public ExtendedLongCounterBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongGauge.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongGauge.java new file mode 100644 index 00000000000..94845f4bef5 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongGauge.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedLongGauge; +import io.opentelemetry.api.incubator.metrics.ExtendedLongGaugeBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkLongGauge extends SdkLongGauge implements ExtendedLongGauge { + + private ExtendedSdkLongGauge( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkLongGaugeBuilder extends SdkLongGaugeBuilder + implements ExtendedLongGaugeBuilder { + + ExtendedSdkLongGaugeBuilder( + SdkMeter sdkMeter, + String name, + String description, + String unit, + Advice.AdviceBuilder adviceBuilder) { + super(sdkMeter, name, description, unit, adviceBuilder); + } + + @Override + public ExtendedSdkLongGauge build() { + return builder.buildSynchronousInstrument(ExtendedSdkLongGauge::new); + } + + @Override + public ExtendedLongGaugeBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongHistogram.java new file mode 100644 index 00000000000..e10851efd54 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongHistogram.java @@ -0,0 +1,51 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedLongHistogram; +import io.opentelemetry.api.incubator.metrics.ExtendedLongHistogramBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkLongHistogram extends SdkLongHistogram implements ExtendedLongHistogram { + + private ExtendedSdkLongHistogram( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkLongHistogramBuilder extends SdkLongHistogramBuilder + implements ExtendedLongHistogramBuilder { + + ExtendedSdkLongHistogramBuilder( + SdkMeter sdkMeter, + String name, + String description, + String unit, + Advice.AdviceBuilder adviceBuilder) { + super(sdkMeter, name, description, unit, adviceBuilder); + } + + @Override + public ExtendedSdkLongHistogram build() { + return builder.buildSynchronousInstrument(ExtendedSdkLongHistogram::new); + } + + @Override + public ExtendedLongHistogramBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongUpDownCounter.java new file mode 100644 index 00000000000..53be08fed63 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ExtendedSdkLongUpDownCounter.java @@ -0,0 +1,53 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleUpDownCounterBuilder; +import io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounter; +import io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounterBuilder; +import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; +import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; +import java.util.List; + +final class ExtendedSdkLongUpDownCounter extends SdkLongUpDownCounter + implements ExtendedLongUpDownCounter { + + private ExtendedSdkLongUpDownCounter( + InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + super(descriptor, sdkMeter, storage); + } + + @Override + public boolean isEnabled() { + return sdkMeter.isMeterEnabled() && storage.isEnabled(); + } + + static final class ExtendedSdkLongUpDownCounterBuilder extends SdkLongUpDownCounterBuilder + implements ExtendedLongUpDownCounterBuilder { + + ExtendedSdkLongUpDownCounterBuilder(SdkMeter sdkMeter, String name) { + super(sdkMeter, name); + } + + @Override + public ExtendedLongUpDownCounter build() { + return builder.buildSynchronousInstrument(ExtendedSdkLongUpDownCounter::new); + } + + @Override + public ExtendedDoubleUpDownCounterBuilder ofDoubles() { + return builder.swapBuilder( + ExtendedSdkDoubleUpDownCounter.ExtendedSdkDoubleUpDownCounterBuilder::new); + } + + @Override + public ExtendedLongUpDownCounterBuilder setAttributesAdvice(List> attributes) { + builder.setAdviceAttributes(attributes); + return this; + } + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java new file mode 100644 index 00000000000..726c938b547 --- /dev/null +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import io.opentelemetry.api.metrics.DoubleGaugeBuilder; +import io.opentelemetry.api.metrics.DoubleHistogramBuilder; +import io.opentelemetry.api.metrics.LongCounterBuilder; +import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; + +final class IncubatingUtil { + + private IncubatingUtil() {} + + static LongCounterBuilder createIncubatingLongCounterBuilder(SdkMeter sdkMeter, String name) { + return new ExtendedSdkLongCounter.ExtendedSdkLongCounterBuilder(sdkMeter, name); + } + + static LongUpDownCounterBuilder createIncubatingLongUpDownCounterBuilder( + SdkMeter sdkMeter, String name) { + return new ExtendedSdkLongUpDownCounter.ExtendedSdkLongUpDownCounterBuilder(sdkMeter, name); + } + + static DoubleHistogramBuilder createIncubatingDoubleHistogramBuilder( + SdkMeter sdkMeter, String name) { + return new ExtendedSdkDoubleHistogram.ExtendedSdkDoubleHistogramBuilder(sdkMeter, name); + } + + static DoubleGaugeBuilder createIncubatingDoubleGaugemBuilder(SdkMeter sdkMeter, String name) { + return new ExtendedSdkDoubleGauge.ExtendedSdkDoubleGaugeBuilder(sdkMeter, name); + } +} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java index de645016076..7fd6e435556 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleCounter.java @@ -5,10 +5,8 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleCounter; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleCounterBuilder; +import io.opentelemetry.api.metrics.DoubleCounter; import io.opentelemetry.api.metrics.DoubleCounterBuilder; import io.opentelemetry.api.metrics.ObservableDoubleCounter; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; @@ -17,19 +15,18 @@ import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; -import java.util.List; import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; -final class SdkDoubleCounter extends AbstractInstrument implements ExtendedDoubleCounter { +class SdkDoubleCounter extends AbstractInstrument implements DoubleCounter { private static final Logger logger = Logger.getLogger(SdkDoubleCounter.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkDoubleCounter( + SdkDoubleCounter( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -59,14 +56,9 @@ public void add(double increment) { add(increment, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkDoubleCounterBuilder implements DoubleCounterBuilder { - static final class SdkDoubleCounterBuilder implements ExtendedDoubleCounterBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkDoubleCounterBuilder( SdkMeter sdkMeter, @@ -109,12 +101,6 @@ public ObservableDoubleMeasurement buildObserver() { return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); } - @Override - public ExtendedDoubleCounterBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public String toString() { return builder.toStringHelper(getClass().getSimpleName()); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java index 03b3f43838d..97076603bb5 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleGauge.java @@ -5,10 +5,8 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleGauge; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleGaugeBuilder; +import io.opentelemetry.api.metrics.DoubleGauge; import io.opentelemetry.api.metrics.DoubleGaugeBuilder; import io.opentelemetry.api.metrics.LongGaugeBuilder; import io.opentelemetry.api.metrics.ObservableDoubleGauge; @@ -16,15 +14,14 @@ import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; -import java.util.List; import java.util.function.Consumer; -final class SdkDoubleGauge extends AbstractInstrument implements ExtendedDoubleGauge { +class SdkDoubleGauge extends AbstractInstrument implements DoubleGauge { - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkDoubleGauge( + SdkDoubleGauge( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -46,16 +43,10 @@ public void set(double increment) { set(increment, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } - - static final class SdkDoubleGaugeBuilder implements ExtendedDoubleGaugeBuilder { - private final InstrumentBuilder builder; + static class SdkDoubleGaugeBuilder implements DoubleGaugeBuilder { + final InstrumentBuilder builder; SdkDoubleGaugeBuilder(SdkMeter sdkMeter, String name) { - builder = new InstrumentBuilder(name, InstrumentType.GAUGE, InstrumentValueType.DOUBLE, sdkMeter); } @@ -77,12 +68,6 @@ public SdkDoubleGauge build() { return builder.buildSynchronousInstrument(SdkDoubleGauge::new); } - @Override - public ExtendedDoubleGaugeBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public LongGaugeBuilder ofLongs() { return builder.swapBuilder(SdkLongGauge.SdkLongGaugeBuilder::new); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java index 5aa8f49de31..de4473ace2e 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleHistogram.java @@ -5,10 +5,8 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogram; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogramBuilder; +import io.opentelemetry.api.metrics.DoubleHistogram; import io.opentelemetry.api.metrics.DoubleHistogramBuilder; import io.opentelemetry.api.metrics.LongHistogramBuilder; import io.opentelemetry.context.Context; @@ -21,14 +19,14 @@ import java.util.logging.Level; import java.util.logging.Logger; -final class SdkDoubleHistogram extends AbstractInstrument implements ExtendedDoubleHistogram { +class SdkDoubleHistogram extends AbstractInstrument implements DoubleHistogram { private static final Logger logger = Logger.getLogger(SdkDoubleHistogram.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkDoubleHistogram( + SdkDoubleHistogram( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -58,14 +56,9 @@ public void record(double value) { record(value, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkDoubleHistogramBuilder implements DoubleHistogramBuilder { - static final class SdkDoubleHistogramBuilder implements ExtendedDoubleHistogramBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkDoubleHistogramBuilder(SdkMeter sdkMeter, String name) { builder = @@ -96,8 +89,7 @@ public LongHistogramBuilder ofLongs() { } @Override - public ExtendedDoubleHistogramBuilder setExplicitBucketBoundariesAdvice( - List bucketBoundaries) { + public DoubleHistogramBuilder setExplicitBucketBoundariesAdvice(List bucketBoundaries) { try { Objects.requireNonNull(bucketBoundaries, "bucketBoundaries must not be null"); ExplicitBucketHistogramUtils.validateBucketBoundaries(bucketBoundaries); @@ -109,12 +101,6 @@ public ExtendedDoubleHistogramBuilder setExplicitBucketBoundariesAdvice( return this; } - @Override - public ExtendedDoubleHistogramBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public String toString() { return builder.toStringHelper(getClass().getSimpleName()); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java index fe21e295c98..4231f58fdc0 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkDoubleUpDownCounter.java @@ -5,10 +5,7 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleUpDownCounter; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.DoubleUpDownCounter; import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.ObservableDoubleMeasurement; @@ -17,16 +14,14 @@ import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; -import java.util.List; import java.util.function.Consumer; -final class SdkDoubleUpDownCounter extends AbstractInstrument - implements ExtendedDoubleUpDownCounter { +class SdkDoubleUpDownCounter extends AbstractInstrument implements DoubleUpDownCounter { - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkDoubleUpDownCounter( + SdkDoubleUpDownCounter( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -48,14 +43,9 @@ public void add(double increment) { add(increment, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkDoubleUpDownCounterBuilder implements DoubleUpDownCounterBuilder { - static final class SdkDoubleUpDownCounterBuilder implements ExtendedDoubleUpDownCounterBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkDoubleUpDownCounterBuilder( SdkMeter sdkMeter, @@ -100,13 +90,6 @@ public ObservableDoubleMeasurement buildObserver() { return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); } - @Override - public ExtendedDoubleUpDownCounterBuilder setAttributesAdvice( - List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public String toString() { return builder.toStringHelper(getClass().getSimpleName()); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java index df2f9b4e660..0de901060f5 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongCounter.java @@ -5,11 +5,9 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedLongCounter; -import io.opentelemetry.api.incubator.metrics.ExtendedLongCounterBuilder; import io.opentelemetry.api.metrics.DoubleCounterBuilder; +import io.opentelemetry.api.metrics.LongCounter; import io.opentelemetry.api.metrics.LongCounterBuilder; import io.opentelemetry.api.metrics.ObservableLongCounter; import io.opentelemetry.api.metrics.ObservableLongMeasurement; @@ -17,20 +15,19 @@ import io.opentelemetry.sdk.internal.ThrottlingLogger; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; -import java.util.List; import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; -final class SdkLongCounter extends AbstractInstrument implements ExtendedLongCounter { +class SdkLongCounter extends AbstractInstrument implements LongCounter { private static final Logger logger = Logger.getLogger(SdkLongCounter.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkLongCounter( + SdkLongCounter( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -60,14 +57,9 @@ public void add(long increment) { add(increment, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkLongCounterBuilder implements LongCounterBuilder { - static final class SdkLongCounterBuilder implements ExtendedLongCounterBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkLongCounterBuilder(SdkMeter sdkMeter, String name) { this.builder = @@ -106,12 +98,6 @@ public ObservableLongMeasurement buildObserver() { return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_COUNTER); } - @Override - public ExtendedLongCounterBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public String toString() { return builder.toStringHelper(getClass().getSimpleName()); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java index 499c4a4443a..5c6aafbb9ef 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongGauge.java @@ -5,10 +5,8 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedLongGauge; -import io.opentelemetry.api.incubator.metrics.ExtendedLongGaugeBuilder; +import io.opentelemetry.api.metrics.LongGauge; import io.opentelemetry.api.metrics.LongGaugeBuilder; import io.opentelemetry.api.metrics.ObservableLongGauge; import io.opentelemetry.api.metrics.ObservableLongMeasurement; @@ -16,16 +14,14 @@ import io.opentelemetry.sdk.metrics.internal.descriptor.Advice; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; -import java.util.List; import java.util.function.Consumer; -final class SdkLongGauge extends AbstractInstrument implements ExtendedLongGauge { +class SdkLongGauge extends AbstractInstrument implements LongGauge { - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkLongGauge( - InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { + SdkLongGauge(InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; this.storage = storage; @@ -46,14 +42,9 @@ public void set(long increment) { set(increment, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkLongGaugeBuilder implements LongGaugeBuilder { - static final class SdkLongGaugeBuilder implements ExtendedLongGaugeBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkLongGaugeBuilder( SdkMeter sdkMeter, @@ -61,7 +52,6 @@ static final class SdkLongGaugeBuilder implements ExtendedLongGaugeBuilder { String description, String unit, Advice.AdviceBuilder adviceBuilder) { - builder = new InstrumentBuilder(name, InstrumentType.GAUGE, InstrumentValueType.LONG, sdkMeter) .setDescription(description) @@ -86,12 +76,6 @@ public SdkLongGauge build() { return builder.buildSynchronousInstrument(SdkLongGauge::new); } - @Override - public ExtendedLongGaugeBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public ObservableLongGauge buildWithCallback(Consumer callback) { return builder.buildLongAsynchronousInstrument(InstrumentType.OBSERVABLE_GAUGE, callback); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java index 0822b1eff57..b777ad34e59 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongHistogram.java @@ -5,10 +5,8 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedLongHistogram; -import io.opentelemetry.api.incubator.metrics.ExtendedLongHistogramBuilder; +import io.opentelemetry.api.metrics.LongHistogram; import io.opentelemetry.api.metrics.LongHistogramBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.internal.ThrottlingLogger; @@ -22,14 +20,14 @@ import java.util.logging.Logger; import java.util.stream.Collectors; -final class SdkLongHistogram extends AbstractInstrument implements ExtendedLongHistogram { +class SdkLongHistogram extends AbstractInstrument implements LongHistogram { private static final Logger logger = Logger.getLogger(SdkLongHistogram.class.getName()); private final ThrottlingLogger throttlingLogger = new ThrottlingLogger(logger); - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkLongHistogram( + SdkLongHistogram( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -59,14 +57,9 @@ public void record(long value) { record(value, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkLongHistogramBuilder implements LongHistogramBuilder { - static final class SdkLongHistogramBuilder implements ExtendedLongHistogramBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkLongHistogramBuilder( SdkMeter sdkMeter, @@ -99,8 +92,7 @@ public SdkLongHistogram build() { } @Override - public ExtendedLongHistogramBuilder setExplicitBucketBoundariesAdvice( - List bucketBoundaries) { + public LongHistogramBuilder setExplicitBucketBoundariesAdvice(List bucketBoundaries) { List boundaries; try { Objects.requireNonNull(bucketBoundaries, "bucketBoundaries must not be null"); @@ -114,12 +106,6 @@ public ExtendedLongHistogramBuilder setExplicitBucketBoundariesAdvice( return this; } - @Override - public ExtendedLongHistogramBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public String toString() { return builder.toStringHelper(getClass().getSimpleName()); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java index daf1e2f97a8..dece771f4b6 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkLongUpDownCounter.java @@ -5,10 +5,7 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounter; -import io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounterBuilder; import io.opentelemetry.api.metrics.DoubleUpDownCounterBuilder; import io.opentelemetry.api.metrics.LongUpDownCounter; import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; @@ -17,15 +14,14 @@ import io.opentelemetry.context.Context; import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.internal.state.WriteableMetricStorage; -import java.util.List; import java.util.function.Consumer; -final class SdkLongUpDownCounter extends AbstractInstrument implements ExtendedLongUpDownCounter { +class SdkLongUpDownCounter extends AbstractInstrument implements LongUpDownCounter { - private final SdkMeter sdkMeter; - private final WriteableMetricStorage storage; + final SdkMeter sdkMeter; + final WriteableMetricStorage storage; - private SdkLongUpDownCounter( + SdkLongUpDownCounter( InstrumentDescriptor descriptor, SdkMeter sdkMeter, WriteableMetricStorage storage) { super(descriptor); this.sdkMeter = sdkMeter; @@ -47,14 +43,9 @@ public void add(long increment) { add(increment, Attributes.empty()); } - @Override - public boolean isEnabled() { - return sdkMeter.isMeterEnabled() && storage.isEnabled(); - } + static class SdkLongUpDownCounterBuilder implements LongUpDownCounterBuilder { - static final class SdkLongUpDownCounterBuilder implements ExtendedLongUpDownCounterBuilder { - - private final InstrumentBuilder builder; + final InstrumentBuilder builder; SdkLongUpDownCounterBuilder(SdkMeter sdkMeter, String name) { this.builder = @@ -96,12 +87,6 @@ public ObservableLongMeasurement buildObserver() { return builder.buildObservableMeasurement(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER); } - @Override - public ExtendedLongUpDownCounterBuilder setAttributesAdvice(List> attributes) { - builder.setAdviceAttributes(attributes); - return this; - } - @Override public String toString() { return builder.toStringHelper(getClass().getSimpleName()); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java index fe278cba4d6..49ec6fbf7de 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java @@ -145,30 +145,54 @@ void resetForTest() { @Override public LongCounterBuilder counterBuilder(String name) { - return checkValidInstrumentName(name) - ? new SdkLongCounter.SdkLongCounterBuilder(this, name) - : NOOP_METER.counterBuilder(NOOP_INSTRUMENT_NAME); + if (!checkValidInstrumentName(name)) { + return NOOP_METER.counterBuilder(NOOP_INSTRUMENT_NAME); + } + try { + Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedLongCounter"); + return IncubatingUtil.createIncubatingLongCounterBuilder(this, name); + } catch (Exception e) { + return new SdkLongCounter.SdkLongCounterBuilder(this, name); + } } @Override public LongUpDownCounterBuilder upDownCounterBuilder(String name) { - return checkValidInstrumentName(name) - ? new SdkLongUpDownCounter.SdkLongUpDownCounterBuilder(this, name) - : NOOP_METER.upDownCounterBuilder(NOOP_INSTRUMENT_NAME); + if (!checkValidInstrumentName(name)) { + return NOOP_METER.upDownCounterBuilder(NOOP_INSTRUMENT_NAME); + } + try { + Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounter"); + return IncubatingUtil.createIncubatingLongUpDownCounterBuilder(this, name); + } catch (Exception e) { + return new SdkLongUpDownCounter.SdkLongUpDownCounterBuilder(this, name); + } } @Override public DoubleHistogramBuilder histogramBuilder(String name) { - return checkValidInstrumentName(name) - ? new SdkDoubleHistogram.SdkDoubleHistogramBuilder(this, name) - : NOOP_METER.histogramBuilder(NOOP_INSTRUMENT_NAME); + if (!checkValidInstrumentName(name)) { + return NOOP_METER.histogramBuilder(NOOP_INSTRUMENT_NAME); + } + try { + Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogram"); + return IncubatingUtil.createIncubatingDoubleHistogramBuilder(this, name); + } catch (Exception e) { + return new SdkDoubleHistogram.SdkDoubleHistogramBuilder(this, name); + } } @Override public DoubleGaugeBuilder gaugeBuilder(String name) { - return checkValidInstrumentName(name) - ? new SdkDoubleGauge.SdkDoubleGaugeBuilder(this, name) - : NOOP_METER.gaugeBuilder(NOOP_INSTRUMENT_NAME); + if (!checkValidInstrumentName(name)) { + return NOOP_METER.gaugeBuilder(NOOP_INSTRUMENT_NAME); + } + try { + Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedDoubleGauge"); + return IncubatingUtil.createIncubatingDoubleGaugemBuilder(this, name); + } catch (Exception e) { + return new SdkDoubleGauge.SdkDoubleGaugeBuilder(this, name); + } } @Override diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/IdentityTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/IdentityTest.java index 981d84dcd82..195c8730e45 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/IdentityTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/IdentityTest.java @@ -8,7 +8,6 @@ import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import io.github.netmikey.logunit.api.LogCapturer; -import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogramBuilder; import io.opentelemetry.internal.testing.slf4j.SuppressLogger; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.internal.state.MetricStorageRegistry; @@ -222,13 +221,17 @@ void sameMeterSameInstrumentNameDifferentNonIdentifyingFieldsNoViews() { // Register histogram1, with and without advice. First registration without advice wins. meterProvider.get("meter1").histogramBuilder("histogram1").build().record(8); - ((ExtendedDoubleHistogramBuilder) meterProvider.get("meter1").histogramBuilder("histogram1")) + meterProvider + .get("meter1") + .histogramBuilder("histogram1") .setExplicitBucketBoundariesAdvice(Arrays.asList(10.0, 20.0, 30.0)) .build() .record(8); // Register histogram2, with and without advice. First registration with advice wins. - ((ExtendedDoubleHistogramBuilder) meterProvider.get("meter1").histogramBuilder("histogram2")) + meterProvider + .get("meter1") + .histogramBuilder("histogram2") .setExplicitBucketBoundariesAdvice(Arrays.asList(10.0, 20.0, 30.0)) .build() .record(8); diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java b/sdk/metrics/src/testIncubating/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java similarity index 100% rename from sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java rename to sdk/metrics/src/testIncubating/java/io/opentelemetry/sdk/metrics/AttributesAdviceTest.java diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterConfigTest.java b/sdk/metrics/src/testIncubating/java/io/opentelemetry/sdk/metrics/MeterConfigTest.java similarity index 100% rename from sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/MeterConfigTest.java rename to sdk/metrics/src/testIncubating/java/io/opentelemetry/sdk/metrics/MeterConfigTest.java diff --git a/sdk/testing/build.gradle.kts b/sdk/testing/build.gradle.kts index 4d3f3aa4ec8..f47b0d98d77 100644 --- a/sdk/testing/build.gradle.kts +++ b/sdk/testing/build.gradle.kts @@ -8,7 +8,7 @@ otelJava.moduleName.set("io.opentelemetry.sdk.testing") dependencies { api(project(":api:all")) - api(project(":api:incubator")) + compileOnly(project(":api:incubator")) api(project(":sdk:all")) compileOnly("org.assertj:assertj-core") @@ -17,6 +17,7 @@ dependencies { annotationProcessor("com.google.auto.value:auto-value") + testImplementation(project(":api:incubator")) testImplementation("junit:junit") testImplementation("org.junit.vintage:junit-vintage-engine") } diff --git a/sdk/trace/build.gradle.kts b/sdk/trace/build.gradle.kts index 2dc5c769508..35d30c8513b 100644 --- a/sdk/trace/build.gradle.kts +++ b/sdk/trace/build.gradle.kts @@ -22,8 +22,7 @@ dependencies { api(project(":api:all")) api(project(":sdk:common")) - implementation(project(":api:incubator")) - + compileOnly(project(":api:incubator")) compileOnly(project(":sdk:trace-shaded-deps")) annotationProcessor("com.google.auto.value:auto-value") @@ -65,6 +64,23 @@ dependencies { jmh("org.testcontainers:testcontainers") // testContainer for OTLP collector } +testing { + suites { + register("testIncubating") { + dependencies { + implementation(project(":sdk:testing")) + implementation(project(":api:incubator")) + } + } + } +} + +tasks { + check { + dependsOn(testing.suites) + } +} + tasks { withType().configureEach { // We catch NoClassDefFoundError to fallback to non-jctools queues. diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkSpanBuilder.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkSpanBuilder.java new file mode 100644 index 00000000000..b68030202ce --- /dev/null +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkSpanBuilder.java @@ -0,0 +1,159 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.trace; + +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.SpanCallable; +import io.opentelemetry.api.incubator.trace.SpanRunnable; +import io.opentelemetry.api.trace.Span; +import io.opentelemetry.api.trace.SpanContext; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.context.propagation.ContextPropagators; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.BiConsumer; + +/** {@link ExtendedSdkSpanBuilder} is SDK implementation of {@link ExtendedSpanBuilder}. */ +final class ExtendedSdkSpanBuilder extends SdkSpanBuilder implements ExtendedSpanBuilder { + + ExtendedSdkSpanBuilder( + String spanName, + InstrumentationScopeInfo instrumentationScopeInfo, + TracerSharedState tracerSharedState, + SpanLimits spanLimits) { + super(spanName, instrumentationScopeInfo, tracerSharedState, spanLimits); + } + + @Override + public ExtendedSpanBuilder setParent(Context context) { + super.setParent(context); + return this; + } + + @Override + public ExtendedSpanBuilder setNoParent() { + super.setNoParent(); + return this; + } + + @Override + public ExtendedSpanBuilder setSpanKind(SpanKind spanKind) { + super.setSpanKind(spanKind); + return this; + } + + @Override + public ExtendedSpanBuilder addLink(SpanContext spanContext) { + super.addLink(spanContext); + return this; + } + + @Override + public ExtendedSpanBuilder addLink(SpanContext spanContext, Attributes attributes) { + super.addLink(spanContext, attributes); + return this; + } + + @Override + public ExtendedSpanBuilder setAttribute(String key, String value) { + super.setAttribute(key, value); + return this; + } + + @Override + public ExtendedSpanBuilder setAttribute(String key, long value) { + super.setAttribute(key, value); + return this; + } + + @Override + public ExtendedSpanBuilder setAttribute(String key, double value) { + super.setAttribute(key, value); + return this; + } + + @Override + public ExtendedSpanBuilder setAttribute(String key, boolean value) { + super.setAttribute(key, value); + return this; + } + + @Override + public ExtendedSpanBuilder setAttribute(AttributeKey key, T value) { + super.setAttribute(key, value); + return this; + } + + @Override + public ExtendedSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { + super.setStartTimestamp(startTimestamp, unit); + return this; + } + + @Override + public ExtendedSpanBuilder setParentFrom( + ContextPropagators propagators, Map carrier) { + super.setParent( + ExtendedContextPropagators.extractTextMapPropagationContext(carrier, propagators)); + return this; + } + + @Override + public T startAndCall(SpanCallable spanCallable) throws E { + return startAndCall(spanCallable, ExtendedSdkSpanBuilder::setSpanError); + } + + @Override + public T startAndCall( + SpanCallable spanCallable, BiConsumer handleException) throws E { + Span span = startSpan(); + + //noinspection unused + try (Scope unused = span.makeCurrent()) { + return spanCallable.callInSpan(); + } catch (Throwable e) { + handleException.accept(span, e); + throw e; + } finally { + span.end(); + } + } + + @Override + public void startAndRun(SpanRunnable runnable) throws E { + startAndRun(runnable, ExtendedSdkSpanBuilder::setSpanError); + } + + @SuppressWarnings("NullAway") + @Override + public void startAndRun( + SpanRunnable runnable, BiConsumer handleException) throws E { + startAndCall( + () -> { + runnable.runInSpan(); + return null; + }, + handleException); + } + + /** + * Marks a span as error. This is the default exception handler. + * + * @param span the span + * @param exception the exception that caused the error + */ + private static void setSpanError(Span span, Throwable exception) { + span.setStatus(StatusCode.ERROR); + span.recordException(exception); + } +} diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkTracer.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkTracer.java new file mode 100644 index 00000000000..7e339fd9a19 --- /dev/null +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/ExtendedSdkTracer.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.trace; + +import io.opentelemetry.api.incubator.trace.ExtendedTracer; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.trace.internal.TracerConfig; + +/** {@link ExtendedSdkTracer} is SDK implementation of {@link ExtendedTracer}. */ +final class ExtendedSdkTracer extends SdkTracer implements ExtendedTracer { + // TODO: add dedicated API for updating scope config. + @SuppressWarnings("FieldCanBeFinal") // For now, allow updating reflectively. + private boolean tracerEnabled; + + ExtendedSdkTracer( + TracerSharedState sharedState, + InstrumentationScopeInfo instrumentationScopeInfo, + TracerConfig tracerConfig) { + super(sharedState, instrumentationScopeInfo, tracerConfig); + this.tracerEnabled = tracerConfig.isEnabled(); + } + + @Override + public boolean isEnabled() { + return tracerEnabled; + } +} diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java new file mode 100644 index 00000000000..885cb9312c8 --- /dev/null +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.trace; + +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.trace.internal.TracerConfig; + +final class IncubatingUtil { + + private IncubatingUtil() {} + + static SdkTracer createIncubatingTracer( + TracerSharedState sharedState, + InstrumentationScopeInfo instrumentationScopeInfo, + TracerConfig tracerConfig) { + return new ExtendedSdkTracer(sharedState, instrumentationScopeInfo, tracerConfig); + } + + static SdkSpanBuilder createIncubatingSpanBuilder( + String spanName, + InstrumentationScopeInfo instrumentationScopeInfo, + TracerSharedState tracerSharedState, + SpanLimits spanLimits) { + return new ExtendedSdkSpanBuilder( + spanName, instrumentationScopeInfo, tracerSharedState, spanLimits); + } +} diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java index d637caa60da..ca4e9ed4bc2 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java @@ -12,10 +12,6 @@ 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.SpanCallable; -import io.opentelemetry.api.incubator.trace.SpanRunnable; import io.opentelemetry.api.internal.ImmutableSpanContext; import io.opentelemetry.api.trace.Span; import io.opentelemetry.api.trace.SpanBuilder; @@ -25,8 +21,6 @@ import io.opentelemetry.api.trace.TraceFlags; import io.opentelemetry.api.trace.TraceState; import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.context.propagation.ContextPropagators; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.internal.AttributeUtil; import io.opentelemetry.sdk.internal.AttributesMap; @@ -36,13 +30,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.concurrent.TimeUnit; -import java.util.function.BiConsumer; import javax.annotation.Nullable; /** {@link SdkSpanBuilder} is SDK implementation of {@link SpanBuilder}. */ -final class SdkSpanBuilder implements ExtendedSpanBuilder { +class SdkSpanBuilder implements SpanBuilder { private final String spanName; private final InstrumentationScopeInfo instrumentationScopeInfo; @@ -68,7 +60,7 @@ final class SdkSpanBuilder implements ExtendedSpanBuilder { } @Override - public ExtendedSpanBuilder setParent(Context context) { + public SpanBuilder setParent(Context context) { if (context == null) { return this; } @@ -77,13 +69,13 @@ public ExtendedSpanBuilder setParent(Context context) { } @Override - public ExtendedSpanBuilder setNoParent() { + public SpanBuilder setNoParent() { this.parent = Context.root(); return this; } @Override - public ExtendedSpanBuilder setSpanKind(SpanKind spanKind) { + public SpanBuilder setSpanKind(SpanKind spanKind) { if (spanKind == null) { return this; } @@ -92,7 +84,7 @@ public ExtendedSpanBuilder setSpanKind(SpanKind spanKind) { } @Override - public ExtendedSpanBuilder addLink(SpanContext spanContext) { + public SpanBuilder addLink(SpanContext spanContext) { if (spanContext == null || !spanContext.isValid()) { return this; } @@ -101,7 +93,7 @@ public ExtendedSpanBuilder addLink(SpanContext spanContext) { } @Override - public ExtendedSpanBuilder addLink(SpanContext spanContext, Attributes attributes) { + public SpanBuilder addLink(SpanContext spanContext, Attributes attributes) { if (spanContext == null || !spanContext.isValid()) { return this; } @@ -135,27 +127,27 @@ private void addLink(LinkData link) { } @Override - public ExtendedSpanBuilder setAttribute(String key, String value) { + public SpanBuilder setAttribute(String key, String value) { return setAttribute(stringKey(key), value); } @Override - public ExtendedSpanBuilder setAttribute(String key, long value) { + public SpanBuilder setAttribute(String key, long value) { return setAttribute(longKey(key), value); } @Override - public ExtendedSpanBuilder setAttribute(String key, double value) { + public SpanBuilder setAttribute(String key, double value) { return setAttribute(doubleKey(key), value); } @Override - public ExtendedSpanBuilder setAttribute(String key, boolean value) { + public SpanBuilder setAttribute(String key, boolean value) { return setAttribute(booleanKey(key), value); } @Override - public ExtendedSpanBuilder setAttribute(AttributeKey key, T value) { + public SpanBuilder setAttribute(AttributeKey key, T value) { if (key == null || key.getKey().isEmpty() || value == null) { return this; } @@ -164,7 +156,7 @@ public ExtendedSpanBuilder setAttribute(AttributeKey key, T value) { } @Override - public ExtendedSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { + public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { if (startTimestamp < 0 || unit == null) { return this; } @@ -172,13 +164,6 @@ public ExtendedSpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) return this; } - @Override - public ExtendedSpanBuilder setParentFrom( - ContextPropagators propagators, Map carrier) { - setParent(ExtendedContextPropagators.extractTextMapPropagationContext(carrier, propagators)); - return this; - } - @Override @SuppressWarnings({"unchecked", "rawtypes"}) public Span startSpan() { @@ -250,44 +235,6 @@ public Span startSpan() { startEpochNanos); } - @Override - public T startAndCall(SpanCallable spanCallable) throws E { - return startAndCall(spanCallable, SdkSpanBuilder::setSpanError); - } - - @Override - public T startAndCall( - SpanCallable spanCallable, BiConsumer handleException) throws E { - Span span = startSpan(); - - //noinspection unused - try (Scope unused = span.makeCurrent()) { - return spanCallable.callInSpan(); - } catch (Throwable e) { - handleException.accept(span, e); - throw e; - } finally { - span.end(); - } - } - - @Override - public void startAndRun(SpanRunnable runnable) throws E { - startAndRun(runnable, SdkSpanBuilder::setSpanError); - } - - @SuppressWarnings("NullAway") - @Override - public void startAndRun( - SpanRunnable runnable, BiConsumer handleException) throws E { - startAndCall( - () -> { - runnable.runInSpan(); - return null; - }, - handleException); - } - private AttributesMap attributes() { AttributesMap attributes = this.attributes; if (attributes == null) { diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java index 7b74c035e82..226898eee53 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java @@ -5,7 +5,6 @@ package io.opentelemetry.sdk.trace; -import io.opentelemetry.api.incubator.trace.ExtendedTracer; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.Tracer; import io.opentelemetry.api.trace.TracerProvider; @@ -13,7 +12,7 @@ import io.opentelemetry.sdk.trace.internal.TracerConfig; /** {@link SdkTracer} is SDK implementation of {@link Tracer}. */ -final class SdkTracer implements ExtendedTracer { +class SdkTracer implements Tracer { static final String FALLBACK_SPAN_NAME = ""; private static final Tracer NOOP_TRACER = TracerProvider.noop().get("noop"); @@ -33,6 +32,19 @@ final class SdkTracer implements ExtendedTracer { this.tracerEnabled = tracerConfig.isEnabled(); } + static SdkTracer create( + TracerSharedState sharedState, + InstrumentationScopeInfo instrumentationScopeInfo, + TracerConfig tracerConfig) { + try { + Class.forName("io.opentelemetry.api.incubator.trace.ExtendedTracer"); + return IncubatingUtil.createIncubatingTracer( + sharedState, instrumentationScopeInfo, tracerConfig); + } catch (Exception e) { + return new SdkTracer(sharedState, instrumentationScopeInfo, tracerConfig); + } + } + @Override public SpanBuilder spanBuilder(String spanName) { if (!tracerEnabled) { @@ -45,17 +57,18 @@ public SpanBuilder spanBuilder(String spanName) { Tracer tracer = TracerProvider.noop().get(instrumentationScopeInfo.getName()); return tracer.spanBuilder(spanName); } - return new SdkSpanBuilder( - spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); + try { + Class.forName("io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder"); + return IncubatingUtil.createIncubatingSpanBuilder( + spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); + } catch (Exception e) { + return new SdkSpanBuilder( + spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); + } } // Visible for testing InstrumentationScopeInfo getInstrumentationScopeInfo() { return instrumentationScopeInfo; } - - @Override - public boolean isEnabled() { - return tracerEnabled; - } } diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java index 036e812c5ab..14ccc37c41f 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracerProvider.java @@ -56,7 +56,7 @@ public static SdkTracerProviderBuilder builder() { this.tracerSdkComponentRegistry = new ComponentRegistry<>( instrumentationScopeInfo -> - new SdkTracer( + SdkTracer.create( sharedState, instrumentationScopeInfo, getTracerConfig(instrumentationScopeInfo))); diff --git a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/TracerConfigTest.java b/sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java similarity index 95% rename from sdk/trace/src/test/java/io/opentelemetry/sdk/trace/TracerConfigTest.java rename to sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java index 4421185aeea..2610e324aa8 100644 --- a/sdk/trace/src/test/java/io/opentelemetry/sdk/trace/TracerConfigTest.java +++ b/sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java @@ -24,6 +24,7 @@ import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; import io.opentelemetry.sdk.trace.internal.TracerConfig; import java.util.stream.Stream; +import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -55,8 +56,8 @@ void disableScopes() throws InterruptedException { parent.setAttribute("a", "1"); child = tracerB.spanBuilder("child").startSpan(); // tracerB is disabled and should behave the same as noop tracer - assertThat(child.getSpanContext()).isEqualTo(parent.getSpanContext()); - assertThat(child.isRecording()).isFalse(); + Assertions.assertThat(child.getSpanContext()).isEqualTo(parent.getSpanContext()); + Assertions.assertThat(child.isRecording()).isFalse(); try (Scope childScope = child.makeCurrent()) { child.setAttribute("b", "1"); grandchild = tracerC.spanBuilder("grandchild").startSpan(); @@ -75,7 +76,7 @@ void disableScopes() throws InterruptedException { // Only contain tracerA:parent and tracerC:child should be seen // tracerC:grandchild should list tracerA:parent as its parent - assertThat(exporter.getFinishedSpanItems()) + Assertions.assertThat(exporter.getFinishedSpanItems()) .satisfiesExactlyInAnyOrder( spanData -> assertThat(spanData) @@ -105,7 +106,7 @@ void tracerConfigurator( TracerConfig expectedTracerConfig) { TracerConfig tracerConfig = tracerConfigurator.apply(scope); tracerConfig = tracerConfig == null ? defaultConfig() : tracerConfig; - assertThat(tracerConfig).isEqualTo(expectedTracerConfig); + Assertions.assertThat(tracerConfig).isEqualTo(expectedTracerConfig); } private static final InstrumentationScopeInfo scopeCat = InstrumentationScopeInfo.create("cat"); From d5a8535f053e80ec9f5b9c50bf4f8c781ec1b146 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 19 Dec 2024 10:20:16 -0600 Subject: [PATCH 3/7] Clean up reflective access --- .../graal-incubating/build.gradle.kts | 1 + ...AutoConfiguredOpenTelemetrySdkBuilder.java | 18 +++++-- .../sdk/autoconfigure/IncubatingUtil.java | 7 ++- .../sdk/logs/IncubatingUtil.java | 9 +++- .../io/opentelemetry/sdk/logs/SdkLogger.java | 33 +++++++------ .../sdk/metrics/IncubatingUtil.java | 13 +++-- .../opentelemetry/sdk/metrics/SdkMeter.java | 48 +++++++++---------- .../sdk/trace/IncubatingUtil.java | 9 +++- .../io/opentelemetry/sdk/trace/SdkTracer.java | 35 ++++++++------ 9 files changed, 106 insertions(+), 67 deletions(-) diff --git a/integration-tests/graal-incubating/build.gradle.kts b/integration-tests/graal-incubating/build.gradle.kts index d050e01dee8..7ad93ffb947 100644 --- a/integration-tests/graal-incubating/build.gradle.kts +++ b/integration-tests/graal-incubating/build.gradle.kts @@ -18,6 +18,7 @@ sourceSets { dependencies { implementation(project(":sdk:all")) implementation(project(":sdk:trace-shaded-deps")) + implementation(project(":sdk:testing")) implementation(project(":exporters:otlp:all")) implementation(project(":api:incubator")) } diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java index 74796d3ed0e..f6ddd6c6f85 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/AutoConfiguredOpenTelemetrySdkBuilder.java @@ -63,6 +63,19 @@ */ public final class AutoConfiguredOpenTelemetrySdkBuilder implements AutoConfigurationCustomizer { + private static final boolean INCUBATOR_AVAILABLE; + + static { + boolean incubatorAvailable = false; + try { + Class.forName("io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider"); + incubatorAvailable = true; + } catch (ClassNotFoundException e) { + // Not available + } + INCUBATOR_AVAILABLE = incubatorAvailable; + } + private static final Logger logger = Logger.getLogger(AutoConfiguredOpenTelemetrySdkBuilder.class.getName()); @@ -590,11 +603,8 @@ private void maybeSetAsGlobal(OpenTelemetrySdk openTelemetrySdk) { return; } GlobalOpenTelemetry.set(openTelemetrySdk); - try { - Class.forName("io.opentelemetry.api.incubator.events.GlobalEventLoggerProvider"); + if (INCUBATOR_AVAILABLE) { IncubatingUtil.setGlobalEventLoggerProvider(openTelemetrySdk.getSdkLoggerProvider()); - } catch (Exception e) { - // do nothing } logger.log( Level.FINE, "Global OpenTelemetry set to {0} by autoconfiguration", openTelemetrySdk); diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java index 2526cda38ed..fa6ebb1abcd 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java @@ -9,10 +9,13 @@ import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.internal.SdkEventLoggerProvider; +/** + * Utilities for interacting with {@code io.opentelemetry:opentelemetry-api-incubator}, which is not + * guaranteed to be present on the classpath. For all methods, callers MUST first separately + * reflectively confirm that the incubator is available on the classpath. + */ final class IncubatingUtil { - private IncubatingUtil() {} - static void setGlobalEventLoggerProvider(SdkLoggerProvider sdkLoggerProvider) { GlobalEventLoggerProvider.set(SdkEventLoggerProvider.create(sdkLoggerProvider)); } diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java index 70ad8bb30e3..fd7582644be 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/IncubatingUtil.java @@ -8,18 +8,23 @@ import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.logs.internal.LoggerConfig; +/** + * Utilities for interacting with {@code io.opentelemetry:opentelemetry-api-incubator}, which is not + * guaranteed to be present on the classpath. For all methods, callers MUST first separately + * reflectively confirm that the incubator is available on the classpath. + */ final class IncubatingUtil { private IncubatingUtil() {} - static SdkLogger createIncubatingLogger( + static SdkLogger createExtendedLogger( LoggerSharedState sharedState, InstrumentationScopeInfo instrumentationScopeInfo, LoggerConfig tracerConfig) { return new ExtendedSdkLogger(sharedState, instrumentationScopeInfo, tracerConfig); } - static SdkLogRecordBuilder createIncubatingLogRecordBuilder( + static SdkLogRecordBuilder createExtendedLogRecordBuilder( LoggerSharedState loggerSharedState, InstrumentationScopeInfo instrumentationScopeInfo) { return new ExtendedSdkLogRecordBuilder(loggerSharedState, instrumentationScopeInfo); } diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java index 1832ae9dad5..8ac51603073 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogger.java @@ -15,6 +15,18 @@ class SdkLogger implements Logger { private static final Logger NOOP_LOGGER = LoggerProvider.noop().get("noop"); + private static final boolean INCUBATOR_AVAILABLE; + + static { + boolean incubatorAvailable = false; + try { + Class.forName("io.opentelemetry.api.incubator.logs.ExtendedDefaultLoggerProvider"); + incubatorAvailable = true; + } catch (ClassNotFoundException e) { + // Not available + } + INCUBATOR_AVAILABLE = incubatorAvailable; + } private final LoggerSharedState loggerSharedState; private final InstrumentationScopeInfo instrumentationScopeInfo; @@ -33,25 +45,18 @@ static SdkLogger create( LoggerSharedState sharedState, InstrumentationScopeInfo instrumentationScopeInfo, LoggerConfig loggerConfig) { - try { - Class.forName("io.opentelemetry.api.incubator.logs.ExtendedLogger"); - return IncubatingUtil.createIncubatingLogger( - sharedState, instrumentationScopeInfo, loggerConfig); - } catch (Exception e) { - return new SdkLogger(sharedState, instrumentationScopeInfo, loggerConfig); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedLogger(sharedState, instrumentationScopeInfo, loggerConfig) + : new SdkLogger(sharedState, instrumentationScopeInfo, loggerConfig); } @Override public LogRecordBuilder logRecordBuilder() { if (loggerEnabled) { - try { - Class.forName("io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder"); - return IncubatingUtil.createIncubatingLogRecordBuilder( - loggerSharedState, instrumentationScopeInfo); - } catch (Exception e) { - return new SdkLogRecordBuilder(loggerSharedState, instrumentationScopeInfo); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedLogRecordBuilder( + loggerSharedState, instrumentationScopeInfo) + : new SdkLogRecordBuilder(loggerSharedState, instrumentationScopeInfo); } return NOOP_LOGGER.logRecordBuilder(); } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java index 726c938b547..03a88bb9382 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/IncubatingUtil.java @@ -10,25 +10,30 @@ import io.opentelemetry.api.metrics.LongCounterBuilder; import io.opentelemetry.api.metrics.LongUpDownCounterBuilder; +/** + * Utilities for interacting with {@code io.opentelemetry:opentelemetry-api-incubator}, which is not + * guaranteed to be present on the classpath. For all methods, callers MUST first separately + * reflectively confirm that the incubator is available on the classpath. + */ final class IncubatingUtil { private IncubatingUtil() {} - static LongCounterBuilder createIncubatingLongCounterBuilder(SdkMeter sdkMeter, String name) { + static LongCounterBuilder createExtendedLongCounterBuilder(SdkMeter sdkMeter, String name) { return new ExtendedSdkLongCounter.ExtendedSdkLongCounterBuilder(sdkMeter, name); } - static LongUpDownCounterBuilder createIncubatingLongUpDownCounterBuilder( + static LongUpDownCounterBuilder createExtendedLongUpDownCounterBuilder( SdkMeter sdkMeter, String name) { return new ExtendedSdkLongUpDownCounter.ExtendedSdkLongUpDownCounterBuilder(sdkMeter, name); } - static DoubleHistogramBuilder createIncubatingDoubleHistogramBuilder( + static DoubleHistogramBuilder createExtendedDoubleHistogramBuilder( SdkMeter sdkMeter, String name) { return new ExtendedSdkDoubleHistogram.ExtendedSdkDoubleHistogramBuilder(sdkMeter, name); } - static DoubleGaugeBuilder createIncubatingDoubleGaugemBuilder(SdkMeter sdkMeter, String name) { + static DoubleGaugeBuilder createExtendedDoubleGaugeBuilder(SdkMeter sdkMeter, String name) { return new ExtendedSdkDoubleGauge.ExtendedSdkDoubleGaugeBuilder(sdkMeter, name); } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java index 49ec6fbf7de..c0f1476077a 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeter.java @@ -49,6 +49,18 @@ final class SdkMeter implements Meter { private static final Logger logger = Logger.getLogger(SdkMeter.class.getName()); + private static final boolean INCUBATOR_AVAILABLE; + + static { + boolean incubatorAvailable = false; + try { + Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedDefaultMeterProvider"); + incubatorAvailable = true; + } catch (ClassNotFoundException e) { + // Not available + } + INCUBATOR_AVAILABLE = incubatorAvailable; + } /** * Instrument names MUST conform to the following syntax. @@ -148,12 +160,9 @@ public LongCounterBuilder counterBuilder(String name) { if (!checkValidInstrumentName(name)) { return NOOP_METER.counterBuilder(NOOP_INSTRUMENT_NAME); } - try { - Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedLongCounter"); - return IncubatingUtil.createIncubatingLongCounterBuilder(this, name); - } catch (Exception e) { - return new SdkLongCounter.SdkLongCounterBuilder(this, name); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedLongCounterBuilder(this, name) + : new SdkLongCounter.SdkLongCounterBuilder(this, name); } @Override @@ -161,12 +170,9 @@ public LongUpDownCounterBuilder upDownCounterBuilder(String name) { if (!checkValidInstrumentName(name)) { return NOOP_METER.upDownCounterBuilder(NOOP_INSTRUMENT_NAME); } - try { - Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounter"); - return IncubatingUtil.createIncubatingLongUpDownCounterBuilder(this, name); - } catch (Exception e) { - return new SdkLongUpDownCounter.SdkLongUpDownCounterBuilder(this, name); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedLongUpDownCounterBuilder(this, name) + : new SdkLongUpDownCounter.SdkLongUpDownCounterBuilder(this, name); } @Override @@ -174,12 +180,9 @@ public DoubleHistogramBuilder histogramBuilder(String name) { if (!checkValidInstrumentName(name)) { return NOOP_METER.histogramBuilder(NOOP_INSTRUMENT_NAME); } - try { - Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogram"); - return IncubatingUtil.createIncubatingDoubleHistogramBuilder(this, name); - } catch (Exception e) { - return new SdkDoubleHistogram.SdkDoubleHistogramBuilder(this, name); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedDoubleHistogramBuilder(this, name) + : new SdkDoubleHistogram.SdkDoubleHistogramBuilder(this, name); } @Override @@ -187,12 +190,9 @@ public DoubleGaugeBuilder gaugeBuilder(String name) { if (!checkValidInstrumentName(name)) { return NOOP_METER.gaugeBuilder(NOOP_INSTRUMENT_NAME); } - try { - Class.forName("io.opentelemetry.api.incubator.metrics.ExtendedDoubleGauge"); - return IncubatingUtil.createIncubatingDoubleGaugemBuilder(this, name); - } catch (Exception e) { - return new SdkDoubleGauge.SdkDoubleGaugeBuilder(this, name); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedDoubleGaugeBuilder(this, name) + : new SdkDoubleGauge.SdkDoubleGaugeBuilder(this, name); } @Override diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java index 885cb9312c8..c7ab15e9c2e 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/IncubatingUtil.java @@ -8,18 +8,23 @@ import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.trace.internal.TracerConfig; +/** + * Utilities for interacting with {@code io.opentelemetry:opentelemetry-api-incubator}, which is not + * guaranteed to be present on the classpath. For all methods, callers MUST first separately + * reflectively confirm that the incubator is available on the classpath. + */ final class IncubatingUtil { private IncubatingUtil() {} - static SdkTracer createIncubatingTracer( + static SdkTracer createExtendedTracer( TracerSharedState sharedState, InstrumentationScopeInfo instrumentationScopeInfo, TracerConfig tracerConfig) { return new ExtendedSdkTracer(sharedState, instrumentationScopeInfo, tracerConfig); } - static SdkSpanBuilder createIncubatingSpanBuilder( + static SdkSpanBuilder createExtendedSpanBuilder( String spanName, InstrumentationScopeInfo instrumentationScopeInfo, TracerSharedState tracerSharedState, diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java index 226898eee53..42210e56b28 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkTracer.java @@ -15,6 +15,18 @@ class SdkTracer implements Tracer { static final String FALLBACK_SPAN_NAME = ""; private static final Tracer NOOP_TRACER = TracerProvider.noop().get("noop"); + private static final boolean INCUBATOR_AVAILABLE; + + static { + boolean incubatorAvailable = false; + try { + Class.forName("io.opentelemetry.api.incubator.trace.ExtendedDefaultTracerProvider"); + incubatorAvailable = true; + } catch (ClassNotFoundException e) { + // Not available + } + INCUBATOR_AVAILABLE = incubatorAvailable; + } private final TracerSharedState sharedState; private final InstrumentationScopeInfo instrumentationScopeInfo; @@ -36,13 +48,9 @@ static SdkTracer create( TracerSharedState sharedState, InstrumentationScopeInfo instrumentationScopeInfo, TracerConfig tracerConfig) { - try { - Class.forName("io.opentelemetry.api.incubator.trace.ExtendedTracer"); - return IncubatingUtil.createIncubatingTracer( - sharedState, instrumentationScopeInfo, tracerConfig); - } catch (Exception e) { - return new SdkTracer(sharedState, instrumentationScopeInfo, tracerConfig); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedTracer(sharedState, instrumentationScopeInfo, tracerConfig) + : new SdkTracer(sharedState, instrumentationScopeInfo, tracerConfig); } @Override @@ -57,14 +65,11 @@ public SpanBuilder spanBuilder(String spanName) { Tracer tracer = TracerProvider.noop().get(instrumentationScopeInfo.getName()); return tracer.spanBuilder(spanName); } - try { - Class.forName("io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder"); - return IncubatingUtil.createIncubatingSpanBuilder( - spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); - } catch (Exception e) { - return new SdkSpanBuilder( - spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); - } + return INCUBATOR_AVAILABLE + ? IncubatingUtil.createExtendedSpanBuilder( + spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()) + : new SdkSpanBuilder( + spanName, instrumentationScopeInfo, sharedState, sharedState.getSpanLimits()); } // Visible for testing From 33f6493ca6b27f324eec2afc7a0790807b06d440 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 19 Dec 2024 11:01:56 -0600 Subject: [PATCH 4/7] Add graal test --- .../graal/IncubatingApiTests.java | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) 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 8d705419452..06ddcd006e1 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 @@ -7,12 +7,31 @@ import static org.assertj.core.api.Assertions.assertThat; +import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; import io.opentelemetry.api.incubator.logs.ExtendedLogger; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleCounter; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleGauge; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleHistogram; +import io.opentelemetry.api.incubator.metrics.ExtendedDoubleUpDownCounter; +import io.opentelemetry.api.incubator.metrics.ExtendedLongCounter; import io.opentelemetry.api.incubator.metrics.ExtendedLongCounterBuilder; +import io.opentelemetry.api.incubator.metrics.ExtendedLongGauge; +import io.opentelemetry.api.incubator.metrics.ExtendedLongHistogram; +import io.opentelemetry.api.incubator.metrics.ExtendedLongUpDownCounter; +import io.opentelemetry.api.incubator.trace.ExtendedSpanBuilder; import io.opentelemetry.api.incubator.trace.ExtendedTracer; import io.opentelemetry.api.logs.LoggerProvider; +import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.api.trace.TracerProvider; +import io.opentelemetry.sdk.logs.SdkLoggerProvider; +import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; +import io.opentelemetry.sdk.metrics.SdkMeterProvider; +import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; +import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader; +import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; +import io.opentelemetry.sdk.trace.SdkTracerProvider; +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; import org.junit.jupiter.api.Test; class IncubatingApiTests { @@ -23,4 +42,63 @@ void incubatingApiIsLoadedViaReflection() { assertThat(MeterProvider.noop().get("test").counterBuilder("test")) .isInstanceOf(ExtendedLongCounterBuilder.class); } + + @Test + void incubatingLogSdk() { + InMemoryLogRecordExporter exporter = InMemoryLogRecordExporter.create(); + SdkLoggerProvider loggerProvider = + SdkLoggerProvider.builder() + .addLogRecordProcessor(SimpleLogRecordProcessor.create(exporter)) + .build(); + + ExtendedLogger logger = (ExtendedLogger) loggerProvider.get("logger"); + logger.isEnabled(); + ((ExtendedLogRecordBuilder) logger.logRecordBuilder()).setBody("message").emit(); + } + + @Test + void incubatingTraceSdk() { + InMemorySpanExporter exporter = InMemorySpanExporter.create(); + SdkTracerProvider tracerProvider = + SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(exporter)).build(); + + ExtendedTracer tracer = (ExtendedTracer) tracerProvider.get("tracer"); + tracer.isEnabled(); + ((ExtendedSpanBuilder) tracer.spanBuilder("span")).startAndRun(() -> {}); + } + + @Test + void incubatingMetricSdk() { + InMemoryMetricReader reader = InMemoryMetricReader.create(); + SdkMeterProvider meterProvider = + SdkMeterProvider.builder().registerMetricReader(reader).build(); + + Meter meter = meterProvider.get("meter"); + + ExtendedLongCounter longCounter = + (ExtendedLongCounter) meter.counterBuilder("longCounter").build(); + longCounter.isEnabled(); + ExtendedDoubleCounter doubleCounter = + (ExtendedDoubleCounter) meter.counterBuilder("doubleCounter").ofDoubles().build(); + doubleCounter.isEnabled(); + ExtendedLongUpDownCounter longUpDownCounter = + (ExtendedLongUpDownCounter) meter.upDownCounterBuilder("longUpDownCounter").build(); + longUpDownCounter.isEnabled(); + ExtendedDoubleUpDownCounter doubleUpDownCounter = + (ExtendedDoubleUpDownCounter) + meter.upDownCounterBuilder("doubleUpDownCounter").ofDoubles().build(); + doubleUpDownCounter.isEnabled(); + ExtendedDoubleHistogram doubleHistogram = + (ExtendedDoubleHistogram) meter.histogramBuilder("doubleHistogram").build(); + doubleHistogram.isEnabled(); + ExtendedLongHistogram longHistogram = + (ExtendedLongHistogram) meter.histogramBuilder("longHistogram").ofLongs().build(); + longHistogram.isEnabled(); + ExtendedDoubleGauge doubleGauge = + (ExtendedDoubleGauge) meter.gaugeBuilder("doubleGauge").build(); + doubleGauge.isEnabled(); + ExtendedLongGauge longGauge = + (ExtendedLongGauge) meter.gaugeBuilder("longGauge").ofLongs().build(); + longGauge.isEnabled(); + } } From 414a5e71fc61e9d6387aa9b5043cec42ded31e2e Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 19 Dec 2024 11:16:57 -0600 Subject: [PATCH 5/7] Revert temp stuff --- buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts b/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts index 9ce7f00b76d..71a119f9b3b 100644 --- a/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts +++ b/buildSrc/src/main/kotlin/otel.java-conventions.gradle.kts @@ -86,7 +86,9 @@ tasks { // https://groups.google.com/forum/#!topic/bazel-discuss/_R3A9TJSoPM "-Xlint:-processing", // We suppress the "options" warning because it prevents compilation on modern JDKs - "-Xlint:-options" + "-Xlint:-options", + // Fail build on any warning + "-Werror", ), ) } From d11991ca968f0b2201b1dc68a9448bfc972abb38 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 19 Dec 2024 12:47:42 -0600 Subject: [PATCH 6/7] Cleanup --- .../sdk/logs/ExtendedSdkLogRecordBuilder.java | 5 ++--- .../io/opentelemetry/sdk/trace/TracerConfigTest.java | 10 +++++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java index ecaab256377..ba1d0e6cfd2 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java @@ -8,7 +8,6 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Value; import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; -import io.opentelemetry.api.logs.LogRecordBuilder; import io.opentelemetry.api.logs.Severity; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; @@ -37,13 +36,13 @@ public ExtendedSdkLogRecordBuilder setTimestamp(Instant instant) { } @Override - public LogRecordBuilder setObservedTimestamp(long timestamp, TimeUnit unit) { + public ExtendedSdkLogRecordBuilder setObservedTimestamp(long timestamp, TimeUnit unit) { super.setObservedTimestamp(timestamp, unit); return this; } @Override - public LogRecordBuilder setObservedTimestamp(Instant instant) { + public ExtendedSdkLogRecordBuilder setObservedTimestamp(Instant instant) { super.setObservedTimestamp(instant); return this; } diff --git a/sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java b/sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java index 2610e324aa8..e7aefbb5152 100644 --- a/sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java +++ b/sdk/trace/src/testIncubating/java/io/opentelemetry/sdk/trace/TracerConfigTest.java @@ -11,6 +11,7 @@ import static io.opentelemetry.sdk.trace.internal.TracerConfig.defaultConfig; import static io.opentelemetry.sdk.trace.internal.TracerConfig.disabled; import static io.opentelemetry.sdk.trace.internal.TracerConfig.enabled; +import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.incubator.trace.ExtendedTracer; @@ -24,7 +25,6 @@ import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor; import io.opentelemetry.sdk.trace.internal.TracerConfig; import java.util.stream.Stream; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -56,8 +56,8 @@ void disableScopes() throws InterruptedException { parent.setAttribute("a", "1"); child = tracerB.spanBuilder("child").startSpan(); // tracerB is disabled and should behave the same as noop tracer - Assertions.assertThat(child.getSpanContext()).isEqualTo(parent.getSpanContext()); - Assertions.assertThat(child.isRecording()).isFalse(); + assertThat(child.getSpanContext()).isEqualTo(parent.getSpanContext()); + assertThat(child.isRecording()).isFalse(); try (Scope childScope = child.makeCurrent()) { child.setAttribute("b", "1"); grandchild = tracerC.spanBuilder("grandchild").startSpan(); @@ -76,7 +76,7 @@ void disableScopes() throws InterruptedException { // Only contain tracerA:parent and tracerC:child should be seen // tracerC:grandchild should list tracerA:parent as its parent - Assertions.assertThat(exporter.getFinishedSpanItems()) + assertThat(exporter.getFinishedSpanItems()) .satisfiesExactlyInAnyOrder( spanData -> assertThat(spanData) @@ -106,7 +106,7 @@ void tracerConfigurator( TracerConfig expectedTracerConfig) { TracerConfig tracerConfig = tracerConfigurator.apply(scope); tracerConfig = tracerConfig == null ? defaultConfig() : tracerConfig; - Assertions.assertThat(tracerConfig).isEqualTo(expectedTracerConfig); + assertThat(tracerConfig).isEqualTo(expectedTracerConfig); } private static final InstrumentationScopeInfo scopeCat = InstrumentationScopeInfo.create("cat"); From ed35f93474eef494b2e4597fa8caf3f1e4e99273 Mon Sep 17 00:00:00 2001 From: Jack Berg Date: Thu, 19 Dec 2024 16:08:56 -0600 Subject: [PATCH 7/7] Fix build --- .../sdk/autoconfigure/IncubatingUtil.java | 2 ++ .../io/opentelemetry/sdk/trace/SdkSpanBuilder.java | 12 ------------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java index fa6ebb1abcd..f66e007cf34 100644 --- a/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java +++ b/sdk-extensions/autoconfigure/src/main/java/io/opentelemetry/sdk/autoconfigure/IncubatingUtil.java @@ -16,6 +16,8 @@ */ final class IncubatingUtil { + private IncubatingUtil() {} + static void setGlobalEventLoggerProvider(SdkLoggerProvider sdkLoggerProvider) { GlobalEventLoggerProvider.set(SdkEventLoggerProvider.create(sdkLoggerProvider)); } diff --git a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java index ca4e9ed4bc2..b12107c7d43 100644 --- a/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java +++ b/sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpanBuilder.java @@ -17,7 +17,6 @@ import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.SpanContext; import io.opentelemetry.api.trace.SpanKind; -import io.opentelemetry.api.trace.StatusCode; import io.opentelemetry.api.trace.TraceFlags; import io.opentelemetry.api.trace.TraceState; import io.opentelemetry.context.Context; @@ -256,15 +255,4 @@ static boolean isRecording(SamplingDecision decision) { static boolean isSampled(SamplingDecision decision) { return SamplingDecision.RECORD_AND_SAMPLE.equals(decision); } - - /** - * Marks a span as error. This is the default exception handler. - * - * @param span the span - * @param exception the exception that caused the error - */ - private static void setSpanError(Span span, Throwable exception) { - span.setStatus(StatusCode.ERROR); - span.recordException(exception); - } }