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 pub priority: Option<i32>,
23
24 pub decision_maker: Option<MetaString>,
31
32 pub otlp_sampling_rate: Option<MetaString>,
37}
38
39impl TraceSampling {
40 pub fn new(
42 dropped_trace: bool, priority: Option<i32>, decision_maker: Option<MetaString>,
43 otlp_sampling_rate: Option<MetaString>,
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(&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(span) {
112 has_match = true;
113 break;
114 }
115 }
116
117 if !has_match {
118 return 0;
119 }
120
121 self.spans.retain(|span| f(span));
122 self.spans.shrink_to_fit();
123 self.spans.len()
124 }
125
126 pub fn resource_tags(&self) -> &SharedTagSet {
128 &self.resource_tags
129 }
130
131 pub fn sampling(&self) -> Option<&TraceSampling> {
133 self.sampling.as_ref()
134 }
135
136 pub fn set_sampling(&mut self, sampling: Option<TraceSampling>) {
138 self.sampling = sampling;
139 }
140}
141
142#[derive(Clone, Debug, PartialEq, Default)]
144pub struct Span {
145 service: MetaString,
147 name: MetaString,
149 resource: MetaString,
151 trace_id: u64,
153 span_id: u64,
155 parent_id: u64,
157 start: u64,
159 duration: u64,
161 error: i32,
163 meta: FastHashMap<MetaString, MetaString>,
165 metrics: FastHashMap<MetaString, f64>,
167 span_type: MetaString,
169 meta_struct: FastHashMap<MetaString, Vec<u8>>,
171 span_links: Vec<SpanLink>,
173 span_events: Vec<SpanEvent>,
175}
176
177impl Span {
178 #[allow(clippy::too_many_arguments)]
180 pub fn new(
181 service: impl Into<MetaString>, name: impl Into<MetaString>, resource: impl Into<MetaString>,
182 span_type: impl Into<MetaString>, trace_id: u64, span_id: u64, parent_id: u64, start: u64, duration: u64,
183 error: i32,
184 ) -> Self {
185 Self {
186 service: service.into(),
187 name: name.into(),
188 resource: resource.into(),
189 span_type: span_type.into(),
190 trace_id,
191 span_id,
192 parent_id,
193 start,
194 duration,
195 error,
196 ..Self::default()
197 }
198 }
199
200 pub fn with_service(mut self, service: impl Into<MetaString>) -> Self {
202 self.service = service.into();
203 self
204 }
205
206 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
208 self.name = name.into();
209 self
210 }
211
212 pub fn with_resource(mut self, resource: impl Into<MetaString>) -> Self {
214 self.resource = resource.into();
215 self
216 }
217
218 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
220 self.trace_id = trace_id;
221 self
222 }
223
224 pub fn with_span_id(mut self, span_id: u64) -> Self {
226 self.span_id = span_id;
227 self
228 }
229
230 pub fn with_parent_id(mut self, parent_id: u64) -> Self {
232 self.parent_id = parent_id;
233 self
234 }
235
236 pub fn with_start(mut self, start: u64) -> Self {
238 self.start = start;
239 self
240 }
241
242 pub fn with_duration(mut self, duration: u64) -> Self {
244 self.duration = duration;
245 self
246 }
247
248 pub fn with_error(mut self, error: i32) -> Self {
250 self.error = error;
251 self
252 }
253
254 pub fn with_span_type(mut self, span_type: impl Into<MetaString>) -> Self {
256 self.span_type = span_type.into();
257 self
258 }
259
260 pub fn with_meta(mut self, meta: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
262 self.meta = meta.into().unwrap_or_default();
263 self
264 }
265
266 pub fn with_metrics(mut self, metrics: impl Into<Option<FastHashMap<MetaString, f64>>>) -> Self {
268 self.metrics = metrics.into().unwrap_or_default();
269 self
270 }
271
272 pub fn with_meta_struct(mut self, meta_struct: impl Into<Option<FastHashMap<MetaString, Vec<u8>>>>) -> Self {
274 self.meta_struct = meta_struct.into().unwrap_or_default();
275 self
276 }
277
278 pub fn with_span_links(mut self, span_links: impl Into<Option<Vec<SpanLink>>>) -> Self {
280 self.span_links = span_links.into().unwrap_or_default();
281 self
282 }
283
284 pub fn with_span_events(mut self, span_events: impl Into<Option<Vec<SpanEvent>>>) -> Self {
286 self.span_events = span_events.into().unwrap_or_default();
287 self
288 }
289
290 pub fn service(&self) -> &str {
292 &self.service
293 }
294
295 pub fn name(&self) -> &str {
297 &self.name
298 }
299
300 pub fn resource(&self) -> &str {
302 &self.resource
303 }
304
305 pub fn set_resource(&mut self, resource: impl Into<MetaString>) {
307 self.resource = resource.into();
308 }
309
310 pub fn trace_id(&self) -> u64 {
312 self.trace_id
313 }
314
315 pub fn span_id(&self) -> u64 {
317 self.span_id
318 }
319
320 pub fn parent_id(&self) -> u64 {
322 self.parent_id
323 }
324
325 pub fn start(&self) -> u64 {
327 self.start
328 }
329
330 pub fn duration(&self) -> u64 {
332 self.duration
333 }
334
335 pub fn error(&self) -> i32 {
337 self.error
338 }
339
340 pub fn span_type(&self) -> &str {
342 &self.span_type
343 }
344
345 pub fn meta(&self) -> &FastHashMap<MetaString, MetaString> {
347 &self.meta
348 }
349
350 pub fn meta_mut(&mut self) -> &mut FastHashMap<MetaString, MetaString> {
352 &mut self.meta
353 }
354
355 pub fn metrics(&self) -> &FastHashMap<MetaString, f64> {
357 &self.metrics
358 }
359
360 pub fn metrics_mut(&mut self) -> &mut FastHashMap<MetaString, f64> {
362 &mut self.metrics
363 }
364
365 pub fn meta_struct(&self) -> &FastHashMap<MetaString, Vec<u8>> {
367 &self.meta_struct
368 }
369
370 pub fn span_links(&self) -> &[SpanLink] {
372 &self.span_links
373 }
374
375 pub fn span_events(&self) -> &[SpanEvent] {
377 &self.span_events
378 }
379}
380
381#[derive(Clone, Debug, PartialEq, Default)]
383pub struct SpanLink {
384 trace_id: u64,
386 trace_id_high: u64,
388 span_id: u64,
390 attributes: FastHashMap<MetaString, MetaString>,
392 tracestate: MetaString,
394 flags: u32,
396}
397
398impl SpanLink {
399 pub fn new(trace_id: u64, span_id: u64) -> Self {
401 Self {
402 trace_id,
403 span_id,
404 ..Self::default()
405 }
406 }
407
408 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
410 self.trace_id = trace_id;
411 self
412 }
413
414 pub fn with_trace_id_high(mut self, trace_id_high: u64) -> Self {
416 self.trace_id_high = trace_id_high;
417 self
418 }
419
420 pub fn with_span_id(mut self, span_id: u64) -> Self {
422 self.span_id = span_id;
423 self
424 }
425
426 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
428 self.attributes = attributes.into().unwrap_or_default();
429 self
430 }
431
432 pub fn with_tracestate(mut self, tracestate: impl Into<MetaString>) -> Self {
434 self.tracestate = tracestate.into();
435 self
436 }
437
438 pub fn with_flags(mut self, flags: u32) -> Self {
440 self.flags = flags;
441 self
442 }
443
444 pub fn trace_id(&self) -> u64 {
446 self.trace_id
447 }
448
449 pub fn trace_id_high(&self) -> u64 {
451 self.trace_id_high
452 }
453
454 pub fn span_id(&self) -> u64 {
456 self.span_id
457 }
458
459 pub fn attributes(&self) -> &FastHashMap<MetaString, MetaString> {
461 &self.attributes
462 }
463
464 pub fn tracestate(&self) -> &str {
466 &self.tracestate
467 }
468
469 pub fn flags(&self) -> u32 {
471 self.flags
472 }
473}
474
475#[derive(Clone, Debug, PartialEq, Default)]
477pub struct SpanEvent {
478 time_unix_nano: u64,
480 name: MetaString,
482 attributes: FastHashMap<MetaString, AttributeValue>,
484}
485
486impl SpanEvent {
487 pub fn new(time_unix_nano: u64, name: impl Into<MetaString>) -> Self {
489 Self {
490 time_unix_nano,
491 name: name.into(),
492 ..Self::default()
493 }
494 }
495
496 pub fn with_time_unix_nano(mut self, time_unix_nano: u64) -> Self {
498 self.time_unix_nano = time_unix_nano;
499 self
500 }
501
502 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
504 self.name = name.into();
505 self
506 }
507
508 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
510 self.attributes = attributes.into().unwrap_or_default();
511 self
512 }
513
514 pub fn time_unix_nano(&self) -> u64 {
516 self.time_unix_nano
517 }
518
519 pub fn name(&self) -> &str {
521 &self.name
522 }
523
524 pub fn attributes(&self) -> &FastHashMap<MetaString, AttributeValue> {
526 &self.attributes
527 }
528}
529
530#[derive(Clone, Debug, PartialEq)]
532pub enum AttributeValue {
533 String(MetaString),
535 Bool(bool),
537 Int(i64),
539 Double(f64),
541 Array(Vec<AttributeScalarValue>),
543}
544
545#[derive(Clone, Debug, PartialEq)]
547pub enum AttributeScalarValue {
548 String(MetaString),
550 Bool(bool),
552 Int(i64),
554 Double(f64),
556}