diff --git a/CHANGELOG.md b/CHANGELOG.md index f9851e542b4..3fb0ad8a4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Fixes +- OpenTelemetry: Apply the GraphQL span-naming property rename from OpenTelemetry 2.33.1. Enabling v3 semantic conventions no longer drops operation names, and explicit settings using either property name remain honored ([#6750](https://github.com/getsentry/sentry-java/pull/6750)) - Profiling: No longer crash on API 35+ devices whose framework can't load `ProfilingManager`. Perfetto profiling is turned off on those devices instead ([#6742](https://github.com/getsentry/sentry-java/pull/6742)) ### Dependencies diff --git a/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/build.gradle.kts b/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/build.gradle.kts index 085eb11b41f..24bedc14fbb 100644 --- a/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/build.gradle.kts +++ b/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/build.gradle.kts @@ -41,8 +41,10 @@ dependencies { testImplementation(projects.sentryTestSupport) testImplementation(kotlin(Config.kotlinStdLib)) testImplementation(libs.awaitility.kotlin) + testImplementation(libs.google.truth) testImplementation(libs.kotlin.test.junit) testImplementation(libs.mockito.kotlin) + testImplementation(libs.otel.extension.autoconfigure.spi) } tasks.withType().configureEach { 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 42277f53c26..c503dfe196d 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 @@ -12,25 +12,25 @@ import io.sentry.internal.ManifestVersionReader; import io.sentry.protocol.SdkVersion; import java.time.Duration; +import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public final class SentryAutoConfigurationCustomizerProvider implements AutoConfigurationCustomizerProvider { - private static final Logger logger = - Logger.getLogger(SentryAutoConfigurationCustomizerProvider.class.getName()); + private static final String GRAPHQL_OPERATION_NAME_PROPERTY = + "otel.instrumentation.graphql.operation-name-in-span-name.enabled"; + private static final String DEPRECATED_GRAPHQL_OPERATION_NAME_PROPERTY = + "otel.instrumentation.graphql.add-operation-name-to-span-name.enabled"; public static volatile boolean skipInit = false; @Override public void customize(AutoConfigurationCustomizer autoConfiguration) { ensureSentryOtelStorageIsInitialized(); - customizeOpenTelemetryDefaults(); final @Nullable ManifestVersionReader.VersionInfoHolder versionInfoHolder = ManifestVersionReader.getInstance().readOpenTelemetryVersion(); @@ -46,8 +46,9 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) { } autoConfiguration - .addTracerProviderCustomizer(this::configureSdkTracerProvider) - .addPropertiesSupplier(this::getDefaultProperties); + .addPropertiesSupplier(this::getDefaultProperties) + .addPropertiesCustomizer(this::customizeOpenTelemetryDefaults) + .addTracerProviderCustomizer(this::configureSdkTracerProvider); } private static void ensureSentryOtelStorageIsInitialized() { @@ -59,16 +60,16 @@ private static void ensureSentryOtelStorageIsInitialized() { Sentry.getGlobalScope(); } - private void customizeOpenTelemetryDefaults() { - try { - if (System.getProperty("otel.instrumentation.graphql.add-operation-name-to-span-name.enabled") - == null) { - System.setProperty( - "otel.instrumentation.graphql.add-operation-name-to-span-name.enabled", "true"); - } - } catch (Exception e) { - logger.log(Level.WARNING, "Unable to change OpenTelemetry defaults for use with Sentry.", e); + Map customizeOpenTelemetryDefaults(final @NotNull ConfigProperties config) { + final @Nullable String configuredValue = config.getString(GRAPHQL_OPERATION_NAME_PROPERTY); + if (configuredValue != null) { + return Collections.emptyMap(); } + + final @Nullable String deprecatedValue = + config.getString(DEPRECATED_GRAPHQL_OPERATION_NAME_PROPERTY); + return Collections.singletonMap( + GRAPHQL_OPERATION_NAME_PROPERTY, deprecatedValue != null ? deprecatedValue : "true"); } private boolean isSentryAutoInitEnabled() { diff --git a/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/test/kotlin/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProviderTest.kt b/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/test/kotlin/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProviderTest.kt new file mode 100644 index 00000000000..31fcc23a61e --- /dev/null +++ b/sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/test/kotlin/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProviderTest.kt @@ -0,0 +1,40 @@ +package io.sentry.opentelemetry + +import com.google.common.truth.Truth.assertThat +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties +import kotlin.test.Test +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever + +class SentryAutoConfigurationCustomizerProviderTest { + private val provider = SentryAutoConfigurationCustomizerProvider() + private val config = mock() + + @Test + fun `current GraphQL operation name property takes precedence`() { + whenever(config.getString(currentProperty)).thenReturn("false") + whenever(config.getString(deprecatedProperty)).thenReturn("true") + + assertThat(provider.customizeOpenTelemetryDefaults(config)).isEmpty() + } + + @Test + fun `migrates deprecated GraphQL operation name property`() { + whenever(config.getString(deprecatedProperty)).thenReturn("false") + + assertThat(provider.customizeOpenTelemetryDefaults(config)) + .containsExactly(currentProperty, "false") + } + + @Test + fun `enables GraphQL operation names by default`() { + assertThat(provider.customizeOpenTelemetryDefaults(config)) + .containsExactly(currentProperty, "true") + } + + private companion object { + const val currentProperty = "otel.instrumentation.graphql.operation-name-in-span-name.enabled" + const val deprecatedProperty = + "otel.instrumentation.graphql.add-operation-name-to-span-name.enabled" + } +} diff --git a/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/AgentApp.kt b/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/AgentApp.kt index 9d63b216e73..0336a66b48b 100644 --- a/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/AgentApp.kt +++ b/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/AgentApp.kt @@ -14,14 +14,18 @@ import org.springframework.boot.web.client.RestTemplateBuilder * The test task decides what initializes Sentry: the agent or, with `SENTRY_AUTO_INIT=false`, the * Spring Boot starter. */ -class AgentApp(args: List = emptyList()) : Closeable { +class AgentApp( + args: List = emptyList(), + additionalJvmArgs: List = emptyList(), +) : Closeable { val server = MockSentryServer() private val app = ChildJvmApp.start( mainClass = "io.sentry.spring.boot.agentfixture.AgentFixtureApplicationKt", classpath = requiredTestProperty("sentry.test.appClasspath"), - jvmArgs = listOf("-javaagent:${requiredTestProperty("sentry.test.agentJar")}"), + jvmArgs = + listOf("-javaagent:${requiredTestProperty("sentry.test.agentJar")}") + additionalJvmArgs, // Environment variables, because the agent doesn't read Spring Boot's properties. env = mapOf( diff --git a/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt b/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt index e881a8af4d0..41448a8c91f 100644 --- a/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt +++ b/sentry-spring-boot/src/integrationTestOtelAgent/kotlin/io/sentry/spring/boot/otelagentintegrationtest/GraphqlIntegrationTest.kt @@ -12,7 +12,7 @@ import kotlin.test.Test * The agent traces each GraphQL operation as one span, with the operation's name as its op and * description, so the queries are named. */ -class GraphqlIntegrationTest : IntegrationTest() { +open class GraphqlIntegrationTest : IntegrationTest() { @Test fun `query is traced`() { @@ -116,3 +116,18 @@ class GraphqlIntegrationTest : IntegrationTest() { .transaction()!! } } + +/** Runs the GraphQL agent tests where deprecated OpenTelemetry properties are ignored. */ +class GraphqlV3IntegrationTest : GraphqlIntegrationTest() { + override val app: AgentApp + get() = v3PreviewApp + + companion object { + private val v3PreviewApp: AgentApp by lazy { + AgentApp(additionalJvmArgs = listOf("-Dotel.instrumentation.common.v3-preview=true")).also { + app -> + Runtime.getRuntime().addShutdownHook(Thread { app.close() }) + } + } + } +}