opentelemetry/trace/tracer_provider.rs
1use crate::{trace::Tracer, InstrumentationScope};
2use std::borrow::Cow;
3
4/// Types that can create instances of [`Tracer`].
5///
6/// See the [`global`] module for examples of storing and retrieving tracer
7/// provider instances.
8///
9/// [`global`]: crate::global
10pub trait TracerProvider {
11 /// The [`Tracer`] type that this provider will return.
12 type Tracer: Tracer;
13
14 /// Returns a new tracer with the given name.
15 ///
16 /// The `name` should be the application name or the name of the library
17 /// providing instrumentation. If the name is empty, then an
18 /// implementation-defined default name may be used instead.
19 ///
20 /// # Examples
21 ///
22 /// ```
23 /// use opentelemetry::{global, trace::TracerProvider};
24 /// use opentelemetry::KeyValue;
25 ///
26 /// let provider = global::tracer_provider();
27 ///
28 /// // tracer used in applications/binaries
29 /// let tracer = provider.tracer("my_app");
30 /// ```
31 fn tracer(&self, name: impl Into<Cow<'static, str>>) -> Self::Tracer {
32 let scope = InstrumentationScope::builder(name).build();
33 self.tracer_with_scope(scope)
34 }
35
36 /// Returns a new versioned tracer with the given instrumentation scope.
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// use opentelemetry::{global, InstrumentationScope, trace::TracerProvider};
42 ///
43 /// let provider = global::tracer_provider();
44 ///
45 /// // tracer used in applications/binaries
46 /// let tracer = provider.tracer("my_app");
47 ///
48 /// // tracer used in libraries/crates that optionally includes version and schema url
49 /// let scope =
50 /// InstrumentationScope::builder(env!("CARGO_PKG_NAME"))
51 /// .with_version(env!("CARGO_PKG_VERSION"))
52 /// .with_schema_url("https://opentelemetry.io/schema/1.0.0")
53 /// .build();
54 ///
55 /// let tracer = provider.tracer_with_scope(scope);
56 /// ```
57 fn tracer_with_scope(&self, scope: InstrumentationScope) -> Self::Tracer;
58}