Skip to content

Commit a1ddfed

Browse files
authored
Collect PerformanceCollectionData only in sampled transactions (#4834)
* compositePerformanceCollector is now set only when transaction is sampled
1 parent e70d6d4 commit a1ddfed

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
### Improvements
1313

1414
- [ANR] Defer some class availability checks ([#4825](https://github.com/getsentry/sentry-java/pull/4825))
15+
- Collect PerformanceCollectionData only for sampled transactions ([#4834](https://github.com/getsentry/sentry-java/pull/4834))
16+
- **Breaking change**: Transactions with a deferred sampling decision (`sampled == null`) won't be collecting any performance data anymore (CPU, RAM, slow/frozen frames).
1517

1618
### Dependencies
1719

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

Lines changed: 6 additions & 4 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

@@ -90,9 +92,9 @@ public SentryTracer(
9092
}
9193

9294
// We are currently sending the performance data only in profiles, but we are always sending
93-
// performance measurements.
94-
if (compositePerformanceCollector != null) {
95-
compositePerformanceCollector.start(this);
95+
// performance measurements (frames data in spans).
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: 28 additions & 4 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
@@ -1170,9 +1171,20 @@ class SentryTracerTest {
11701171
}
11711172

11721173
@Test
1173-
fun `when transaction is created, but not profiled, compositePerformanceCollector is started anyway`() {
1174-
val transaction = fixture.getSut()
1174+
fun `when transaction is created and sampled, but not profiled, compositePerformanceCollector is started anyway`() {
1175+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
11751176
verify(fixture.compositePerformanceCollector).start(anyOrNull<ITransaction>())
1177+
assertEquals(
1178+
fixture.compositePerformanceCollector,
1179+
transaction.getProperty("compositePerformanceCollector"),
1180+
)
1181+
}
1182+
1183+
@Test
1184+
fun `when transaction is created, but not sampled, compositePerformanceCollector is not started nor set`() {
1185+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(false))
1186+
verify(fixture.compositePerformanceCollector, never()).start(anyOrNull<ITransaction>())
1187+
assertNull(transaction.getProperty("compositePerformanceCollector"))
11761188
}
11771189

11781190
@Test
@@ -1188,15 +1200,15 @@ class SentryTracerTest {
11881200

11891201
@Test
11901202
fun `when transaction is finished, compositePerformanceCollector is stopped`() {
1191-
val transaction = fixture.getSut()
1203+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
11921204
transaction.finish()
11931205
verify(fixture.compositePerformanceCollector)
11941206
.stop(check<ITransaction> { assertEquals(transaction, it) })
11951207
}
11961208

11971209
@Test
11981210
fun `when a span is started and finished the compositePerformanceCollector gets notified`() {
1199-
val transaction = fixture.getSut()
1211+
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
12001212

12011213
val span = transaction.startChild("op.span")
12021214
span.finish()
@@ -1205,6 +1217,17 @@ class SentryTracerTest {
12051217
verify(fixture.compositePerformanceCollector).onSpanFinished(check { assertEquals(span, it) })
12061218
}
12071219

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+
12081231
@Test
12091232
fun `changing transaction name without source sets source to custom`() {
12101233
val transaction = fixture.getSut()
@@ -1393,6 +1416,7 @@ class SentryTracerTest {
13931416
}
13941417
val transaction =
13951418
fixture.getSut(
1419+
samplingDecision = TracesSamplingDecision(true),
13961420
optionsConfiguration = { it.profilesSampleRate = 1.0 },
13971421
performanceCollector = mockPerformanceCollector,
13981422
)

0 commit comments

Comments
 (0)