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

feat(otlp): infer span category from attributes #4509

Merged
merged 9 commits into from
Feb 20, 2025
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 @@ -5,6 +5,7 @@
**Features**:

- Add new `relay-threading` crate with asynchronous thread pool. ([#4500](https://github.com/getsentry/relay/pull/4500))
- Support span `category` inference from span attributes. ([#4509](https://github.com/getsentry/relay/pull/4509))

## 25.2.0

Expand Down
159 changes: 152 additions & 7 deletions relay-event-normalization/src/normalize/span/tag_extraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use relay_event_schema::protocol::{
AppContext, BrowserContext, Event, Measurement, OsContext, ProfileContext, SentryTags, Span,
Timestamp, TraceContext,
};
use relay_protocol::{Annotated, Value};
use relay_protocol::{Annotated, Empty, Value};
use sqlparser::ast::Visit;
use sqlparser::ast::{ObjectName, Visitor};
use url::Url;
Expand Down Expand Up @@ -510,14 +510,14 @@ pub fn extract_tags(

span_tags.op = span_op.to_owned().into();

let category = span_op_to_category(&span_op);
if let Some(category) = category {
let category = category_for_span(span);
if let Some(ref category) = category {
span_tags.category = category.to_owned().into();
}

let (scrubbed_description, parsed_sql) = scrub_span_description(span, span_allowed_hosts);

let action = match (category, span_op.as_str(), &scrubbed_description) {
let action = match (category.as_deref(), span_op.as_str(), &scrubbed_description) {
(Some("http"), _, _) => span
.data
.value()
Expand Down Expand Up @@ -704,7 +704,7 @@ pub fn extract_tags(
span_tags.description = truncated.into();
}

if category == Some("ai") {
if category == Some("ai".to_owned()) {
if let Some(ai_pipeline_name) = span
.data
.value()
Expand Down Expand Up @@ -1128,6 +1128,52 @@ fn extract_captured_substring<'a>(string: &'a str, pattern: &'a Lazy<Regex>) ->
None
}

fn category_for_span(span: &Span) -> Option<String> {
// Allow clients to explicitly set the category via attribute.
if let Some(Value::String(category)) = span
.data
.value()
.and_then(|v| v.other.get("sentry.category"))
.and_then(|c| c.value())
{
return Some(category.to_owned());
}

// If we're given an op, derive the category from that.
if let Some(unsanitized_span_op) = span.op.value() {
let span_op = unsanitized_span_op.to_lowercase();
if let Some(category) = span_op_to_category(&span_op) {
return Some(category.to_owned());
}
}

// Derive the category from the span's attributes.
let span_data = span.data.value()?;

fn value_is_set(value: &Annotated<Value>) -> bool {
value.value().is_some_and(|v| !v.is_empty())
}

if value_is_set(&span_data.db_system) {
Some("db".to_owned())
} else if value_is_set(&span_data.http_request_method) {
Some("http".to_owned())
} else if value_is_set(&span_data.ui_component_name) {
Some("ui".to_owned())
} else if value_is_set(&span_data.resource_render_blocking_status) {
Some("resource".to_owned())
} else if span_data
.other
.get("sentry.origin")
.and_then(|v| v.as_str())
.is_some_and(|v| v == "auto.ui.browser.metrics")
{
Some("browser".to_owned())
} else {
None
}
}

/// Returns the category of a span from its operation. The mapping is available in:
/// <https://develop.sentry.dev/sdk/performance/span-operations/>
fn span_op_to_category(op: &str) -> Option<&str> {
Expand Down Expand Up @@ -1173,8 +1219,8 @@ fn get_event_start_type(event: &Event) -> Option<&'static str> {
#[cfg(test)]
mod tests {
use insta::assert_debug_snapshot;
use relay_event_schema::protocol::Request;
use relay_protocol::{get_value, Getter};
use relay_event_schema::protocol::{Request, SpanData};
use relay_protocol::{get_value, Getter, Object};

use super::*;
use crate::span::description::{scrub_queries, Mode};
Expand Down Expand Up @@ -2712,4 +2758,103 @@ LIMIT 1
assert_eq!(get_value!(span.sentry_tags.thread_id!), "42",);
assert_eq!(get_value!(span.sentry_tags.thread_name!), "main",);
}

#[test]
fn span_category_from_explicit_attribute_overrides_op() {
let span = Span {
op: "app.start".to_owned().into(),
data: SpanData {
other: Object::from([(
"sentry.category".into(),
Value::String("db".into()).into(),
)]),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("db".into()));
}

#[test]
fn span_category_from_op_overrides_inference() {
let span = Span {
op: "app.start".to_owned().into(),
data: SpanData {
db_system: Value::String("postgresql".into()).into(),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("app".into()));
}

#[test]
fn infers_db_category_from_attributes() {
let span = Span {
data: SpanData {
db_system: Value::String("postgresql".into()).into(),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("db".into()));
}

#[test]
fn infers_http_category_from_attributes() {
let span = Span {
data: SpanData {
http_request_method: Value::String("POST".into()).into(),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("http".into()));
}

#[test]
fn infers_ui_category_from_attributes() {
let span = Span {
data: SpanData {
ui_component_name: Value::String("MainComponent".into()).into(),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("ui".into()));
}

#[test]
fn infers_resource_category_from_attributes() {
let span = Span {
data: SpanData {
resource_render_blocking_status: Value::String("true".into()).into(),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("resource".into()));
}

#[test]
fn infers_browser_category_from_attributes() {
let span = Span {
data: SpanData {
other: Object::from([(
"sentry.origin".into(),
Value::String("auto.ui.browser.metrics".into()).into(),
)]),
..Default::default()
}
.into(),
..Default::default()
};
assert_eq!(category_for_span(&span), Some("browser".into()));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: relay-server/src/metrics_extraction/event.rs
expression: metrics
expression: metrics.project_metrics
---
[
Bucket {
Expand Down Expand Up @@ -8749,6 +8749,7 @@ expression: metrics
],
),
tags: {
"span.category": "ui",
"span.description": "my-component-name",
"span.group": "e674f9eca1d88a4d",
"span.op": "ui.interaction.click",
Expand Down Expand Up @@ -8798,6 +8799,7 @@ expression: metrics
],
),
tags: {
"span.category": "ui",
"span.description": "my-component-name",
"span.group": "e674f9eca1d88a4d",
"span.op": "ui.interaction.click",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: relay-server/src/metrics_extraction/event.rs
expression: metrics
expression: metrics.project_metrics
---
[
Bucket {
Expand Down Expand Up @@ -8174,6 +8174,7 @@ expression: metrics
],
),
tags: {
"span.category": "ui",
Copy link
Member Author

Choose a reason for hiding this comment

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

Based on our docs on categories this update seems correct (in other words, it was incorrect that this was previously missing).

"span.description": "my-component-name",
"span.group": "e674f9eca1d88a4d",
"span.op": "ui.interaction.click",
Expand Down Expand Up @@ -8223,6 +8224,7 @@ expression: metrics
],
),
tags: {
"span.category": "ui",
"span.description": "my-component-name",
"span.group": "e674f9eca1d88a4d",
"span.op": "ui.interaction.click",
Expand Down
Loading