1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
use conjure_object::serde::{ser, de};
use conjure_object::serde::ser::SerializeStruct as SerializeStruct_;
use std::fmt;
///A Zipkin-compatible Span object.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Span {
    trace_id: String,
    id: String,
    name: String,
    parent_id: Option<String>,
    timestamp: conjure_object::SafeLong,
    duration: conjure_object::SafeLong,
    annotations: Vec<super::Annotation>,
    tags: std::collections::BTreeMap<String, String>,
}
impl Span {
    /// Returns a new builder.
    #[inline]
    pub fn builder() -> BuilderStage0 {
        Default::default()
    }
    ///16-digit hex trace identifier
    #[inline]
    pub fn trace_id(&self) -> &str {
        &*self.trace_id
    }
    ///16-digit hex span identifier
    #[inline]
    pub fn id(&self) -> &str {
        &*self.id
    }
    ///Name of the span (typically the operation/RPC/method name for corresponding to this span)
    #[inline]
    pub fn name(&self) -> &str {
        &*self.name
    }
    ///16-digit hex identifer of the parent span
    #[inline]
    pub fn parent_id(&self) -> Option<&str> {
        self.parent_id.as_ref().map(|o| &**o)
    }
    ///Timestamp of the start of this span (epoch microsecond value)
    #[inline]
    pub fn timestamp(&self) -> conjure_object::SafeLong {
        self.timestamp
    }
    ///Duration of this span (microseconds)
    #[inline]
    pub fn duration(&self) -> conjure_object::SafeLong {
        self.duration
    }
    #[inline]
    pub fn annotations(&self) -> &[super::Annotation] {
        &*self.annotations
    }
    ///Additional dimensions that describe the instance of the trace span
    #[inline]
    pub fn tags(&self) -> &std::collections::BTreeMap<String, String> {
        &self.tags
    }
}
impl Default for BuilderStage0 {
    #[inline]
    fn default() -> Self {
        BuilderStage0 {}
    }
}
impl From<Span> for BuilderStage5 {
    #[inline]
    fn from(value: Span) -> Self {
        BuilderStage5 {
            trace_id: value.trace_id,
            id: value.id,
            name: value.name,
            parent_id: value.parent_id,
            timestamp: value.timestamp,
            duration: value.duration,
            annotations: value.annotations,
            tags: value.tags,
        }
    }
}
///The stage 0 builder for the [`Span`] type
#[derive(Debug, Clone)]
pub struct BuilderStage0 {}
impl BuilderStage0 {
    ///16-digit hex trace identifier
    #[inline]
    pub fn trace_id<T>(self, trace_id: T) -> BuilderStage1
    where
        T: Into<String>,
    {
        BuilderStage1 {
            trace_id: trace_id.into(),
        }
    }
}
///The stage 1 builder for the [`Span`] type
#[derive(Debug, Clone)]
pub struct BuilderStage1 {
    trace_id: String,
}
impl BuilderStage1 {
    ///16-digit hex span identifier
    #[inline]
    pub fn id<T>(self, id: T) -> BuilderStage2
    where
        T: Into<String>,
    {
        BuilderStage2 {
            trace_id: self.trace_id,
            id: id.into(),
        }
    }
}
///The stage 2 builder for the [`Span`] type
#[derive(Debug, Clone)]
pub struct BuilderStage2 {
    trace_id: String,
    id: String,
}
impl BuilderStage2 {
    ///Name of the span (typically the operation/RPC/method name for corresponding to this span)
    #[inline]
    pub fn name<T>(self, name: T) -> BuilderStage3
    where
        T: Into<String>,
    {
        BuilderStage3 {
            trace_id: self.trace_id,
            id: self.id,
            name: name.into(),
        }
    }
}
///The stage 3 builder for the [`Span`] type
#[derive(Debug, Clone)]
pub struct BuilderStage3 {
    trace_id: String,
    id: String,
    name: String,
}
impl BuilderStage3 {
    ///Timestamp of the start of this span (epoch microsecond value)
    #[inline]
    pub fn timestamp(self, timestamp: conjure_object::SafeLong) -> BuilderStage4 {
        BuilderStage4 {
            trace_id: self.trace_id,
            id: self.id,
            name: self.name,
            timestamp: timestamp,
        }
    }
}
///The stage 4 builder for the [`Span`] type
#[derive(Debug, Clone)]
pub struct BuilderStage4 {
    trace_id: String,
    id: String,
    name: String,
    timestamp: conjure_object::SafeLong,
}
impl BuilderStage4 {
    ///Duration of this span (microseconds)
    #[inline]
    pub fn duration(self, duration: conjure_object::SafeLong) -> BuilderStage5 {
        BuilderStage5 {
            trace_id: self.trace_id,
            id: self.id,
            name: self.name,
            timestamp: self.timestamp,
            duration: duration,
            parent_id: Default::default(),
            annotations: Default::default(),
            tags: Default::default(),
        }
    }
}
///The stage 5 builder for the [`Span`] type
#[derive(Debug, Clone)]
pub struct BuilderStage5 {
    trace_id: String,
    id: String,
    name: String,
    timestamp: conjure_object::SafeLong,
    duration: conjure_object::SafeLong,
    parent_id: Option<String>,
    annotations: Vec<super::Annotation>,
    tags: std::collections::BTreeMap<String, String>,
}
impl BuilderStage5 {
    ///16-digit hex trace identifier
    #[inline]
    pub fn trace_id<T>(mut self, trace_id: T) -> Self
    where
        T: Into<String>,
    {
        self.trace_id = trace_id.into();
        self
    }
    ///16-digit hex span identifier
    #[inline]
    pub fn id<T>(mut self, id: T) -> Self
    where
        T: Into<String>,
    {
        self.id = id.into();
        self
    }
    ///Name of the span (typically the operation/RPC/method name for corresponding to this span)
    #[inline]
    pub fn name<T>(mut self, name: T) -> Self
    where
        T: Into<String>,
    {
        self.name = name.into();
        self
    }
    ///Timestamp of the start of this span (epoch microsecond value)
    #[inline]
    pub fn timestamp(mut self, timestamp: conjure_object::SafeLong) -> Self {
        self.timestamp = timestamp;
        self
    }
    ///Duration of this span (microseconds)
    #[inline]
    pub fn duration(mut self, duration: conjure_object::SafeLong) -> Self {
        self.duration = duration;
        self
    }
    ///16-digit hex identifer of the parent span
    #[inline]
    pub fn parent_id<T>(mut self, parent_id: T) -> Self
    where
        T: Into<Option<String>>,
    {
        self.parent_id = parent_id.into();
        self
    }
    #[inline]
    pub fn annotations<T>(mut self, annotations: T) -> Self
    where
        T: IntoIterator<Item = super::Annotation>,
    {
        self.annotations = annotations.into_iter().collect();
        self
    }
    #[inline]
    pub fn extend_annotations<T>(mut self, annotations: T) -> Self
    where
        T: IntoIterator<Item = super::Annotation>,
    {
        self.annotations.extend(annotations);
        self
    }
    #[inline]
    pub fn push_annotations(mut self, value: super::Annotation) -> Self {
        self.annotations.push(value);
        self
    }
    ///Additional dimensions that describe the instance of the trace span
    #[inline]
    pub fn tags<T>(mut self, tags: T) -> Self
    where
        T: IntoIterator<Item = (String, String)>,
    {
        self.tags = tags.into_iter().collect();
        self
    }
    ///Additional dimensions that describe the instance of the trace span
    #[inline]
    pub fn extend_tags<T>(mut self, tags: T) -> Self
    where
        T: IntoIterator<Item = (String, String)>,
    {
        self.tags.extend(tags);
        self
    }
    ///Additional dimensions that describe the instance of the trace span
    #[inline]
    pub fn insert_tags<K, V>(mut self, key: K, value: V) -> Self
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.tags.insert(key.into(), value.into());
        self
    }
    /// Consumes the builder, constructing a new instance of the type.
    #[inline]
    pub fn build(self) -> Span {
        Span {
            trace_id: self.trace_id,
            id: self.id,
            name: self.name,
            parent_id: self.parent_id,
            timestamp: self.timestamp,
            duration: self.duration,
            annotations: self.annotations,
            tags: self.tags,
        }
    }
}
impl ser::Serialize for Span {
    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        let mut size = 5usize;
        let skip_parent_id = self.parent_id.is_none();
        if !skip_parent_id {
            size += 1;
        }
        let skip_annotations = self.annotations.is_empty();
        if !skip_annotations {
            size += 1;
        }
        let skip_tags = self.tags.is_empty();
        if !skip_tags {
            size += 1;
        }
        let mut s = s.serialize_struct("Span", size)?;
        s.serialize_field("traceId", &self.trace_id)?;
        s.serialize_field("id", &self.id)?;
        s.serialize_field("name", &self.name)?;
        if skip_parent_id {
            s.skip_field("parentId")?;
        } else {
            s.serialize_field("parentId", &self.parent_id)?;
        }
        s.serialize_field("timestamp", &self.timestamp)?;
        s.serialize_field("duration", &self.duration)?;
        if skip_annotations {
            s.skip_field("annotations")?;
        } else {
            s.serialize_field("annotations", &self.annotations)?;
        }
        if skip_tags {
            s.skip_field("tags")?;
        } else {
            s.serialize_field("tags", &self.tags)?;
        }
        s.end()
    }
}
impl<'de> de::Deserialize<'de> for Span {
    fn deserialize<D>(d: D) -> Result<Span, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_struct(
            "Span",
            &[
                "traceId",
                "id",
                "name",
                "parentId",
                "timestamp",
                "duration",
                "annotations",
                "tags",
            ],
            Visitor_,
        )
    }
}
struct Visitor_;
impl<'de> de::Visitor<'de> for Visitor_ {
    type Value = Span;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("map")
    }
    fn visit_map<A>(self, mut map_: A) -> Result<Span, A::Error>
    where
        A: de::MapAccess<'de>,
    {
        let mut trace_id = None;
        let mut id = None;
        let mut name = None;
        let mut parent_id = None;
        let mut timestamp = None;
        let mut duration = None;
        let mut annotations = None;
        let mut tags = None;
        while let Some(field_) = map_.next_key()? {
            match field_ {
                Field_::TraceId => trace_id = Some(map_.next_value()?),
                Field_::Id => id = Some(map_.next_value()?),
                Field_::Name => name = Some(map_.next_value()?),
                Field_::ParentId => parent_id = Some(map_.next_value()?),
                Field_::Timestamp => timestamp = Some(map_.next_value()?),
                Field_::Duration => duration = Some(map_.next_value()?),
                Field_::Annotations => annotations = Some(map_.next_value()?),
                Field_::Tags => tags = Some(map_.next_value()?),
                Field_::Unknown_ => {
                    map_.next_value::<de::IgnoredAny>()?;
                }
            }
        }
        let trace_id = match trace_id {
            Some(v) => v,
            None => return Err(de::Error::missing_field("traceId")),
        };
        let id = match id {
            Some(v) => v,
            None => return Err(de::Error::missing_field("id")),
        };
        let name = match name {
            Some(v) => v,
            None => return Err(de::Error::missing_field("name")),
        };
        let parent_id = match parent_id {
            Some(v) => v,
            None => Default::default(),
        };
        let timestamp = match timestamp {
            Some(v) => v,
            None => return Err(de::Error::missing_field("timestamp")),
        };
        let duration = match duration {
            Some(v) => v,
            None => return Err(de::Error::missing_field("duration")),
        };
        let annotations = match annotations {
            Some(v) => v,
            None => Default::default(),
        };
        let tags = match tags {
            Some(v) => v,
            None => Default::default(),
        };
        Ok(Span {
            trace_id,
            id,
            name,
            parent_id,
            timestamp,
            duration,
            annotations,
            tags,
        })
    }
}
enum Field_ {
    TraceId,
    Id,
    Name,
    ParentId,
    Timestamp,
    Duration,
    Annotations,
    Tags,
    Unknown_,
}
impl<'de> de::Deserialize<'de> for Field_ {
    fn deserialize<D>(d: D) -> Result<Field_, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        d.deserialize_str(FieldVisitor_)
    }
}
struct FieldVisitor_;
impl<'de> de::Visitor<'de> for FieldVisitor_ {
    type Value = Field_;
    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str("string")
    }
    fn visit_str<E>(self, value: &str) -> Result<Field_, E>
    where
        E: de::Error,
    {
        let v = match value {
            "traceId" => Field_::TraceId,
            "id" => Field_::Id,
            "name" => Field_::Name,
            "parentId" => Field_::ParentId,
            "timestamp" => Field_::Timestamp,
            "duration" => Field_::Duration,
            "annotations" => Field_::Annotations,
            "tags" => Field_::Tags,
            _ => Field_::Unknown_,
        };
        Ok(v)
    }
}