1
- use crate :: { trace:: Tracer , InstrumentationLibrary , KeyValue } ;
1
+ use crate :: { trace:: Tracer , InstrumentationLibrary , InstrumentationLibraryBuilder , KeyValue } ;
2
2
use std:: { borrow:: Cow , sync:: Arc } ;
3
3
4
4
/// Types that can create instances of [`Tracer`].
@@ -76,9 +76,47 @@ pub trait TracerProvider {
76
76
schema_url : Option < impl Into < Cow < ' static , str > > > ,
77
77
attributes : Option < Vec < KeyValue > > ,
78
78
) -> Self :: Tracer {
79
- self . library_tracer ( Arc :: new ( InstrumentationLibrary :: new (
80
- name, version, schema_url, attributes,
81
- ) ) )
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_version ( s) ;
85
+ }
86
+ if let Some ( a) = attributes {
87
+ builder = builder. with_attributes ( a) ;
88
+ }
89
+
90
+ self . library_tracer ( Arc :: new ( builder. build ( ) ) )
91
+ }
92
+
93
+ /// Returns a new builder for creating a [`Tracer`] instance
94
+ ///
95
+ /// The `name` should be the application name or the name of the library
96
+ /// providing instrumentation. If the name is empty, then an
97
+ /// implementation-defined default name may be used instead.
98
+ ///
99
+ /// # Examples
100
+ ///
101
+ /// ```
102
+ /// use opentelemetry::{global, trace::TracerProvider};
103
+ ///
104
+ /// let provider = global::tracer_provider();
105
+ ///
106
+ /// // tracer used in applications/binaries
107
+ /// let tracer = provider::tracer_builder("my_app").build();
108
+ ///
109
+ /// // tracer used in libraries/crates that optionally includes version and schema url
110
+ /// let tracer = provider::tracer_builder("my_library")
111
+ /// .with_version(env!("CARGO_PKG_VERSION"))
112
+ /// .with_schema_url("https://opentelemetry.io/schema/1.0.0")
113
+ /// .build();
114
+ /// ```
115
+ fn tracer_builder ( & self , name : impl Into < Cow < ' static , str > > ) -> TracerBuilder < ' _ , Self > {
116
+ TracerBuilder {
117
+ provider : self ,
118
+ library_builder : InstrumentationLibraryBuilder :: new ( name) ,
119
+ }
82
120
}
83
121
84
122
/// Returns a new versioned tracer with the given instrumentation library.
@@ -104,3 +142,31 @@ pub trait TracerProvider {
104
142
/// ```
105
143
fn library_tracer ( & self , library : Arc < InstrumentationLibrary > ) -> Self :: Tracer ;
106
144
}
145
+
146
+ #[ derive( Debug ) ]
147
+ pub struct TracerBuilder < ' a , T : TracerProvider + ?Sized > {
148
+ provider : & ' a T ,
149
+ library_builder : InstrumentationLibraryBuilder ,
150
+ }
151
+
152
+ impl < ' a , T : TracerProvider + ?Sized > TracerBuilder < ' a , T > {
153
+ pub fn with_version ( mut self , version : impl Into < Cow < ' static , str > > ) -> Self {
154
+ self . library_builder = self . library_builder . with_version ( version) ;
155
+ self
156
+ }
157
+
158
+ pub fn with_schema_url ( mut self , schema_url : impl Into < Cow < ' static , str > > ) -> Self {
159
+ self . library_builder = self . library_builder . with_schema_url ( schema_url) ;
160
+ self
161
+ }
162
+
163
+ pub fn with_attributes ( mut self , attributes : impl Into < Vec < KeyValue > > ) -> Self {
164
+ self . library_builder = self . library_builder . with_attributes ( attributes) ;
165
+ self
166
+ }
167
+
168
+ pub fn build ( self ) -> T :: Tracer {
169
+ self . provider
170
+ . library_tracer ( Arc :: new ( self . library_builder . build ( ) ) )
171
+ }
172
+ }
0 commit comments