diff --git a/CHANGELOG.md b/CHANGELOG.md index f4c875a974..318def381c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,9 @@ - You may also want to set `OTEL_LOGS_EXPORTER=none;OTEL_METRICS_EXPORTER=none;OTEL_TRACES_EXPORTER=none` env vars to not have the log flooded with error messages regarding OpenTelemetry features we don't use. - `OpenTelemetryUtil.applyOpenTelemetryOptions` now takes an enum instead of a boolean for its mode - Add `openTelemetryMode` option ([#3994](https://github.com/getsentry/sentry-java/pull/3994)) - - It defaults to `AUTO` meaning the SDK will figure out how to best configure itself for use with OpenTelemetry + - It defaults to `AUTO` meaning the SDK will figure out how to best configure itself for use with OpenTelemetry + - Use of OpenTelemetry can also be disabled completely by setting it to `OFF` ([#3995](https://github.com/getsentry/sentry-java/pull/3995)) + - In this case even if OpenTelemetry is present, the Sentry SDK will not use it - Use `AGENT` when using `sentry-opentelemetry-agent` - Use `AGENTLESS` when using `sentry-opentelemetry-agentless` - Use `AGENTLESS_SPRING` when using `sentry-opentelemetry-agentless-spring` diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java index 62900c25b8..5d4658bcf9 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidOptionsInitializer.java @@ -15,6 +15,7 @@ import io.sentry.SendFireAndForgetEnvelopeSender; import io.sentry.SendFireAndForgetOutboxSender; import io.sentry.SentryLevel; +import io.sentry.SentryOpenTelemetryMode; import io.sentry.android.core.cache.AndroidEnvelopeCache; import io.sentry.android.core.internal.debugmeta.AssetsDebugMetaLoader; import io.sentry.android.core.internal.gestures.AndroidViewGestureTargetLocator; @@ -101,7 +102,7 @@ static void loadDefaultAndMetadataOptions( options.setLogger(logger); options.setDefaultScopeType(ScopeType.CURRENT); - + options.setOpenTelemetryMode(SentryOpenTelemetryMode.OFF); options.setDateProvider(new SentryAndroidDateProvider()); // set a lower flush timeout on Android to avoid ANRs diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt index fb433ff598..3e1ce2c2d0 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/it/SentrySpringIntegrationTest.kt @@ -250,7 +250,7 @@ open class App { open fun optionsCallback() = Sentry.OptionsConfiguration { options -> // due to OTel being on the classpath we need to set the default again options.spanFactory = DefaultSpanFactory() - options.openTelemetryMode = SentryOpenTelemetryMode.ALL_ORIGINS + options.openTelemetryMode = SentryOpenTelemetryMode.OFF } } diff --git a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt index b6a2a18c0f..59b87ea0cf 100644 --- a/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt +++ b/sentry-spring-boot/src/test/kotlin/io/sentry/spring/boot/it/SentrySpringIntegrationTest.kt @@ -251,7 +251,7 @@ open class App { // due to OTel being on the classpath we need to set the default again options.spanFactory = DefaultSpanFactory() // to test the actual spring implementation - options.openTelemetryMode = SentryOpenTelemetryMode.ALL_ORIGINS + options.openTelemetryMode = SentryOpenTelemetryMode.OFF } } diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 5fb709943c..ef446fd35b 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -2785,8 +2785,8 @@ public final class io/sentry/SentryOpenTelemetryMode : java/lang/Enum { public static final field AGENT Lio/sentry/SentryOpenTelemetryMode; public static final field AGENTLESS Lio/sentry/SentryOpenTelemetryMode; public static final field AGENTLESS_SPRING Lio/sentry/SentryOpenTelemetryMode; - public static final field ALL_ORIGINS Lio/sentry/SentryOpenTelemetryMode; public static final field AUTO Lio/sentry/SentryOpenTelemetryMode; + public static final field OFF Lio/sentry/SentryOpenTelemetryMode; public static fun valueOf (Ljava/lang/String;)Lio/sentry/SentryOpenTelemetryMode; public static fun values ()[Lio/sentry/SentryOpenTelemetryMode; } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index a71c5613af..b0dc1b112d 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -47,8 +47,7 @@ public final class Sentry { private Sentry() {} // TODO logger? - private static volatile @NotNull IScopesStorage scopesStorage = - ScopesStorageFactory.create(new LoadClass(), NoOpLogger.getInstance()); + private static volatile @NotNull IScopesStorage scopesStorage = NoOpScopesStorage.getInstance(); /** The root Scopes or NoOp if Sentry is disabled. */ private static volatile @NotNull IScopes rootScopes = NoOpScopes.getInstance(); @@ -322,6 +321,7 @@ private static void init(final @NotNull SentryOptions options, final boolean glo final IScope rootIsolationScope = new Scope(options); rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init"); + initForOpenTelemetryMaybe(options); getScopesStorage().set(rootScopes); initConfigurations(options); @@ -357,6 +357,27 @@ private static void init(final @NotNull SentryOptions options, final boolean glo } } + private static void initForOpenTelemetryMaybe(SentryOptions options) { + if (SentryOpenTelemetryMode.OFF == options.getOpenTelemetryMode()) { + options.setSpanFactory(new DefaultSpanFactory()); + // } else { + // enabling this causes issues with agentless where OTel spans seem to be randomly ended + // options.setSpanFactory(SpanFactoryFactory.create(new LoadClass(), + // NoOpLogger.getInstance())); + } + initScopesStorage(options); + OpenTelemetryUtil.applyIgnoredSpanOrigins(options, new LoadClass()); + } + + private static void initScopesStorage(SentryOptions options) { + getScopesStorage().close(); + if (SentryOpenTelemetryMode.OFF == options.getOpenTelemetryMode()) { + scopesStorage = new DefaultScopesStorage(); + } else { + scopesStorage = ScopesStorageFactory.create(new LoadClass(), options.getLogger()); + } + } + @SuppressWarnings("FutureReturnValueIgnored") private static void handleAppStartProfilingConfig( final @NotNull SentryOptions options, diff --git a/sentry/src/main/java/io/sentry/SentryOpenTelemetryMode.java b/sentry/src/main/java/io/sentry/SentryOpenTelemetryMode.java index 5c452e4ca6..034cbb1be3 100644 --- a/sentry/src/main/java/io/sentry/SentryOpenTelemetryMode.java +++ b/sentry/src/main/java/io/sentry/SentryOpenTelemetryMode.java @@ -5,16 +5,13 @@ * use it and what way to use it in. */ public enum SentryOpenTelemetryMode { - /** Let the SDK figure out what mode OpenTelemetry is in and whether to even use OpenTelemetry */ - AUTO, /** - * For now this only means no span origins will be ignored. This does however not mean, the SDK - * won't try tro use OpenTelemetry if available. - * - *

Due to some parts of the SDK being initialized before any config mechanism is available, we - * cannot completely disable the OpenTelemetry parts with this setting. + * Let the SDK figure out what mode OpenTelemetry is in and whether to even use OpenTelemetry This + * is the default for non Android. */ - ALL_ORIGINS, + AUTO, + /** Do not try to use OpenTelemetry, even if it is available. This is the default for Android. */ + OFF, /** The `sentry-opentelemetry-agent` is used */ AGENT, /** diff --git a/sentry/src/test/java/io/sentry/HubAdapterTest.kt b/sentry/src/test/java/io/sentry/HubAdapterTest.kt index f57c45ba4e..76e79b0e48 100644 --- a/sentry/src/test/java/io/sentry/HubAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/HubAdapterTest.kt @@ -3,6 +3,7 @@ package io.sentry import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import io.sentry.test.createSentryClientMock +import io.sentry.test.initForTest import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq @@ -19,6 +20,9 @@ class HubAdapterTest { @BeforeTest fun `set up`() { + initForTest { + it.dsn = "https://key@localhost/proj" + } Sentry.setCurrentScopes(scopes) } diff --git a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt index db2ed593ca..ea274d438b 100644 --- a/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt +++ b/sentry/src/test/java/io/sentry/ScopesAdapterTest.kt @@ -3,6 +3,7 @@ package io.sentry import io.sentry.protocol.SentryTransaction import io.sentry.protocol.User import io.sentry.test.createSentryClientMock +import io.sentry.test.initForTest import org.mockito.kotlin.any import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.eq @@ -19,6 +20,9 @@ class ScopesAdapterTest { @BeforeTest fun `set up`() { + initForTest { + it.dsn = "https://key@localhost/proj" + } Sentry.setCurrentScopes(scopes) }