saluki_core/data_model/event/trace/
mod.rs1use saluki_common::collections::FastHashMap;
4use stringtheory::MetaString;
5
6#[derive(Clone, Debug, PartialEq)]
10pub struct Trace {
11 spans: Vec<Span>,
13}
14
15impl Trace {
16 pub fn new(spans: Vec<Span>) -> Self {
18 Self { spans }
19 }
20
21 pub fn spans(&self) -> &[Span] {
23 &self.spans
24 }
25
26 pub fn spans_mut(&mut self) -> &mut Vec<Span> {
28 &mut self.spans
29 }
30}
31
32#[derive(Clone, Debug, PartialEq, Default)]
34pub struct Span {
35 service: MetaString,
37 name: MetaString,
39 resource: MetaString,
41 trace_id: u64,
43 span_id: u64,
45 parent_id: u64,
47 start: i64,
49 duration: i64,
51 error: i32,
53 meta: FastHashMap<MetaString, MetaString>,
55 metrics: FastHashMap<MetaString, f64>,
57 span_type: MetaString,
59 meta_struct: FastHashMap<MetaString, Vec<u8>>,
61 span_links: Vec<SpanLink>,
63 span_events: Vec<SpanEvent>,
65}
66
67impl Span {
68 pub fn new(
70 service: impl Into<MetaString>, name: impl Into<MetaString>, resource: impl Into<MetaString>, trace_id: u64,
71 span_id: u64,
72 ) -> Self {
73 Self {
74 service: service.into(),
75 name: name.into(),
76 resource: resource.into(),
77 trace_id,
78 span_id,
79 ..Self::default()
80 }
81 }
82
83 pub fn with_service(mut self, service: impl Into<MetaString>) -> Self {
85 self.service = service.into();
86 self
87 }
88
89 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
91 self.name = name.into();
92 self
93 }
94
95 pub fn with_resource(mut self, resource: impl Into<MetaString>) -> Self {
97 self.resource = resource.into();
98 self
99 }
100
101 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
103 self.trace_id = trace_id;
104 self
105 }
106
107 pub fn with_span_id(mut self, span_id: u64) -> Self {
109 self.span_id = span_id;
110 self
111 }
112
113 pub fn with_parent_id(mut self, parent_id: u64) -> Self {
115 self.parent_id = parent_id;
116 self
117 }
118
119 pub fn with_start(mut self, start: i64) -> Self {
121 self.start = start;
122 self
123 }
124
125 pub fn with_duration(mut self, duration: i64) -> Self {
127 self.duration = duration;
128 self
129 }
130
131 pub fn with_error(mut self, error: i32) -> Self {
133 self.error = error;
134 self
135 }
136
137 pub fn with_span_type(mut self, span_type: impl Into<MetaString>) -> Self {
139 self.span_type = span_type.into();
140 self
141 }
142
143 pub fn with_meta(mut self, meta: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
145 self.meta = meta.into().unwrap_or_default();
146 self
147 }
148
149 pub fn with_metrics(mut self, metrics: impl Into<Option<FastHashMap<MetaString, f64>>>) -> Self {
151 self.metrics = metrics.into().unwrap_or_default();
152 self
153 }
154
155 pub fn with_meta_struct(mut self, meta_struct: impl Into<Option<FastHashMap<MetaString, Vec<u8>>>>) -> Self {
157 self.meta_struct = meta_struct.into().unwrap_or_default();
158 self
159 }
160
161 pub fn with_span_links(mut self, span_links: impl Into<Option<Vec<SpanLink>>>) -> Self {
163 self.span_links = span_links.into().unwrap_or_default();
164 self
165 }
166
167 pub fn with_span_events(mut self, span_events: impl Into<Option<Vec<SpanEvent>>>) -> Self {
169 self.span_events = span_events.into().unwrap_or_default();
170 self
171 }
172
173 pub fn service(&self) -> &str {
175 &self.service
176 }
177
178 pub fn name(&self) -> &str {
180 &self.name
181 }
182
183 pub fn resource(&self) -> &str {
185 &self.resource
186 }
187
188 pub fn trace_id(&self) -> u64 {
190 self.trace_id
191 }
192
193 pub fn span_id(&self) -> u64 {
195 self.span_id
196 }
197
198 pub fn parent_id(&self) -> u64 {
200 self.parent_id
201 }
202
203 pub fn start(&self) -> i64 {
205 self.start
206 }
207
208 pub fn duration(&self) -> i64 {
210 self.duration
211 }
212
213 pub fn error(&self) -> i32 {
215 self.error
216 }
217
218 pub fn span_type(&self) -> &str {
220 &self.span_type
221 }
222
223 pub fn meta(&self) -> &FastHashMap<MetaString, MetaString> {
225 &self.meta
226 }
227
228 pub fn metrics(&self) -> &FastHashMap<MetaString, f64> {
230 &self.metrics
231 }
232
233 pub fn meta_struct(&self) -> &FastHashMap<MetaString, Vec<u8>> {
235 &self.meta_struct
236 }
237
238 pub fn span_links(&self) -> &[SpanLink] {
240 &self.span_links
241 }
242
243 pub fn span_events(&self) -> &[SpanEvent] {
245 &self.span_events
246 }
247}
248
249#[derive(Clone, Debug, PartialEq, Default)]
251pub struct SpanLink {
252 trace_id: u64,
254 trace_id_high: u64,
256 span_id: u64,
258 attributes: FastHashMap<MetaString, MetaString>,
260 tracestate: MetaString,
262 flags: u32,
264}
265
266impl SpanLink {
267 pub fn new(trace_id: u64, span_id: u64) -> Self {
269 Self {
270 trace_id,
271 span_id,
272 ..Self::default()
273 }
274 }
275
276 pub fn with_trace_id(mut self, trace_id: u64) -> Self {
278 self.trace_id = trace_id;
279 self
280 }
281
282 pub fn with_trace_id_high(mut self, trace_id_high: u64) -> Self {
284 self.trace_id_high = trace_id_high;
285 self
286 }
287
288 pub fn with_span_id(mut self, span_id: u64) -> Self {
290 self.span_id = span_id;
291 self
292 }
293
294 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, MetaString>>>) -> Self {
296 self.attributes = attributes.into().unwrap_or_default();
297 self
298 }
299
300 pub fn with_tracestate(mut self, tracestate: impl Into<MetaString>) -> Self {
302 self.tracestate = tracestate.into();
303 self
304 }
305
306 pub fn with_flags(mut self, flags: u32) -> Self {
308 self.flags = flags;
309 self
310 }
311
312 pub fn trace_id(&self) -> u64 {
314 self.trace_id
315 }
316
317 pub fn trace_id_high(&self) -> u64 {
319 self.trace_id_high
320 }
321
322 pub fn span_id(&self) -> u64 {
324 self.span_id
325 }
326
327 pub fn attributes(&self) -> &FastHashMap<MetaString, MetaString> {
329 &self.attributes
330 }
331
332 pub fn tracestate(&self) -> &str {
334 &self.tracestate
335 }
336
337 pub fn flags(&self) -> u32 {
339 self.flags
340 }
341}
342
343#[derive(Clone, Debug, PartialEq, Default)]
345pub struct SpanEvent {
346 time_unix_nano: u64,
348 name: MetaString,
350 attributes: FastHashMap<MetaString, AttributeValue>,
352}
353
354impl SpanEvent {
355 pub fn new(time_unix_nano: u64, name: impl Into<MetaString>) -> Self {
357 Self {
358 time_unix_nano,
359 name: name.into(),
360 ..Self::default()
361 }
362 }
363
364 pub fn with_time_unix_nano(mut self, time_unix_nano: u64) -> Self {
366 self.time_unix_nano = time_unix_nano;
367 self
368 }
369
370 pub fn with_name(mut self, name: impl Into<MetaString>) -> Self {
372 self.name = name.into();
373 self
374 }
375
376 pub fn with_attributes(mut self, attributes: impl Into<Option<FastHashMap<MetaString, AttributeValue>>>) -> Self {
378 self.attributes = attributes.into().unwrap_or_default();
379 self
380 }
381
382 pub fn time_unix_nano(&self) -> u64 {
384 self.time_unix_nano
385 }
386
387 pub fn name(&self) -> &str {
389 &self.name
390 }
391
392 pub fn attributes(&self) -> &FastHashMap<MetaString, AttributeValue> {
394 &self.attributes
395 }
396}
397
398#[derive(Clone, Debug, PartialEq)]
400pub enum AttributeValue {
401 String(MetaString),
403 Bool(bool),
405 Int(i64),
407 Double(f64),
409 Array(Vec<AttributeScalarValue>),
411}
412
413#[derive(Clone, Debug, PartialEq)]
415pub enum AttributeScalarValue {
416 String(MetaString),
418 Bool(bool),
420 Int(i64),
422 Double(f64),
424}