Skip to content

Commit d698679

Browse files
committed
Pass OpenTelemetry span attributes into TracesSampler callback
1 parent 878fd7b commit d698679

File tree

5 files changed

+127
-31
lines changed

5 files changed

+127
-31
lines changed

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
import io.sentry.TransactionContext;
2424
import io.sentry.clientreport.DiscardReason;
2525
import io.sentry.protocol.SentryId;
26+
27+
import java.util.HashMap;
2628
import java.util.List;
29+
import java.util.Map;
30+
2731
import org.jetbrains.annotations.NotNull;
2832
import org.jetbrains.annotations.Nullable;
2933

@@ -64,13 +68,13 @@ public SamplingResult shouldSample(
6468
if (samplingDecision != null) {
6569
return new SentrySamplingResult(samplingDecision);
6670
} else {
67-
return handleRootOtelSpan(traceId, parentContext);
71+
return handleRootOtelSpan(traceId, parentContext, attributes);
6872
}
6973
}
7074
}
7175

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

101105
if (!sentryDecision.getSampled()) {
102106
scopes
@@ -135,6 +139,22 @@ public SamplingResult shouldSample(
135139
}
136140
}
137141

142+
private @NotNull Map<String, Object> toMapWithStringKeys(final @NotNull Attributes attributes) {
143+
final @NotNull Map<String, Object> mapWithStringKeys = new HashMap<>(attributes.size());
144+
145+
if (attributes != null) {
146+
attributes.forEach(
147+
(key, value) -> {
148+
if (key != null) {
149+
final @NotNull String stringKey = key.getKey();
150+
mapWithStringKeys.put(stringKey, value);
151+
}
152+
});
153+
}
154+
155+
return mapWithStringKeys;
156+
}
157+
138158
@Override
139159
public String getDescription() {
140160
return "SentrySampler";

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import org.jetbrains.annotations.NotNull;
77
import org.jetbrains.annotations.Nullable;
88

9+
import java.util.Collections;
10+
import java.util.Map;
11+
912
/**
1013
* Context used by {@link io.sentry.SentryOptions.TracesSamplerCallback} to determine if transaction
1114
* is going to be sampled.
@@ -14,6 +17,7 @@ public final class SamplingContext {
1417
private final @NotNull TransactionContext transactionContext;
1518
private final @Nullable CustomSamplingContext customSamplingContext;
1619
private final @NotNull Double sampleRand;
20+
private final @NotNull Map<String, Object> attributes;
1721

1822
@Deprecated
1923
@SuppressWarnings("InlineMeSuggester")
@@ -23,18 +27,20 @@ public final class SamplingContext {
2327
public SamplingContext(
2428
final @NotNull TransactionContext transactionContext,
2529
final @Nullable CustomSamplingContext customSamplingContext) {
26-
this(transactionContext, customSamplingContext, SentryRandom.current().nextDouble());
30+
this(transactionContext, customSamplingContext, SentryRandom.current().nextDouble(), null);
2731
}
2832

2933
@ApiStatus.Internal
3034
public SamplingContext(
3135
final @NotNull TransactionContext transactionContext,
3236
final @Nullable CustomSamplingContext customSamplingContext,
33-
final @NotNull Double sampleRand) {
37+
final @NotNull Double sampleRand,
38+
final @Nullable Map<String, Object> attributes) {
3439
this.transactionContext =
3540
Objects.requireNonNull(transactionContext, "transactionContexts is required");
3641
this.customSamplingContext = customSamplingContext;
3742
this.sampleRand = sampleRand;
43+
this.attributes = attributes == null ? Collections.emptyMap() : attributes;
3844
}
3945

4046
public @Nullable CustomSamplingContext getCustomSamplingContext() {
@@ -48,4 +54,11 @@ public SamplingContext(
4854
public @NotNull Double getSampleRand() {
4955
return sampleRand;
5056
}
57+
58+
public @Nullable Object getAttribute(final @Nullable String key) {
59+
if (key == null) {
60+
return null;
61+
}
62+
return this.attributes.get(key);
63+
}
5164
}

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ 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(appStartTransactionContext, null, SentryRandom.current().nextDouble(), null);
464464
return options.getInternalTracesSampler().sample(appStartSamplingContext);
465465
}
466466

0 commit comments

Comments
 (0)