Skip to content

Commit 04bbb15

Browse files
authored
tracing-opentelemetry: implement additional record types (bool, i64, u64) (#1007)
## Motivation While using `tracing-opentelemetry` I noticed all the data gets sent to the collector as a string. This implements the additional data types and (possibly) saves bandwidth. ## Solution I just implemented additional `fn record_$type(...)` methods of the `field::Visit` trait to `SpanEventVisitor` and `SpanAttributeVisitor`.
1 parent 0fcc9fd commit 04bbb15

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

tracing-opentelemetry/src/layer.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,51 @@ fn str_to_span_kind(s: &str) -> Option<api::SpanKind> {
103103
struct SpanEventVisitor<'a>(&'a mut api::Event);
104104

105105
impl<'a> field::Visit for SpanEventVisitor<'a> {
106+
/// Record events on the underlying OpenTelemetry [`Span`] from `bool` values.
107+
///
108+
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
109+
fn record_bool(&mut self, field: &field::Field, value: bool) {
110+
match field.name() {
111+
"message" => self.0.name = value.to_string(),
112+
// Skip fields that are actually log metadata that have already been handled
113+
#[cfg(feature = "tracing-log")]
114+
name if name.starts_with("log.") => (),
115+
name => {
116+
self.0.attributes.push(api::KeyValue::new(name, value));
117+
}
118+
}
119+
}
120+
121+
/// Record events on the underlying OpenTelemetry [`Span`] from `i64` values.
122+
///
123+
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
124+
fn record_i64(&mut self, field: &field::Field, value: i64) {
125+
match field.name() {
126+
"message" => self.0.name = value.to_string(),
127+
// Skip fields that are actually log metadata that have already been handled
128+
#[cfg(feature = "tracing-log")]
129+
name if name.starts_with("log.") => (),
130+
name => {
131+
self.0.attributes.push(api::KeyValue::new(name, value));
132+
}
133+
}
134+
}
135+
136+
/// Record events on the underlying OpenTelemetry [`Span`] from `u64` values.
137+
///
138+
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
139+
fn record_u64(&mut self, field: &field::Field, value: u64) {
140+
match field.name() {
141+
"message" => self.0.name = value.to_string(),
142+
// Skip fields that are actually log metadata that have already been handled
143+
#[cfg(feature = "tracing-log")]
144+
name if name.starts_with("log.") => (),
145+
name => {
146+
self.0.attributes.push(api::KeyValue::new(name, value));
147+
}
148+
}
149+
}
150+
106151
/// Record events on the underlying OpenTelemetry [`Span`] from `&str` values.
107152
///
108153
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
@@ -140,6 +185,42 @@ impl<'a> field::Visit for SpanEventVisitor<'a> {
140185
struct SpanAttributeVisitor<'a>(&'a mut api::SpanBuilder);
141186

142187
impl<'a> field::Visit for SpanAttributeVisitor<'a> {
188+
/// Set attributes on the underlying OpenTelemetry [`Span`] from `bool` values.
189+
///
190+
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
191+
fn record_bool(&mut self, field: &field::Field, value: bool) {
192+
let attribute = api::KeyValue::new(field.name(), value);
193+
if let Some(attributes) = &mut self.0.attributes {
194+
attributes.push(attribute);
195+
} else {
196+
self.0.attributes = Some(vec![attribute]);
197+
}
198+
}
199+
200+
/// Set attributes on the underlying OpenTelemetry [`Span`] from `i64` values.
201+
///
202+
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
203+
fn record_i64(&mut self, field: &field::Field, value: i64) {
204+
let attribute = api::KeyValue::new(field.name(), value);
205+
if let Some(attributes) = &mut self.0.attributes {
206+
attributes.push(attribute);
207+
} else {
208+
self.0.attributes = Some(vec![attribute]);
209+
}
210+
}
211+
212+
/// Set attributes on the underlying OpenTelemetry [`Span`] from `u64` values.
213+
///
214+
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
215+
fn record_u64(&mut self, field: &field::Field, value: u64) {
216+
let attribute = api::KeyValue::new(field.name(), value);
217+
if let Some(attributes) = &mut self.0.attributes {
218+
attributes.push(attribute);
219+
} else {
220+
self.0.attributes = Some(vec![attribute]);
221+
}
222+
}
223+
143224
/// Set attributes on the underlying OpenTelemetry [`Span`] from `&str` values.
144225
///
145226
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html

0 commit comments

Comments
 (0)