Skip to content

Commit d7c8f8f

Browse files
committed
compositePerformanceCollector is now set only when transaction is sampled
1 parent 3c35ad4 commit d7c8f8f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

sentry/src/main/java/io/sentry/SentryTracer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public SentryTracer(
7777
this.name = context.getName();
7878
this.instrumenter = context.getInstrumenter();
7979
this.scopes = scopes;
80-
this.compositePerformanceCollector = compositePerformanceCollector;
80+
// Let's collect performance data (cpu, ram, frames) only when the transaction is sampled
81+
this.compositePerformanceCollector =
82+
Boolean.TRUE.equals(isSampled()) ? compositePerformanceCollector : null;
8183
this.transactionNameSource = context.getTransactionNameSource();
8284
this.transactionOptions = transactionOptions;
8385

@@ -91,8 +93,8 @@ public SentryTracer(
9193

9294
// We are currently sending the performance data only in profiles, but we are always sending
9395
// performance measurements (frames data in spans).
94-
if (compositePerformanceCollector != null && Boolean.TRUE.equals(isSampled())) {
95-
compositePerformanceCollector.start(this);
96+
if (this.compositePerformanceCollector != null) {
97+
this.compositePerformanceCollector.start(this);
9698
}
9799

98100
if (transactionOptions.getIdleTimeout() != null

sentry/src/test/java/io/sentry/SentryTracerTest.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import io.sentry.protocol.SentryId
44
import io.sentry.protocol.TransactionNameSource
55
import io.sentry.protocol.User
66
import io.sentry.test.createTestScopes
7+
import io.sentry.test.getProperty
78
import io.sentry.util.thread.IThreadChecker
89
import java.time.LocalDateTime
910
import java.time.ZoneOffset
@@ -1173,12 +1174,17 @@ class SentryTracerTest {
11731174
fun `when transaction is created and sampled, but not profiled, compositePerformanceCollector is started anyway`() {
11741175
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
11751176
verify(fixture.compositePerformanceCollector).start(anyOrNull<ITransaction>())
1177+
assertEquals(
1178+
fixture.compositePerformanceCollector,
1179+
transaction.getProperty("compositePerformanceCollector"),
1180+
)
11761181
}
11771182

11781183
@Test
1179-
fun `when transaction is created, but not sampled, compositePerformanceCollector is not started`() {
1184+
fun `when transaction is created, but not sampled, compositePerformanceCollector is not started nor set`() {
11801185
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(false))
11811186
verify(fixture.compositePerformanceCollector, never()).start(anyOrNull<ITransaction>())
1187+
assertNull(transaction.getProperty("compositePerformanceCollector"))
11821188
}
11831189

11841190
@Test
@@ -1194,15 +1200,15 @@ class SentryTracerTest {
11941200

11951201
@Test
11961202
fun `when transaction is finished, compositePerformanceCollector is stopped`() {
1197-
val transaction = fixture.getSut()
1203+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
11981204
transaction.finish()
11991205
verify(fixture.compositePerformanceCollector)
12001206
.stop(check<ITransaction> { assertEquals(transaction, it) })
12011207
}
12021208

12031209
@Test
12041210
fun `when a span is started and finished the compositePerformanceCollector gets notified`() {
1205-
val transaction = fixture.getSut()
1211+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
12061212

12071213
val span = transaction.startChild("op.span")
12081214
span.finish()
@@ -1211,6 +1217,17 @@ class SentryTracerTest {
12111217
verify(fixture.compositePerformanceCollector).onSpanFinished(check { assertEquals(span, it) })
12121218
}
12131219

1220+
@Test
1221+
fun `when a span is started and finished the compositePerformanceCollector gets never notified if not sampled`() {
1222+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(false))
1223+
1224+
val span = transaction.startChild("op.span")
1225+
span.finish()
1226+
1227+
verify(fixture.compositePerformanceCollector, never()).onSpanStarted(any())
1228+
verify(fixture.compositePerformanceCollector, never()).onSpanFinished(any())
1229+
}
1230+
12141231
@Test
12151232
fun `changing transaction name without source sets source to custom`() {
12161233
val transaction = fixture.getSut()
@@ -1399,6 +1416,7 @@ class SentryTracerTest {
13991416
}
14001417
val transaction =
14011418
fixture.getSut(
1419+
samplingDecision = TracesSamplingDecision(true),
14021420
optionsConfiguration = { it.profilesSampleRate = 1.0 },
14031421
performanceCollector = mockPerformanceCollector,
14041422
)

0 commit comments

Comments
 (0)