Skip to content

Commit

Permalink
feat(profiles): Extract thread ID and name in spans (#3771)
Browse files Browse the repository at this point in the history
  • Loading branch information
phacops authored Jul 3, 2024
1 parent e59b49b commit ed2fc8c
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Add web vitals support for mobile browsers. ([#3762](https://github.com/getsentry/relay/pull/3762))
- Ingest profiler_id in the profile context and in spans. ([#3714](https://github.com/getsentry/relay/pull/3714), [#3784](https://github.com/getsentry/relay/pull/3784))
- Support extrapolation of metrics extracted from sampled data, as long as the sample rate is set in the DynamicSamplingContext. ([#3753](https://github.com/getsentry/relay/pull/3753))
- Extract thread ID and name in spans. ([#3771](https://github.com/getsentry/relay/pull/3771))

## 24.6.0

Expand Down
124 changes: 123 additions & 1 deletion relay-event-normalization/src/normalize/span/tag_extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pub enum SpanTagKey {
TraceStatus,
MessagingDestinationName,
MessagingMessageId,
ThreadName,
ThreadId,
ProfilerId,
}

Expand Down Expand Up @@ -134,7 +136,8 @@ impl SpanTagKey {
SpanTagKey::TraceStatus => "trace.status",
SpanTagKey::MessagingDestinationName => "messaging.destination.name",
SpanTagKey::MessagingMessageId => "messaging.message.id",

SpanTagKey::ThreadName => "thread.name",
SpanTagKey::ThreadId => "thread.id",
SpanTagKey::ProfilerId => "profiler_id",
}
}
Expand Down Expand Up @@ -343,6 +346,19 @@ fn extract_shared_tags(event: &Event) -> BTreeMap<SpanTagKey, String> {
event.platform.as_str().unwrap_or("other").into(),
);

if let Some(data) = event
.context::<TraceContext>()
.and_then(|trace_context| trace_context.data.value())
{
if let Some(thread_id) = data.thread_id.value() {
tags.insert(SpanTagKey::ThreadId, thread_id.to_string());
}

if let Some(thread_name) = data.thread_name.value() {
tags.insert(SpanTagKey::ThreadName, thread_name.to_string());
}
}

tags
}

Expand Down Expand Up @@ -763,6 +779,16 @@ pub fn extract_tags(
span_tags.insert(SpanTagKey::BrowserName, browser_name.clone());
}

if let Some(data) = span.data.value() {
if let Some(thread_id) = data.thread_id.value() {
span_tags.insert(SpanTagKey::ThreadId, thread_id.to_string());
}

if let Some(thread_name) = data.thread_name.value().and_then(|name| name.as_str()) {
span_tags.insert(SpanTagKey::ThreadName, thread_name.into());
}
}

span_tags
}

Expand Down Expand Up @@ -2486,4 +2512,100 @@ LIMIT 1
"admin@sentry.io"
);
}

#[test]
fn extract_thread_id_name_from_span_data_into_sentry_tags() {
let json = r#"
{
"type": "transaction",
"platform": "javascript",
"start_timestamp": "2021-04-26T07:59:01+0100",
"timestamp": "2021-04-26T08:00:00+0100",
"transaction": "foo",
"contexts": {
"trace": {
"trace_id": "ff62a8b040f340bda5d830223def1d81",
"span_id": "bd429c44b67a3eb4"
}
},
"spans": [
{
"op": "before_first_display",
"span_id": "bd429c44b67a3eb1",
"start_timestamp": 1597976300.0000000,
"timestamp": 1597976302.0000000,
"trace_id": "ff62a8b040f340bda5d830223def1d81",
"data": {
"thread.name": "main",
"thread.id": 42
}
}
]
}
"#;

let mut event = Annotated::<Event>::from_json(json).unwrap();

normalize_event(
&mut event,
&NormalizationConfig {
enrich_spans: true,
..Default::default()
},
);

let spans = get_value!(event.spans!);
let span = &spans[0];

assert_eq!(get_value!(span.sentry_tags["thread.id"]!), "42",);
assert_eq!(get_value!(span.sentry_tags["thread.name"]!), "main",);
}

#[test]
fn extract_thread_id_name_from_trace_context_into_sentry_tags() {
let json = r#"
{
"type": "transaction",
"platform": "python",
"start_timestamp": "2021-04-26T07:59:01+0100",
"timestamp": "2021-04-26T08:00:00+0100",
"transaction": "foo",
"contexts": {
"trace": {
"op": "queue.process",
"status": "ok",
"data": {
"thread.name": "main",
"thread.id": 42
}
}
},
"spans": [
{
"op": "before_first_display",
"span_id": "bd429c44b67a3eb1",
"start_timestamp": 1597976300.0000000,
"timestamp": 1597976302.0000000,
"trace_id": "ff62a8b040f340bda5d830223def1d81"
}
]
}
"#;

let mut event = Annotated::<Event>::from_json(json).unwrap();

normalize_event(
&mut event,
&NormalizationConfig {
enrich_spans: true,
..Default::default()
},
);

let spans = get_value!(event.spans!);
let span = &spans[0];

assert_eq!(get_value!(span.sentry_tags["thread.id"]!), "42",);
assert_eq!(get_value!(span.sentry_tags["thread.name"]!), "main",);
}
}
10 changes: 9 additions & 1 deletion relay-event-schema/src/protocol/contexts/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use relay_jsonschema_derive::JsonSchema;
use relay_protocol::{Annotated, Empty, Error, FromValue, IntoValue, Object, Value};

use crate::processor::ProcessValue;
use crate::protocol::{OperationType, OriginType, SpanStatus};
use crate::protocol::{OperationType, OriginType, SpanStatus, ThreadId};

/// A 32-character hex string as described in the W3C trace context spec.
#[derive(Clone, Debug, Default, PartialEq, Empty, IntoValue, ProcessValue)]
Expand Down Expand Up @@ -191,6 +191,14 @@ pub struct Data {
#[metastructure(field = "messaging.message.body.size")]
pub messaging_message_body_size: Annotated<Value>,

/// The ID of the thread from which the transaction originated from
#[metastructure(field = "thread.id")]
pub thread_id: Annotated<ThreadId>,

/// The name of the thread from which the transaction originated from
#[metastructure(field = "thread.name")]
pub thread_name: Annotated<String>,

/// Additional arbitrary fields for forwards compatibility.
#[metastructure(
additional_properties,
Expand Down
7 changes: 6 additions & 1 deletion relay-event-schema/src/protocol/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use relay_protocol::{Annotated, Empty, FromValue, Getter, IntoValue, Object, Val
use crate::processor::ProcessValue;
use crate::protocol::{
EventId, JsonLenientString, LenientString, Measurements, MetricsSummary, OperationType,
OriginType, SpanId, SpanStatus, Timestamp, TraceId,
OriginType, SpanId, SpanStatus, ThreadId, Timestamp, TraceId,
};

#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
Expand Down Expand Up @@ -314,6 +314,10 @@ pub struct SpanData {
#[metastructure(field = "thread.name")]
pub thread_name: Annotated<Value>,

/// ID of thread from where the span originated.
#[metastructure(field = "thread.id")]
pub thread_id: Annotated<ThreadId>,

/// Name of the segment that this span belongs to (see `segment_id`).
///
/// This corresponds to the transaction name in the transaction-based model.
Expand Down Expand Up @@ -673,6 +677,7 @@ mod tests {
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
Expand Down
1 change: 1 addition & 0 deletions relay-event-schema/src/protocol/span/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ mod tests {
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: "my 1st transaction",
ui_component_name: ~,
url_scheme: ~,
Expand Down
11 changes: 11 additions & 0 deletions relay-event-schema/src/protocol/thread.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt;

#[cfg(feature = "jsonschema")]
use relay_jsonschema_derive::JsonSchema;
use relay_protocol::{
Expand Down Expand Up @@ -71,6 +73,15 @@ impl Empty for ThreadId {
}
}

impl fmt::Display for ThreadId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ThreadId::Int(id) => write!(f, "{}", id),
ThreadId::String(id) => write!(f, "{}", id),
}
}
}

/// Possible lock types responsible for a thread's blocked state
#[derive(Debug, Copy, Clone, Eq, PartialEq, ProcessValue, Empty)]
#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ expression: "(&event.value().unwrap().spans, metrics)"
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
Expand Down Expand Up @@ -426,6 +427,7 @@ expression: "(&event.value().unwrap().spans, metrics)"
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
Expand Down Expand Up @@ -526,6 +528,7 @@ expression: "(&event.value().unwrap().spans, metrics)"
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
Expand Down Expand Up @@ -674,6 +677,7 @@ expression: "(&event.value().unwrap().spans, metrics)"
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
Expand Down Expand Up @@ -774,6 +778,7 @@ expression: "(&event.value().unwrap().spans, metrics)"
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: ~,
ui_component_name: ~,
url_scheme: ~,
Expand Down
20 changes: 20 additions & 0 deletions relay-server/tests/snapshots/test_fixtures__event_schema.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,26 @@ expression: "relay_event_schema::protocol::event_json_schema()"
"type": "null"
}
]
},
"thread.id": {
"description": " The ID of the thread from which the transaction originated from",
"default": null,
"anyOf": [
{
"$ref": "#/definitions/ThreadId"
},
{
"type": "null"
}
]
},
"thread.name": {
"description": " The name of the thread from which the transaction originated from",
"default": null,
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
Expand Down
1 change: 1 addition & 0 deletions relay-spans/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ mod tests {
ai_input_messages: ~,
ai_responses: ~,
thread_name: ~,
thread_id: ~,
segment_name: "my 1st transaction",
ui_component_name: ~,
url_scheme: ~,
Expand Down

0 comments on commit ed2fc8c

Please sign in to comment.