Skip to content

Commit c746b6f

Browse files
committedFeb 21, 2024
Builder for tracer
1 parent e1f05cb commit c746b6f

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed
 

‎opentelemetry/src/trace/tracer_provider.rs

+39-43
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{trace::Tracer, InstrumentationLibrary, KeyValue};
1+
use crate::{trace::Tracer, InstrumentationLibrary, InstrumentationLibraryBuilder, KeyValue};
22
use std::{borrow::Cow, sync::Arc};
33

44
/// Types that can create instances of [`Tracer`].
@@ -45,48 +45,11 @@ pub trait TracerProvider {
4545
)
4646
}
4747

48-
/// Returns a new versioned tracer with a given name.
49-
///
50-
/// The `name` should be the application name or the name of the library
51-
/// providing instrumentation. If the name is empty, then an
52-
/// implementation-defined default name may be used instead.
53-
///
54-
/// # Examples
55-
///
56-
/// ```
57-
/// use opentelemetry::{global, trace::TracerProvider};
58-
///
59-
/// let provider = global::tracer_provider();
60-
///
61-
/// // tracer used in applications/binaries
62-
/// let tracer = provider.tracer("my_app");
63-
///
64-
/// // tracer used in libraries/crates that optionally includes version and schema url
65-
/// let tracer = provider.versioned_tracer(
66-
/// "my_library",
67-
/// Some(env!("CARGO_PKG_VERSION")),
68-
/// Some("https://opentelemetry.io/schema/1.0.0"),
69-
/// None,
70-
/// );
71-
/// ```
72-
fn versioned_tracer(
73-
&self,
74-
name: impl Into<Cow<'static, str>>,
75-
version: Option<impl Into<Cow<'static, str>>>,
76-
schema_url: Option<impl Into<Cow<'static, str>>>,
77-
attributes: Option<Vec<KeyValue>>,
78-
) -> Self::Tracer {
79-
let mut builder = InstrumentationLibrary::builder(name);
80-
if let Some(v) = version {
81-
builder = builder.with_version(v);
82-
}
83-
if let Some(s) = schema_url {
84-
builder = builder.with_schema_url(s);
85-
}
86-
if let Some(a) = attributes {
87-
builder = builder.with_attributes(a);
88-
}
89-
self.library_tracer(Arc::new(builder.build()))
48+
fn tracer_builder(self, name: impl Into<Cow<'static, str>>) -> TracerBuilder<Self>
49+
where
50+
Self: Sized,
51+
{
52+
TracerBuilder::new(self, name)
9053
}
9154

9255
/// Returns a new versioned tracer with the given instrumentation library.
@@ -112,3 +75,36 @@ pub trait TracerProvider {
11275
/// ```
11376
fn library_tracer(&self, library: Arc<InstrumentationLibrary>) -> Self::Tracer;
11477
}
78+
79+
struct TracerBuilder<T: TracerProvider> {
80+
provider: T,
81+
library_builder: InstrumentationLibraryBuilder,
82+
}
83+
84+
impl<T: TracerProvider> TracerBuilder<T> {
85+
fn new(provider: T, name: impl Into<Cow<'static, str>>) -> Self {
86+
TracerBuilder {
87+
provider: provider,
88+
library_builder: InstrumentationLibraryBuilder::new(name),
89+
}
90+
}
91+
92+
pub fn with_version(mut self, version: impl Into<Cow<'static, str>>) -> Self {
93+
self.library_builder = self.library_builder.with_version(version);
94+
self
95+
}
96+
97+
pub fn with_schema_url(mut self, schema_url: impl Into<Cow<'static, str>>) -> Self {
98+
self.library_builder = self.library_builder.with_schema_url(schema_url);
99+
self
100+
}
101+
102+
pub fn with_attributes(mut self, attributes: impl Into<Vec<KeyValue>>) -> Self {
103+
self.library_builder = self.library_builder.with_attributes(attributes);
104+
self
105+
}
106+
107+
pub fn build(self) -> T::Tracer {
108+
self.provider.library_tracer(self.library_builder.build())
109+
}
110+
}

0 commit comments

Comments
 (0)