diff --git a/sentry-opentelemetry/sentry-opentelemetry-agent/README.md b/sentry-opentelemetry/sentry-opentelemetry-agent/README.md index 0cb681aba6..92ddb89a1c 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-agent/README.md +++ b/sentry-opentelemetry/sentry-opentelemetry-agent/README.md @@ -38,6 +38,13 @@ Sentry.init( Using the `otel` instrumenter will ensure `Sentry` instrumentation will be done via OpenTelemetry and integrations as well as direct interactions with transactions and spans have no effect. +## Controlling auto initialization of Sentry + +By default if you pass either `SENTRY_DSN` or `SENTRY_PROPERTIES_FILE` as environment variable, +Sentry will automatically be initialized by this agent. To disable this behaviour, you can set +`SENTRY_AUTO_INIT=false` as environment variable. You will then have to initialize Sentry inside +the target application. + ## Debugging To enable debug logging for Sentry, please provide `SENTRY_DEBUG=true` as environment variable or diff --git a/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/main/java/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProvider.java b/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/main/java/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProvider.java index 0f2e4db976..b11e988dd3 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/main/java/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProvider.java +++ b/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/main/java/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProvider.java @@ -23,10 +23,7 @@ public final class SentryAutoConfigurationCustomizerProvider @Override public void customize(AutoConfigurationCustomizer autoConfiguration) { - final @Nullable String sentryPropertiesFile = System.getenv("SENTRY_PROPERTIES_FILE"); - final @Nullable String sentryDsn = System.getenv("SENTRY_DSN"); - - if (sentryPropertiesFile != null || sentryDsn != null) { + if (isSentryAutoInitEnabled()) { Sentry.init( options -> { options.setEnableExternalConfiguration(true); @@ -43,6 +40,19 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) { .addPropertiesSupplier(this::getDefaultProperties); } + private boolean isSentryAutoInitEnabled() { + final @Nullable String sentryAutoInit = System.getenv("SENTRY_AUTO_INIT"); + + if (sentryAutoInit != null) { + return "true".equalsIgnoreCase(sentryAutoInit); + } else { + final @Nullable String sentryPropertiesFile = System.getenv("SENTRY_PROPERTIES_FILE"); + final @Nullable String sentryDsn = System.getenv("SENTRY_DSN"); + + return sentryPropertiesFile != null || sentryDsn != null; + } + } + private @Nullable SdkVersion createSdkVersion(final @NotNull SentryOptions sentryOptions) { SdkVersion sdkVersion = sentryOptions.getSdkVersion();