Skip to content

Commit 93b327f

Browse files
authored
Add OTEL propagator (#2380)
1 parent 71d79f1 commit 93b327f

File tree

41 files changed

+497
-168
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+497
-168
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixes
66

77
- Fix `Gpu.vendorId` should be a String ([#2343](https://github.com/getsentry/sentry-java/pull/2343))
8+
- Do not attach empty `sentry-trace` and `baggage` headers ([#2385](https://github.com/getsentry/sentry-java/pull/2385))
89

910
### Features
1011

buildSrc/src/main/java/Config.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ object Config {
200200
val SENTRY_SPRING_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.spring"
201201
val SENTRY_SPRING_BOOT_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.spring-boot"
202202
val SENTRY_SPRING_BOOT_JAKARTA_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.spring-boot.jakarta"
203+
val SENTRY_OPENTELEMETRY_AGENT_SDK_NAME = "$SENTRY_JAVA_SDK_NAME.opentelemetry.agent"
203204
val group = "io.sentry"
204205
val description = "SDK for sentry.io"
205206
val versionNameProp = "versionName"

sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class SentryOkHttpInterceptor(
6464
var code: Int? = null
6565
try {
6666
val requestBuilder = request.newBuilder()
67-
if (span != null &&
67+
if (span != null && !span.isNoOp &&
6868
PropagationTargetsUtils.contain(hub.options.tracePropagationTargets, request.url.toString())
6969
) {
7070
span.toSentryTrace().let {

sentry-apollo-3/src/main/java/io/sentry/apollo3/SentryApollo3HttpInterceptor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class SentryApollo3HttpInterceptor @JvmOverloads constructor(private val hub: IH
3333

3434
var cleanedHeaders = removeSentryInternalHeaders(request.headers).toMutableList()
3535

36-
if (PropagationTargetsUtils.contain(hub.options.tracePropagationTargets, request.url)) {
36+
if (!span.isNoOp && PropagationTargetsUtils.contain(hub.options.tracePropagationTargets, request.url)) {
3737
val sentryTraceHeader = span.toSentryTrace()
3838
val baggageHeader = span.toBaggageHeader(request.headers.filter { it.name == BaggageHeader.BAGGAGE_HEADER }.map { it.value })
3939
cleanedHeaders.add(HttpHeader(sentryTraceHeader.name, sentryTraceHeader.value))

sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ class SentryApolloInterceptor(
3737
chain.proceedAsync(request, dispatcher, callBack)
3838
} else {
3939
val span = startChild(request, activeSpan)
40-
val sentryTraceHeader = span.toSentryTrace()
4140

42-
// we have no access to URI, no way to verify tracing origins
43-
val requestHeaderBuilder = request.requestHeaders.toBuilder()
44-
requestHeaderBuilder.addHeader(sentryTraceHeader.name, sentryTraceHeader.value)
45-
span.toBaggageHeader(listOf(request.requestHeaders.headerValue(BaggageHeader.BAGGAGE_HEADER)))?.let {
46-
requestHeaderBuilder.addHeader(it.name, it.value)
41+
val requestWithHeader = if (span.isNoOp) {
42+
request
43+
} else {
44+
val sentryTraceHeader = span.toSentryTrace()
45+
46+
// we have no access to URI, no way to verify tracing origins
47+
val requestHeaderBuilder = request.requestHeaders.toBuilder()
48+
requestHeaderBuilder.addHeader(sentryTraceHeader.name, sentryTraceHeader.value)
49+
span.toBaggageHeader(listOf(request.requestHeaders.headerValue(BaggageHeader.BAGGAGE_HEADER)))
50+
?.let {
51+
requestHeaderBuilder.addHeader(it.name, it.value)
52+
}
53+
val headers = requestHeaderBuilder.build()
54+
request.toBuilder().requestHeaders(headers).build()
4755
}
48-
val headers = requestHeaderBuilder.build()
49-
val requestWithHeader = request.toBuilder().requestHeaders(headers).build()
56+
5057
span.setData("operationId", requestWithHeader.operation.operationId())
5158
span.setData("variables", requestWithHeader.operation.variables().valueMap().toString())
5259

sentry-jdbc/src/main/java/io/sentry/jdbc/SentryJdbcEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public SentryJdbcEventListener() {
3030
@Override
3131
public void onBeforeAnyExecute(final @NotNull StatementInformation statementInformation) {
3232
final ISpan parent = hub.getSpan();
33-
if (parent != null) {
33+
if (parent != null && !parent.isNoOp()) {
3434
final ISpan span = parent.startChild("db.query", statementInformation.getSql());
3535
CURRENT_SPAN.set(span);
3636
}

sentry-openfeign/src/main/java/io/sentry/openfeign/SentryFeignClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public Response execute(final @NotNull Request request, final @NotNull Request.O
5555

5656
final RequestWrapper requestWrapper = new RequestWrapper(request);
5757

58-
if (PropagationTargetsUtils.contain(hub.getOptions().getTracePropagationTargets(), url)) {
58+
if (!span.isNoOp()
59+
&& PropagationTargetsUtils.contain(hub.getOptions().getTracePropagationTargets(), url)) {
5960
final SentryTraceHeader sentryTraceHeader = span.toSentryTrace();
6061
final @Nullable Collection<String> requestBaggageHeader =
6162
request.headers().get(BaggageHeader.BAGGAGE_HEADER);

sentry-opentelemetry/sentry-opentelemetry-agent/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ tasks {
147147
attributes.put("Can-Retransform-Classes", "true")
148148
attributes.put("Implementation-Vendor", "Sentry")
149149
attributes.put("Implementation-Version", "sentry-${project.version}-otel-${Config.Libs.otelJavaagentVersion}")
150+
attributes.put("Sentry-Version-Name", project.version)
151+
attributes.put("Sentry-Opentelemetry-SDK-Name", Config.Sentry.SENTRY_OPENTELEMETRY_AGENT_SDK_NAME)
152+
attributes.put("Sentry-Opentelemetry-Version-Name", Config.Libs.otelVersion)
153+
attributes.put("Sentry-Opentelemetry-Javaagent-Version-Name", Config.Libs.otelJavaagentVersion)
150154
}
151155
}
152156

sentry-opentelemetry/sentry-opentelemetry-agentcustomization/api/sentry-opentelemetry-agentcustomization.api

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ public final class io/sentry/opentelemetry/SentryBootstrapPackagesProvider : io/
88
public fun configure (Lio/opentelemetry/javaagent/tooling/bootstrap/BootstrapPackagesBuilder;Lio/opentelemetry/sdk/autoconfigure/spi/ConfigProperties;)V
99
}
1010

11+
public final class io/sentry/opentelemetry/SentryPropagatorProvider : io/opentelemetry/sdk/autoconfigure/spi/ConfigurablePropagatorProvider {
12+
public fun <init> ()V
13+
public fun getName ()Ljava/lang/String;
14+
public fun getPropagator (Lio/opentelemetry/sdk/autoconfigure/spi/ConfigProperties;)Lio/opentelemetry/context/propagation/TextMapPropagator;
15+
}
16+

sentry-opentelemetry/sentry-opentelemetry-agentcustomization/src/main/java/io/sentry/opentelemetry/SentryAutoConfigurationCustomizerProvider.java

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,97 @@
44
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
55
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
66
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
7+
import io.sentry.Instrumenter;
8+
import io.sentry.Sentry;
9+
import io.sentry.SentryOptions;
10+
import io.sentry.protocol.SdkVersion;
11+
import java.io.IOException;
12+
import java.net.URL;
13+
import java.util.Enumeration;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import java.util.jar.Attributes;
17+
import java.util.jar.Manifest;
18+
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
720

821
public final class SentryAutoConfigurationCustomizerProvider
922
implements AutoConfigurationCustomizerProvider {
1023

1124
@Override
1225
public void customize(AutoConfigurationCustomizer autoConfiguration) {
13-
autoConfiguration.addTracerProviderCustomizer(this::configureSdkTracerProvider);
14-
// .addPropertiesSupplier(this::getDefaultProperties);
26+
final @Nullable String sentryPropertiesFile = System.getenv("SENTRY_PROPERTIES_FILE");
27+
final @Nullable String sentryDsn = System.getenv("SENTRY_DSN");
28+
29+
if (sentryPropertiesFile != null || sentryDsn != null) {
30+
Sentry.init(
31+
options -> {
32+
options.setEnableExternalConfiguration(true);
33+
options.setInstrumenter(Instrumenter.OTEL);
34+
final @Nullable SdkVersion sdkVersion = createSdkVersion(options);
35+
if (sdkVersion != null) {
36+
options.setSdkVersion(sdkVersion);
37+
}
38+
});
39+
}
40+
41+
autoConfiguration
42+
.addTracerProviderCustomizer(this::configureSdkTracerProvider)
43+
.addPropertiesSupplier(this::getDefaultProperties);
44+
}
45+
46+
private @Nullable SdkVersion createSdkVersion(final @NotNull SentryOptions sentryOptions) {
47+
SdkVersion sdkVersion = sentryOptions.getSdkVersion();
48+
49+
try {
50+
final @NotNull Enumeration<URL> resources =
51+
ClassLoader.getSystemClassLoader().getResources("META-INF/MANIFEST.MF");
52+
while (resources.hasMoreElements()) {
53+
try {
54+
final @NotNull Manifest manifest = new Manifest(resources.nextElement().openStream());
55+
final @Nullable Attributes mainAttributes = manifest.getMainAttributes();
56+
if (mainAttributes != null) {
57+
final @Nullable String name = mainAttributes.getValue("Sentry-Opentelemetry-SDK-Name");
58+
final @Nullable String version = mainAttributes.getValue("Sentry-Version-Name");
59+
60+
if (name != null && version != null) {
61+
sdkVersion = SdkVersion.updateSdkVersion(sdkVersion, name, version);
62+
sdkVersion.addPackage("maven:io.sentry:sentry-opentelemetry-agent", version);
63+
final @Nullable String otelVersion =
64+
mainAttributes.getValue("Sentry-Opentelemetry-Version-Name");
65+
if (otelVersion != null) {
66+
sdkVersion.addPackage("maven:io.opentelemetry:opentelemetry-sdk", otelVersion);
67+
}
68+
final @Nullable String otelJavaagentVersion =
69+
mainAttributes.getValue("Sentry-Opentelemetry-Javaagent-Version-Name");
70+
if (otelJavaagentVersion != null) {
71+
sdkVersion.addPackage(
72+
"maven:io.opentelemetry.javaagent:opentelemetry-javaagent",
73+
otelJavaagentVersion);
74+
}
75+
}
76+
}
77+
} catch (Exception e) {
78+
// ignore
79+
}
80+
}
81+
} catch (IOException e) {
82+
// ignore
83+
}
84+
85+
return sdkVersion;
1586
}
1687

1788
private SdkTracerProviderBuilder configureSdkTracerProvider(
1889
SdkTracerProviderBuilder tracerProvider, ConfigProperties config) {
19-
2090
return tracerProvider.addSpanProcessor(new SentrySpanProcessor());
2191
}
2292

23-
// private Map<String, String> getDefaultProperties() {
24-
// Map<String, String> properties = new HashMap<>();
25-
// properties.put("otel.exporter.otlp.endpoint", "http://backend:8080");
26-
// properties.put("otel.exporter.otlp.insecure", "true");
27-
// properties.put("otel.config.max.attrs", "16");
28-
// properties.put("otel.traces.sampler", "demo");
29-
// return properties;
30-
// }
93+
private Map<String, String> getDefaultProperties() {
94+
Map<String, String> properties = new HashMap<>();
95+
properties.put("otel.traces.exporter", "none");
96+
properties.put("otel.metrics.exporter", "none");
97+
properties.put("otel.propagators", "sentry");
98+
return properties;
99+
}
31100
}

0 commit comments

Comments
 (0)