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/// Tracer configuration
13#[derive(Debug)]
14#[non_exhaustive]
15pub struct Config {
16    /// The sampler that the sdk should use
17    pub sampler: Box<dyn ShouldSample>,
18
19    /// The id generator that the sdk should use
20    pub id_generator: Box<dyn IdGenerator>,
21
22    /// span limits
23    pub span_limits: SpanLimits,
24
25    /// Contains attributes representing an entity that produces telemetry.
26    pub resource: Cow<'static, Resource>,
27}
28
29impl Default for Config {
30    /// Create default global sdk configuration.
31    fn default() -> Self {
32        let mut config = Config {
33            sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
34            id_generator: Box::<RandomIdGenerator>::default(),
35            span_limits: SpanLimits::default(),
36            resource: Cow::Owned(Resource::builder().build()),
37        };
38
39        if let Some(max_attributes_per_span) = env::var("OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT")
40            .ok()
41            .and_then(|count_limit| u32::from_str(&count_limit).ok())
42        {
43            config.span_limits.max_attributes_per_span = max_attributes_per_span;
44        }
45
46        if let Some(max_events_per_span) = env::var("OTEL_SPAN_EVENT_COUNT_LIMIT")
47            .ok()
48            .and_then(|max_events| u32::from_str(&max_events).ok())
49        {
50            config.span_limits.max_events_per_span = max_events_per_span;
51        }
52
53        if let Some(max_links_per_span) = env::var("OTEL_SPAN_LINK_COUNT_LIMIT")
54            .ok()
55            .and_then(|max_links| u32::from_str(&max_links).ok())
56        {
57            config.span_limits.max_links_per_span = max_links_per_span;
58        }
59
60        let sampler_arg = env::var("OTEL_TRACES_SAMPLER_ARG").ok();
61        if let Ok(sampler) = env::var("OTEL_TRACES_SAMPLER") {
62            config.sampler = match sampler.as_str() {
63                "always_on" => Box::new(Sampler::AlwaysOn),
64                "always_off" => Box::new(Sampler::AlwaysOff),
65                "traceidratio" => {
66                    let ratio = sampler_arg.as_ref().and_then(|r| r.parse::<f64>().ok());
67                    if let Some(r) = ratio {
68                        Box::new(Sampler::TraceIdRatioBased(r))
69                    } else {
70                        otel_warn!(
71                            name: "TracerProvider.Config.InvalidSamplerArgument",
72                            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)",
73                            otel_traces_sampler_arg = format!("{:?}", sampler_arg)
74                        );
75                        Box::new(Sampler::TraceIdRatioBased(1.0))
76                    }
77                }
78                "parentbased_always_on" => {
79                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
80                }
81                "parentbased_always_off" => {
82                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOff)))
83                }
84                "parentbased_traceidratio" => {
85                    let ratio = sampler_arg.as_ref().and_then(|r| r.parse::<f64>().ok());
86                    if let Some(r) = ratio {
87                        Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
88                            r,
89                        ))))
90                    } else {
91                        otel_warn!(
92                            name: "TracerProvider.Config.InvalidSamplerArgument",
93                            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)",
94                            otel_traces_sampler_arg = format!("{:?}", sampler_arg)
95                        );
96                        Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
97                            1.0,
98                        ))))
99                    }
100                }
101                "parentbased_jaeger_remote" => {
102                    otel_warn!(
103                        name: "TracerProvider.Config.UnsupportedSampler",
104                        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"
105                    );
106                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
107                }
108                "jaeger_remote" => {
109                    otel_warn!(
110                        name: "TracerProvider.Config.UnsupportedSampler",
111                        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"
112                    );
113                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
114                }
115                "xray" => {
116                    otel_warn!(
117                        name: "TracerProvider.Config.UnsupportedSampler",
118                        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"
119                    );
120                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
121                }
122                s => {
123                    otel_warn!(
124                        name: "TracerProvider.Config.InvalidSamplerType",
125                        message = format!(
126                            "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)",
127                            s
128                        ),
129                    );
130                    Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
131                }
132            }
133        }
134
135        config
136    }
137}