Skip to content

Commit b2a4b4d

Browse files
Fix: Discard unfinished Spans before sending them over to Sentry (#1279)
Fixes #1262
1 parent cfc37c4 commit b2a4b4d

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vNext
22

3+
34
* Fix: Remove experimental annotation for Attachment #1257
45
* Fix: Mark stacktrace as snapshot if captured at arbitrary moment #1231
56
* Enchancement: Improve EventProcessor nullability annotations (#1229).
@@ -18,6 +19,7 @@
1819
* Enchancement: Add Request to the Scope. #1270
1920
* Fix: Fix SentryTransaction#getStatus NPE (#1273)
2021
* Enchancement: Optimize SentryTracingFilter when hub is disabled.
22+
* Fix: Discard unfinished Spans before sending them over to Sentry (#1279)
2123
* Fix: Interrupt the thread in QueuedThreadPoolExecutor (#1276)
2224
* Fix: SentryTransaction#finish should not clear another transaction from the scope (#1278)
2325

sentry/src/main/java/io/sentry/SentryClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,18 @@ public void captureSession(final @NotNull Session session, final @Nullable Objec
409409
}
410410
}
411411
}
412+
final List<Span> unfinishedSpans = new ArrayList<>();
413+
for (Span span : transaction.getSpans()) {
414+
if (!span.isFinished()) {
415+
unfinishedSpans.add(span);
416+
}
417+
}
418+
if (!unfinishedSpans.isEmpty()) {
419+
options
420+
.getLogger()
421+
.log(SentryLevel.WARNING, "Dropping %d unfinished spans", unfinishedSpans.size());
422+
}
423+
transaction.getSpans().removeAll(unfinishedSpans);
412424
return transaction;
413425
}
414426

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,25 @@ class SentryClientTest {
757757
}, eq(null))
758758
}
759759

760+
@Test
761+
fun `when captureTransactions unfinished spans are removed`() {
762+
val sut = fixture.getSut()
763+
val transaction = SentryTransaction("a-transaction", "op")
764+
val span1 = transaction.startChild("span1")
765+
span1.finish()
766+
val span2 = transaction.startChild("span2")
767+
768+
sut.captureTransaction(transaction, mock(), null)
769+
verify(fixture.transport).send(check {
770+
val sentTransaction = it.items.first().getTransaction(fixture.sentryOptions.serializer)
771+
assertNotNull(sentTransaction) { tx ->
772+
val sentSpanIds = tx.spans.map { span -> span.spanId }
773+
assertTrue(sentSpanIds.contains(span1.spanContext.spanId))
774+
assertFalse(sentSpanIds.contains(span2.spanContext.spanId))
775+
}
776+
}, eq(null))
777+
}
778+
760779
@Test
761780
fun `when captureTransaction with attachments`() {
762781
val transaction = SentryTransaction("a-transaction", "op")

0 commit comments

Comments
 (0)