1use crate::protocol::{BrowserContext, Event, ProfileContext, Span, SpanData, TraceContext};
4
5impl From<&Event> for Span {
6 fn from(event: &Event) -> Self {
7 let Event {
8 transaction,
9
10 platform,
11 timestamp,
12 start_timestamp,
13 received,
14 release,
15 environment,
16 tags,
17
18 measurements,
19 _metrics,
20 _performance_issues_spans,
21 ..
22 } = event;
23
24 let trace = event.context::<TraceContext>();
25
26 let mut data = trace
28 .map(|c| c.data.clone().map_value(SpanData::from))
29 .unwrap_or_default();
30
31 let span_data = data.get_or_insert_with(Default::default);
33 span_data.segment_name = transaction.clone();
34 span_data.release = release.clone();
35 span_data.environment = environment.clone();
36 if let Some(browser) = event.context::<BrowserContext>() {
37 span_data.browser_name = browser.name.clone();
38 }
39 if let Some(client_sdk) = event.client_sdk.value() {
40 span_data.sdk_name = client_sdk.name.clone();
41 span_data.sdk_version = client_sdk.version.clone();
42 }
43
44 Self {
45 timestamp: timestamp.clone(),
46 start_timestamp: start_timestamp.clone(),
47 exclusive_time: trace.map(|c| c.exclusive_time.clone()).unwrap_or_default(),
48 op: trace.map(|c| c.op.clone()).unwrap_or_default(),
49 span_id: trace.map(|c| c.span_id.clone()).unwrap_or_default(),
50 parent_span_id: trace.map(|c| c.parent_span_id.clone()).unwrap_or_default(),
51 trace_id: trace.map(|c| c.trace_id.clone()).unwrap_or_default(),
52 segment_id: trace.map(|c| c.span_id.clone()).unwrap_or_default(),
53 is_segment: true.into(),
54 is_remote: true.into(),
59 status: trace.map(|c| c.status.clone()).unwrap_or_default(),
60 description: transaction.clone(),
61 tags: tags.clone().map_value(|t| t.into()),
62 origin: trace.map(|c| c.origin.clone()).unwrap_or_default(),
63 profile_id: event
64 .context::<ProfileContext>()
65 .map(|c| c.profile_id.clone())
66 .unwrap_or_default(),
67 data,
68 links: trace.map(|c| c.links.clone()).unwrap_or_default(),
69 sentry_tags: Default::default(),
70 received: received.clone(),
71 measurements: measurements.clone(),
72 platform: platform.clone(),
73 was_transaction: true.into(),
74 kind: Default::default(),
75 _performance_issues_spans: _performance_issues_spans.clone(),
76 other: Default::default(),
77 }
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use relay_protocol::Annotated;
84
85 use super::*;
86
87 #[test]
88 fn convert() {
89 let event = Annotated::<Event>::from_json(
90 r#"{
91 "type": "transaction",
92 "platform": "php",
93 "sdk": {"name": "sentry.php", "version": "1.2.3"},
94 "release": "myapp@1.0.0",
95 "environment": "prod",
96 "transaction": "my 1st transaction",
97 "contexts": {
98 "browser": {"name": "Chrome"},
99 "profile": {"profile_id": "a0aaaaaaaaaaaaaaaaaaaaaaaaaaaaab"},
100 "trace": {
101 "trace_id": "4C79F60C11214EB38604F4AE0781BFB2",
102 "span_id": "FA90FDEAD5F74052",
103 "type": "trace",
104 "origin": "manual",
105 "op": "myop",
106 "status": "ok",
107 "exclusive_time": 123.4,
108 "parent_span_id": "FA90FDEAD5F74051",
109 "data": {
110 "custom_attribute": 42
111 },
112 "links": [
113 {
114 "trace_id": "4c79f60c11214eb38604f4ae0781bfb2",
115 "span_id": "fa90fdead5f74052",
116 "sampled": true,
117 "attributes": {
118 "sentry.link.type": "previous_trace"
119 }
120 }
121 ]
122 }
123 },
124 "measurements": {
125 "memory": {
126 "value": 9001.0,
127 "unit": "byte"
128 }
129 }
130 }"#,
131 )
132 .unwrap()
133 .into_value()
134 .unwrap();
135
136 let span_from_event = Span::from(&event);
137 insta::assert_debug_snapshot!(span_from_event, @r#"
138 Span {
139 timestamp: ~,
140 start_timestamp: ~,
141 exclusive_time: 123.4,
142 op: "myop",
143 span_id: SpanId(
144 "fa90fdead5f74052",
145 ),
146 parent_span_id: SpanId(
147 "fa90fdead5f74051",
148 ),
149 trace_id: TraceId("4c79f60c11214eb38604f4ae0781bfb2"),
150 segment_id: SpanId(
151 "fa90fdead5f74052",
152 ),
153 is_segment: true,
154 is_remote: true,
155 status: Ok,
156 description: "my 1st transaction",
157 tags: ~,
158 origin: "manual",
159 profile_id: EventId(
160 a0aaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab,
161 ),
162 data: SpanData {
163 app_start_type: ~,
164 ai_total_tokens_used: ~,
165 ai_prompt_tokens_used: ~,
166 ai_completion_tokens_used: ~,
167 browser_name: "Chrome",
168 code_filepath: ~,
169 code_lineno: ~,
170 code_function: ~,
171 code_namespace: ~,
172 db_operation: ~,
173 db_system: ~,
174 db_collection_name: ~,
175 environment: "prod",
176 release: LenientString(
177 "myapp@1.0.0",
178 ),
179 http_decoded_response_content_length: ~,
180 http_request_method: ~,
181 http_response_content_length: ~,
182 http_response_transfer_size: ~,
183 resource_render_blocking_status: ~,
184 server_address: ~,
185 cache_hit: ~,
186 cache_key: ~,
187 cache_item_size: ~,
188 http_response_status_code: ~,
189 ai_pipeline_name: ~,
190 ai_model_id: ~,
191 ai_input_messages: ~,
192 ai_responses: ~,
193 thread_name: ~,
194 thread_id: ~,
195 segment_name: "my 1st transaction",
196 ui_component_name: ~,
197 url_scheme: ~,
198 user: ~,
199 user_email: ~,
200 user_full_name: ~,
201 user_geo_country_code: ~,
202 user_geo_city: ~,
203 user_geo_subdivision: ~,
204 user_geo_region: ~,
205 user_hash: ~,
206 user_id: ~,
207 user_name: ~,
208 user_roles: ~,
209 exclusive_time: ~,
210 profile_id: ~,
211 replay_id: ~,
212 sdk_name: "sentry.php",
213 sdk_version: "1.2.3",
214 frames_slow: ~,
215 frames_frozen: ~,
216 frames_total: ~,
217 frames_delay: ~,
218 messaging_destination_name: ~,
219 messaging_message_retry_count: ~,
220 messaging_message_receive_latency: ~,
221 messaging_message_body_size: ~,
222 messaging_message_id: ~,
223 messaging_operation_name: ~,
224 messaging_operation_type: ~,
225 user_agent_original: ~,
226 url_full: ~,
227 client_address: ~,
228 route: ~,
229 previous_route: ~,
230 lcp_element: ~,
231 lcp_size: ~,
232 lcp_id: ~,
233 lcp_url: ~,
234 other: {
235 "custom_attribute": I64(
236 42,
237 ),
238 },
239 },
240 links: [
241 SpanLink {
242 trace_id: TraceId("4c79f60c11214eb38604f4ae0781bfb2"),
243 span_id: SpanId(
244 "fa90fdead5f74052",
245 ),
246 sampled: true,
247 attributes: {
248 "sentry.link.type": String(
249 "previous_trace",
250 ),
251 },
252 other: {},
253 },
254 ],
255 sentry_tags: ~,
256 received: ~,
257 measurements: Measurements(
258 {
259 "memory": Measurement {
260 value: 9001.0,
261 unit: Information(
262 Byte,
263 ),
264 },
265 },
266 ),
267 platform: "php",
268 was_transaction: true,
269 kind: ~,
270 _performance_issues_spans: ~,
271 other: {},
272 }
273 "#);
274 }
275}