strands/telemetry/
types.rs1#[cfg(feature = "otel")]
4use opentelemetry::{Array, StringValue};
5
6#[derive(Debug, Clone, PartialEq)]
16#[non_exhaustive]
17pub enum AttributeValue {
18 String(String),
20 Bool(bool),
22 I64(i64),
24 F64(f64),
26 StringArray(Vec<String>),
28 BoolArray(Vec<bool>),
30 I64Array(Vec<i64>),
32 F64Array(Vec<f64>),
34}
35
36impl From<&str> for AttributeValue {
37 fn from(value: &str) -> Self {
38 Self::String(value.to_owned())
39 }
40}
41
42impl From<String> for AttributeValue {
43 fn from(value: String) -> Self {
44 Self::String(value)
45 }
46}
47
48impl From<&String> for AttributeValue {
49 fn from(value: &String) -> Self {
50 Self::String(value.clone())
51 }
52}
53
54impl From<bool> for AttributeValue {
55 fn from(value: bool) -> Self {
56 Self::Bool(value)
57 }
58}
59
60impl From<i64> for AttributeValue {
61 fn from(value: i64) -> Self {
62 Self::I64(value)
63 }
64}
65
66impl From<i32> for AttributeValue {
67 fn from(value: i32) -> Self {
68 Self::I64(i64::from(value))
69 }
70}
71
72impl From<u32> for AttributeValue {
73 fn from(value: u32) -> Self {
74 Self::I64(i64::from(value))
75 }
76}
77
78impl From<u64> for AttributeValue {
79 fn from(value: u64) -> Self {
81 Self::I64(i64::try_from(value).unwrap_or(i64::MAX))
82 }
83}
84
85impl From<f64> for AttributeValue {
86 fn from(value: f64) -> Self {
87 Self::F64(value)
88 }
89}
90
91impl From<Vec<String>> for AttributeValue {
92 fn from(value: Vec<String>) -> Self {
93 Self::StringArray(value)
94 }
95}
96
97impl From<Vec<&str>> for AttributeValue {
98 fn from(value: Vec<&str>) -> Self {
99 Self::StringArray(value.into_iter().map(str::to_owned).collect())
100 }
101}
102
103impl From<Vec<bool>> for AttributeValue {
104 fn from(value: Vec<bool>) -> Self {
105 Self::BoolArray(value)
106 }
107}
108
109impl From<Vec<i64>> for AttributeValue {
110 fn from(value: Vec<i64>) -> Self {
111 Self::I64Array(value)
112 }
113}
114
115impl From<Vec<f64>> for AttributeValue {
116 fn from(value: Vec<f64>) -> Self {
117 Self::F64Array(value)
118 }
119}
120
121#[cfg(feature = "otel")]
122impl From<AttributeValue> for opentelemetry::Value {
123 fn from(value: AttributeValue) -> Self {
124 match value {
125 AttributeValue::String(value) => Self::String(value.into()),
126 AttributeValue::Bool(value) => Self::Bool(value),
127 AttributeValue::I64(value) => Self::I64(value),
128 AttributeValue::F64(value) => Self::F64(value),
129 AttributeValue::StringArray(values) => {
130 Self::Array(Array::String(values.into_iter().map(StringValue::from).collect()))
131 }
132 AttributeValue::BoolArray(values) => Self::Array(Array::Bool(values)),
133 AttributeValue::I64Array(values) => Self::Array(Array::I64(values)),
134 AttributeValue::F64Array(values) => Self::Array(Array::F64(values)),
135 }
136 }
137}