opentelemetry_spanprocessor_any/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::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
11/// Default trace configuration
12pub fn config() -> Config {
13    Config::default()
14}
15
16/// Tracer configuration
17#[derive(Debug)]
18pub struct Config {
19    /// The sampler that the sdk should use
20    pub sampler: Box<dyn sdk::trace::ShouldSample>,
21    /// The id generator that the sdk should use
22    pub id_generator: Box<dyn IdGenerator>,
23    /// span limits
24    pub span_limits: SpanLimits,
25    /// Contains attributes representing an entity that produces telemetry.
26    pub resource: Option<Arc<sdk::Resource>>,
27}
28
29impl Config {
30    /// Specify the sampler to be used.
31    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    /// Specify the id generator to be used.
37    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    /// Specify the number of events to be recorded per span.
43    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    /// Specify the number of attributes to be recorded per span.
49    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    /// Specify the number of events to be recorded per span.
55    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    /// Specify the number of attributes one event can have.
61    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    /// Specify the number of attributes one link can have.
67    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    /// Specify all limit via the span_limits
73    pub fn with_span_limits(mut self, span_limits: SpanLimits) -> Self {
74        self.span_limits = span_limits;
75        self
76    }
77
78    /// Specify the attributes representing the entity that produces telemetry
79    pub fn with_resource(mut self, resource: sdk::Resource) -> Self {
80        self.resource = Some(Arc::new(resource));
81        self
82    }
83
84    /// Use empty resource instead of default resource in this config.
85    ///
86    /// Usually if no resource is provided, SDK will assign a default resource
87    /// to the `TracerProvider`, which could impact the performance. Performance
88    /// sensitive application can use function to disable such behavior and assign
89    /// no resource to `TracerProvider`.
90    pub fn with_no_resource(self) -> Self {
91        self.with_resource(sdk::Resource::empty())
92    }
93}
94
95impl Default for Config {
96    /// Create default global sdk configuration.
97    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}