opentelemetry_sdk/trace/
config.rs1use 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#[deprecated(since = "0.23.0", note = "Use Config::default() instead")]
14pub fn config() -> Config {
15 Config::default()
16}
17
18#[derive(Debug)]
20#[non_exhaustive]
21pub struct Config {
22 pub sampler: Box<dyn ShouldSample>,
24
25 pub id_generator: Box<dyn IdGenerator>,
27
28 pub span_limits: SpanLimits,
30
31 pub resource: Cow<'static, Resource>,
33}
34
35impl Config {
36 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 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}