|
9 | 9 | html_favicon_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png" |
10 | 10 | )] |
11 | 11 |
|
| 12 | +use opentelemetry_proto::tonic::common::v1::any_value; |
12 | 13 | use opentelemetry_proto::tonic::{ |
13 | 14 | common::v1::{InstrumentationScope, any_value::Value as OtelValue}, |
14 | 15 | resource::v1::Resource, |
15 | 16 | }; |
| 17 | +use opentelemetry_semantic_conventions::attribute as otel_semconv; |
16 | 18 | use relay_event_schema::protocol::{Attribute, AttributeType, Attributes}; |
17 | 19 | use relay_protocol::{Annotated, Value}; |
18 | 20 |
|
@@ -129,6 +131,32 @@ pub fn otel_scope_into_attributes( |
129 | 131 | } |
130 | 132 | } |
131 | 133 |
|
| 134 | +/// Returns the telemetry language SDK from the resource, mapped into a `sentry.platform` value. |
| 135 | +pub fn otel_resource_to_platform(resource: &Resource) -> Option<&str> { |
| 136 | + let any_value::Value::StringValue(language) = resource |
| 137 | + .attributes |
| 138 | + .iter() |
| 139 | + .find(|attr| attr.key == otel_semconv::TELEMETRY_SDK_LANGUAGE)? |
| 140 | + .value |
| 141 | + .as_ref()? |
| 142 | + .value |
| 143 | + .as_ref()? |
| 144 | + else { |
| 145 | + return None; |
| 146 | + }; |
| 147 | + |
| 148 | + // Smooth out some naming differences between OTel |
| 149 | + // (https://opentelemetry.io/docs/specs/semconv/resource/#telemetry-sdk) |
| 150 | + // and Sentry |
| 151 | + // (https://github.com/getsentry/relay/blob/8e6c963cdd79dc9ba2bebc21518a3553f70feeb3/relay-event-schema/src/protocol/event.rs#L251-L253). |
| 152 | + Some(match language.as_str() { |
| 153 | + "dotnet" => "csharp", |
| 154 | + "nodejs" => "node", |
| 155 | + "webjs" => "javascript", |
| 156 | + _ => language, |
| 157 | + }) |
| 158 | +} |
| 159 | + |
132 | 160 | #[cfg(test)] |
133 | 161 | mod tests { |
134 | 162 | use super::*; |
@@ -288,4 +316,34 @@ mod tests { |
288 | 316 | } |
289 | 317 | "#); |
290 | 318 | } |
| 319 | + |
| 320 | + #[test] |
| 321 | + fn test_otel_resource_to_attribute_without_language() { |
| 322 | + let resource = serde_json::from_value(serde_json::json!({"attributes": []})).unwrap(); |
| 323 | + assert_eq!(otel_resource_to_platform(&resource), None); |
| 324 | + } |
| 325 | + |
| 326 | + #[test] |
| 327 | + fn test_otel_resource_to_attribute_with_unmapped_language() { |
| 328 | + let resource = serde_json::from_value(serde_json::json!({ |
| 329 | + "attributes": [{ |
| 330 | + "key": "telemetry.sdk.language", |
| 331 | + "value": {"stringValue": "foo"}, |
| 332 | + }, |
| 333 | + ]})) |
| 334 | + .unwrap(); |
| 335 | + assert_eq!(otel_resource_to_platform(&resource), Some("foo")); |
| 336 | + } |
| 337 | + |
| 338 | + #[test] |
| 339 | + fn test_otel_resource_to_attribute_with_mapped_language() { |
| 340 | + let resource = serde_json::from_value(serde_json::json!({ |
| 341 | + "attributes": [{ |
| 342 | + "key": "telemetry.sdk.language", |
| 343 | + "value": {"stringValue": "nodejs"}, |
| 344 | + }, |
| 345 | + ]})) |
| 346 | + .unwrap(); |
| 347 | + assert_eq!(otel_resource_to_platform(&resource), Some("node")); |
| 348 | + } |
291 | 349 | } |
0 commit comments