From 5782fd2ced3e812c8045b1d8e1893b1f4a997e47 Mon Sep 17 00:00:00 2001 From: Folyd Date: Fri, 26 Mar 2021 22:31:13 +0800 Subject: [PATCH 1/2] opentelemetry: slightly improve `OpentemetrySubscriber`'s performance by pre-determined the attribute length --- tracing-opentelemetry/src/subscriber.rs | 42 +++++++++---------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/tracing-opentelemetry/src/subscriber.rs b/tracing-opentelemetry/src/subscriber.rs index cb38b8318a..38ee1e2175 100644 --- a/tracing-opentelemetry/src/subscriber.rs +++ b/tracing-opentelemetry/src/subscriber.rs @@ -177,29 +177,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. @@ -211,14 +210,7 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> { SPAN_KIND_FIELD => self.0.span_kind = str_to_span_kind(value), SPAN_STATUS_CODE_FIELD => self.0.status_code = str_to_status_code(value), SPAN_STATUS_MESSAGE_FIELD => self.0.status_message = Some(value.to_owned().into()), - _ => { - 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())), } } @@ -236,14 +228,7 @@ impl<'a> field::Visit for SpanAttributeVisitor<'a> { SPAN_STATUS_MESSAGE_FIELD => { self.0.status_message = Some(format!("{:?}", value).into()) } - _ => { - 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))), } } } @@ -421,6 +406,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); } From eccdb750aee3181902399836a6e60cb110fdf094 Mon Sep 17 00:00:00 2001 From: Folyd Date: Sun, 16 May 2021 11:18:31 +0800 Subject: [PATCH 2/2] opentelemetry: replace `attrs.metadata().fields()` with `attr.fields()` --- tracing-opentelemetry/src/subscriber.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tracing-opentelemetry/src/subscriber.rs b/tracing-opentelemetry/src/subscriber.rs index 38ee1e2175..052f6da8bd 100644 --- a/tracing-opentelemetry/src/subscriber.rs +++ b/tracing-opentelemetry/src/subscriber.rs @@ -406,7 +406,7 @@ where builder.trace_id = Some(self.tracer.new_trace_id()); } - builder.attributes = Some(Vec::with_capacity(attrs.metadata().fields().len())); + builder.attributes = Some(Vec::with_capacity(attrs.fields().len())); attrs.record(&mut SpanAttributeVisitor(&mut builder)); extensions.insert(builder); }