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("fa90fdead5f74052"),
144 parent_span_id: SpanId("fa90fdead5f74051"),
145 trace_id: TraceId("4c79f60c11214eb38604f4ae0781bfb2"),
146 segment_id: SpanId("fa90fdead5f74052"),
147 is_segment: true,
148 is_remote: true,
149 status: Ok,
150 description: "my 1st transaction",
151 tags: ~,
152 origin: "manual",
153 profile_id: EventId(
154 a0aaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab,
155 ),
156 data: SpanData {
157 app_start_type: ~,
158 gen_ai_usage_total_tokens: ~,
159 gen_ai_usage_input_tokens: ~,
160 gen_ai_usage_output_tokens: ~,
161 gen_ai_usage_total_cost: ~,
162 browser_name: "Chrome",
163 code_filepath: ~,
164 code_lineno: ~,
165 code_function: ~,
166 code_namespace: ~,
167 db_operation: ~,
168 db_system: ~,
169 db_collection_name: ~,
170 environment: "prod",
171 release: LenientString(
172 "myapp@1.0.0",
173 ),
174 http_decoded_response_content_length: ~,
175 http_request_method: ~,
176 http_response_content_length: ~,
177 http_response_transfer_size: ~,
178 resource_render_blocking_status: ~,
179 server_address: ~,
180 cache_hit: ~,
181 cache_key: ~,
182 cache_item_size: ~,
183 http_response_status_code: ~,
184 ai_pipeline_name: ~,
185 ai_model_id: ~,
186 ai_input_messages: ~,
187 ai_responses: ~,
188 thread_name: ~,
189 thread_id: ~,
190 segment_name: "my 1st transaction",
191 ui_component_name: ~,
192 url_scheme: ~,
193 user: ~,
194 user_email: ~,
195 user_full_name: ~,
196 user_geo_country_code: ~,
197 user_geo_city: ~,
198 user_geo_subdivision: ~,
199 user_geo_region: ~,
200 user_hash: ~,
201 user_id: ~,
202 user_name: ~,
203 user_roles: ~,
204 exclusive_time: ~,
205 profile_id: ~,
206 replay_id: ~,
207 sdk_name: "sentry.php",
208 sdk_version: "1.2.3",
209 frames_slow: ~,
210 frames_frozen: ~,
211 frames_total: ~,
212 frames_delay: ~,
213 messaging_destination_name: ~,
214 messaging_message_retry_count: ~,
215 messaging_message_receive_latency: ~,
216 messaging_message_body_size: ~,
217 messaging_message_id: ~,
218 messaging_operation_name: ~,
219 messaging_operation_type: ~,
220 user_agent_original: ~,
221 url_full: ~,
222 client_address: ~,
223 route: ~,
224 previous_route: ~,
225 lcp_element: ~,
226 lcp_size: ~,
227 lcp_id: ~,
228 lcp_url: ~,
229 other: {
230 "custom_attribute": I64(
231 42,
232 ),
233 },
234 },
235 links: [
236 SpanLink {
237 trace_id: TraceId("4c79f60c11214eb38604f4ae0781bfb2"),
238 span_id: SpanId("fa90fdead5f74052"),
239 sampled: true,
240 attributes: {
241 "sentry.link.type": String(
242 "previous_trace",
243 ),
244 },
245 other: {},
246 },
247 ],
248 sentry_tags: ~,
249 received: ~,
250 measurements: Measurements(
251 {
252 "memory": Measurement {
253 value: 9001.0,
254 unit: Information(
255 Byte,
256 ),
257 },
258 },
259 ),
260 platform: "php",
261 was_transaction: true,
262 kind: ~,
263 _performance_issues_spans: ~,
264 other: {},
265 }
266 "#);
267 }
268}