saluki_core/data_model/event/trace/
mod.rs1use std::sync::Arc;
4
5use saluki_common::collections::FastHashMap;
6use stringtheory::MetaString;
7
8mod model_traits;
9pub use self::model_traits::{AttributeValue, AttributeValueExt, KeyValueList, SalukiValues};
10
11#[derive(Clone, Debug, PartialEq, Default)]
16pub struct PayloadFields {
17 pub container_id: MetaString,
19 pub language_name: MetaString,
21 pub language_version: MetaString,
23 pub tracer_version: MetaString,
25 pub runtime_id: MetaString,
27 pub env: MetaString,
29 pub hostname: MetaString,
31 pub app_version: MetaString,
33 pub container_tags: Option<Arc<Vec<MetaString>>>,
38 pub client_dropped_p0s_weight: f64,
40}
41
42impl PayloadFields {
43 pub fn resolved_container_tags(&self) -> &[MetaString] {
45 self.container_tags.as_deref().map_or(&[], Vec::as_slice)
46 }
47}
48
49#[derive(Clone, Copy, Debug, PartialEq)]
54pub enum TraceIngestInfo {
55 Datadog,
57
58 Otlp {
60 receiver_sampling_rate: f64,
65 },
66}
67
68#[derive(Clone, Debug, PartialEq)]
72pub struct Trace {
73 spans: Vec<Span>,
75 pub trace_id_high: u64,
77 pub trace_id_low: u64,
79 pub origin: MetaString,
81 pub payload: PayloadFields,
83 pub attributes: Arc<FastHashMap<MetaString, AttributeValue>>,
85 pub priority: Option<i32>,
87 pub dropped_trace: bool,
89 pub sampling_mechanism: u32,
91 pub decision_maker: Option<MetaString>,
93 ingest_info: TraceIngestInfo,
95}
96
97impl Trace {
98 pub fn new(spans: Vec<Span>, ingest_info: TraceIngestInfo) -> Self {
103 Self {
104 spans,
105 trace_id_high: 0,
106 trace_id_low: 0,
107 origin: MetaString::empty(),
108 payload: PayloadFields::default(),
109 attributes: Arc::new(FastHashMap::default()),
110 priority: None,
111 dropped_trace: false,
112 sampling_mechanism: 0,
113 decision_maker: None,
114 ingest_info,
115 }
116 }
117
118 pub fn ingest_info(&self) -> TraceIngestInfo {
122 self.ingest_info
123 }
124
125 pub fn spans(&self) -> &[Span] {
127 &self.spans
128 }
129
130 pub fn spans_mut(&mut self) -> &mut [Span] {
132 &mut self.spans
133 }
134
135 pub fn set_spans(&mut self, spans: Vec<Span>) {
137 self.spans = spans;
138 }
139
140 pub fn retain_spans<F>(&mut self, mut f: F) -> usize
144 where
145 F: FnMut(&Trace, &Span) -> bool,
146 {
147 if self.spans.is_empty() {
148 return 0;
149 }
150
151 let mut has_match = false;
152 for span in self.spans.iter() {
153 if f(self, span) {
154 has_match = true;
155 break;
156 }
157 }
158
159 if !has_match {
160 return 0;
161 }
162
163 let mut spans = std::mem::take(&mut self.spans);
164 spans.retain(|span| f(self, span));
165 spans.shrink_to_fit();
166 let _ = std::mem::replace(&mut self.spans, spans);
167
168 self.spans.len()
169 }
170
171 pub fn remove_spans<F>(&mut self, mut f: F)
173 where
174 F: FnMut(&Trace, &Span) -> bool,
175 {
176 if self.spans.is_empty() {
177 return;
178 }
179
180 let mut spans = std::mem::take(&mut self.spans);
181 spans.retain(|span| !f(self, span));
182 spans.shrink_to_fit();
183 let _ = std::mem::replace(&mut self.spans, spans);
184 }
185}
186
187#[derive(Clone, Debug, PartialEq, Default)]
189pub struct Span {
190 service: MetaString,
192 name: MetaString,
194 resource: MetaString,
196 span_id: u64,
198 parent_id: u64,
200 start: u64,
202 duration: u64,
204 error: i32,
206 span_type: MetaString,
208 span_links: Vec<SpanLink>,
210 span_events: Vec<SpanEvent>,
212 pub env: MetaString,
214 pub version: MetaString,
216 pub component: MetaString,
218 pub kind: u32,
220 pub attributes: FastHashMap<MetaString, AttributeValue>,
222}
223
224impl Span {
225 #[allow(clippy::too_many_arguments)]
227 pub fn new(
228 service: impl Into<MetaString>, name: impl Into<MetaString>, resource: impl Into<MetaString>,
229 span_type: impl Into<MetaString>, span_id: u64, parent_id: u64, start: u64, duration: u64, error: i32,
230 ) -> Self {
231 Self {
232 service: service.into(),
233 name: name.into(),
234 resource: resource.into(),
235 span_type: span_type.into(),
236 span_id,
237 parent_id,
238 start,
239 duration,
240 error,
241 ..Self::default()
242 }
243 }
244
245 pub fn with_service(mut self, service: impl Into<MetaString>) -> Self {
247 self.service = service.into();
248 self
249 }
250
251 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
253 self.name = name.into();
254 self
255 }
256
257 pub fn with_resource(mut self, resource: impl Into<MetaString>) -> Self {
259 self.resource = resource.into();
260 self
261 }
262
263 pub fn with_span_id(mut self, span_id: u64) -> Self {
265 self.span_id = span_id;
266 self
267 }
268
269 pub fn with_parent_id(mut self, parent_id: u64) -> Self {
271 self.parent_id = parent_id;
272 self
273 }
274
275 pub fn with_start(mut self, start: u64) -> Self {
277 self.start = start;
278 self
279 }
280
281 pub fn with_duration(mut self, duration: u64) -> Self {
283 self.duration = duration;
284 self
285 }
286
287 pub fn with_error(mut self, error: i32) -> Self {
289 self.error = error;
290 self
291 }
292
293 pub fn with_span_type(mut self, span_type: impl Into<MetaString>) -> Self {
295 self.span_type = span_type.into();
296 self
297 }
298
299 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
301 self.attributes = attributes.into().unwrap_or_default();
302 self
303 }
304
305 pub fn with_span_links(mut self, span_links: impl Into<Option<Vec<SpanLink>>>) -> Self {
307 self.span_links = span_links.into().unwrap_or_default();
308 self
309 }
310
311 pub fn with_span_events(mut self, span_events: impl Into<Option<Vec<SpanEvent>>>) -> Self {
313 self.span_events = span_events.into().unwrap_or_default();
314 self
315 }
316
317 pub fn with_env(mut self, env: impl Into<MetaString>) -> Self {
319 self.env = env.into();
320 self
321 }
322
323 pub fn with_version(mut self, version: impl Into<MetaString>) -> Self {
325 self.version = version.into();
326 self
327 }
328
329 pub fn with_component(mut self, component: impl Into<MetaString>) -> Self {
331 self.component = component.into();
332 self
333 }
334
335 pub fn with_kind(mut self, kind: u32) -> Self {
337 self.kind = kind;
338 self
339 }
340
341 pub fn service(&self) -> &str {
343 &self.service
344 }
345
346 pub fn name(&self) -> &str {
348 &self.name
349 }
350
351 pub fn resource(&self) -> &str {
353 &self.resource
354 }
355
356 pub fn set_resource(&mut self, resource: impl Into<MetaString>) {
358 self.resource = resource.into();
359 }
360
361 pub fn span_id(&self) -> u64 {
363 self.span_id
364 }
365
366 pub fn parent_id(&self) -> u64 {
368 self.parent_id
369 }
370
371 pub fn start(&self) -> u64 {
373 self.start
374 }
375
376 pub fn duration(&self) -> u64 {
378 self.duration
379 }
380
381 pub fn error(&self) -> i32 {
383 self.error
384 }
385
386 pub fn span_type(&self) -> &str {
388 &self.span_type
389 }
390
391 pub fn span_links(&self) -> &[SpanLink] {
393 &self.span_links
394 }
395
396 pub fn span_links_mut(&mut self) -> &mut Vec<SpanLink> {
398 &mut self.span_links
399 }
400
401 pub fn span_events(&self) -> &[SpanEvent] {
403 &self.span_events
404 }
405
406 pub fn span_events_mut(&mut self) -> &mut Vec<SpanEvent> {
408 &mut self.span_events
409 }
410}
411
412#[derive(Clone, Debug, PartialEq, Default)]
414pub struct SpanLink {
415 trace_id: u64,
417 trace_id_high: u64,
419 span_id: u64,
421 attributes: FastHashMap<MetaString, AttributeValue>,
423 tracestate: MetaString,
425 flags: u32,
427}
428
429impl SpanLink {
430 pub fn new(trace_id: u64, span_id: u64) -> Self {
432 Self {
433 trace_id,
434 span_id,
435 ..Self::default()
436 }
437 }
438
439 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
441 self.trace_id = trace_id;
442 self
443 }
444
445 pub fn with_trace_id_high(mut self, trace_id_high: u64) -> Self {
447 self.trace_id_high = trace_id_high;
448 self
449 }
450
451 pub fn with_span_id(mut self, span_id: u64) -> Self {
453 self.span_id = span_id;
454 self
455 }
456
457 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
459 self.attributes = attributes.into().unwrap_or_default();
460 self
461 }
462
463 pub fn with_tracestate(mut self, tracestate: impl Into<MetaString>) -> Self {
465 self.tracestate = tracestate.into();
466 self
467 }
468
469 pub fn with_flags(mut self, flags: u32) -> Self {
471 self.flags = flags;
472 self
473 }
474
475 pub fn trace_id(&self) -> u64 {
477 self.trace_id
478 }
479
480 pub fn trace_id_high(&self) -> u64 {
482 self.trace_id_high
483 }
484
485 pub fn span_id(&self) -> u64 {
487 self.span_id
488 }
489
490 pub fn attributes(&self) -> &FastHashMap<MetaString, AttributeValue> {
492 &self.attributes
493 }
494
495 pub fn tracestate(&self) -> &str {
497 &self.tracestate
498 }
499
500 pub fn flags(&self) -> u32 {
502 self.flags
503 }
504}
505
506#[derive(Clone, Debug, PartialEq, Default)]
508pub struct SpanEvent {
509 time_unix_nano: u64,
511 name: MetaString,
513 attributes: FastHashMap<MetaString, AttributeValue>,
515}
516
517impl SpanEvent {
518 pub fn new(time_unix_nano: u64, name: impl Into<MetaString>) -> Self {
520 Self {
521 time_unix_nano,
522 name: name.into(),
523 ..Self::default()
524 }
525 }
526
527 pub fn with_time_unix_nano(mut self, time_unix_nano: u64) -> Self {
529 self.time_unix_nano = time_unix_nano;
530 self
531 }
532
533 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
535 self.name = name.into();
536 self
537 }
538
539 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
541 self.attributes = attributes.into().unwrap_or_default();
542 self
543 }
544
545 pub fn time_unix_nano(&self) -> u64 {
547 self.time_unix_nano
548 }
549
550 pub fn name(&self) -> &str {
552 &self.name
553 }
554
555 pub fn attributes(&self) -> &FastHashMap<MetaString, AttributeValue> {
557 &self.attributes
558 }
559
560 pub fn attributes_mut(&mut self) -> &mut FastHashMap<MetaString, AttributeValue> {
562 &mut self.attributes
563 }
564}