1mod convert;
2
3use std::fmt;
4use std::ops::Deref;
5use std::str::FromStr;
6
7use relay_protocol::{
8 Annotated, Array, Empty, Error, FromValue, Getter, IntoValue, Object, Val, Value,
9};
10
11use crate::processor::ProcessValue;
12use crate::protocol::{
13 EventId, IpAddr, JsonLenientString, LenientString, Measurements, OperationType, OriginType,
14 SpanId, SpanStatus, ThreadId, Timestamp, TraceId,
15};
16
17#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
18#[metastructure(process_func = "process_span", value_type = "Span", trim = false)]
19pub struct Span {
20 #[metastructure(required = true)]
22 pub timestamp: Annotated<Timestamp>,
23
24 #[metastructure(required = true)]
26 pub start_timestamp: Annotated<Timestamp>,
27
28 pub exclusive_time: Annotated<f64>,
31
32 #[metastructure(max_chars = 128)]
34 pub op: Annotated<OperationType>,
35
36 #[metastructure(required = true)]
38 pub span_id: Annotated<SpanId>,
39
40 pub parent_span_id: Annotated<SpanId>,
42
43 #[metastructure(required = true)]
45 pub trace_id: Annotated<TraceId>,
46
47 pub segment_id: Annotated<SpanId>,
52
53 pub is_segment: Annotated<bool>,
55
56 pub is_remote: Annotated<bool>,
66
67 pub status: Annotated<SpanStatus>,
69
70 #[metastructure(pii = "maybe")]
72 pub description: Annotated<String>,
73
74 #[metastructure(pii = "maybe")]
76 pub tags: Annotated<Object<JsonLenientString>>,
77
78 #[metastructure(max_chars = 128, allow_chars = "a-zA-Z0-9_.")]
80 pub origin: Annotated<OriginType>,
81
82 pub profile_id: Annotated<EventId>,
84
85 #[metastructure(pii = "true")]
90 pub data: Annotated<SpanData>,
91
92 #[metastructure(pii = "maybe")]
94 pub links: Annotated<Array<SpanLink>>,
95
96 pub sentry_tags: Annotated<SentryTags>,
98
99 pub received: Annotated<Timestamp>,
101
102 #[metastructure(skip_serialization = "empty")]
104 #[metastructure(omit_from_schema)] pub measurements: Annotated<Measurements>,
106
107 #[metastructure(skip_serialization = "empty")]
111 pub platform: Annotated<String>,
112
113 #[metastructure(skip_serialization = "empty")]
115 pub was_transaction: Annotated<bool>,
116
117 #[metastructure(skip_serialization = "empty", trim = false)]
122 pub kind: Annotated<SpanKind>,
123
124 #[metastructure(
131 field = "_performance_issues_spans",
132 skip_serialization = "empty",
133 trim = false
134 )]
135 pub performance_issues_spans: Annotated<bool>,
136
137 #[metastructure(additional_properties, pii = "maybe")]
139 pub other: Object<Value>,
140}
141
142impl Span {
143 fn attribute(&self, key: &str) -> Option<Val<'_>> {
148 Some(match self.data.value()?.get_value(key) {
149 Some(value) => value,
150 None => self.tags.value()?.get(key)?.as_str()?.into(),
151 })
152 }
153}
154
155impl Getter for Span {
156 fn get_value(&self, path: &str) -> Option<Val<'_>> {
157 let span_prefix = path.strip_prefix("span.");
158 if let Some(span_prefix) = span_prefix {
159 return Some(match span_prefix {
160 "exclusive_time" => self.exclusive_time.value()?.into(),
161 "description" => self.description.as_str()?.into(),
162 "op" => self.op.as_str()?.into(),
163 "span_id" => self.span_id.value()?.into(),
164 "parent_span_id" => self.parent_span_id.value()?.into(),
165 "trace_id" => self.trace_id.value()?.deref().into(),
166 "status" => self.status.as_str()?.into(),
167 "is_segment" => self.is_segment.value()?.into(),
168 "origin" => self.origin.as_str()?.into(),
169 "duration" => {
170 let start_timestamp = *self.start_timestamp.value()?;
171 let timestamp = *self.timestamp.value()?;
172 relay_common::time::chrono_to_positive_millis(timestamp - start_timestamp)
173 .into()
174 }
175 "was_transaction" => self.was_transaction.value().unwrap_or(&false).into(),
176 path => {
177 if let Some(key) = path.strip_prefix("tags.") {
178 self.tags.value()?.get(key)?.as_str()?.into()
179 } else if let Some(key) = path.strip_prefix("data.") {
180 self.attribute(key)?
181 } else if let Some(key) = path.strip_prefix("sentry_tags.") {
182 self.sentry_tags.value()?.get_value(key)?
183 } else if let Some(rest) = path.strip_prefix("measurements.") {
184 let name = rest.strip_suffix(".value")?;
185 self.measurements
186 .value()?
187 .get(name)?
188 .value()?
189 .value
190 .value()?
191 .into()
192 } else {
193 return None;
194 }
195 }
196 });
197 }
198
199 let event_prefix = path.strip_prefix("event.")?;
202 Some(match event_prefix {
203 "release" => self.data.value()?.release.as_str()?.into(),
204 "environment" => self.data.value()?.environment.as_str()?.into(),
205 "transaction" => self.data.value()?.segment_name.as_str()?.into(),
206 "contexts.browser.name" => self.data.value()?.browser_name.as_str()?.into(),
207 _ => return None,
209 })
210 }
211}
212
213#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
215#[metastructure(trim = false, pii = "maybe")]
216pub struct SentryTags {
217 pub release: Annotated<String>,
218 #[metastructure(pii = "true")]
219 pub user: Annotated<String>,
220 #[metastructure(pii = "true", field = "user.id")]
221 pub user_id: Annotated<String>,
222 #[metastructure(pii = "true", field = "user.ip")]
223 pub user_ip: Annotated<String>,
224 #[metastructure(pii = "true", field = "user.username")]
225 pub user_username: Annotated<String>,
226 #[metastructure(pii = "true", field = "user.email")]
227 pub user_email: Annotated<String>,
228 pub environment: Annotated<String>,
229 pub transaction: Annotated<String>,
230 #[metastructure(field = "transaction.method")]
231 pub transaction_method: Annotated<String>,
232 #[metastructure(field = "transaction.op")]
233 pub transaction_op: Annotated<String>,
234 #[metastructure(field = "browser.name")]
235 pub browser_name: Annotated<String>,
236 #[metastructure(field = "sdk.name")]
237 pub sdk_name: Annotated<String>,
238 #[metastructure(field = "sdk.version")]
239 pub sdk_version: Annotated<String>,
240 pub platform: Annotated<String>,
241 pub mobile: Annotated<String>,
243 #[metastructure(field = "device.class")]
244 pub device_class: Annotated<String>,
245 #[metastructure(field = "device.family")]
246 pub device_family: Annotated<String>,
247 #[metastructure(field = "device.arch")]
248 pub device_arch: Annotated<String>,
249 #[metastructure(field = "device.battery_level")]
250 pub device_battery_level: Annotated<String>,
251 #[metastructure(field = "device.brand")]
252 pub device_brand: Annotated<String>,
253 #[metastructure(field = "device.charging")]
254 pub device_charging: Annotated<String>,
255 #[metastructure(field = "device.locale")]
256 pub device_locale: Annotated<String>,
257 #[metastructure(field = "device.model_id")]
258 pub device_model_id: Annotated<String>,
259 #[metastructure(field = "device.name")]
260 pub device_name: Annotated<String>,
261 #[metastructure(field = "device.online")]
262 pub device_online: Annotated<String>,
263 #[metastructure(field = "device.orientation")]
264 pub device_orientation: Annotated<String>,
265 #[metastructure(field = "device.screen_density")]
266 pub device_screen_density: Annotated<String>,
267 #[metastructure(field = "device.screen_dpi")]
268 pub device_screen_dpi: Annotated<String>,
269 #[metastructure(field = "device.screen_height_pixels")]
270 pub device_screen_height_pixels: Annotated<String>,
271 #[metastructure(field = "device.screen_width_pixels")]
272 pub device_screen_width_pixels: Annotated<String>,
273 #[metastructure(field = "device.simulator")]
274 pub device_simulator: Annotated<String>,
275 #[metastructure(field = "device.uuid")]
276 pub device_uuid: Annotated<String>,
277 #[metastructure(field = "app.device")]
278 pub app_device: Annotated<String>,
279 #[metastructure(field = "device.model")]
280 pub device_model: Annotated<String>,
281 pub runtime: Annotated<String>,
282 #[metastructure(field = "runtime.name")]
283 pub runtime_name: Annotated<String>,
284 pub browser: Annotated<String>,
285 pub os: Annotated<String>,
286 #[metastructure(field = "os.rooted")]
287 pub os_rooted: Annotated<String>,
288 #[metastructure(field = "gpu.name")]
289 pub gpu_name: Annotated<String>,
290 #[metastructure(field = "gpu.vendor")]
291 pub gpu_vendor: Annotated<String>,
292 #[metastructure(field = "monitor.id")]
293 pub monitor_id: Annotated<String>,
294 #[metastructure(field = "monitor.slug")]
295 pub monitor_slug: Annotated<String>,
296 #[metastructure(field = "request.url")]
297 pub request_url: Annotated<String>,
298 #[metastructure(field = "request.method")]
299 pub request_method: Annotated<String>,
300 #[metastructure(field = "os.name")]
302 pub os_name: Annotated<String>,
303 pub action: Annotated<String>,
304 pub category: Annotated<String>,
305 pub description: Annotated<String>,
306 pub domain: Annotated<String>,
307 pub raw_domain: Annotated<String>,
308 pub group: Annotated<String>,
309 #[metastructure(field = "http.decoded_response_content_length")]
310 pub http_decoded_response_content_length: Annotated<String>,
311 #[metastructure(field = "http.response_content_length")]
312 pub http_response_content_length: Annotated<String>,
313 #[metastructure(field = "http.response_transfer_size")]
314 pub http_response_transfer_size: Annotated<String>,
315 #[metastructure(field = "resource.render_blocking_status")]
316 pub resource_render_blocking_status: Annotated<String>,
317 pub op: Annotated<String>,
318 pub status: Annotated<String>,
319 pub status_code: Annotated<String>,
320 pub system: Annotated<String>,
321 pub ttid: Annotated<String>,
323 pub ttfd: Annotated<String>,
325 pub file_extension: Annotated<String>,
327 pub main_thread: Annotated<String>,
329 pub app_start_type: Annotated<String>,
331 pub replay_id: Annotated<String>,
332 #[metastructure(field = "cache.hit")]
333 pub cache_hit: Annotated<String>,
334 #[metastructure(field = "cache.key")]
335 pub cache_key: Annotated<String>,
336 #[metastructure(field = "trace.status")]
337 pub trace_status: Annotated<String>,
338 #[metastructure(field = "messaging.destination.name")]
339 pub messaging_destination_name: Annotated<String>,
340 #[metastructure(field = "messaging.message.id")]
341 pub messaging_message_id: Annotated<String>,
342 #[metastructure(field = "messaging.operation.name")]
343 pub messaging_operation_name: Annotated<String>,
344 #[metastructure(field = "messaging.operation.type")]
345 pub messaging_operation_type: Annotated<String>,
346 #[metastructure(field = "thread.name")]
347 pub thread_name: Annotated<String>,
348 #[metastructure(field = "thread.id")]
349 pub thread_id: Annotated<String>,
350 pub profiler_id: Annotated<String>,
351 #[metastructure(field = "user.geo.city")]
352 pub user_city: Annotated<String>,
353 #[metastructure(field = "user.geo.country_code")]
354 pub user_country_code: Annotated<String>,
355 #[metastructure(field = "user.geo.region")]
356 pub user_region: Annotated<String>,
357 #[metastructure(field = "user.geo.subdivision")]
358 pub user_subdivision: Annotated<String>,
359 #[metastructure(field = "user.geo.subregion")]
360 pub user_subregion: Annotated<String>,
361 pub name: Annotated<String>,
362 }
365
366impl Getter for SentryTags {
367 fn get_value(&self, path: &str) -> Option<Val<'_>> {
368 let value = match path {
369 "action" => &self.action,
370 "app_start_type" => &self.app_start_type,
371 "browser.name" => &self.browser_name,
372 "cache.hit" => &self.cache_hit,
373 "cache.key" => &self.cache_key,
374 "category" => &self.category,
375 "description" => &self.description,
376 "device.class" => &self.device_class,
377 "device.family" => &self.device_family,
378 "device.arch" => &self.device_arch,
379 "device.battery_level" => &self.device_battery_level,
380 "device.brand" => &self.device_brand,
381 "device.charging" => &self.device_charging,
382 "device.locale" => &self.device_locale,
383 "device.model_id" => &self.device_model_id,
384 "device.name" => &self.device_name,
385 "device.online" => &self.device_online,
386 "device.orientation" => &self.device_orientation,
387 "device.screen_density" => &self.device_screen_density,
388 "device.screen_dpi" => &self.device_screen_dpi,
389 "device.screen_height_pixels" => &self.device_screen_height_pixels,
390 "device.screen_width_pixels" => &self.device_screen_width_pixels,
391 "device.simulator" => &self.device_simulator,
392 "device.uuid" => &self.device_uuid,
393 "app.device" => &self.app_device,
394 "device.model" => &self.device_model,
395 "runtime" => &self.runtime,
396 "runtime.name" => &self.runtime_name,
397 "browser" => &self.browser,
398 "os" => &self.os,
399 "os.rooted" => &self.os_rooted,
400 "gpu.name" => &self.gpu_name,
401 "gpu.vendor" => &self.gpu_vendor,
402 "monitor.id" => &self.monitor_id,
403 "monitor.slug" => &self.monitor_slug,
404 "request.url" => &self.request_url,
405 "request.method" => &self.request_method,
406 "domain" => &self.domain,
407 "environment" => &self.environment,
408 "file_extension" => &self.file_extension,
409 "group" => &self.group,
410 "http.decoded_response_content_length" => &self.http_decoded_response_content_length,
411 "http.response_content_length" => &self.http_response_content_length,
412 "http.response_transfer_size" => &self.http_response_transfer_size,
413 "main_thread" => &self.main_thread,
414 "messaging.destination.name" => &self.messaging_destination_name,
415 "messaging.message.id" => &self.messaging_message_id,
416 "messaging.operation.name" => &self.messaging_operation_name,
417 "messaging.operation.type" => &self.messaging_operation_type,
418 "mobile" => &self.mobile,
419 "name" => &self.name,
420 "op" => &self.op,
421 "os.name" => &self.os_name,
422 "platform" => &self.platform,
423 "profiler_id" => &self.profiler_id,
424 "raw_domain" => &self.raw_domain,
425 "release" => &self.release,
426 "replay_id" => &self.replay_id,
427 "resource.render_blocking_status" => &self.resource_render_blocking_status,
428 "sdk.name" => &self.sdk_name,
429 "sdk.version" => &self.sdk_version,
430 "status_code" => &self.status_code,
431 "status" => &self.status,
432 "system" => &self.system,
433 "thread.id" => &self.thread_id,
434 "thread.name" => &self.thread_name,
435 "trace.status" => &self.trace_status,
436 "transaction.method" => &self.transaction_method,
437 "transaction.op" => &self.transaction_op,
438 "transaction" => &self.transaction,
439 "ttfd" => &self.ttfd,
440 "ttid" => &self.ttid,
441 "user.email" => &self.user_email,
442 "user.geo.city" => &self.user_city,
443 "user.geo.country_code" => &self.user_country_code,
444 "user.geo.region" => &self.user_region,
445 "user.geo.subdivision" => &self.user_subdivision,
446 "user.geo.subregion" => &self.user_subregion,
447 "user.id" => &self.user_id,
448 "user.ip" => &self.user_ip,
449 "user.username" => &self.user_username,
450 "user" => &self.user,
451 _ => return None,
452 };
453 Some(value.as_str()?.into())
454 }
455}
456
457#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
462#[metastructure(trim = false, pii = "maybe")]
463pub struct SpanData {
464 #[metastructure(field = "app_start_type")] pub app_start_type: Annotated<Value>,
469
470 #[metastructure(field = "gen_ai.request.max_tokens")]
472 pub gen_ai_request_max_tokens: Annotated<Value>,
473
474 #[metastructure(field = "gen_ai.pipeline.name", legacy_alias = "ai.pipeline.name")]
476 pub gen_ai_pipeline_name: Annotated<Value>,
477
478 #[metastructure(
480 field = "gen_ai.usage.total_tokens",
481 legacy_alias = "ai.total_tokens.used"
482 )]
483 pub gen_ai_usage_total_tokens: Annotated<Value>,
484
485 #[metastructure(
487 field = "gen_ai.usage.input_tokens",
488 legacy_alias = "ai.prompt_tokens.used",
489 legacy_alias = "gen_ai.usage.prompt_tokens"
490 )]
491 pub gen_ai_usage_input_tokens: Annotated<Value>,
492
493 #[metastructure(field = "gen_ai.usage.input_tokens.cached")]
496 pub gen_ai_usage_input_tokens_cached: Annotated<Value>,
497
498 #[metastructure(field = "gen_ai.usage.input_tokens.cache_write")]
500 pub gen_ai_usage_input_tokens_cache_write: Annotated<Value>,
501
502 #[metastructure(field = "gen_ai.usage.input_tokens.cache_miss")]
504 pub gen_ai_usage_input_tokens_cache_miss: Annotated<Value>,
505
506 #[metastructure(
508 field = "gen_ai.usage.output_tokens",
509 legacy_alias = "ai.completion_tokens.used",
510 legacy_alias = "gen_ai.usage.completion_tokens"
511 )]
512 pub gen_ai_usage_output_tokens: Annotated<Value>,
513
514 #[metastructure(field = "gen_ai.usage.output_tokens.reasoning")]
517 pub gen_ai_usage_output_tokens_reasoning: Annotated<Value>,
518
519 #[metastructure(field = "gen_ai.usage.output_tokens.prediction_accepted")]
521 pub gen_ai_usage_output_tokens_prediction_accepted: Annotated<Value>,
522
523 #[metastructure(field = "gen_ai.usage.output_tokens.prediction_rejected")]
525 pub gen_ai_usage_output_tokens_prediction_rejected: Annotated<Value>,
526
527 #[metastructure(field = "gen_ai.response.model")]
529 pub gen_ai_response_model: Annotated<Value>,
530
531 #[metastructure(field = "gen_ai.request.model", legacy_alias = "ai.model_id")]
533 pub gen_ai_request_model: Annotated<Value>,
534
535 #[metastructure(field = "gen_ai.context.window_size")]
537 pub gen_ai_context_window_size: Annotated<Value>,
538
539 #[metastructure(field = "gen_ai.context.utilization")]
541 pub gen_ai_context_utilization: Annotated<Value>,
542
543 #[metastructure(field = "gen_ai.cost.total_tokens")]
545 pub gen_ai_cost_total_tokens: Annotated<Value>,
546
547 #[metastructure(field = "gen_ai.cost.input_tokens")]
549 pub gen_ai_cost_input_tokens: Annotated<Value>,
550
551 #[metastructure(field = "gen_ai.cost.output_tokens")]
553 pub gen_ai_cost_output_tokens: Annotated<Value>,
554
555 #[metastructure(
557 field = "gen_ai.input.messages",
558 legacy_alias = "gen_ai.prompt",
559 legacy_alias = "gen_ai.request.messages",
560 legacy_alias = "ai.prompt.messages"
561 )]
562 pub gen_ai_input_messages: Annotated<Value>,
563
564 #[metastructure(
566 field = "gen_ai.tool.call.arguments",
567 legacy_alias = "gen_ai.tool.input",
568 legacy_alias = "ai.toolCall.args"
569 )]
570 pub gen_ai_tool_call_arguments: Annotated<Value>,
571
572 #[metastructure(
574 field = "gen_ai.tool.call.result",
575 legacy_alias = "gen_ai.tool.output",
576 legacy_alias = "ai.toolCall.result"
577 )]
578 pub gen_ai_tool_call_result: Annotated<Value>,
579
580 #[metastructure(
582 field = "gen_ai.output.messages",
583 legacy_alias = "gen_ai.response.tool_calls",
584 legacy_alias = "ai.response.toolCalls",
585 legacy_alias = "ai.tool_calls",
586 legacy_alias = "gen_ai.response.text",
587 legacy_alias = "ai.response.text",
588 legacy_alias = "ai.responses"
589 )]
590 pub gen_ai_output_messages: Annotated<Value>,
591
592 #[metastructure(field = "gen_ai.response.object")]
594 pub gen_ai_response_object: Annotated<Value>,
595
596 #[metastructure(
598 field = "gen_ai.response.streaming",
599 legacy_alias = "ai.streaming",
600 pii = "false"
601 )]
602 pub gen_ai_response_streaming: Annotated<Value>,
603
604 #[metastructure(field = "gen_ai.response.tokens_per_second")]
606 pub gen_ai_response_tokens_per_second: Annotated<Value>,
607
608 #[metastructure(field = "gen_ai.response.time_to_first_token")]
610 pub gen_ai_response_time_to_first_token: Annotated<Value>,
611
612 #[metastructure(
614 field = "gen_ai.tool.definitions",
615 legacy_alias = "gen_ai.request.available_tools",
616 legacy_alias = "ai.tools"
617 )]
618 pub gen_ai_tool_definitions: Annotated<Value>,
619
620 #[metastructure(
622 field = "gen_ai.request.frequency_penalty",
623 legacy_alias = "ai.frequency_penalty"
624 )]
625 pub gen_ai_request_frequency_penalty: Annotated<Value>,
626
627 #[metastructure(
629 field = "gen_ai.request.presence_penalty",
630 legacy_alias = "ai.presence_penalty"
631 )]
632 pub gen_ai_request_presence_penalty: Annotated<Value>,
633
634 #[metastructure(field = "gen_ai.request.seed", legacy_alias = "ai.seed")]
636 pub gen_ai_request_seed: Annotated<Value>,
637
638 #[metastructure(field = "gen_ai.request.temperature", legacy_alias = "ai.temperature")]
640 pub gen_ai_request_temperature: Annotated<Value>,
641
642 #[metastructure(field = "gen_ai.request.top_k", legacy_alias = "ai.top_k")]
644 pub gen_ai_request_top_k: Annotated<Value>,
645
646 #[metastructure(field = "gen_ai.request.top_p", legacy_alias = "ai.top_p")]
648 pub gen_ai_request_top_p: Annotated<Value>,
649
650 #[metastructure(
652 field = "gen_ai.response.finish_reasons",
653 legacy_alias = "gen_ai.response.finish_reason",
654 legacy_alias = "ai.finish_reason"
655 )]
656 pub gen_ai_response_finish_reasons: Annotated<Value>,
657
658 #[metastructure(field = "gen_ai.response.id", legacy_alias = "ai.generation_id")]
660 pub gen_ai_response_id: Annotated<Value>,
661
662 #[metastructure(
664 field = "gen_ai.provider.name",
665 legacy_alias = "gen_ai.system",
666 legacy_alias = "ai.model.provider"
667 )]
668 pub gen_ai_provider_name: Annotated<Value>,
669
670 #[metastructure(
672 field = "gen_ai.system_instructions",
673 legacy_alias = "gen_ai.system.message"
674 )]
675 pub gen_ai_system_instructions: Annotated<Value>,
676
677 #[metastructure(field = "gen_ai.tool.name", legacy_alias = "ai.function_call")]
679 pub gen_ai_tool_name: Annotated<Value>,
680
681 #[metastructure(field = "gen_ai.operation.name")]
683 pub gen_ai_operation_name: Annotated<String>,
684
685 #[metastructure(field = "gen_ai.operation.type")]
687 pub gen_ai_operation_type: Annotated<String>,
688
689 #[metastructure(field = "gen_ai.agent.name")]
691 pub gen_ai_agent_name: Annotated<String>,
692
693 #[metastructure(field = "gen_ai.function_id")]
695 pub gen_ai_function_id: Annotated<String>,
696
697 #[metastructure(field = "mcp.prompt.result")]
699 pub mcp_prompt_result: Annotated<Value>,
700
701 #[metastructure(field = "mcp.tool.result.content")]
703 pub mcp_tool_result_content: Annotated<Value>,
704
705 #[metastructure(field = "browser.name")]
707 pub browser_name: Annotated<String>,
708
709 #[metastructure(field = "code.filepath")]
711 pub code_filepath: Annotated<Value>,
712 #[metastructure(field = "code.lineno")]
714 pub code_lineno: Annotated<Value>,
715 #[metastructure(field = "code.function")]
719 pub code_function: Annotated<Value>,
720 #[metastructure(field = "code.namespace")]
726 pub code_namespace: Annotated<Value>,
727
728 #[metastructure(field = "db.operation")]
733 pub db_operation: Annotated<Value>,
734
735 #[metastructure(field = "db.system")]
739 pub db_system: Annotated<Value>,
740
741 #[metastructure(
745 field = "db.collection.name",
746 legacy_alias = "db.cassandra.table",
747 legacy_alias = "db.cosmosdb.container",
748 legacy_alias = "db.mongodb.collection",
749 legacy_alias = "db.sql.table"
750 )]
751 pub db_collection_name: Annotated<Value>,
752
753 #[metastructure(
755 field = "sentry.environment",
756 legacy_alias = "environment",
757 pii = "false"
758 )]
759 pub environment: Annotated<String>,
760
761 #[metastructure(field = "sentry.release", legacy_alias = "release", pii = "false")]
763 pub release: Annotated<LenientString>,
764
765 #[metastructure(field = "http.decoded_response_content_length")]
767 pub http_decoded_response_content_length: Annotated<Value>,
768
769 #[metastructure(
771 field = "http.request_method",
772 legacy_alias = "http.method",
773 legacy_alias = "method"
774 )]
775 pub http_request_method: Annotated<Value>,
776
777 #[metastructure(field = "http.response_content_length")]
779 pub http_response_content_length: Annotated<Value>,
780
781 #[metastructure(field = "http.response_transfer_size")]
783 pub http_response_transfer_size: Annotated<Value>,
784
785 #[metastructure(field = "resource.render_blocking_status")]
787 pub resource_render_blocking_status: Annotated<Value>,
788
789 #[metastructure(field = "server.address")]
791 pub server_address: Annotated<Value>,
792
793 #[metastructure(field = "cache.hit")]
795 pub cache_hit: Annotated<Value>,
796
797 #[metastructure(field = "cache.key")]
799 pub cache_key: Annotated<Value>,
800
801 #[metastructure(field = "cache.item_size")]
803 pub cache_item_size: Annotated<Value>,
804
805 #[metastructure(field = "http.response.status_code", legacy_alias = "status_code")]
807 pub http_response_status_code: Annotated<Value>,
808
809 #[metastructure(field = "thread.name")]
811 pub thread_name: Annotated<String>,
812
813 #[metastructure(field = "thread.id", pii = "false")]
815 pub thread_id: Annotated<ThreadId>,
816
817 #[metastructure(field = "sentry.segment.name", legacy_alias = "transaction")]
823 pub segment_name: Annotated<String>,
824
825 #[metastructure(field = "ui.component_name")]
827 pub ui_component_name: Annotated<Value>,
828
829 #[metastructure(field = "url.scheme")]
831 pub url_scheme: Annotated<Value>,
832
833 #[metastructure(field = "user", pii = "true")]
835 pub user: Annotated<Value>,
836
837 #[metastructure(field = "user.email", pii = "true")]
841 pub user_email: Annotated<String>,
842
843 #[metastructure(field = "user.full_name", pii = "true")]
847 pub user_full_name: Annotated<String>,
848
849 #[metastructure(field = "user.geo.country_code")]
853 pub user_geo_country_code: Annotated<String>,
854
855 #[metastructure(field = "user.geo.city")]
859 pub user_geo_city: Annotated<String>,
860
861 #[metastructure(field = "user.geo.subdivision")]
865 pub user_geo_subdivision: Annotated<String>,
866
867 #[metastructure(field = "user.geo.region")]
871 pub user_geo_region: Annotated<String>,
872
873 #[metastructure(field = "user.hash", pii = "true")]
877 pub user_hash: Annotated<String>,
878
879 #[metastructure(field = "user.id", pii = "true")]
883 pub user_id: Annotated<String>,
884
885 #[metastructure(field = "user.name", pii = "true")]
889 pub user_name: Annotated<String>,
890
891 #[metastructure(field = "user.roles", pii = "true")]
895 pub user_roles: Annotated<Array<String>>,
896
897 #[metastructure(field = "sentry.exclusive_time")]
899 pub exclusive_time: Annotated<Value>,
900
901 #[metastructure(field = "profile_id", pii = "false")]
903 pub profile_id: Annotated<Value>,
904
905 #[metastructure(field = "sentry.replay_id", legacy_alias = "replay_id", pii = "false")]
907 pub replay_id: Annotated<Value>,
908
909 #[metastructure(field = "sentry.sdk.name", pii = "false")]
911 pub sdk_name: Annotated<String>,
912
913 #[metastructure(field = "sentry.sdk.version", pii = "false")]
915 pub sdk_version: Annotated<String>,
916
917 #[metastructure(field = "sentry.frames.slow", legacy_alias = "frames.slow")]
919 pub frames_slow: Annotated<Value>,
920
921 #[metastructure(field = "sentry.frames.frozen", legacy_alias = "frames.frozen")]
923 pub frames_frozen: Annotated<Value>,
924
925 #[metastructure(field = "sentry.frames.total", legacy_alias = "frames.total")]
927 pub frames_total: Annotated<Value>,
928
929 #[metastructure(field = "frames.delay")]
931 pub frames_delay: Annotated<Value>,
932
933 #[metastructure(field = "messaging.destination.name")]
935 pub messaging_destination_name: Annotated<String>,
936
937 #[metastructure(field = "messaging.message.retry.count")]
939 pub messaging_message_retry_count: Annotated<Value>,
940
941 #[metastructure(field = "messaging.message.receive.latency")]
943 pub messaging_message_receive_latency: Annotated<Value>,
944
945 #[metastructure(field = "messaging.message.body.size")]
947 pub messaging_message_body_size: Annotated<Value>,
948
949 #[metastructure(field = "messaging.message.id")]
951 pub messaging_message_id: Annotated<String>,
952
953 #[metastructure(field = "messaging.operation.name")]
955 pub messaging_operation_name: Annotated<String>,
956
957 #[metastructure(field = "messaging.operation.type")]
959 pub messaging_operation_type: Annotated<String>,
960
961 #[metastructure(field = "user_agent.original")]
963 pub user_agent_original: Annotated<String>,
964
965 #[metastructure(field = "url.full", pii = "true")]
967 pub url_full: Annotated<String>,
968
969 #[metastructure(field = "url.query", pii = "true")]
971 pub url_query: Annotated<String>,
972
973 #[metastructure(field = "http.query", pii = "true")]
975 pub http_query: Annotated<String>,
976
977 #[metastructure(field = "client.address", pii = "true")]
979 pub client_address: Annotated<IpAddr>,
980
981 #[metastructure(skip_serialization = "empty")]
985 pub route: Annotated<Route>,
986 #[metastructure(field = "previousRoute", skip_serialization = "empty")]
990 pub previous_route: Annotated<Route>,
991
992 #[metastructure(field = "lcp.element")]
994 pub lcp_element: Annotated<String>,
995
996 #[metastructure(field = "lcp.size")]
998 pub lcp_size: Annotated<u64>,
999
1000 #[metastructure(field = "lcp.id")]
1002 pub lcp_id: Annotated<String>,
1003
1004 #[metastructure(field = "lcp.url")]
1006 pub lcp_url: Annotated<String>,
1007
1008 #[metastructure(field = "sentry.name")]
1011 pub span_name: Annotated<String>,
1012
1013 #[metastructure(
1015 additional_properties,
1016 pii = "true",
1017 retain = true,
1018 skip_serialization = "null" )]
1020 pub other: Object<Value>,
1021}
1022
1023impl Getter for SpanData {
1024 fn get_value(&self, path: &str) -> Option<Val<'_>> {
1025 Some(match path {
1026 "app_start_type" => self.app_start_type.value()?.into(),
1027 "browser\\.name" => self.browser_name.as_str()?.into(),
1028 "code\\.filepath" => self.code_filepath.value()?.into(),
1029 "code\\.function" => self.code_function.value()?.into(),
1030 "code\\.lineno" => self.code_lineno.value()?.into(),
1031 "code\\.namespace" => self.code_namespace.value()?.into(),
1032 "db.operation" => self.db_operation.value()?.into(),
1033 "db\\.system" => self.db_system.value()?.into(),
1034 "environment" => self.environment.as_str()?.into(),
1035 "gen_ai\\.request\\.max_tokens" => self.gen_ai_request_max_tokens.value()?.into(),
1036 "gen_ai\\.usage\\.total_tokens" => self.gen_ai_usage_total_tokens.value()?.into(),
1037 "gen_ai\\.cost\\.total_tokens" => self.gen_ai_cost_total_tokens.value()?.into(),
1038 "gen_ai\\.cost\\.input_tokens" => self.gen_ai_cost_input_tokens.value()?.into(),
1039 "gen_ai\\.cost\\.output_tokens" => self.gen_ai_cost_output_tokens.value()?.into(),
1040 "gen_ai\\.input\\.messages" => self.gen_ai_input_messages.value()?.into(),
1041 "gen_ai\\.output\\.messages" => self.gen_ai_output_messages.value()?.into(),
1042 "gen_ai\\.operation\\.name" => self.gen_ai_operation_name.as_str()?.into(),
1043 "gen_ai\\.agent\\.name" => self.gen_ai_agent_name.as_str()?.into(),
1044 "gen_ai\\.request\\.model" => self.gen_ai_request_model.value()?.into(),
1045 "http\\.decoded_response_content_length" => {
1046 self.http_decoded_response_content_length.value()?.into()
1047 }
1048 "http\\.request_method" | "http\\.method" | "method" => {
1049 self.http_request_method.value()?.into()
1050 }
1051 "http\\.response_content_length" => self.http_response_content_length.value()?.into(),
1052 "http\\.response_transfer_size" => self.http_response_transfer_size.value()?.into(),
1053 "http\\.response.status_code" | "status_code" => {
1054 self.http_response_status_code.value()?.into()
1055 }
1056 "resource\\.render_blocking_status" => {
1057 self.resource_render_blocking_status.value()?.into()
1058 }
1059 "server\\.address" => self.server_address.value()?.into(),
1060 "thread\\.name" => self.thread_name.as_str()?.into(),
1061 "ui\\.component_name" => self.ui_component_name.value()?.into(),
1062 "url\\.scheme" => self.url_scheme.value()?.into(),
1063 "url\\.query" => self.url_query.as_str()?.into(),
1064 "http\\.query" => self.http_query.as_str()?.into(),
1065 "user" => self.user.value()?.into(),
1066 "user\\.email" => self.user_email.as_str()?.into(),
1067 "user\\.full_name" => self.user_full_name.as_str()?.into(),
1068 "user\\.geo\\.city" => self.user_geo_city.as_str()?.into(),
1069 "user\\.geo\\.country_code" => self.user_geo_country_code.as_str()?.into(),
1070 "user\\.geo\\.region" => self.user_geo_region.as_str()?.into(),
1071 "user\\.geo\\.subdivision" => self.user_geo_subdivision.as_str()?.into(),
1072 "user\\.hash" => self.user_hash.as_str()?.into(),
1073 "user\\.id" => self.user_id.as_str()?.into(),
1074 "user\\.name" => self.user_name.as_str()?.into(),
1075 "transaction" => self.segment_name.as_str()?.into(),
1076 "release" => self.release.as_str()?.into(),
1077 _ => {
1078 let escaped = path.replace("\\.", "\0");
1079 let mut path = escaped.split('.').map(|s| s.replace('\0', "."));
1080 let root = path.next()?;
1081
1082 let mut val = self.other.get(&root)?.value()?;
1083 for part in path {
1084 let relay_protocol::Value::Object(map) = val else {
1086 return None;
1087 };
1088 val = map.get(&part)?.value()?;
1089 }
1090 val.into()
1091 }
1092 })
1093 }
1094}
1095
1096#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
1098#[metastructure(trim = false)]
1099pub struct SpanLink {
1100 #[metastructure(required = true, trim = false)]
1102 pub trace_id: Annotated<TraceId>,
1103
1104 #[metastructure(required = true, trim = false)]
1106 pub span_id: Annotated<SpanId>,
1107
1108 #[metastructure(trim = false)]
1110 pub sampled: Annotated<bool>,
1111
1112 #[metastructure(pii = "maybe", trim = false)]
1114 pub attributes: Annotated<Object<Value>>,
1115
1116 #[metastructure(additional_properties, retain = true, pii = "maybe", trim = false)]
1118 pub other: Object<Value>,
1119}
1120
1121#[derive(Clone, Debug, Default, PartialEq, Empty, IntoValue, ProcessValue)]
1123pub struct Route {
1124 #[metastructure(pii = "maybe", skip_serialization = "empty")]
1126 pub name: Annotated<String>,
1127
1128 #[metastructure(
1130 pii = "true",
1131 skip_serialization = "empty",
1132 max_depth = 5,
1133 max_bytes = 2048
1134 )]
1135 pub params: Annotated<Object<Value>>,
1136
1137 #[metastructure(
1139 additional_properties,
1140 retain = true,
1141 pii = "maybe",
1142 skip_serialization = "empty"
1143 )]
1144 pub other: Object<Value>,
1145}
1146
1147impl FromValue for Route {
1148 fn from_value(value: Annotated<Value>) -> Annotated<Self>
1149 where
1150 Self: Sized,
1151 {
1152 match value {
1153 Annotated(Some(Value::String(name)), meta) => Annotated(
1154 Some(Route {
1155 name: Annotated::new(name),
1156 ..Default::default()
1157 }),
1158 meta,
1159 ),
1160 Annotated(Some(Value::Object(mut values)), meta) => {
1161 let mut route: Route = Default::default();
1162 if let Some(Annotated(Some(Value::String(name)), _)) = values.remove("name") {
1163 route.name = Annotated::new(name);
1164 }
1165 if let Some(Annotated(Some(Value::Object(params)), _)) = values.remove("params") {
1166 route.params = Annotated::new(params);
1167 }
1168
1169 if !values.is_empty() {
1170 route.other = values;
1171 }
1172
1173 Annotated(Some(route), meta)
1174 }
1175 Annotated(None, meta) => Annotated(None, meta),
1176 Annotated(Some(value), mut meta) => {
1177 meta.add_error(Error::expected("route expected to be an object"));
1178 meta.set_original_value(Some(value));
1179 Annotated(None, meta)
1180 }
1181 }
1182 }
1183}
1184
1185#[derive(Clone, Debug, PartialEq, ProcessValue, Default)]
1190pub enum SpanKind {
1191 #[default]
1193 Internal,
1194 Server,
1196 Client,
1198 Producer,
1200 Consumer,
1202 Unknown(String),
1204}
1205
1206impl SpanKind {
1207 pub fn as_str(&self) -> &str {
1208 match self {
1209 Self::Internal => "internal",
1210 Self::Server => "server",
1211 Self::Client => "client",
1212 Self::Producer => "producer",
1213 Self::Consumer => "consumer",
1214 Self::Unknown(s) => s.as_str(),
1215 }
1216 }
1217}
1218
1219impl Empty for SpanKind {
1220 fn is_empty(&self) -> bool {
1221 false
1222 }
1223}
1224
1225#[derive(Debug)]
1226pub struct ParseSpanKindError;
1227
1228impl std::str::FromStr for SpanKind {
1229 type Err = ParseSpanKindError;
1230
1231 fn from_str(s: &str) -> Result<Self, Self::Err> {
1232 Ok(match s {
1233 "internal" => SpanKind::Internal,
1234 "server" => SpanKind::Server,
1235 "client" => SpanKind::Client,
1236 "producer" => SpanKind::Producer,
1237 "consumer" => SpanKind::Consumer,
1238 other => SpanKind::Unknown(other.to_owned()),
1239 })
1240 }
1241}
1242
1243impl fmt::Display for SpanKind {
1244 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1245 write!(f, "{}", self.as_str())
1246 }
1247}
1248
1249impl FromValue for SpanKind {
1250 fn from_value(value: Annotated<Value>) -> Annotated<Self>
1251 where
1252 Self: Sized,
1253 {
1254 match value {
1255 Annotated(Some(Value::String(s)), meta) => Annotated(SpanKind::from_str(&s).ok(), meta),
1256 Annotated(_, meta) => Annotated(None, meta),
1257 }
1258 }
1259}
1260
1261impl IntoValue for SpanKind {
1262 fn into_value(self) -> Value
1263 where
1264 Self: Sized,
1265 {
1266 Value::String(self.to_string())
1267 }
1268
1269 fn serialize_payload<S>(
1270 &self,
1271 s: S,
1272 _behavior: relay_protocol::SkipSerialization,
1273 ) -> Result<S::Ok, S::Error>
1274 where
1275 Self: Sized,
1276 S: serde::Serializer,
1277 {
1278 s.serialize_str(self.as_str())
1279 }
1280}
1281
1282#[cfg(test)]
1283mod tests {
1284 use crate::protocol::Measurement;
1285 use chrono::{TimeZone, Utc};
1286 use relay_base_schema::metrics::{InformationUnit, MetricUnit};
1287 use relay_protocol::RuleCondition;
1288 use similar_asserts::assert_eq;
1289
1290 use super::*;
1291
1292 #[test]
1293 fn test_span_serialization() {
1294 let json = r#"{
1295 "timestamp": 0.0,
1296 "start_timestamp": -63158400.0,
1297 "exclusive_time": 1.23,
1298 "op": "operation",
1299 "span_id": "fa90fdead5f74052",
1300 "trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
1301 "status": "ok",
1302 "description": "desc",
1303 "origin": "auto.http",
1304 "links": [
1305 {
1306 "trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
1307 "span_id": "fa90fdead5f74052",
1308 "sampled": true,
1309 "attributes": {
1310 "boolAttr": true,
1311 "numAttr": 123,
1312 "stringAttr": "foo"
1313 }
1314 }
1315 ],
1316 "measurements": {
1317 "memory": {
1318 "value": 9001.0,
1319 "unit": "byte"
1320 }
1321 },
1322 "kind": "server"
1323}"#;
1324 let mut measurements = Object::new();
1325 measurements.insert(
1326 "memory".into(),
1327 Annotated::new(Measurement {
1328 value: Annotated::new(9001.0.try_into().unwrap()),
1329 unit: Annotated::new(MetricUnit::Information(InformationUnit::Byte)),
1330 }),
1331 );
1332
1333 let links = Annotated::new(vec![Annotated::new(SpanLink {
1334 trace_id: Annotated::new("4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap()),
1335 span_id: Annotated::new("fa90fdead5f74052".parse().unwrap()),
1336 sampled: Annotated::new(true),
1337 attributes: Annotated::new({
1338 let mut map: std::collections::BTreeMap<String, Annotated<Value>> = Object::new();
1339 map.insert(
1340 "stringAttr".into(),
1341 Annotated::new(Value::String("foo".into())),
1342 );
1343 map.insert("numAttr".into(), Annotated::new(Value::I64(123)));
1344 map.insert("boolAttr".into(), Value::Bool(true).into());
1345 map
1346 }),
1347 ..Default::default()
1348 })]);
1349
1350 let span = Annotated::new(Span {
1351 timestamp: Annotated::new(Utc.with_ymd_and_hms(1970, 1, 1, 0, 0, 0).unwrap().into()),
1352 start_timestamp: Annotated::new(
1353 Utc.with_ymd_and_hms(1968, 1, 1, 0, 0, 0).unwrap().into(),
1354 ),
1355 exclusive_time: Annotated::new(1.23),
1356 description: Annotated::new("desc".to_owned()),
1357 op: Annotated::new("operation".to_owned()),
1358 trace_id: Annotated::new("4c79f60c11214eb38604f4ae0781bfb2".parse().unwrap()),
1359 span_id: Annotated::new("fa90fdead5f74052".parse().unwrap()),
1360 status: Annotated::new(SpanStatus::Ok),
1361 origin: Annotated::new("auto.http".to_owned()),
1362 kind: Annotated::new(SpanKind::Server),
1363 measurements: Annotated::new(Measurements(measurements)),
1364 links,
1365 ..Default::default()
1366 });
1367 assert_eq!(json, span.to_json_pretty().unwrap());
1368
1369 let span_from_string = Annotated::from_json(json).unwrap();
1370 assert_eq!(span, span_from_string);
1371 }
1372
1373 #[test]
1374 fn test_getter_span_data() {
1375 let span = Annotated::<Span>::from_json(
1376 r#"{
1377 "data": {
1378 "foo": {"bar": 1},
1379 "foo.bar": 2
1380 },
1381 "measurements": {
1382 "some": {"value": 100.0}
1383 }
1384 }"#,
1385 )
1386 .unwrap()
1387 .into_value()
1388 .unwrap();
1389
1390 assert_eq!(span.get_value("span.data.foo.bar"), Some(Val::I64(1)));
1391 assert_eq!(span.get_value(r"span.data.foo\.bar"), Some(Val::I64(2)));
1392
1393 assert_eq!(span.get_value("span.data"), None);
1394 assert_eq!(span.get_value("span.data."), None);
1395 assert_eq!(span.get_value("span.data.x"), None);
1396
1397 assert_eq!(
1398 span.get_value("span.measurements.some.value"),
1399 Some(Val::F64(100.0))
1400 );
1401 }
1402
1403 #[test]
1404 fn test_getter_was_transaction() {
1405 let mut span = Span::default();
1406 assert_eq!(
1407 span.get_value("span.was_transaction"),
1408 Some(Val::Bool(false))
1409 );
1410 assert!(RuleCondition::eq("span.was_transaction", false).matches(&span));
1411 assert!(!RuleCondition::eq("span.was_transaction", true).matches(&span));
1412
1413 span.was_transaction.set_value(Some(false));
1414 assert_eq!(
1415 span.get_value("span.was_transaction"),
1416 Some(Val::Bool(false))
1417 );
1418 assert!(RuleCondition::eq("span.was_transaction", false).matches(&span));
1419 assert!(!RuleCondition::eq("span.was_transaction", true).matches(&span));
1420
1421 span.was_transaction.set_value(Some(true));
1422 assert_eq!(
1423 span.get_value("span.was_transaction"),
1424 Some(Val::Bool(true))
1425 );
1426 assert!(RuleCondition::eq("span.was_transaction", true).matches(&span));
1427 assert!(!RuleCondition::eq("span.was_transaction", false).matches(&span));
1428 }
1429
1430 #[test]
1431 fn test_span_fields_as_event() {
1432 let span = Annotated::<Span>::from_json(
1433 r#"{
1434 "data": {
1435 "release": "1.0",
1436 "environment": "prod",
1437 "sentry.segment.name": "/api/endpoint"
1438 }
1439 }"#,
1440 )
1441 .unwrap()
1442 .into_value()
1443 .unwrap();
1444
1445 assert_eq!(span.get_value("event.release"), Some(Val::String("1.0")));
1446 assert_eq!(
1447 span.get_value("event.environment"),
1448 Some(Val::String("prod"))
1449 );
1450 assert_eq!(
1451 span.get_value("event.transaction"),
1452 Some(Val::String("/api/endpoint"))
1453 );
1454 }
1455
1456 #[test]
1457 fn test_span_duration() {
1458 let span = Annotated::<Span>::from_json(
1459 r#"{
1460 "start_timestamp": 1694732407.8367,
1461 "timestamp": 1694732408.31451233
1462 }"#,
1463 )
1464 .unwrap()
1465 .into_value()
1466 .unwrap();
1467
1468 assert_eq!(span.get_value("span.duration"), Some(Val::F64(477.812)));
1469 }
1470
1471 #[test]
1472 fn test_span_data() {
1473 let data = r#"{
1474 "foo": 2,
1475 "bar": "3",
1476 "db.system": "mysql",
1477 "code.filepath": "task.py",
1478 "code.lineno": 123,
1479 "code.function": "fn()",
1480 "code.namespace": "ns",
1481 "frames.slow": 1,
1482 "frames.frozen": 2,
1483 "frames.total": 9,
1484 "frames.delay": 100,
1485 "messaging.destination.name": "default",
1486 "messaging.message.retry.count": 3,
1487 "messaging.message.receive.latency": 40,
1488 "messaging.message.body.size": 100,
1489 "messaging.message.id": "abc123",
1490 "messaging.operation.name": "publish",
1491 "messaging.operation.type": "create",
1492 "user_agent.original": "Chrome",
1493 "url.full": "my_url.com",
1494 "client.address": "192.168.0.1"
1495 }"#;
1496 let data = Annotated::<SpanData>::from_json(data)
1497 .unwrap()
1498 .into_value()
1499 .unwrap();
1500 insta::assert_debug_snapshot!(data, @r#"
1501 SpanData {
1502 app_start_type: ~,
1503 gen_ai_request_max_tokens: ~,
1504 gen_ai_pipeline_name: ~,
1505 gen_ai_usage_total_tokens: ~,
1506 gen_ai_usage_input_tokens: ~,
1507 gen_ai_usage_input_tokens_cached: ~,
1508 gen_ai_usage_input_tokens_cache_write: ~,
1509 gen_ai_usage_input_tokens_cache_miss: ~,
1510 gen_ai_usage_output_tokens: ~,
1511 gen_ai_usage_output_tokens_reasoning: ~,
1512 gen_ai_usage_output_tokens_prediction_accepted: ~,
1513 gen_ai_usage_output_tokens_prediction_rejected: ~,
1514 gen_ai_response_model: ~,
1515 gen_ai_request_model: ~,
1516 gen_ai_context_window_size: ~,
1517 gen_ai_context_utilization: ~,
1518 gen_ai_cost_total_tokens: ~,
1519 gen_ai_cost_input_tokens: ~,
1520 gen_ai_cost_output_tokens: ~,
1521 gen_ai_input_messages: ~,
1522 gen_ai_tool_call_arguments: ~,
1523 gen_ai_tool_call_result: ~,
1524 gen_ai_output_messages: ~,
1525 gen_ai_response_object: ~,
1526 gen_ai_response_streaming: ~,
1527 gen_ai_response_tokens_per_second: ~,
1528 gen_ai_response_time_to_first_token: ~,
1529 gen_ai_tool_definitions: ~,
1530 gen_ai_request_frequency_penalty: ~,
1531 gen_ai_request_presence_penalty: ~,
1532 gen_ai_request_seed: ~,
1533 gen_ai_request_temperature: ~,
1534 gen_ai_request_top_k: ~,
1535 gen_ai_request_top_p: ~,
1536 gen_ai_response_finish_reasons: ~,
1537 gen_ai_response_id: ~,
1538 gen_ai_provider_name: ~,
1539 gen_ai_system_instructions: ~,
1540 gen_ai_tool_name: ~,
1541 gen_ai_operation_name: ~,
1542 gen_ai_operation_type: ~,
1543 gen_ai_agent_name: ~,
1544 gen_ai_function_id: ~,
1545 mcp_prompt_result: ~,
1546 mcp_tool_result_content: ~,
1547 browser_name: ~,
1548 code_filepath: String(
1549 "task.py",
1550 ),
1551 code_lineno: I64(
1552 123,
1553 ),
1554 code_function: String(
1555 "fn()",
1556 ),
1557 code_namespace: String(
1558 "ns",
1559 ),
1560 db_operation: ~,
1561 db_system: String(
1562 "mysql",
1563 ),
1564 db_collection_name: ~,
1565 environment: ~,
1566 release: ~,
1567 http_decoded_response_content_length: ~,
1568 http_request_method: ~,
1569 http_response_content_length: ~,
1570 http_response_transfer_size: ~,
1571 resource_render_blocking_status: ~,
1572 server_address: ~,
1573 cache_hit: ~,
1574 cache_key: ~,
1575 cache_item_size: ~,
1576 http_response_status_code: ~,
1577 thread_name: ~,
1578 thread_id: ~,
1579 segment_name: ~,
1580 ui_component_name: ~,
1581 url_scheme: ~,
1582 user: ~,
1583 user_email: ~,
1584 user_full_name: ~,
1585 user_geo_country_code: ~,
1586 user_geo_city: ~,
1587 user_geo_subdivision: ~,
1588 user_geo_region: ~,
1589 user_hash: ~,
1590 user_id: ~,
1591 user_name: ~,
1592 user_roles: ~,
1593 exclusive_time: ~,
1594 profile_id: ~,
1595 replay_id: ~,
1596 sdk_name: ~,
1597 sdk_version: ~,
1598 frames_slow: I64(
1599 1,
1600 ),
1601 frames_frozen: I64(
1602 2,
1603 ),
1604 frames_total: I64(
1605 9,
1606 ),
1607 frames_delay: I64(
1608 100,
1609 ),
1610 messaging_destination_name: "default",
1611 messaging_message_retry_count: I64(
1612 3,
1613 ),
1614 messaging_message_receive_latency: I64(
1615 40,
1616 ),
1617 messaging_message_body_size: I64(
1618 100,
1619 ),
1620 messaging_message_id: "abc123",
1621 messaging_operation_name: "publish",
1622 messaging_operation_type: "create",
1623 user_agent_original: "Chrome",
1624 url_full: "my_url.com",
1625 url_query: ~,
1626 http_query: ~,
1627 client_address: IpAddr(
1628 "192.168.0.1",
1629 ),
1630 route: ~,
1631 previous_route: ~,
1632 lcp_element: ~,
1633 lcp_size: ~,
1634 lcp_id: ~,
1635 lcp_url: ~,
1636 span_name: ~,
1637 other: {
1638 "bar": String(
1639 "3",
1640 ),
1641 "foo": I64(
1642 2,
1643 ),
1644 },
1645 }
1646 "#);
1647
1648 assert_eq!(data.get_value("foo"), Some(Val::U64(2)));
1649 assert_eq!(data.get_value("bar"), Some(Val::String("3")));
1650 assert_eq!(data.get_value("db\\.system"), Some(Val::String("mysql")));
1651 assert_eq!(data.get_value("code\\.lineno"), Some(Val::U64(123)));
1652 assert_eq!(data.get_value("code\\.function"), Some(Val::String("fn()")));
1653 assert_eq!(data.get_value("code\\.namespace"), Some(Val::String("ns")));
1654 assert_eq!(data.get_value("unknown"), None);
1655 }
1656
1657 #[test]
1658 fn test_span_data_empty_well_known_field() {
1659 let span = r#"{
1660 "data": {
1661 "lcp.url": ""
1662 }
1663 }"#;
1664 let span: Annotated<Span> = Annotated::from_json(span).unwrap();
1665 assert_eq!(span.to_json().unwrap(), r#"{"data":{"lcp.url":""}}"#);
1666 }
1667
1668 #[test]
1669 fn test_span_data_empty_custom_field() {
1670 let span = r#"{
1671 "data": {
1672 "custom_field_empty": ""
1673 }
1674 }"#;
1675 let span: Annotated<Span> = Annotated::from_json(span).unwrap();
1676 assert_eq!(
1677 span.to_json().unwrap(),
1678 r#"{"data":{"custom_field_empty":""}}"#
1679 );
1680 }
1681
1682 #[test]
1683 fn test_span_data_completely_empty() {
1684 let span = r#"{
1685 "data": {}
1686 }"#;
1687 let span: Annotated<Span> = Annotated::from_json(span).unwrap();
1688 assert_eq!(span.to_json().unwrap(), r#"{"data":{}}"#);
1689 }
1690
1691 #[test]
1692 fn test_span_links() {
1693 let span = r#"{
1694 "links": [
1695 {
1696 "trace_id": "5c79f60c11214eb38604f4ae0781bfb2",
1697 "span_id": "ab90fdead5f74052",
1698 "sampled": true,
1699 "attributes": {
1700 "sentry.link.type": "previous_trace"
1701 }
1702 },
1703 {
1704 "trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
1705 "span_id": "fa90fdead5f74052",
1706 "sampled": true,
1707 "attributes": {
1708 "sentry.link.type": "next_trace"
1709 }
1710 }
1711 ]
1712 }"#;
1713
1714 let span: Annotated<Span> = Annotated::from_json(span).unwrap();
1715 assert_eq!(
1716 span.to_json().unwrap(),
1717 r#"{"links":[{"trace_id":"5c79f60c11214eb38604f4ae0781bfb2","span_id":"ab90fdead5f74052","sampled":true,"attributes":{"sentry.link.type":"previous_trace"}},{"trace_id":"4c79f60c11214eb38604f4ae0781bfb2","span_id":"fa90fdead5f74052","sampled":true,"attributes":{"sentry.link.type":"next_trace"}}]}"#
1718 );
1719 }
1720
1721 #[test]
1722 fn test_span_kind() {
1723 let span = Annotated::<Span>::from_json(
1724 r#"{
1725 "kind": "???"
1726 }"#,
1727 )
1728 .unwrap()
1729 .into_value()
1730 .unwrap();
1731 assert_eq!(
1732 span.kind.value().unwrap(),
1733 &SpanKind::Unknown("???".to_owned())
1734 );
1735 }
1736}