Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 mechanism to allow ingestion only from trusted relays. ([#4772](https://github.com/getsentry/relay/pull/4772))
- Serialize OTEL span array attributes to JSON. ([#4930](https://github.com/getsentry/relay/pull/4930))

**Bug Fixes**:

Expand Down
69 changes: 69 additions & 0 deletions relay-spans/src/otel_to_sentry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,74 @@ mod tests {
"###);
}

#[test]
fn parse_array_attribute() {
let json = r#"{
"traceId": "4c79f60c11214eb38604f4ae0781bfb2",
"spanId": "fa90fdead5f74052",
"parentSpanId": "fa90fdead5f74051",
"startTimeUnixNano": "123000000000",
"endTimeUnixNano": "123500000000",
"name": "cmd.run",
"status": {"code": 0},
"attributes": [
{
"key": "process.args",
"value": {
"arrayValue": {
"values": [
{"stringValue": "node"},
{"stringValue": "--require"},
{"stringValue": "preflight.cjs"}
]
}
}
},
{
"key": "process.info",
"value": {
"arrayValue": {
"values": [
{"intValue": 41},
{
"arrayValue": {
"values": [
{"intValue": 42}
]}
}
]
}
}
}
]
}"#;

let otel_span: OtelSpan = serde_json::from_str(json).unwrap();
let event_span = otel_to_sentry_span(otel_span).unwrap();

let annotated_span: Annotated<EventSpan> = Annotated::new(event_span);
insta::assert_json_snapshot!(SerializableAnnotated(&annotated_span), @r###"
{
"timestamp": 123.5,
"start_timestamp": 123.0,
"exclusive_time": 500.0,
"op": "default",
"span_id": "fa90fdead5f74052",
"parent_span_id": "fa90fdead5f74051",
"trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
"status": "ok",
"description": "cmd.run",
"data": {
"process.args": "[\"node\",\"--require\",\"preflight.cjs\"]",
"process.info": "[41]",
"sentry.name": "cmd.run",
"sentry.status.message": ""
},
"links": []
}
"###);
}

/// Intended to be synced with `relay-event-schema::protocol::span::convert::tests::roundtrip`.
#[test]
fn parse_sentry_attributes() {
Expand Down Expand Up @@ -545,6 +613,7 @@ mod tests {
"sentry.release": "[email protected]",
"sentry.segment.name": "my 1st transaction",
"sentry.sdk.name": "sentry.php",
"sentry.metrics_summary.some_metric": "[]",
"sentry.name": "myname",
"sentry.status.message": "foo"
},
Expand Down
41 changes: 40 additions & 1 deletion relay-spans/src/otel_to_sentry_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,42 @@ fn otel_value_to_attr(otel_value: OtelValue) -> Option<Attribute> {
let s = String::from_utf8(bytes).ok()?;
(AttributeType::String, Value::String(s))
}
OtelValue::ArrayValue(_) | OtelValue::KvlistValue(_) => return None,
OtelValue::ArrayValue(array) => {
// Technically, `ArrayValue` can contain other arrays, or nested key-value lists. This
// is not usually allowed by the OTLP protocol, but for safety we filter those values
// out before serializing.
let safe_values: Vec<serde_json::Value> = array
.values
.into_iter()
.filter_map(|v| match v.value? {
OtelValue::StringValue(s) => Some(serde_json::Value::String(s)),
OtelValue::BoolValue(b) => Some(serde_json::Value::Bool(b)),
OtelValue::IntValue(i) => {
Some(serde_json::Value::Number(serde_json::Number::from(i)))
}
OtelValue::DoubleValue(d) => {
serde_json::Number::from_f64(d).map(serde_json::Value::Number)
}
OtelValue::BytesValue(bytes) => {
String::from_utf8(bytes).ok().map(serde_json::Value::String)
}
OtelValue::ArrayValue(_) | OtelValue::KvlistValue(_) => None,
})
.collect();

// Serialize the arrays values as a JSON string. Even though there is some nominal
// support for array values in Sentry, it's not robust and not ready to be used.
// Instead, serialize arrays to a JSON string, and have the UI decode the JSON if
// possible.
let json = serde_json::to_string(&safe_values).unwrap_or_default();
(AttributeType::String, Value::String(json))
}
OtelValue::KvlistValue(_) => {
// Key-value pairs are supported by the type definition, but the OTLP protocol does
// _not_ allow setting this kind of value on a span, so we don't need to handle this
// case
return None;
}
};

Some(Attribute::new(ty, value))
Expand Down Expand Up @@ -682,6 +717,10 @@ mod tests {
"type": "string",
"value": "prod"
},
"sentry.metrics_summary.some_metric": {
"type": "string",
"value": "[]"
},
"sentry.op": {
"type": "string",
"value": "myop"
Expand Down
Loading