From b0526ec53631593fd6e23f0e0fb7168b5389df82 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 2 Dec 2022 11:21:54 +0100 Subject: [PATCH 1/2] Add SENTRY_AUTO_INIT env var to control OpenTelemetry Agent init --- .../sentry-opentelemetry-agent/README.md | 7 +++++++ ...tryAutoConfigurationCustomizerProvider.java | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/sentry-opentelemetry/sentry-opentelemetry-agent/README.md b/sentry-opentelemetry/sentry-opentelemetry-agent/README.md index 0cb681aba62..92ddb89a1c4 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 0f2e4db9760..b11e988dd32 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(); From c787a0f8d0ab97caf9ea75ce78f316ee6b1a2eca Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Fri, 2 Dec 2022 11:27:56 +0100 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc8a65f3a64..f85dad51cd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - No longer disable OpenTelemetry exporters in default Java Agent config ([#2408](https://github.com/getsentry/sentry-java/pull/2408)) +### Features + +- Add SENTRY_AUTO_INIT environment variable to control OpenTelemetry Agent init ([#2410](https://github.com/getsentry/sentry-java/pull/2410)) + ## 6.9.1 ### Fixes