Skip to content

Commit

Permalink
opentelemetry: slightly improve OpentemetrySubscriber's performance…
Browse files Browse the repository at this point in the history
… by pre-determined the attribute length
  • Loading branch information
Folyd committed Apr 2, 2021
1 parent 0e8f920 commit c812e42
Showing 1 changed file with 14 additions and 24 deletions.
38 changes: 14 additions & 24 deletions tracing-opentelemetry/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,29 +171,28 @@ impl<'a> field::Visit for SpanEventVisitor<'a> {

struct SpanAttributeVisitor<'a>(&'a mut otel::SpanBuilder);

impl<'a> SpanAttributeVisitor<'a> {
fn record(&mut self, attribute: KeyValue) {
debug_assert!(self.0.attributes.is_some());
if let Some(v) = self.0.attributes.as_mut() {
v.push(attribute);
}
}
}

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

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

/// Set attributes on the underlying OpenTelemetry [`Span`] from `&str` values.
Expand All @@ -205,12 +204,7 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> {
} else if field.name() == SPAN_KIND_FIELD {
self.0.span_kind = str_to_span_kind(value);
} else {
let attribute = KeyValue::new(field.name(), value.to_string());
if let Some(attributes) = &mut self.0.attributes {
attributes.push(attribute);
} else {
self.0.attributes = Some(vec![attribute]);
}
self.record(KeyValue::new(field.name(), value.to_string()));
}
}

Expand All @@ -224,12 +218,7 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> {
} else if field.name() == SPAN_KIND_FIELD {
self.0.span_kind = str_to_span_kind(&format!("{:?}", value));
} else {
let attribute = Key::new(field.name()).string(format!("{:?}", value));
if let Some(attributes) = &mut self.0.attributes {
attributes.push(attribute);
} else {
self.0.attributes = Some(vec![attribute]);
}
self.record(Key::new(field.name()).string(format!("{:?}", value)));
}
}
}
Expand Down Expand Up @@ -418,6 +407,7 @@ where
builder.trace_id = Some(self.tracer.new_trace_id());
}

builder.attributes = Some(Vec::with_capacity(attrs.metadata().fields().len()));
attrs.record(&mut SpanAttributeVisitor(&mut builder));
extensions.insert(builder);
}
Expand Down

0 comments on commit c812e42

Please sign in to comment.