zipkin_types/
lib.rs

1//  Copyright 2017 Palantir Technologies, Inc.
2//
3//  Licensed under the Apache License, Version 2.0 (the "License");
4//  you may not use this file except in compliance with the License.
5//  You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14
15//! Type definitions for Zipkin distributed trace information.
16//!
17//! This library corresponds to version 2 of the Zipkin [specification].
18//!
19//! # Serialization
20//!
21//! If the `serde` Cargo feature is enabled, `Annotation`, `Endpoint`, `Kind`, `Span`, `SpanId`, and
22//! `TraceId` implement `Serialize` and `Deserialize` in the standard Zipkin format.
23//!
24//! [specification]: https://github.com/openzipkin/zipkin-api/blob/master/zipkin2-api.yaml
25#![doc(html_root_url = "https://docs.rs/zipkin-types/0.1")]
26#![warn(missing_docs)]
27
28#[doc(inline)]
29pub use crate::annotation::Annotation;
30#[doc(inline)]
31pub use crate::endpoint::Endpoint;
32#[doc(inline)]
33pub use crate::span::{Kind, Span};
34#[doc(inline)]
35pub use crate::span_id::SpanId;
36#[doc(inline)]
37pub use crate::trace_id::TraceId;
38
39pub mod annotation;
40pub mod endpoint;
41pub mod span;
42pub mod span_id;
43pub mod trace_id;
44
45#[cfg(feature = "serde")]
46mod time_micros {
47    use serde::{Deserialize, Deserializer, Serialize, Serializer};
48    use std::time::{Duration, SystemTime, UNIX_EPOCH};
49
50    pub fn to_wire(time: &SystemTime) -> u64 {
51        super::duration_micros::to_wire(
52            &time
53                .duration_since(UNIX_EPOCH)
54                .unwrap_or(Duration::from_secs(0)),
55        )
56    }
57
58    pub fn from_wire(time: u64) -> SystemTime {
59        let duration = super::duration_micros::from_wire(time);
60        UNIX_EPOCH + duration
61    }
62
63    pub fn serialize<S>(time: &SystemTime, s: S) -> Result<S::Ok, S::Error>
64    where
65        S: Serializer,
66    {
67        to_wire(time).serialize(s)
68    }
69
70    pub fn deserialize<'de, D>(d: D) -> Result<SystemTime, D::Error>
71    where
72        D: Deserializer<'de>,
73    {
74        u64::deserialize(d).map(from_wire)
75    }
76}
77
78#[cfg(feature = "serde")]
79mod duration_micros {
80    use std::time::Duration;
81
82    pub fn to_wire(duration: &Duration) -> u64 {
83        let micros = duration.as_secs() * 1_000_000 + duration.subsec_nanos() as u64 / 1_000;
84        micros.max(1)
85    }
86
87    pub fn from_wire(duration: u64) -> Duration {
88        let seconds = duration / 1_000_000;
89        let subsec_nanos = (duration % 1_000_000) * 1_000;
90        Duration::new(seconds, subsec_nanos as u32)
91    }
92}
93
94#[cfg(feature = "serde")]
95mod opt_time_micros {
96    use serde::{Deserialize, Deserializer, Serializer};
97    use std::time::SystemTime;
98
99    pub fn serialize<S>(time: &Option<SystemTime>, s: S) -> Result<S::Ok, S::Error>
100    where
101        S: Serializer,
102    {
103        match *time {
104            Some(ref time) => s.serialize_some(&super::time_micros::to_wire(time)),
105            None => s.serialize_none(),
106        }
107    }
108
109    pub fn deserialize<'de, D>(d: D) -> Result<Option<SystemTime>, D::Error>
110    where
111        D: Deserializer<'de>,
112    {
113        Option::<u64>::deserialize(d).map(|o| o.map(super::time_micros::from_wire))
114    }
115}
116
117#[cfg(feature = "serde")]
118mod opt_duration_micros {
119    use serde::{Deserialize, Deserializer, Serializer};
120    use std::time::Duration;
121
122    pub fn serialize<S>(duration: &Option<Duration>, s: S) -> Result<S::Ok, S::Error>
123    where
124        S: Serializer,
125    {
126        match *duration {
127            Some(ref duration) => s.serialize_some(&super::duration_micros::to_wire(duration)),
128            None => s.serialize_none(),
129        }
130    }
131
132    pub fn deserialize<'de, D>(d: D) -> Result<Option<Duration>, D::Error>
133    where
134        D: Deserializer<'de>,
135    {
136        Option::<u64>::deserialize(d).map(|o| o.map(super::duration_micros::from_wire))
137    }
138}