Skip to content

Commit

Permalink
POTEL 68 - Add SentryOpenTelemetryMode.OFF (#3995)
Browse files Browse the repository at this point in the history
* add agentless module for OpenTelemetry setup without any agent

* Replace bool with enum for OpenTelemetryUtil mode

* Move OpenTelemetryUtil into core sentry

* Add openTelemetryMode option

* Add SentryOpenTelemetryMode.OFF

* fix HubAdapter and ScopeAdapter tests by calling Sentry.init so that the ScopesStorage is initialized

* fix build

* changelog

* fix agentless due to late call to SpanFactoryFactory

* Format code

* changelog

* fix order of scope storage close vs scopes close

* use initForTest

* remove unused suppress annotation

---------

Co-authored-by: Lukas Bloder <[email protected]>
Co-authored-by: Sentry Github Bot <[email protected]>
  • Loading branch information
3 people authored Dec 20, 2024
1 parent 710f5dd commit 5a15f74
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 15 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ open class App {
open fun optionsCallback() = Sentry.OptionsConfiguration<SentryOptions> { 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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 23 additions & 2 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions sentry/src/main/java/io/sentry/SentryOpenTelemetryMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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,
/**
Expand Down
4 changes: 4 additions & 0 deletions sentry/src/test/java/io/sentry/HubAdapterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +20,9 @@ class HubAdapterTest {

@BeforeTest
fun `set up`() {
initForTest {
it.dsn = "https://key@localhost/proj"
}
Sentry.setCurrentScopes(scopes)
}

Expand Down
4 changes: 4 additions & 0 deletions sentry/src/test/java/io/sentry/ScopesAdapterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,6 +20,9 @@ class ScopesAdapterTest {

@BeforeTest
fun `set up`() {
initForTest {
it.dsn = "https://key@localhost/proj"
}
Sentry.setCurrentScopes(scopes)
}

Expand Down

0 comments on commit 5a15f74

Please sign in to comment.