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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread information when a span is created #2998

Merged
merged 7 commits into from
Oct 24, 2023
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

## Unreleased

### Fixes
### Features

- Fix crash when HTTP connection error message contains formatting symbols ([#3002](https://github.com/getsentry/sentry-java/pull/3002))
- Add thread information to spans ([#2998](https://github.com/getsentry/sentry-java/pull/2998))

## 6.32.0

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we agree to use the same blocked_main_thread key? I'd rather us choose something different, because we don't know for sure if it blocked the main thread, we know that it was just started there? Also, if we pick a different key, the current behavior for File I/O or DB spans will remain the same with blocked_main_thread.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, this wasn't discussed yet - thanks for bringing this up. @shruthilayaj I think Roman has some good points here, would it make sense for you as well to have a separate flag for main thread flagging?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it makes sense to have these as separate flags!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit lengthy, but how about started_on_main_thread? Would also make it clear that the flag is determined on span start.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rfc https://github.com/getsentry/rfcs/pull/75/files got merged just a couple of days ago, so we probably should adhere to what is defined there. Here's the develop docs PR which adds the the new keys to the span data conventions getsentry/develop#1056, which means we have to send thread.id and thread.name, how does that sounds @shruthilayaj @markushi ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

h: I just wanted to make the same comment 猬嗭笍 . Thanks @romtsn.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃憤
just to confirm, sending thread.id and thread.name in addition to started_on_main_thread flag right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mm, since in the RFC we say it's the thread the span originated from, I think thread.name: main would be the same thing pretty much, so we don't need both flags?
image

Copy link
Member

@shruthilayaj shruthilayaj Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense, and will thread name always be standardized to main if it started on the main thread regardless of the SDK it's coming from? (just so we know if we just look for main or need to maintain some sort of map/allow list for extracting this info in relay)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd start with just main, I guess we can expand it later. We can ensure that it's called main from the SDKs side

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";
}
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))
}
}
Loading