opentelemetry_proto/transform/
common.rs

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
#[cfg(all(
    feature = "gen-tonic-messages",
    any(feature = "trace", feature = "metrics", feature = "logs")
))]
use std::time::{Duration, SystemTime, UNIX_EPOCH};

#[cfg(all(
    feature = "gen-tonic-messages",
    any(feature = "trace", feature = "metrics", feature = "logs")
))]
pub(crate) fn to_nanos(time: SystemTime) -> u64 {
    time.duration_since(UNIX_EPOCH)
        .unwrap_or_else(|_| Duration::from_secs(0))
        .as_nanos() as u64
}

#[cfg(feature = "gen-tonic-messages")]
pub mod tonic {
    use crate::proto::tonic::common::v1::{
        any_value, AnyValue, ArrayValue, InstrumentationScope, KeyValue,
    };
    use opentelemetry::{Array, Value};
    use std::borrow::Cow;

    #[cfg(any(feature = "trace", feature = "logs"))]
    #[derive(Debug, Default)]
    pub struct ResourceAttributesWithSchema {
        pub attributes: Attributes,
        pub schema_url: Option<String>,
    }

    #[cfg(any(feature = "trace", feature = "logs"))]
    impl From<&opentelemetry_sdk::Resource> for ResourceAttributesWithSchema {
        fn from(resource: &opentelemetry_sdk::Resource) -> Self {
            ResourceAttributesWithSchema {
                attributes: resource_attributes(resource),
                schema_url: resource.schema_url().map(ToString::to_string),
            }
        }
    }

    #[cfg(any(feature = "trace", feature = "logs"))]
    use opentelemetry_sdk::Resource;

    impl
        From<(
            opentelemetry::InstrumentationScope,
            Option<Cow<'static, str>>,
        )> for InstrumentationScope
    {
        fn from(
            data: (
                opentelemetry::InstrumentationScope,
                Option<Cow<'static, str>>,
            ),
        ) -> Self {
            let (library, target) = data;
            if let Some(t) = target {
                InstrumentationScope {
                    name: t.to_string(),
                    version: String::new(),
                    attributes: vec![],
                    ..Default::default()
                }
            } else {
                InstrumentationScope {
                    name: library.name().to_owned(),
                    version: library.version().map(ToOwned::to_owned).unwrap_or_default(),
                    attributes: Attributes::from(library.attributes().cloned()).0,
                    ..Default::default()
                }
            }
        }
    }

    impl
        From<(
            &opentelemetry::InstrumentationScope,
            Option<Cow<'static, str>>,
        )> for InstrumentationScope
    {
        fn from(
            data: (
                &opentelemetry::InstrumentationScope,
                Option<Cow<'static, str>>,
            ),
        ) -> Self {
            let (library, target) = data;
            if let Some(t) = target {
                InstrumentationScope {
                    name: t.to_string(),
                    version: String::new(),
                    attributes: vec![],
                    ..Default::default()
                }
            } else {
                InstrumentationScope {
                    name: library.name().to_owned(),
                    version: library.version().map(ToOwned::to_owned).unwrap_or_default(),
                    attributes: Attributes::from(library.attributes().cloned()).0,
                    ..Default::default()
                }
            }
        }
    }

    /// Wrapper type for Vec<`KeyValue`>
    #[derive(Default, Debug)]
    pub struct Attributes(pub ::std::vec::Vec<crate::proto::tonic::common::v1::KeyValue>);

    impl<I: IntoIterator<Item = opentelemetry::KeyValue>> From<I> for Attributes {
        fn from(kvs: I) -> Self {
            Attributes(
                kvs.into_iter()
                    .map(|api_kv| KeyValue {
                        key: api_kv.key.as_str().to_string(),
                        value: Some(api_kv.value.into()),
                    })
                    .collect(),
            )
        }
    }

    #[cfg(feature = "logs")]
    impl<K: Into<String>, V: Into<AnyValue>> FromIterator<(K, V)> for Attributes {
        fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
            Attributes(
                iter.into_iter()
                    .map(|(k, v)| KeyValue {
                        key: k.into(),
                        value: Some(v.into()),
                    })
                    .collect(),
            )
        }
    }

    impl From<Value> for AnyValue {
        fn from(value: Value) -> Self {
            AnyValue {
                value: match value {
                    Value::Bool(val) => Some(any_value::Value::BoolValue(val)),
                    Value::I64(val) => Some(any_value::Value::IntValue(val)),
                    Value::F64(val) => Some(any_value::Value::DoubleValue(val)),
                    Value::String(val) => Some(any_value::Value::StringValue(val.to_string())),
                    Value::Array(array) => Some(any_value::Value::ArrayValue(match array {
                        Array::Bool(vals) => array_into_proto(vals),
                        Array::I64(vals) => array_into_proto(vals),
                        Array::F64(vals) => array_into_proto(vals),
                        Array::String(vals) => array_into_proto(vals),
                        _ => unreachable!("Nonexistent array type"), // Needs to be updated when new array types are added
                    })),
                    _ => unreachable!("Nonexistent value type"), // Needs to be updated when new value types are added
                },
            }
        }
    }

    fn array_into_proto<T>(vals: Vec<T>) -> ArrayValue
    where
        Value: From<T>,
    {
        let values = vals
            .into_iter()
            .map(|val| AnyValue::from(Value::from(val)))
            .collect();

        ArrayValue { values }
    }

    #[cfg(any(feature = "trace", feature = "logs"))]
    pub(crate) fn resource_attributes(resource: &Resource) -> Attributes {
        resource
            .iter()
            .map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone()))
            .collect::<Vec<_>>()
            .into()
    }
}