Skip to content

Commit a31a59e

Browse files
POTEL 33 - Record dropped spans when sampling OpenTelemetry spans (#3552)
* Use parent span id from sentry-trace header * attach active span to scope * record dropped span/transaction in SentrySampler * changelog * record lost span when copying parent decision * Format code * refactor: use sampling decision --------- Co-authored-by: Sentry Github Bot <[email protected]>
1 parent 46d0f03 commit a31a59e

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Traces were broken because on an incoming request, OtelSentrySpanProcessor did not set the parentSpanId on the span correctly. Traces were not referencing the actual parent span but some other (random) span ID which the server doesn't know.
1010
- Attach active span to scope when using OpenTelemetry ([#3549](https://github.com/getsentry/sentry-java/pull/3549))
1111
- Errors weren't linked to traces correctly due to parts of the SDK not knowing the current span
12+
- Record dropped spans in client report when sampling out OpenTelemetry spans ([#3552](https://github.com/getsentry/sentry-java/pull/3552))
1213

1314
## 8.0.0-alpha.3
1415

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.opentelemetry.sdk.trace.samplers.SamplingDecision;
1212
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
1313
import io.sentry.Baggage;
14+
import io.sentry.DataCategory;
1415
import io.sentry.IScopes;
1516
import io.sentry.PropagationContext;
1617
import io.sentry.SamplingContext;
@@ -19,6 +20,7 @@
1920
import io.sentry.SpanId;
2021
import io.sentry.TracesSamplingDecision;
2122
import io.sentry.TransactionContext;
23+
import io.sentry.clientreport.DiscardReason;
2224
import io.sentry.protocol.SentryId;
2325
import java.util.List;
2426
import org.jetbrains.annotations.NotNull;
@@ -94,7 +96,18 @@ public SamplingResult shouldSample(
9496
.getOptions()
9597
.getInternalTracesSampler()
9698
.sample(new SamplingContext(transactionContext, null));
97-
// TODO [POTEL] if sampling decision = false, we should record it in client report
99+
100+
if (!sentryDecision.getSampled()) {
101+
scopes
102+
.getOptions()
103+
.getClientReportRecorder()
104+
.recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Transaction);
105+
scopes
106+
.getOptions()
107+
.getClientReportRecorder()
108+
.recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Span);
109+
}
110+
98111
return new SentrySamplingResult(sentryDecision);
99112
}
100113

@@ -103,6 +116,12 @@ public SamplingResult shouldSample(
103116
final @Nullable TracesSamplingDecision parentSamplingDecision =
104117
parentSentrySpan.getSamplingDecision();
105118
if (parentSamplingDecision != null) {
119+
if (!parentSamplingDecision.getSampled()) {
120+
scopes
121+
.getOptions()
122+
.getClientReportRecorder()
123+
.recordLostEvent(DiscardReason.SAMPLE_RATE, DataCategory.Span);
124+
}
106125
return new SentrySamplingResult(parentSamplingDecision);
107126
} else {
108127
// this should never happen and only serve to calm the compiler

0 commit comments

Comments
 (0)