saluki_core/data_model/event/trace/
mod.rs1use saluki_common::collections::FastHashMap;
4use saluki_context::tags::SharedTagSet;
5use stringtheory::MetaString;
6
7#[derive(Clone, Debug, PartialEq)]
12pub struct TraceSampling {
13 pub dropped_trace: bool,
15
16 pub priority: Option<i32>,
24
25 pub decision_maker: Option<MetaString>,
32
33 pub otlp_sampling_rate: Option<f64>,
38}
39
40impl TraceSampling {
41 pub fn new(
43 dropped_trace: bool, priority: Option<i32>, decision_maker: Option<MetaString>, otlp_sampling_rate: Option<f64>,
44 ) -> Self {
45 Self {
46 dropped_trace,
47 priority,
48 decision_maker,
49 otlp_sampling_rate,
50 }
51 }
52}
53
54#[derive(Clone, Debug, PartialEq)]
58pub struct Trace {
59 spans: Vec<Span>,
61 resource_tags: SharedTagSet,
65 sampling: Option<TraceSampling>,
71}
72
73impl Trace {
74 pub fn new(spans: Vec<Span>, resource_tags: impl Into<SharedTagSet>) -> Self {
76 Self {
77 spans,
78 resource_tags: resource_tags.into(),
79 sampling: None,
80 }
81 }
82
83 pub fn spans(&self) -> &[Span] {
85 &self.spans
86 }
87
88 pub fn spans_mut(&mut self) -> &mut [Span] {
90 &mut self.spans
91 }
92
93 pub fn set_spans(&mut self, spans: Vec<Span>) {
95 self.spans = spans;
96 }
97
98 pub fn retain_spans<F>(&mut self, mut f: F) -> usize
102 where
103 F: FnMut(&Trace, &Span) -> bool,
104 {
105 if self.spans.is_empty() {
106 return 0;
107 }
108
109 let mut has_match = false;
110 for span in self.spans.iter() {
111 if f(self, span) {
112 has_match = true;
113 break;
114 }
115 }
116
117 if !has_match {
118 return 0;
119 }
120
121 let mut spans = std::mem::take(&mut self.spans);
122 spans.retain(|span| f(self, span));
123 spans.shrink_to_fit();
124 let _ = std::mem::replace(&mut self.spans, spans);
125
126 self.spans.len()
127 }
128
129 pub fn remove_spans<F>(&mut self, mut f: F)
131 where
132 F: FnMut(&Trace, &Span) -> bool,
133 {
134 if self.spans.is_empty() {
135 return;
136 }
137
138 let mut spans = std::mem::take(&mut self.spans);
139 spans.retain(|span| !f(self, span));
140 spans.shrink_to_fit();
141 let _ = std::mem::replace(&mut self.spans, spans);
142 }
143
144 pub fn resource_tags(&self) -> &SharedTagSet {
146 &self.resource_tags
147 }
148
149 pub fn sampling(&self) -> Option<&TraceSampling> {
151 self.sampling.as_ref()
152 }
153
154 pub fn set_sampling(&mut self, sampling: Option<TraceSampling>) {
156 self.sampling = sampling;
157 }
158}
159
160#[derive(Clone, Debug, PartialEq, Default)]
162pub struct Span {
163 service: MetaString,
165 name: MetaString,
167 resource: MetaString,
169 trace_id: u64,
171 span_id: u64,
173 parent_id: u64,
175 start: u64,
177 duration: u64,
179 error: i32,
181 meta: FastHashMap<MetaString, MetaString>,
183 metrics: FastHashMap<MetaString, f64>,
185 span_type: MetaString,
187 meta_struct: FastHashMap<MetaString, Vec<u8>>,
189 span_links: Vec<SpanLink>,
191 span_events: Vec<SpanEvent>,
193}
194
195impl Span {
196 #[allow(clippy::too_many_arguments)]
198 pub fn new(
199 service: impl Into<MetaString>, name: impl Into<MetaString>, resource: impl Into<MetaString>,
200 span_type: impl Into<MetaString>, trace_id: u64, span_id: u64, parent_id: u64, start: u64, duration: u64,
201 error: i32,
202 ) -> Self {
203 Self {
204 service: service.into(),
205 name: name.into(),
206 resource: resource.into(),
207 span_type: span_type.into(),
208 trace_id,
209 span_id,
210 parent_id,
211 start,
212 duration,
213 error,
214 ..Self::default()
215 }
216 }
217
218 pub fn with_service(mut self, service: impl Into<MetaString>) -> Self {
220 self.service = service.into();
221 self
222 }
223
224 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
226 self.name = name.into();
227 self
228 }
229
230 pub fn with_resource(mut self, resource: impl Into<MetaString>) -> Self {
232 self.resource = resource.into();
233 self
234 }
235
236 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
238 self.trace_id = trace_id;
239 self
240 }
241
242 pub fn with_span_id(mut self, span_id: u64) -> Self {
244 self.span_id = span_id;
245 self
246 }
247
248 pub fn with_parent_id(mut self, parent_id: u64) -> Self {
250 self.parent_id = parent_id;
251 self
252 }
253
254 pub fn with_start(mut self, start: u64) -> Self {
256 self.start = start;
257 self
258 }
259
260 pub fn with_duration(mut self, duration: u64) -> Self {
262 self.duration = duration;
263 self
264 }
265
266 pub fn with_error(mut self, error: i32) -> Self {
268 self.error = error;
269 self
270 }
271
272 pub fn with_span_type(mut self, span_type: impl Into<MetaString>) -> Self {
274 self.span_type = span_type.into();
275 self
276 }
277
278 pub fn with_meta(mut self, meta: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
280 self.meta = meta.into().unwrap_or_default();
281 self
282 }
283
284 pub fn with_metrics(mut self, metrics: impl Into<Option<FastHashMap<MetaString, f64>>>) -> Self {
286 self.metrics = metrics.into().unwrap_or_default();
287 self
288 }
289
290 pub fn with_meta_struct(mut self, meta_struct: impl Into<Option<FastHashMap<MetaString, Vec<u8>>>>) -> Self {
292 self.meta_struct = meta_struct.into().unwrap_or_default();
293 self
294 }
295
296 pub fn with_span_links(mut self, span_links: impl Into<Option<Vec<SpanLink>>>) -> Self {
298 self.span_links = span_links.into().unwrap_or_default();
299 self
300 }
301
302 pub fn with_span_events(mut self, span_events: impl Into<Option<Vec<SpanEvent>>>) -> Self {
304 self.span_events = span_events.into().unwrap_or_default();
305 self
306 }
307
308 pub fn service(&self) -> &str {
310 &self.service
311 }
312
313 pub fn name(&self) -> &str {
315 &self.name
316 }
317
318 pub fn resource(&self) -> &str {
320 &self.resource
321 }
322
323 pub fn set_resource(&mut self, resource: impl Into<MetaString>) {
325 self.resource = resource.into();
326 }
327
328 pub fn trace_id(&self) -> u64 {
330 self.trace_id
331 }
332
333 pub fn span_id(&self) -> u64 {
335 self.span_id
336 }
337
338 pub fn parent_id(&self) -> u64 {
340 self.parent_id
341 }
342
343 pub fn start(&self) -> u64 {
345 self.start
346 }
347
348 pub fn duration(&self) -> u64 {
350 self.duration
351 }
352
353 pub fn error(&self) -> i32 {
355 self.error
356 }
357
358 pub fn span_type(&self) -> &str {
360 &self.span_type
361 }
362
363 pub fn meta(&self) -> &FastHashMap<MetaString, MetaString> {
365 &self.meta
366 }
367
368 pub fn meta_mut(&mut self) -> &mut FastHashMap<MetaString, MetaString> {
370 &mut self.meta
371 }
372
373 pub fn metrics(&self) -> &FastHashMap<MetaString, f64> {
375 &self.metrics
376 }
377
378 pub fn metrics_mut(&mut self) -> &mut FastHashMap<MetaString, f64> {
380 &mut self.metrics
381 }
382
383 pub fn meta_struct(&self) -> &FastHashMap<MetaString, Vec<u8>> {
385 &self.meta_struct
386 }
387
388 pub fn span_links(&self) -> &[SpanLink] {
390 &self.span_links
391 }
392
393 pub fn span_events(&self) -> &[SpanEvent] {
395 &self.span_events
396 }
397}
398
399#[derive(Clone, Debug, PartialEq, Default)]
401pub struct SpanLink {
402 trace_id: u64,
404 trace_id_high: u64,
406 span_id: u64,
408 attributes: FastHashMap<MetaString, MetaString>,
410 tracestate: MetaString,
412 flags: u32,
414}
415
416impl SpanLink {
417 pub fn new(trace_id: u64, span_id: u64) -> Self {
419 Self {
420 trace_id,
421 span_id,
422 ..Self::default()
423 }
424 }
425
426 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
428 self.trace_id = trace_id;
429 self
430 }
431
432 pub fn with_trace_id_high(mut self, trace_id_high: u64) -> Self {
434 self.trace_id_high = trace_id_high;
435 self
436 }
437
438 pub fn with_span_id(mut self, span_id: u64) -> Self {
440 self.span_id = span_id;
441 self
442 }
443
444 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
446 self.attributes = attributes.into().unwrap_or_default();
447 self
448 }
449
450 pub fn with_tracestate(mut self, tracestate: impl Into<MetaString>) -> Self {
452 self.tracestate = tracestate.into();
453 self
454 }
455
456 pub fn with_flags(mut self, flags: u32) -> Self {
458 self.flags = flags;
459 self
460 }
461
462 pub fn trace_id(&self) -> u64 {
464 self.trace_id
465 }
466
467 pub fn trace_id_high(&self) -> u64 {
469 self.trace_id_high
470 }
471
472 pub fn span_id(&self) -> u64 {
474 self.span_id
475 }
476
477 pub fn attributes(&self) -> &FastHashMap<MetaString, MetaString> {
479 &self.attributes
480 }
481
482 pub fn tracestate(&self) -> &str {
484 &self.tracestate
485 }
486
487 pub fn flags(&self) -> u32 {
489 self.flags
490 }
491}
492
493#[derive(Clone, Debug, PartialEq, Default)]
495pub struct SpanEvent {
496 time_unix_nano: u64,
498 name: MetaString,
500 attributes: FastHashMap<MetaString, AttributeValue>,
502}
503
504impl SpanEvent {
505 pub fn new(time_unix_nano: u64, name: impl Into<MetaString>) -> Self {
507 Self {
508 time_unix_nano,
509 name: name.into(),
510 ..Self::default()
511 }
512 }
513
514 pub fn with_time_unix_nano(mut self, time_unix_nano: u64) -> Self {
516 self.time_unix_nano = time_unix_nano;
517 self
518 }
519
520 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
522 self.name = name.into();
523 self
524 }
525
526 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
528 self.attributes = attributes.into().unwrap_or_default();
529 self
530 }
531
532 pub fn time_unix_nano(&self) -> u64 {
534 self.time_unix_nano
535 }
536
537 pub fn name(&self) -> &str {
539 &self.name
540 }
541
542 pub fn attributes(&self) -> &FastHashMap<MetaString, AttributeValue> {
544 &self.attributes
545 }
546}
547
548#[derive(Clone, Debug, PartialEq)]
550pub enum AttributeValue {
551 String(MetaString),
553 Bool(bool),
555 Int(i64),
557 Double(f64),
559 Array(Vec<AttributeScalarValue>),
561}
562
563#[derive(Clone, Debug, PartialEq)]
565pub enum AttributeScalarValue {
566 String(MetaString),
568 Bool(bool),
570 Int(i64),
572 Double(f64),
574}