opentelemetry_sdk/trace/
config.rs

1//! SDK Configuration
2//!
3//! Configuration represents the global tracing configuration, overrides
4//! can be set for the default OpenTelemetry limits and Sampler.
5use crate::trace::{span_limit::SpanLimits, IdGenerator, RandomIdGenerator, Sampler, ShouldSample};
6use crate::Resource;
7use opentelemetry::otel_warn;
8use std::borrow::Cow;
9use std::env;
10use std::str::FromStr;
11
12/// Default trace configuration
13#[deprecated(since = "0.23.0", note = "Use Config::default() instead")]
14pub fn config() -> Config {
15    Config::default()
16}
17
18/// Tracer configuration
19#[derive(Debug)]
20#[non_exhaustive]
21pub struct Config {
22    /// The sampler that the sdk should use
23    pub sampler: Box<dyn ShouldSample>,
24
25    /// The id generator that the sdk should use
26    pub id_generator: Box<dyn IdGenerator>,
27
28    /// span limits
29    pub span_limits: SpanLimits,
30
31    /// Contains attributes representing an entity that produces telemetry.
32    pub resource: Cow<'static, Resource>,
33}
34
35impl Config {
36    /// Specify the sampler to be used.
37    #[deprecated(
38        since = "0.27.1",
39        note = "Config is becoming private. Please use Builder::with_sampler(...) instead."
40    )]
41    pub fn with_sampler<T: crate::trace::ShouldSample + 'static>(mut self, sampler: T) -> Self {
42        self.sampler = Box::new(sampler);
43        self
44    }
45
46    /// Specify the id generator to be used.
47    #[deprecated(
48        since = "0.27.1",
49        note = "Config is becoming private. Please use Builder::with_id_generator(...) instead."
50    )]
51    pub fn with_id_generator<T: IdGenerator + 'static>(mut self, id_generator: T) -> Self {
52        self.id_generator = Box::new(id_generator);
53        self
54    }
55
56    /// Specify the maximum number of events that can be recorded per span.
57    #[deprecated(
58        since = "0.27.1",
59        note = "Config is becoming private. Please use Builder::with_max_events_per_span(...) instead."
60    )]
61    pub fn with_max_events_per_span(mut self, max_events: u32) -> Self {
62        self.span_limits.max_events_per_span = max_events;
63        self
64    }
65
66    /// Specify the maximum number of attributes that can be recorded per span.
67    #[deprecated(
68        since = "0.27.1",
69        note = "Config is becoming private. Please use Builder::with_max_attributes_per_span(...) instead."
70    )]
71    pub fn with_max_attributes_per_span(mut self, max_attributes: u32) -> Self {
72        self.span_limits.max_attributes_per_span = max_attributes;
73        self
74    }
75
76    /// Specify the maximum number of links that can be recorded per span.
77    #[deprecated(
78        since = "0.27.1",
79        note = "Config is becoming private. Please use Builder::with_max_links_per_span(...) instead."
80    )]
81    pub fn with_max_links_per_span(mut self, max_links: u32) -> Self {
82        self.span_limits.max_links_per_span = max_links;
83        self
84    }
85
86    /// Specify the maximum number of attributes one event can have.
87    #[deprecated(
88        since = "0.27.1",
89        note = "Config is becoming private. Please use Builder::with_max_attributes_per_event(...) instead."
90    )]
91    pub fn with_max_attributes_per_event(mut self, max_attributes: u32) -> Self {
92        self.span_limits.max_attributes_per_event = max_attributes;
93        self
94    }
95
96    /// Specify the maximum number of attributes one link can have.
97    #[deprecated(
98        since = "0.27.1",
99        note = "Config is becoming private. Please use Builder::with_max_attributes_per_link(...) instead."
100    )]
101    pub fn with_max_attributes_per_link(mut self, max_attributes: u32) -> Self {
102        self.span_limits.max_attributes_per_link = max_attributes;
103        self
104    }
105
106    /// Specify all limit via the span_limits
107    #[deprecated(
108        since = "0.27.1",
109        note = "Config is becoming private. Please use Builder::with_span_limits(...) instead."
110    )]
111    pub fn with_span_limits(mut self, span_limits: SpanLimits) -> Self {
112        self.span_limits = span_limits;
113        self
114    }
115
116    /// Specify the attributes representing the entity that produces telemetry
117    #[deprecated(
118        since = "0.27.1",
119        note = "Config is becoming private. Please use Builder::with_resource(...) instead."
120    )]
121    pub fn with_resource(mut self, resource: Resource) -> Self {
122        self.resource = Cow::Owned(resource);
123        self
124    }
125}
126
127impl Default for Config {
128    /// Create default global sdk configuration.
129    fn default() -> Self {
130        let mut config = Config {
131            sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
132            id_generator: Box::<RandomIdGenerator>::default(),
133            span_limits: SpanLimits::default(),
134            resource: Cow::Owned(Resource::builder().build()),
135        };
136
137        if let Some(max_attributes_per_span) = env::var("OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT")
138            .ok()
139            .and_then(|count_limit| u32::from_str(&count_limit).ok())
140        {
141            config.span_limits.max_attributes_per_span = max_attributes_per_span;
142        }
143
144        if let Some(max_events_per_span) = env::var("OTEL_SPAN_EVENT_COUNT_LIMIT")
145            .ok()
146            .and_then(|max_events| u32::from_str(&max_events).ok())
147        {
148            config.span_limits.max_events_per_span = max_events_per_span;
149        }
150
151        if let Some(max_links_per_span) = env::var("OTEL_SPAN_LINK_COUNT_LIMIT")
152            .ok()
153            .and_then(|max_links| u32::from_str(&max_links).ok())
154        {
155            config.span_limits.max_links_per_span = max_links_per_span;
156        }
157
158        let sampler_arg = env::var("OTEL_TRACES_SAMPLER_ARG").ok();
159        if let Ok(sampler) = env::var("OTEL_TRACES_SAMPLER") {
160            config.sampler = match sampler.as_str() {
161                "always_on" => Box::new(Sampler::AlwaysOn),
162                "always_off" => Box::new(Sampler::AlwaysOff),
163                "traceidratio" => {
164                    let ratio = sampler_arg.as_ref().and_then(|r| r.parse::<f64>().ok());
165                    if let Some(r) = ratio {
166                        Box::new(Sampler::TraceIdRatioBased(r))
167                    } else {
168                        otel_warn!(
169                            name: "TracerProvider.Config.InvalidSamplerArgument",
170                            message = "OTEL_TRACES_SAMPLER is set to 'traceidratio' but OTEL_TRACES_SAMPLER_ARG environment variable is missing or invalid. OTEL_TRACES_SAMPLER_ARG must be a valid float between 0.0 and 1.0 representing the desired sampling probability (0.0 = no traces sampled, 1.0 = all traces sampled, 0.5 = 50% of traces sampled). Falling back to default ratio: 1.0 (100% sampling)",
171                            otel_traces_sampler_arg = format!("{:?}", sampler_arg)
172                        );
173                        Box::new(Sampler::TraceIdRatioBased(1.0))
174                    }
175                }
176                "parentbased_always_on" => {
177                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
178                }
179                "parentbased_always_off" => {
180                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOff)))
181                }
182                "parentbased_traceidratio" => {
183                    let ratio = sampler_arg.as_ref().and_then(|r| r.parse::<f64>().ok());
184                    if let Some(r) = ratio {
185                        Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
186                            r,
187                        ))))
188                    } else {
189                        otel_warn!(
190                            name: "TracerProvider.Config.InvalidSamplerArgument",
191                            message = "OTEL_TRACES_SAMPLER is set to 'parentbased_traceidratio' but OTEL_TRACES_SAMPLER_ARG environment variable is missing or invalid. OTEL_TRACES_SAMPLER_ARG must be a valid float between 0.0 and 1.0 representing the desired sampling probability (0.0 = no traces sampled, 1.0 = all traces sampled, 0.5 = 50% of traces sampled). Falling back to default ratio: 1.0 (100% sampling)",
192                            otel_traces_sampler_arg = format!("{:?}", sampler_arg)
193                        );
194                        Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
195                            1.0,
196                        ))))
197                    }
198                }
199                "parentbased_jaeger_remote" => {
200                    otel_warn!(
201                        name: "TracerProvider.Config.UnsupportedSampler",
202                        message = "OTEL_TRACES_SAMPLER is set to 'parentbased_jaeger_remote' which is not implemented in this SDK version. Using fallback sampler: ParentBased(AlwaysOn). Configure an alternative sampler using OTEL_TRACES_SAMPLER"
203                    );
204                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
205                }
206                "jaeger_remote" => {
207                    otel_warn!(
208                        name: "TracerProvider.Config.UnsupportedSampler",
209                        message = "OTEL_TRACES_SAMPLER is set to 'jaeger_remote' which is implemented in this SDK version. Using fallback sampler: ParentBased(AlwaysOn). Configure an alternative sampler using OTEL_TRACES_SAMPLER"
210                    );
211                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
212                }
213                "xray" => {
214                    otel_warn!(
215                        name: "TracerProvider.Config.UnsupportedSampler",
216                        message = "OTEL_TRACES_SAMPLER is set to 'xray'. AWS X-Ray sampler is not implemented in this SDK version. Using fallback sampler: ParentBased(AlwaysOn). Configure an alternative sampler using OTEL_TRACES_SAMPLER"
217                    );
218                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
219                }
220                s => {
221                    otel_warn!(
222                        name: "TracerProvider.Config.InvalidSamplerType",
223                        message = format!(
224                            "Unrecognized sampler type '{}' in OTEL_TRACES_SAMPLER environment variable. Valid values are: always_on, always_off, traceidratio, parentbased_always_on, parentbased_always_off, parentbased_traceidratio. Using fallback sampler: ParentBased(AlwaysOn)",
225                            s
226                        ),
227                    );
228                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
229                }
230            }
231        }
232
233        config
234    }
235}