Skip to content

Commit 9683566

Browse files
adinauergetsentry-botlcian
authored
Pass OpenTelemetry span attributes into TracesSampler callback (#4253)
* Pass OpenTelemetry span attributes into TracesSampler callback * Format code * changelog * api * Apply suggestions from code review Co-authored-by: Lorenzo Cian <[email protected]> --------- Co-authored-by: Sentry Github Bot <[email protected]> Co-authored-by: Lorenzo Cian <[email protected]>
1 parent 878fd7b commit 9683566

File tree

7 files changed

+132
-32
lines changed

7 files changed

+132
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
- Fix "class ch.qos.logback.classic.spi.ThrowableProxyVO cannot be cast to class ch.qos.logback.classic.spi.ThrowableProxy" ([#4206](https://github.com/getsentry/sentry-java/pull/4206))
1717
- In this case we cannot report the `Throwable` to Sentry as it's not available
1818
- If you are using OpenTelemetry v1 `OpenTelemetryAppender`, please consider upgrading to v2
19+
- Pass OpenTelemetry span attributes into TracesSampler callback ([#4253](https://github.com/getsentry/sentry-java/pull/4253))
20+
- `SamplingContext` now has a `getAttribute` method that grants access to OpenTelemetry span attributes via their String key (e.g. `http.request.method`)
1921

2022
### Features
2123

sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SentrySampler.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import io.sentry.TransactionContext;
2424
import io.sentry.clientreport.DiscardReason;
2525
import io.sentry.protocol.SentryId;
26+
import java.util.HashMap;
2627
import java.util.List;
28+
import java.util.Map;
2729
import org.jetbrains.annotations.NotNull;
2830
import org.jetbrains.annotations.Nullable;
2931

@@ -64,13 +66,15 @@ public SamplingResult shouldSample(
6466
if (samplingDecision != null) {
6567
return new SentrySamplingResult(samplingDecision);
6668
} else {
67-
return handleRootOtelSpan(traceId, parentContext);
69+
return handleRootOtelSpan(traceId, parentContext, attributes);
6870
}
6971
}
7072
}
7173

7274
private @NotNull SamplingResult handleRootOtelSpan(
73-
final @NotNull String traceId, final @NotNull Context parentContext) {
75+
final @NotNull String traceId,
76+
final @NotNull Context parentContext,
77+
final @NotNull Attributes attributes) {
7478
if (!scopes.getOptions().isTracingEnabled()) {
7579
return SamplingResult.create(SamplingDecision.RECORD_ONLY);
7680
}
@@ -96,7 +100,11 @@ public SamplingResult shouldSample(
96100
.getOptions()
97101
.getInternalTracesSampler()
98102
.sample(
99-
new SamplingContext(transactionContext, null, propagationContext.getSampleRand()));
103+
new SamplingContext(
104+
transactionContext,
105+
null,
106+
propagationContext.getSampleRand(),
107+
toMapWithStringKeys(attributes)));
100108

101109
if (!sentryDecision.getSampled()) {
102110
scopes
@@ -135,6 +143,22 @@ public SamplingResult shouldSample(
135143
}
136144
}
137145

146+
private @NotNull Map<String, Object> toMapWithStringKeys(final @NotNull Attributes attributes) {
147+
final @NotNull Map<String, Object> mapWithStringKeys = new HashMap<>(attributes.size());
148+
149+
if (attributes != null) {
150+
attributes.forEach(
151+
(key, value) -> {
152+
if (key != null) {
153+
final @NotNull String stringKey = key.getKey();
154+
mapWithStringKeys.put(stringKey, value);
155+
}
156+
});
157+
}
158+
159+
return mapWithStringKeys;
160+
}
161+
138162
@Override
139163
public String getDescription() {
140164
return "SentrySampler";

sentry/api/sentry.api

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,8 @@ public final class io/sentry/RequestDetails {
20232023

20242024
public final class io/sentry/SamplingContext {
20252025
public fun <init> (Lio/sentry/TransactionContext;Lio/sentry/CustomSamplingContext;)V
2026-
public fun <init> (Lio/sentry/TransactionContext;Lio/sentry/CustomSamplingContext;Ljava/lang/Double;)V
2026+
public fun <init> (Lio/sentry/TransactionContext;Lio/sentry/CustomSamplingContext;Ljava/lang/Double;Ljava/util/Map;)V
2027+
public fun getAttribute (Ljava/lang/String;)Ljava/lang/Object;
20272028
public fun getCustomSamplingContext ()Lio/sentry/CustomSamplingContext;
20282029
public fun getSampleRand ()Ljava/lang/Double;
20292030
public fun getTransactionContext ()Lio/sentry/TransactionContext;

sentry/src/main/java/io/sentry/SamplingContext.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import io.sentry.util.Objects;
44
import io.sentry.util.SentryRandom;
5+
import java.util.Collections;
6+
import java.util.Map;
57
import org.jetbrains.annotations.ApiStatus;
68
import org.jetbrains.annotations.NotNull;
79
import org.jetbrains.annotations.Nullable;
@@ -14,6 +16,7 @@ public final class SamplingContext {
1416
private final @NotNull TransactionContext transactionContext;
1517
private final @Nullable CustomSamplingContext customSamplingContext;
1618
private final @NotNull Double sampleRand;
19+
private final @NotNull Map<String, Object> attributes;
1720

1821
@Deprecated
1922
@SuppressWarnings("InlineMeSuggester")
@@ -23,18 +26,20 @@ public final class SamplingContext {
2326
public SamplingContext(
2427
final @NotNull TransactionContext transactionContext,
2528
final @Nullable CustomSamplingContext customSamplingContext) {
26-
this(transactionContext, customSamplingContext, SentryRandom.current().nextDouble());
29+
this(transactionContext, customSamplingContext, SentryRandom.current().nextDouble(), null);
2730
}
2831

2932
@ApiStatus.Internal
3033
public SamplingContext(
3134
final @NotNull TransactionContext transactionContext,
3235
final @Nullable CustomSamplingContext customSamplingContext,
33-
final @NotNull Double sampleRand) {
36+
final @NotNull Double sampleRand,
37+
final @Nullable Map<String, Object> attributes) {
3438
this.transactionContext =
3539
Objects.requireNonNull(transactionContext, "transactionContexts is required");
3640
this.customSamplingContext = customSamplingContext;
3741
this.sampleRand = sampleRand;
42+
this.attributes = attributes == null ? Collections.emptyMap() : attributes;
3843
}
3944

4045
public @Nullable CustomSamplingContext getCustomSamplingContext() {
@@ -48,4 +53,11 @@ public SamplingContext(
4853
public @NotNull Double getSampleRand() {
4954
return sampleRand;
5055
}
56+
57+
public @Nullable Object getAttribute(final @Nullable String key) {
58+
if (key == null) {
59+
return null;
60+
}
61+
return this.attributes.get(key);
62+
}
5163
}

sentry/src/main/java/io/sentry/Scopes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ public void flush(long timeoutMillis) {
860860
final Double sampleRand = getSampleRand(transactionContext);
861861
final SamplingContext samplingContext =
862862
new SamplingContext(
863-
transactionContext, transactionOptions.getCustomSamplingContext(), sampleRand);
863+
transactionContext, transactionOptions.getCustomSamplingContext(), sampleRand, null);
864864
final @NotNull TracesSampler tracesSampler = getOptions().getInternalTracesSampler();
865865
@NotNull TracesSamplingDecision samplingDecision = tracesSampler.sample(samplingContext);
866866
transactionContext.setSamplingDecision(samplingDecision);

sentry/src/main/java/io/sentry/Sentry.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,8 @@ private static void handleAppStartProfilingConfig(
460460
TransactionContext appStartTransactionContext = new TransactionContext("app.launch", "profile");
461461
appStartTransactionContext.setForNextAppStart(true);
462462
SamplingContext appStartSamplingContext =
463-
new SamplingContext(appStartTransactionContext, null, SentryRandom.current().nextDouble());
463+
new SamplingContext(
464+
appStartTransactionContext, null, SentryRandom.current().nextDouble(), null);
464465
return options.getInternalTracesSampler().sample(appStartSamplingContext);
465466
}
466467

0 commit comments

Comments
 (0)