Skip to content

Commit

Permalink
tracing-opentelemetry: implement additional record types (bool, i64, …
Browse files Browse the repository at this point in the history
…u64) (#1007) (#1013)

This backports #1007 to v0.1.x.

## 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`.

(cherry picked from commit 04bbb15)
  • Loading branch information
lovesegfault authored Oct 5, 2020
1 parent be2f80a commit ca9b668
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tracing-opentelemetry/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,51 @@ fn str_to_span_kind(s: &str) -> Option<api::SpanKind> {
struct SpanEventVisitor<'a>(&'a mut api::Event);

impl<'a> field::Visit for SpanEventVisitor<'a> {
/// Record events on the underlying OpenTelemetry [`Span`] from `bool` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
fn record_bool(&mut self, field: &field::Field, value: bool) {
match field.name() {
"message" => self.0.name = value.to_string(),
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => (),
name => {
self.0.attributes.push(api::KeyValue::new(name, value));
}
}
}

/// Record events on the underlying OpenTelemetry [`Span`] from `i64` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
fn record_i64(&mut self, field: &field::Field, value: i64) {
match field.name() {
"message" => self.0.name = value.to_string(),
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => (),
name => {
self.0.attributes.push(api::KeyValue::new(name, value));
}
}
}

/// Record events on the underlying OpenTelemetry [`Span`] from `u64` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
fn record_u64(&mut self, field: &field::Field, value: u64) {
match field.name() {
"message" => self.0.name = value.to_string(),
// Skip fields that are actually log metadata that have already been handled
#[cfg(feature = "tracing-log")]
name if name.starts_with("log.") => (),
name => {
self.0.attributes.push(api::KeyValue::new(name, value));
}
}
}

/// Record events on the underlying OpenTelemetry [`Span`] from `&str` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
Expand Down Expand Up @@ -140,6 +185,42 @@ impl<'a> field::Visit for SpanEventVisitor<'a> {
struct SpanAttributeVisitor<'a>(&'a mut api::SpanBuilder);

impl<'a> field::Visit for SpanAttributeVisitor<'a> {
/// Set attributes on the underlying OpenTelemetry [`Span`] from `bool` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
fn record_bool(&mut self, field: &field::Field, value: bool) {
let attribute = api::KeyValue::new(field.name(), value);
if let Some(attributes) = &mut self.0.attributes {
attributes.push(attribute);
} else {
self.0.attributes = Some(vec![attribute]);
}
}

/// Set attributes on the underlying OpenTelemetry [`Span`] from `i64` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
fn record_i64(&mut self, field: &field::Field, value: i64) {
let attribute = api::KeyValue::new(field.name(), value);
if let Some(attributes) = &mut self.0.attributes {
attributes.push(attribute);
} else {
self.0.attributes = Some(vec![attribute]);
}
}

/// Set attributes on the underlying OpenTelemetry [`Span`] from `u64` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
fn record_u64(&mut self, field: &field::Field, value: u64) {
let attribute = api::KeyValue::new(field.name(), value);
if let Some(attributes) = &mut self.0.attributes {
attributes.push(attribute);
} else {
self.0.attributes = Some(vec![attribute]);
}
}

/// Set attributes on the underlying OpenTelemetry [`Span`] from `&str` values.
///
/// [`Span`]: https://docs.rs/opentelemetry/latest/opentelemetry/api/trace/span/trait.Span.html
Expand Down

0 comments on commit ca9b668

Please sign in to comment.