Skip to main content

relay_spans/
op.rs

1use relay_conventions::attributes::*;
2use relay_event_schema::protocol::{Attributes, SpanKind};
3use relay_protocol::Annotated;
4
5/// Generates a `sentry.op` attribute for V2 span, if possible.
6///
7/// This uses attributes of the span to figure out an appropriate operation name, inferring what the
8/// SDK might have sent. Reliably infers an op for well-known OTel span kinds like database
9/// operations. Does not infer an op for frontend and mobile spans sent by Sentry SDKs that don't
10/// have an OTel equivalent (e.g., resource loads).
11pub fn derive_op_for_v2_span(attributes: &Annotated<Attributes>) -> String {
12    // NOTE: `op` is not a required field in the SDK, so the fallback is an empty string.
13    let op = String::from("default");
14
15    let Some(attributes) = attributes.value() else {
16        return op;
17    };
18
19    if attributes.contains_key(HTTP__REQUEST__METHOD) {
20        let kind = attributes.get_value(SENTRY__KIND).and_then(|v| v.as_str());
21        return match kind {
22            Some(kind) if kind == SpanKind::Client.as_str() => String::from("http.client"),
23            Some(kind) if kind == SpanKind::Server.as_str() => String::from("http.server"),
24            _ => {
25                if attributes.contains_key(SENTRY__HTTP__PREFETCH) {
26                    String::from("http.prefetch")
27                } else {
28                    String::from("http")
29                }
30            }
31        };
32    }
33
34    if attributes.contains_key(DB__SYSTEM__NAME) {
35        return String::from("db");
36    }
37
38    if attributes.contains_key(GEN_AI__PROVIDER__NAME) {
39        return String::from("gen_ai");
40    }
41
42    if attributes.contains_key(RPC__SERVICE) {
43        return String::from("rpc");
44    }
45
46    if attributes.contains_key(MESSAGING__SYSTEM) {
47        return String::from("message");
48    }
49
50    if let Some(faas_trigger) = attributes.get_value(FAAS__TRIGGER).and_then(|v| v.as_str()) {
51        return faas_trigger.to_owned();
52    }
53
54    op
55}