Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix don't overwrite the span status of unfinished spans #2859

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Breaking changes:

Breaking changes:
- Move enableNdk from SentryOptions to SentryAndroidOptions ([#2793](https://github.com/getsentry/sentry-java/pull/2793))
- Fix don't overwrite the span status of unfinished spans ([#2859](https://github.com/getsentry/sentry-java/pull/2859))
markushi marked this conversation as resolved.
Show resolved Hide resolved

## Unreleased

Expand Down
13 changes: 5 additions & 8 deletions sentry/src/main/java/io/sentry/SentryTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,11 @@ public void finish(
performanceCollectionData.clear();
}

// finish unfinished children
for (final Span child : children) {
if (!child.isFinished()) {
child.setSpanFinishedCallback(
null); // reset the callback, as we're already in the finish method
child.finish(SpanStatus.DEADLINE_EXCEEDED, finishTimestamp);
}
}
// any un-finished childs will remain unfinished
// as relay takes care of setting the end-timestamp + deadline_exceeded
// see
// https://github.com/getsentry/relay/blob/40697d0a1c54e5e7ad8d183fc7f9543b94fe3839/relay-general/src/store/transactions/processor.rs#L374-L378

root.finish(finishStatus.spanStatus, finishTimestamp);

hub.configureScope(
Expand Down
6 changes: 4 additions & 2 deletions sentry/src/main/java/io/sentry/protocol/SentrySpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public SentrySpan(final @NotNull Span span, final @Nullable Map<String, Object>
this.tags = tagsCopy != null ? tagsCopy : new ConcurrentHashMap<>();
// we lose precision here, from potential nanosecond precision down to 10 microsecond precision
this.timestamp =
DateUtils.nanosToSeconds(
span.getStartDate().laterDateNanosTimestampByDiff(span.getFinishDate()));
span.getFinishDate() == null
? null
: DateUtils.nanosToSeconds(
span.getStartDate().laterDateNanosTimestampByDiff(span.getFinishDate()));
// we lose precision here, from potential nanosecond precision down to 10 microsecond precision
this.startTimestamp = DateUtils.nanosToSeconds(span.getStartDate().nanoTimestamp());
this.data = data;
Expand Down
19 changes: 14 additions & 5 deletions sentry/src/test/java/io/sentry/SentryTracerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,27 @@ class SentryTracerTest {
}

@Test
fun `finishing unfinished spans with the transaction timestamp`() {
fun `when finishing, unfinished spans won't have any automatic end-date or status`() {
val transaction = fixture.getSut(samplingDecision = TracesSamplingDecision(true))
// span with no status set
val span = transaction.startChild("op") as Span
transaction.startChild("op2")
span.finish(SpanStatus.INVALID_ARGUMENT)

// span with a status
val span1 = transaction.startChild("op2")

transaction.finish(SpanStatus.INVALID_ARGUMENT)

verify(fixture.hub, times(1)).captureTransaction(
check {
assertEquals(2, it.spans.size)
assertEquals(transaction.root.finishDate, span.finishDate)
assertEquals(SpanStatus.DEADLINE_EXCEEDED, it.spans[0].status)
assertEquals(SpanStatus.DEADLINE_EXCEEDED, it.spans[1].status)
// span status/timestamp is retained
assertNotNull(it.spans[0].status)
assertNotNull(it.spans[0].timestamp)

// span status/timestamp remains untouched
assertNull(it.spans[1].status)
assertNull(it.spans[1].timestamp)
},
anyOrNull<TraceContext>(),
anyOrNull(),
Expand Down
35 changes: 35 additions & 0 deletions sentry/src/test/java/io/sentry/protocol/SentrySpanTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.sentry.protocol

import io.sentry.IHub
import io.sentry.SentryLongDate
import io.sentry.SentryTracer
import io.sentry.Span
import io.sentry.SpanOptions
import io.sentry.TransactionContext
import org.junit.Test
import org.mockito.kotlin.mock
import kotlin.test.assertEquals
import kotlin.test.assertNull

class SentrySpanTest {

@Test
fun `end timestamps is kept null if not provided`() {
// when a span with a start timestamp is generated
val span = Span(
TransactionContext("name", "op"),
mock<SentryTracer>(),
mock<IHub>(),
SentryLongDate(1000000),
SpanOptions()
)

val sentrySpan = SentrySpan(span)

// then the start timestamp should be correctly set
assertEquals(0.001, sentrySpan.startTimestamp)

// but the end time should remain untouched
assertNull(sentrySpan.timestamp)
}
}
Loading