Skip to content

Commit

Permalink
Tag all spans with main thread flag (#2998)
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi committed Oct 24, 2023
1 parent 1ab82a3 commit 93a76ca
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Add thread information to spans ([#2998](https://github.com/getsentry/sentry-java/pull/2998))

### Fixes

- Fix crash when HTTP connection error message contains formatting symbols ([#3002](https://github.com/getsentry/sentry-java/pull/3002))
Expand Down
2 changes: 2 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,8 @@ public abstract interface class io/sentry/SpanDataConvention {
public static final field HTTP_QUERY_KEY Ljava/lang/String;
public static final field HTTP_RESPONSE_CONTENT_LENGTH_KEY Ljava/lang/String;
public static final field HTTP_STATUS_CODE_KEY Ljava/lang/String;
public static final field THREAD_ID Ljava/lang/String;
public static final field THREAD_NAME Ljava/lang/String;
}

public final class io/sentry/SpanId : io/sentry/JsonSerializable {
Expand Down
6 changes: 6 additions & 0 deletions sentry/src/main/java/io/sentry/SentryTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ private ISpan createChild(
}
});
span.setDescription(description);
span.setData(SpanDataConvention.THREAD_ID, String.valueOf(Thread.currentThread().getId()));
span.setData(
SpanDataConvention.THREAD_NAME,
hub.getOptions().getMainThreadChecker().isMainThread()
? "main"
: Thread.currentThread().getName());
this.children.add(span);
return span;
}
Expand Down
2 changes: 2 additions & 0 deletions sentry/src/main/java/io/sentry/SpanDataConvention.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface SpanDataConvention {
String HTTP_RESPONSE_CONTENT_LENGTH_KEY = "http.response_content_length";
String BLOCKED_MAIN_THREAD_KEY = "blocked_main_thread";
String CALL_STACK_KEY = "call_stack";
String THREAD_ID = "thread.id";
String THREAD_NAME = "thread.name";
}
2 changes: 2 additions & 0 deletions sentry/src/test/java/io/sentry/OutboxSenderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.sentry.hints.Retryable
import io.sentry.protocol.SentryId
import io.sentry.protocol.SentryTransaction
import io.sentry.util.HintUtils
import io.sentry.util.thread.NoOpMainThreadChecker
import org.mockito.kotlin.any
import org.mockito.kotlin.argWhere
import org.mockito.kotlin.check
Expand Down Expand Up @@ -38,6 +39,7 @@ class OutboxSenderTest {
init {
whenever(options.dsn).thenReturn("https://key@sentry.io/proj")
whenever(options.dateProvider).thenReturn(SentryNanotimeDateProvider())
whenever(options.mainThreadChecker).thenReturn(NoOpMainThreadChecker.getInstance())
whenever(hub.options).thenReturn(this.options)
}

Expand Down
29 changes: 29 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTracerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry

import io.sentry.protocol.TransactionNameSource
import io.sentry.protocol.User
import io.sentry.util.thread.IMainThreadChecker
import org.awaitility.kotlin.await
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
Expand All @@ -11,12 +12,14 @@ import org.mockito.kotlin.never
import org.mockito.kotlin.spy
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.Date
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertSame
Expand Down Expand Up @@ -1236,4 +1239,30 @@ class SentryTracerTest {
assertTrue(tracer.isFinished)
verify(fixture.hub).captureTransaction(any(), anyOrNull(), anyOrNull(), anyOrNull())
}

@Test
fun `when a span is launched on the main thread, the thread info should be set correctly`() {
val mainThreadChecker = mock<IMainThreadChecker>()
whenever(mainThreadChecker.isMainThread).thenReturn(true)

val tracer = fixture.getSut(optionsConfiguration = { options ->
options.mainThreadChecker = mainThreadChecker
})
val span = tracer.startChild("span.op")
assertNotNull(span.getData(SpanDataConvention.THREAD_ID))
assertEquals("main", span.getData(SpanDataConvention.THREAD_NAME))
}

@Test
fun `when a span is launched on the background thread, the thread info should be set correctly`() {
val mainThreadChecker = mock<IMainThreadChecker>()
whenever(mainThreadChecker.isMainThread).thenReturn(false)

val tracer = fixture.getSut(optionsConfiguration = { options ->
options.mainThreadChecker = mainThreadChecker
})
val span = tracer.startChild("span.op")
assertNotNull(span.getData(SpanDataConvention.THREAD_ID))
assertNotEquals("main", span.getData(SpanDataConvention.THREAD_NAME))
}
}

0 comments on commit 93a76ca

Please sign in to comment.