opentelemetry_spanprocessor_any/sdk/trace/
config.rs1use crate::sdk::trace::span_limit::SpanLimits;
6use crate::{sdk, sdk::trace::Sampler, trace::IdGenerator};
7use std::env;
8use std::str::FromStr;
9use std::sync::Arc;
10
11pub fn config() -> Config {
13 Config::default()
14}
15
16#[derive(Debug)]
18pub struct Config {
19 pub sampler: Box<dyn sdk::trace::ShouldSample>,
21 pub id_generator: Box<dyn IdGenerator>,
23 pub span_limits: SpanLimits,
25 pub resource: Option<Arc<sdk::Resource>>,
27}
28
29impl Config {
30 pub fn with_sampler<T: sdk::trace::ShouldSample + 'static>(mut self, sampler: T) -> Self {
32 self.sampler = Box::new(sampler);
33 self
34 }
35
36 pub fn with_id_generator<T: IdGenerator + 'static>(mut self, id_generator: T) -> Self {
38 self.id_generator = Box::new(id_generator);
39 self
40 }
41
42 pub fn with_max_events_per_span(mut self, max_events: u32) -> Self {
44 self.span_limits.max_events_per_span = max_events;
45 self
46 }
47
48 pub fn with_max_attributes_per_span(mut self, max_attributes: u32) -> Self {
50 self.span_limits.max_attributes_per_span = max_attributes;
51 self
52 }
53
54 pub fn with_max_links_per_span(mut self, max_links: u32) -> Self {
56 self.span_limits.max_links_per_span = max_links;
57 self
58 }
59
60 pub fn with_max_attributes_per_event(mut self, max_attributes: u32) -> Self {
62 self.span_limits.max_attributes_per_event = max_attributes;
63 self
64 }
65
66 pub fn with_max_attributes_per_link(mut self, max_attributes: u32) -> Self {
68 self.span_limits.max_attributes_per_link = max_attributes;
69 self
70 }
71
72 pub fn with_span_limits(mut self, span_limits: SpanLimits) -> Self {
74 self.span_limits = span_limits;
75 self
76 }
77
78 pub fn with_resource(mut self, resource: sdk::Resource) -> Self {
80 self.resource = Some(Arc::new(resource));
81 self
82 }
83
84 pub fn with_no_resource(self) -> Self {
91 self.with_resource(sdk::Resource::empty())
92 }
93}
94
95impl Default for Config {
96 fn default() -> Self {
98 let mut config = Config {
99 sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
100 id_generator: Box::new(sdk::trace::IdGenerator::default()),
101 span_limits: SpanLimits::default(),
102 resource: None,
103 };
104
105 if let Some(max_attributes_per_span) = env::var("OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT")
106 .ok()
107 .and_then(|count_limit| u32::from_str(&count_limit).ok())
108 {
109 config.span_limits.max_attributes_per_span = max_attributes_per_span;
110 }
111
112 if let Some(max_events_per_span) = env::var("OTEL_SPAN_EVENT_COUNT_LIMIT")
113 .ok()
114 .and_then(|max_events| u32::from_str(&max_events).ok())
115 {
116 config.span_limits.max_events_per_span = max_events_per_span;
117 }
118
119 if let Some(max_links_per_span) = env::var("OTEL_SPAN_LINK_COUNT_LIMIT")
120 .ok()
121 .and_then(|max_links| u32::from_str(&max_links).ok())
122 {
123 config.span_limits.max_links_per_span = max_links_per_span;
124 }
125
126 config
127 }
128}