From 96e425337c21e35bcb5a675a1a9cefdcaa0f9428 Mon Sep 17 00:00:00 2001 From: Julian Tescher Date: Wed, 31 Mar 2021 13:04:22 -0700 Subject: [PATCH] Reduce string allocations where possible --- examples/async/src/main.rs | 2 +- .../src/exporter/model/mod.rs | 7 ++-- opentelemetry-jaeger/src/exporter/mod.rs | 21 +++++----- opentelemetry-otlp/src/transform/traces.rs | 41 ++++++++++++------- opentelemetry-stackdriver/src/lib.rs | 2 +- .../src/exporter/model/mod.rs | 7 +++- .../src/exporter/model/span.rs | 7 ++-- opentelemetry/src/global/trace.rs | 19 ++++++--- opentelemetry/src/sdk/env.rs | 10 ++--- opentelemetry/src/sdk/export/trace/mod.rs | 27 ++++++------ opentelemetry/src/sdk/resource.rs | 8 ++-- opentelemetry/src/sdk/trace/config.rs | 6 +-- opentelemetry/src/sdk/trace/span.rs | 15 +++---- opentelemetry/src/sdk/trace/tracer.rs | 17 +++++--- opentelemetry/src/testing/trace.rs | 4 +- opentelemetry/src/trace/noop.rs | 12 ++++-- opentelemetry/src/trace/tracer.rs | 19 ++++++--- 17 files changed, 132 insertions(+), 92 deletions(-) diff --git a/examples/async/src/main.rs b/examples/async/src/main.rs index d2b05105f9..4f273c4c52 100644 --- a/examples/async/src/main.rs +++ b/examples/async/src/main.rs @@ -46,7 +46,7 @@ async fn write(stream: &mut TcpStream) -> io::Result { async fn run(addr: &SocketAddr) -> io::Result { let tracer = global::tracer("runner"); - let span = tracer.start(&format!("running: {}", addr)); + let span = tracer.start(format!("running: {}", addr)); let cx = Context::current_with_span(span); let mut stream = connect(addr).with_context(cx.clone()).await?; diff --git a/opentelemetry-datadog/src/exporter/model/mod.rs b/opentelemetry-datadog/src/exporter/model/mod.rs index b0eae33647..8dc9b77f62 100644 --- a/opentelemetry-datadog/src/exporter/model/mod.rs +++ b/opentelemetry-datadog/src/exporter/model/mod.rs @@ -80,7 +80,6 @@ pub(crate) mod tests { trace::{SpanContext, SpanId, SpanKind, StatusCode, TraceId, TraceState}, Key, }; - use std::sync::Arc; use std::time::{Duration, SystemTime}; fn get_traces() -> Vec> { @@ -110,15 +109,15 @@ pub(crate) mod tests { span_context, parent_span_id: SpanId::from_u64(parent_span_id), span_kind: SpanKind::Client, - name: "resource".to_string(), + name: "resource".into(), start_time, end_time, attributes, message_events, links, status_code: StatusCode::Ok, - status_message: String::new(), - resource: Arc::new(sdk::Resource::default()), + status_message: "".into(), + resource: None, instrumentation_lib: InstrumentationLibrary::new("component", None), } } diff --git a/opentelemetry-jaeger/src/exporter/mod.rs b/opentelemetry-jaeger/src/exporter/mod.rs index 4fa6bb103c..15395ca88b 100644 --- a/opentelemetry-jaeger/src/exporter/mod.rs +++ b/opentelemetry-jaeger/src/exporter/mod.rs @@ -499,7 +499,7 @@ fn convert_otel_span_into_jaeger_span( trace_id_high, span_id: span.span_context.span_id().to_u64() as i64, parent_span_id: span.parent_span_id.to_u64() as i64, - operation_name: span.name, + operation_name: span.name.into_owned(), references: links_to_references(span.links), flags: span.span_context.trace_flags() as i32, start_time: span @@ -520,7 +520,7 @@ fn convert_otel_span_into_jaeger_span( None }, span.status_code, - span.status_message, + span.status_message.into_owned(), span.span_kind, )), logs: events_to_logs(span.message_events), @@ -530,16 +530,15 @@ fn convert_otel_span_into_jaeger_span( fn build_process_tags( span_data: &trace::SpanData, ) -> Option + '_> { - if span_data.resource.is_empty() { - None - } else { - Some( - span_data - .resource + span_data + .resource + .as_ref() + .filter(|resource| !resource.is_empty()) + .map(|resource| { + resource .iter() - .map(|(k, v)| KeyValue::new(k.clone(), v.clone()).into()), - ) - } + .map(|(k, v)| KeyValue::new(k.clone(), v.clone()).into()) + }) } fn build_span_tags( diff --git a/opentelemetry-otlp/src/transform/traces.rs b/opentelemetry-otlp/src/transform/traces.rs index ad0565a94c..7dfb0ce8c6 100644 --- a/opentelemetry-otlp/src/transform/traces.rs +++ b/opentelemetry-otlp/src/transform/traces.rs @@ -60,7 +60,10 @@ mod tonic { let span_kind: span::SpanKind = source_span.span_kind.into(); ResourceSpans { resource: Some(Resource { - attributes: resource_attributes(&source_span.resource).0, + attributes: resource_attributes( + source_span.resource.as_ref().map(AsRef::as_ref), + ) + .0, dropped_attributes_count: 0, }), instrumentation_library_spans: vec![InstrumentationLibrarySpans { @@ -86,7 +89,7 @@ mod tonic { vec![] } }, - name: source_span.name, + name: source_span.name.into_owned(), kind: span_kind as i32, start_time_unix_nano: to_nanos(source_span.start_time), end_time_unix_nano: to_nanos(source_span.end_time), @@ -107,7 +110,7 @@ mod tonic { links: source_span.links.into_iter().map(Into::into).collect(), status: Some(Status { code: status::StatusCode::from(source_span.status_code).into(), - message: source_span.status_message, + message: source_span.status_message.into_owned(), ..Default::default() }), }], @@ -116,11 +119,14 @@ mod tonic { } } - fn resource_attributes(resource: &sdk::Resource) -> Attributes { + fn resource_attributes(resource: Option<&sdk::Resource>) -> Attributes { resource - .iter() - .map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone())) - .collect::>() + .map(|res| { + res.iter() + .map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone())) + .collect::>() + }) + .unwrap_or_default() .into() } } @@ -186,7 +192,10 @@ mod grpcio { fn from(source_span: SpanData) -> Self { ResourceSpans { resource: SingularPtrField::from(Some(Resource { - attributes: resource_attributes(&source_span.resource).0, + attributes: resource_attributes( + source_span.resource.as_ref().map(AsRef::as_ref), + ) + .0, dropped_attributes_count: 0, ..Default::default() })), @@ -214,7 +223,7 @@ mod grpcio { vec![] } }, - name: source_span.name, + name: source_span.name.into_owned(), kind: source_span.span_kind.into(), start_time_unix_nano: to_nanos(source_span.start_time), end_time_unix_nano: to_nanos(source_span.end_time), @@ -240,7 +249,7 @@ mod grpcio { ), status: SingularPtrField::some(Status { code: Status_StatusCode::from(source_span.status_code), - message: source_span.status_message, + message: source_span.status_message.into_owned(), ..Default::default() }), ..Default::default() @@ -253,11 +262,15 @@ mod grpcio { } } - fn resource_attributes(resource: &sdk::Resource) -> Attributes { + fn resource_attributes(resource: Option<&sdk::Resource>) -> Attributes { resource - .iter() - .map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone())) - .collect::>() + .map(|resource| { + resource + .iter() + .map(|(k, v)| opentelemetry::KeyValue::new(k.clone(), v.clone())) + .collect::>() + }) + .unwrap_or_default() .into() } } diff --git a/opentelemetry-stackdriver/src/lib.rs b/opentelemetry-stackdriver/src/lib.rs index 777d2c1735..c00f4a2ac3 100644 --- a/opentelemetry-stackdriver/src/lib.rs +++ b/opentelemetry-stackdriver/src/lib.rs @@ -171,7 +171,7 @@ impl StackDriverExporter { hex::encode(span.span_context.trace_id().to_u128().to_be_bytes()), hex::encode(span.span_context.span_id().to_u64().to_be_bytes()) ), - display_name: Some(to_truncate(span.name.clone())), + display_name: Some(to_truncate(span.name.into_owned())), span_id: hex::encode(span.span_context.span_id().to_u64().to_be_bytes()), parent_span_id: hex::encode(span.parent_span_id.to_u64().to_be_bytes()), start_time: Some(span.start_time.into()), diff --git a/opentelemetry-zipkin/src/exporter/model/mod.rs b/opentelemetry-zipkin/src/exporter/model/mod.rs index adaba6df0a..f072dd2cd9 100644 --- a/opentelemetry-zipkin/src/exporter/model/mod.rs +++ b/opentelemetry-zipkin/src/exporter/model/mod.rs @@ -71,7 +71,10 @@ pub(crate) fn into_zipkin_span(local_endpoint: Endpoint, span_data: trace::SpanD ); if let Some(status_code) = from_statuscode_to_str(span_data.status_code) { if status_code == "ERROR" { - tags.insert(OTEL_ERROR_DESCRIPTION.into(), span_data.status_message); + tags.insert( + OTEL_ERROR_DESCRIPTION.into(), + span_data.status_message.into_owned(), + ); } tags.insert(OTEL_STATUS_CODE.into(), status_code.into()); } @@ -80,7 +83,7 @@ pub(crate) fn into_zipkin_span(local_endpoint: Endpoint, span_data: trace::SpanD .trace_id(span_data.span_context.trace_id().to_hex()) .parent_id(span_data.parent_span_id.to_hex()) .id(span_data.span_context.span_id().to_hex()) - .name(span_data.name) + .name(span_data.name.into_owned()) .kind(if user_defined_span_kind { None } else { diff --git a/opentelemetry-zipkin/src/exporter/model/span.rs b/opentelemetry-zipkin/src/exporter/model/span.rs index 6e4b34472f..9c4f9168d8 100644 --- a/opentelemetry-zipkin/src/exporter/model/span.rs +++ b/opentelemetry-zipkin/src/exporter/model/span.rs @@ -64,7 +64,6 @@ mod tests { use opentelemetry::trace::{SpanContext, SpanId, SpanKind, StatusCode, TraceId}; use std::collections::HashMap; use std::net::Ipv4Addr; - use std::sync::Arc; use std::time::SystemTime; #[test] @@ -170,15 +169,15 @@ mod tests { ), parent_span_id: SpanId::from_u64(1), span_kind: SpanKind::Client, - name: "".to_string(), + name: "".into(), start_time: SystemTime::now(), end_time: SystemTime::now(), attributes: EvictedHashMap::new(20, 20), message_events: EvictedQueue::new(20), links: EvictedQueue::new(20), status_code, - status_message: status_msg, - resource: Arc::new(Default::default()), + status_message: status_msg.into(), + resource: None, instrumentation_lib: Default::default(), }; let local_endpoint = Endpoint::new("test".into(), None); diff --git a/opentelemetry/src/global/trace.rs b/opentelemetry/src/global/trace.rs index 4ff11b42b0..98635c7cc9 100644 --- a/opentelemetry/src/global/trace.rs +++ b/opentelemetry/src/global/trace.rs @@ -1,5 +1,6 @@ use crate::trace::NoopTracerProvider; use crate::{trace, trace::TracerProvider, Context, KeyValue}; +use std::borrow::Cow; use std::fmt; use std::mem; use std::sync::{Arc, RwLock}; @@ -92,15 +93,21 @@ impl trace::Tracer for BoxedTracer { /// trace. A span is said to be a _root span_ if it does not have a parent. Each /// trace includes a single root span, which is the shared ancestor of all other /// spans in the trace. - fn start_with_context(&self, name: &str, cx: Context) -> Self::Span { - BoxedSpan(self.0.start_with_context_boxed(name, cx)) + fn start_with_context(&self, name: T, cx: Context) -> Self::Span + where + T: Into>, + { + BoxedSpan(self.0.start_with_context_boxed(name.into(), cx)) } /// Creates a span builder /// /// An ergonomic way for attributes to be configured before the `Span` is started. - fn span_builder(&self, name: &str) -> trace::SpanBuilder { - trace::SpanBuilder::from_name(name.to_string()) + fn span_builder(&self, name: T) -> trace::SpanBuilder + where + T: Into>, + { + trace::SpanBuilder::from_name(name) } /// Create a span from a `SpanBuilder` @@ -119,7 +126,7 @@ pub trait GenericTracer: fmt::Debug + 'static { /// Returns a trait object so the underlying implementation can be swapped /// out at runtime. - fn start_with_context_boxed(&self, name: &str, cx: Context) -> Box; + fn start_with_context_boxed(&self, name: Cow<'static, str>, cx: Context) -> Box; /// Returns a trait object so the underlying implementation can be swapped /// out at runtime. @@ -138,7 +145,7 @@ where /// Returns a trait object so the underlying implementation can be swapped /// out at runtime. - fn start_with_context_boxed(&self, name: &str, cx: Context) -> Box { + fn start_with_context_boxed(&self, name: Cow<'static, str>, cx: Context) -> Box { Box::new(self.start_with_context(name, cx)) } diff --git a/opentelemetry/src/sdk/env.rs b/opentelemetry/src/sdk/env.rs index fee858450a..ccbef94044 100644 --- a/opentelemetry/src/sdk/env.rs +++ b/opentelemetry/src/sdk/env.rs @@ -62,7 +62,7 @@ mod tests { use crate::sdk::env::OTEL_RESOURCE_ATTRIBUTES; use crate::sdk::resource::{Resource, ResourceDetector}; use crate::sdk::EnvResourceDetector; - use crate::{Key, KeyValue}; + use crate::KeyValue; use std::{env, time}; #[test] @@ -75,10 +75,10 @@ mod tests { assert_eq!( resource, Resource::new(vec![ - KeyValue::new(Key::new("key".to_string()), "value"), - KeyValue::new(Key::new("k".to_string()), "v"), - KeyValue::new(Key::new("a".to_string()), "x"), - KeyValue::new(Key::new("a".to_string()), "z"), + KeyValue::new("key", "value"), + KeyValue::new("k", "v"), + KeyValue::new("a", "x"), + KeyValue::new("a", "z"), ]) ); diff --git a/opentelemetry/src/sdk/export/trace/mod.rs b/opentelemetry/src/sdk/export/trace/mod.rs index 4943f1bc3c..9531e7fd20 100644 --- a/opentelemetry/src/sdk/export/trace/mod.rs +++ b/opentelemetry/src/sdk/export/trace/mod.rs @@ -1,16 +1,15 @@ //! Trace exporters -use std::fmt::Debug; -use std::sync::Arc; -use std::time::SystemTime; - -use async_trait::async_trait; -#[cfg(feature = "serialize")] -use serde::{Deserialize, Serialize}; - use crate::{ sdk, trace::{Event, Link, SpanContext, SpanId, SpanKind, StatusCode, TraceError}, }; +use async_trait::async_trait; +#[cfg(feature = "serialize")] +use serde::{Deserialize, Serialize}; +use std::borrow::Cow; +use std::fmt::Debug; +use std::sync::Arc; +use std::time::SystemTime; pub mod stdout; @@ -66,7 +65,7 @@ pub struct SpanData { /// Span kind pub span_kind: SpanKind, /// Span name - pub name: String, + pub name: Cow<'static, str>, /// Span start time pub start_time: SystemTime, /// Span end time @@ -80,9 +79,9 @@ pub struct SpanData { /// Span status code pub status_code: StatusCode, /// Span status message - pub status_message: String, + pub status_message: Cow<'static, str>, /// Resource contains attributes representing an entity that produced this span. - pub resource: Arc, + pub resource: Option>, /// Instrumentation library that produced this span #[cfg_attr(feature = "serialize", serde(skip))] pub instrumentation_lib: sdk::InstrumentationLibrary, @@ -111,7 +110,7 @@ mod tests { let parent_span_id = 1; let span_kind = SpanKind::Client; - let name = "foo/bar baz 人?!".to_string(); + let name = "foo/bar baz 人?!".into(); let start_time = crate::time::now(); let end_time = crate::time::now(); @@ -121,8 +120,8 @@ mod tests { let links = sdk::trace::EvictedQueue::new(capacity); let status_code = StatusCode::Ok; - let status_message = String::new(); - let resource = Arc::new(sdk::Resource::default()); + let status_message = "".into(); + let resource = None; let span_data = SpanData { span_context, diff --git a/opentelemetry/src/sdk/resource.rs b/opentelemetry/src/sdk/resource.rs index 7ee0b9fede..2d221fa859 100644 --- a/opentelemetry/src/sdk/resource.rs +++ b/opentelemetry/src/sdk/resource.rs @@ -260,10 +260,10 @@ mod tests { assert_eq!( resource, Resource::new(vec![ - KeyValue::new(Key::new("key".to_string()), "value"), - KeyValue::new(Key::new("k".to_string()), "v"), - KeyValue::new(Key::new("a".to_string()), "x"), - KeyValue::new(Key::new("a".to_string()), "z") + KeyValue::new("key", "value"), + KeyValue::new("k", "v"), + KeyValue::new("a", "x"), + KeyValue::new("a", "z") ]) ) } diff --git a/opentelemetry/src/sdk/trace/config.rs b/opentelemetry/src/sdk/trace/config.rs index ccbf4460a4..a17e91fe43 100644 --- a/opentelemetry/src/sdk/trace/config.rs +++ b/opentelemetry/src/sdk/trace/config.rs @@ -26,7 +26,7 @@ pub struct Config { /// The max links that can be added to a `Span`. pub max_links_per_span: u32, /// Contains attributes representing an entity that produces telemetry. - pub resource: Arc, + pub resource: Option>, } impl Config { @@ -62,7 +62,7 @@ impl Config { /// Specify the attributes representing the entity that produces telemetry pub fn with_resource(mut self, resource: sdk::Resource) -> Self { - self.resource = Arc::new(resource); + self.resource = Some(Arc::new(resource)); self } } @@ -76,7 +76,7 @@ impl Default for Config { max_events_per_span: 128, max_attributes_per_span: 128, max_links_per_span: 128, - resource: Arc::new(sdk::Resource::default()), + resource: None, }; if let Some(max_attributes_per_span) = env::var("OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT") diff --git a/opentelemetry/src/sdk/trace/span.rs b/opentelemetry/src/sdk/trace/span.rs index 866f1780e2..06942afb4c 100644 --- a/opentelemetry/src/sdk/trace/span.rs +++ b/opentelemetry/src/sdk/trace/span.rs @@ -10,6 +10,7 @@ //! These cannot be changed after the `Span`'s end time has been set. use crate::trace::{Event, SpanContext, SpanId, SpanKind, StatusCode}; use crate::{sdk, trace, KeyValue}; +use std::borrow::Cow; use std::sync::Arc; use std::time::SystemTime; @@ -28,7 +29,7 @@ pub(crate) struct SpanData { /// Span kind pub(crate) span_kind: SpanKind, /// Span name - pub(crate) name: String, + pub(crate) name: Cow<'static, str>, /// Span start time pub(crate) start_time: SystemTime, /// Span end time @@ -42,7 +43,7 @@ pub(crate) struct SpanData { /// Span status code pub(crate) status_code: StatusCode, /// Span status message - pub(crate) status_message: String, + pub(crate) status_message: Cow<'static, str>, } impl Span { @@ -113,7 +114,7 @@ impl crate::trace::Span for Span { fn set_status(&mut self, code: StatusCode, message: String) { self.with_data(|data| { if code == StatusCode::Error { - data.status_message = message; + data.status_message = message.into(); } data.status_code = code; }); @@ -122,7 +123,7 @@ impl crate::trace::Span for Span { /// Updates the `Span`'s name. fn update_name(&mut self, new_name: String) { self.with_data(|data| { - data.name = new_name; + data.name = new_name.into(); }); } @@ -180,7 +181,7 @@ impl Drop for Span { fn build_export_data( data: SpanData, span_context: SpanContext, - resource: Arc, + resource: Option>, tracer: &sdk::trace::Tracer, ) -> sdk::export::trace::SpanData { sdk::export::trace::SpanData { @@ -213,14 +214,14 @@ mod tests { let data = SpanData { parent_span_id: SpanId::from_u64(0), span_kind: trace::SpanKind::Internal, - name: "opentelemetry".to_string(), + name: "opentelemetry".into(), start_time: crate::time::now(), end_time: crate::time::now(), attributes: sdk::trace::EvictedHashMap::new(config.max_attributes_per_span, 0), message_events: sdk::trace::EvictedQueue::new(config.max_events_per_span), links: sdk::trace::EvictedQueue::new(config.max_links_per_span), status_code: StatusCode::Unset, - status_message: "".to_string(), + status_message: "".into(), }; (tracer, data) } diff --git a/opentelemetry/src/sdk/trace/tracer.rs b/opentelemetry/src/sdk/trace/tracer.rs index f9369d5813..699bc0b3da 100644 --- a/opentelemetry/src/sdk/trace/tracer.rs +++ b/opentelemetry/src/sdk/trace/tracer.rs @@ -20,6 +20,7 @@ use crate::trace::{ TraceState, TRACE_FLAG_SAMPLED, }; use crate::{Context, KeyValue}; +use std::borrow::Cow; use std::fmt; use std::sync::Weak; @@ -134,7 +135,10 @@ impl crate::trace::Tracer for Tracer { /// trace. A span is said to be a _root span_ if it does not have a parent. Each /// trace includes a single root span, which is the shared ancestor of all other /// spans in the trace. - fn start_with_context(&self, name: &str, cx: Context) -> Self::Span { + fn start_with_context(&self, name: T, cx: Context) -> Self::Span + where + T: Into>, + { let mut builder = self.span_builder(name); builder.parent_context = Some(cx); @@ -144,8 +148,11 @@ impl crate::trace::Tracer for Tracer { /// Creates a span builder /// /// An ergonomic way for attributes to be configured before the `Span` is started. - fn span_builder(&self, name: &str) -> SpanBuilder { - SpanBuilder::from_name(name.to_string()) + fn span_builder(&self, name: T) -> SpanBuilder + where + T: Into>, + { + SpanBuilder::from_name(name) } /// Starts a span from a `SpanBuilder`. @@ -271,12 +278,12 @@ impl crate::trace::Tracer for Tracer { message_events.append_vec(&mut events); } let status_code = builder.status_code.unwrap_or(StatusCode::Unset); - let status_message = builder.status_message.unwrap_or_default(); + let status_message = builder.status_message.unwrap_or(Cow::Borrowed("")); SpanData { parent_span_id, span_kind, - name: builder.name.into(), + name: builder.name, start_time, end_time, attributes, diff --git a/opentelemetry/src/testing/trace.rs b/opentelemetry/src/testing/trace.rs index 6f2f3ebd89..db4c708443 100644 --- a/opentelemetry/src/testing/trace.rs +++ b/opentelemetry/src/testing/trace.rs @@ -43,14 +43,14 @@ pub fn new_test_export_span_data() -> SpanData { span_context: SpanContext::empty_context(), parent_span_id: SpanId::from_u64(0), span_kind: SpanKind::Internal, - name: "opentelemetry".to_string(), + name: "opentelemetry".into(), start_time: crate::time::now(), end_time: crate::time::now(), attributes: EvictedHashMap::new(config.max_attributes_per_span, 0), message_events: EvictedQueue::new(config.max_events_per_span), links: EvictedQueue::new(config.max_links_per_span), status_code: StatusCode::Unset, - status_message: "".to_string(), + status_message: "".into(), resource: config.resource, instrumentation_lib: InstrumentationLibrary::default(), } diff --git a/opentelemetry/src/trace/noop.rs b/opentelemetry/src/trace/noop.rs index e2ee5d0949..fd3277d47b 100644 --- a/opentelemetry/src/trace/noop.rs +++ b/opentelemetry/src/trace/noop.rs @@ -132,15 +132,21 @@ impl trace::Tracer for NoopTracer { /// Starts a new `NoopSpan` with a given context. /// /// If the context contains a valid span context, it is propagated. - fn start_with_context(&self, name: &str, cx: Context) -> Self::Span { + fn start_with_context(&self, name: T, cx: Context) -> Self::Span + where + T: Into>, + { let mut builder = self.span_builder(name); builder.parent_context = Some(cx); self.build(builder) } /// Starts a `SpanBuilder`. - fn span_builder(&self, name: &str) -> trace::SpanBuilder { - trace::SpanBuilder::from_name(name.to_string()) + fn span_builder(&self, name: T) -> trace::SpanBuilder + where + T: Into>, + { + trace::SpanBuilder::from_name(name) } /// Builds a `NoopSpan` from a `SpanBuilder`. diff --git a/opentelemetry/src/trace/tracer.rs b/opentelemetry/src/trace/tracer.rs index 065606f4d1..5f7548d815 100644 --- a/opentelemetry/src/trace/tracer.rs +++ b/opentelemetry/src/trace/tracer.rs @@ -187,7 +187,10 @@ pub trait Tracer: fmt::Debug + 'static { /// created in another process. Each propagators' deserialization must set /// `is_remote` to true on a parent `SpanContext` so `Span` creation knows if the /// parent is remote. - fn start(&self, name: &str) -> Self::Span { + fn start(&self, name: T) -> Self::Span + where + T: Into>, + { self.start_with_context(name, Context::current()) } @@ -214,12 +217,16 @@ pub trait Tracer: fmt::Debug + 'static { /// created in another process. Each propagators' deserialization must set /// `is_remote` to true on a parent `SpanContext` so `Span` creation knows if the /// parent is remote. - fn start_with_context(&self, name: &str, context: Context) -> Self::Span; + fn start_with_context(&self, name: T, context: Context) -> Self::Span + where + T: Into>; /// Creates a span builder /// /// An ergonomic way for attributes to be configured before the `Span` is started. - fn span_builder(&self, name: &str) -> SpanBuilder; + fn span_builder(&self, name: T) -> SpanBuilder + where + T: Into>; /// Create a span from a `SpanBuilder` fn build(&self, builder: SpanBuilder) -> Self::Span; @@ -352,7 +359,7 @@ pub struct SpanBuilder { /// Span status code pub status_code: Option, /// Span status message - pub status_message: Option, + pub status_message: Option>, /// Sampling result pub sampling_result: Option, } @@ -459,9 +466,9 @@ impl SpanBuilder { } /// Assign status message - pub fn with_status_message(self, message: String) -> Self { + pub fn with_status_message>>(self, message: T) -> Self { SpanBuilder { - status_message: Some(message), + status_message: Some(message.into()), ..self } }