saluki_core/data_model/event/trace/
mod.rs1use saluki_common::collections::FastHashMap;
4use saluki_context::tags::TagSet;
5use stringtheory::MetaString;
6#[derive(Clone, Debug, PartialEq)]
10pub struct Trace {
11 spans: Vec<Span>,
13 resource_tags: TagSet,
17}
18
19impl Trace {
20 pub fn new(spans: Vec<Span>, resource_tags: TagSet) -> Self {
22 Self { spans, resource_tags }
23 }
24
25 pub fn spans(&self) -> &[Span] {
27 &self.spans
28 }
29
30 pub fn spans_mut(&mut self) -> &mut [Span] {
32 &mut self.spans
33 }
34
35 pub fn resource_tags(&self) -> &TagSet {
37 &self.resource_tags
38 }
39}
40
41#[derive(Clone, Debug, PartialEq, Default)]
43pub struct Span {
44 service: MetaString,
46 name: MetaString,
48 resource: MetaString,
50 trace_id: u64,
52 span_id: u64,
54 parent_id: u64,
56 start: u64,
58 duration: u64,
60 error: i32,
62 meta: FastHashMap<MetaString, MetaString>,
64 metrics: FastHashMap<MetaString, f64>,
66 span_type: MetaString,
68 meta_struct: FastHashMap<MetaString, Vec<u8>>,
70 span_links: Vec<SpanLink>,
72 span_events: Vec<SpanEvent>,
74}
75
76impl Span {
77 #[allow(clippy::too_many_arguments)]
79 pub fn new(
80 service: impl Into<MetaString>, name: impl Into<MetaString>, resource: impl Into<MetaString>,
81 span_type: impl Into<MetaString>, trace_id: u64, span_id: u64, parent_id: u64, start: u64, duration: u64,
82 error: i32,
83 ) -> Self {
84 Self {
85 service: service.into(),
86 name: name.into(),
87 resource: resource.into(),
88 span_type: span_type.into(),
89 trace_id,
90 span_id,
91 parent_id,
92 start,
93 duration,
94 error,
95 ..Self::default()
96 }
97 }
98
99 pub fn with_service(mut self, service: impl Into<MetaString>) -> Self {
101 self.service = service.into();
102 self
103 }
104
105 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
107 self.name = name.into();
108 self
109 }
110
111 pub fn with_resource(mut self, resource: impl Into<MetaString>) -> Self {
113 self.resource = resource.into();
114 self
115 }
116
117 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
119 self.trace_id = trace_id;
120 self
121 }
122
123 pub fn with_span_id(mut self, span_id: u64) -> Self {
125 self.span_id = span_id;
126 self
127 }
128
129 pub fn with_parent_id(mut self, parent_id: u64) -> Self {
131 self.parent_id = parent_id;
132 self
133 }
134
135 pub fn with_start(mut self, start: u64) -> Self {
137 self.start = start;
138 self
139 }
140
141 pub fn with_duration(mut self, duration: u64) -> Self {
143 self.duration = duration;
144 self
145 }
146
147 pub fn with_error(mut self, error: i32) -> Self {
149 self.error = error;
150 self
151 }
152
153 pub fn with_span_type(mut self, span_type: impl Into<MetaString>) -> Self {
155 self.span_type = span_type.into();
156 self
157 }
158
159 pub fn with_meta(mut self, meta: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
161 self.meta = meta.into().unwrap_or_default();
162 self
163 }
164
165 pub fn with_metrics(mut self, metrics: impl Into<Option<FastHashMap<MetaString, f64>>>) -> Self {
167 self.metrics = metrics.into().unwrap_or_default();
168 self
169 }
170
171 pub fn with_meta_struct(mut self, meta_struct: impl Into<Option<FastHashMap<MetaString, Vec<u8>>>>) -> Self {
173 self.meta_struct = meta_struct.into().unwrap_or_default();
174 self
175 }
176
177 pub fn with_span_links(mut self, span_links: impl Into<Option<Vec<SpanLink>>>) -> Self {
179 self.span_links = span_links.into().unwrap_or_default();
180 self
181 }
182
183 pub fn with_span_events(mut self, span_events: impl Into<Option<Vec<SpanEvent>>>) -> Self {
185 self.span_events = span_events.into().unwrap_or_default();
186 self
187 }
188
189 pub fn service(&self) -> &str {
191 &self.service
192 }
193
194 pub fn name(&self) -> &str {
196 &self.name
197 }
198
199 pub fn resource(&self) -> &str {
201 &self.resource
202 }
203
204 pub fn trace_id(&self) -> u64 {
206 self.trace_id
207 }
208
209 pub fn span_id(&self) -> u64 {
211 self.span_id
212 }
213
214 pub fn parent_id(&self) -> u64 {
216 self.parent_id
217 }
218
219 pub fn start(&self) -> u64 {
221 self.start
222 }
223
224 pub fn duration(&self) -> u64 {
226 self.duration
227 }
228
229 pub fn error(&self) -> i32 {
231 self.error
232 }
233
234 pub fn span_type(&self) -> &str {
236 &self.span_type
237 }
238
239 pub fn meta(&self) -> &FastHashMap<MetaString, MetaString> {
241 &self.meta
242 }
243
244 pub fn meta_mut(&mut self) -> &mut FastHashMap<MetaString, MetaString> {
246 &mut self.meta
247 }
248
249 pub fn metrics(&self) -> &FastHashMap<MetaString, f64> {
251 &self.metrics
252 }
253
254 pub fn meta_struct(&self) -> &FastHashMap<MetaString, Vec<u8>> {
256 &self.meta_struct
257 }
258
259 pub fn span_links(&self) -> &[SpanLink] {
261 &self.span_links
262 }
263
264 pub fn span_events(&self) -> &[SpanEvent] {
266 &self.span_events
267 }
268}
269
270#[derive(Clone, Debug, PartialEq, Default)]
272pub struct SpanLink {
273 trace_id: u64,
275 trace_id_high: u64,
277 span_id: u64,
279 attributes: FastHashMap<MetaString, MetaString>,
281 tracestate: MetaString,
283 flags: u32,
285}
286
287impl SpanLink {
288 pub fn new(trace_id: u64, span_id: u64) -> Self {
290 Self {
291 trace_id,
292 span_id,
293 ..Self::default()
294 }
295 }
296
297 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
299 self.trace_id = trace_id;
300 self
301 }
302
303 pub fn with_trace_id_high(mut self, trace_id_high: u64) -> Self {
305 self.trace_id_high = trace_id_high;
306 self
307 }
308
309 pub fn with_span_id(mut self, span_id: u64) -> Self {
311 self.span_id = span_id;
312 self
313 }
314
315 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
317 self.attributes = attributes.into().unwrap_or_default();
318 self
319 }
320
321 pub fn with_tracestate(mut self, tracestate: impl Into<MetaString>) -> Self {
323 self.tracestate = tracestate.into();
324 self
325 }
326
327 pub fn with_flags(mut self, flags: u32) -> Self {
329 self.flags = flags;
330 self
331 }
332
333 pub fn trace_id(&self) -> u64 {
335 self.trace_id
336 }
337
338 pub fn trace_id_high(&self) -> u64 {
340 self.trace_id_high
341 }
342
343 pub fn span_id(&self) -> u64 {
345 self.span_id
346 }
347
348 pub fn attributes(&self) -> &FastHashMap<MetaString, MetaString> {
350 &self.attributes
351 }
352
353 pub fn tracestate(&self) -> &str {
355 &self.tracestate
356 }
357
358 pub fn flags(&self) -> u32 {
360 self.flags
361 }
362}
363
364#[derive(Clone, Debug, PartialEq, Default)]
366pub struct SpanEvent {
367 time_unix_nano: u64,
369 name: MetaString,
371 attributes: FastHashMap<MetaString, AttributeValue>,
373}
374
375impl SpanEvent {
376 pub fn new(time_unix_nano: u64, name: impl Into<MetaString>) -> Self {
378 Self {
379 time_unix_nano,
380 name: name.into(),
381 ..Self::default()
382 }
383 }
384
385 pub fn with_time_unix_nano(mut self, time_unix_nano: u64) -> Self {
387 self.time_unix_nano = time_unix_nano;
388 self
389 }
390
391 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
393 self.name = name.into();
394 self
395 }
396
397 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
399 self.attributes = attributes.into().unwrap_or_default();
400 self
401 }
402
403 pub fn time_unix_nano(&self) -> u64 {
405 self.time_unix_nano
406 }
407
408 pub fn name(&self) -> &str {
410 &self.name
411 }
412
413 pub fn attributes(&self) -> &FastHashMap<MetaString, AttributeValue> {
415 &self.attributes
416 }
417}
418
419#[derive(Clone, Debug, PartialEq)]
421pub enum AttributeValue {
422 String(MetaString),
424 Bool(bool),
426 Int(i64),
428 Double(f64),
430 Array(Vec<AttributeScalarValue>),
432}
433
434#[derive(Clone, Debug, PartialEq)]
436pub enum AttributeScalarValue {
437 String(MetaString),
439 Bool(bool),
441 Int(i64),
443 Double(f64),
445}