Skip to content

POTEL 68 - Add SentryOpenTelemetryMode.OFF #3995

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to keep using SpanFactoryFactory in the SentryOptions ctor for now as setting the OtelSpanFactory here causes OpenTelemetry spans to be ended in the agentless sample.

I haven't investigated yet why that's happening.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, let's keep this for now and maybe create a separate issue to investigate

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #4005 to track

// 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
Loading