opentelemetry_sdk/trace/
config.rsuse crate::trace::{span_limit::SpanLimits, IdGenerator, RandomIdGenerator, Sampler, ShouldSample};
use crate::Resource;
use opentelemetry::global::{handle_error, Error};
use std::borrow::Cow;
use std::env;
use std::str::FromStr;
#[deprecated(since = "0.23.0", note = "Use Config::default() instead")]
pub fn config() -> Config {
Config::default()
}
#[derive(Debug)]
#[non_exhaustive]
pub struct Config {
pub sampler: Box<dyn ShouldSample>,
pub id_generator: Box<dyn IdGenerator>,
pub span_limits: SpanLimits,
pub resource: Cow<'static, Resource>,
}
impl Config {
pub fn with_sampler<T: crate::trace::ShouldSample + 'static>(mut self, sampler: T) -> Self {
self.sampler = Box::new(sampler);
self
}
pub fn with_id_generator<T: IdGenerator + 'static>(mut self, id_generator: T) -> Self {
self.id_generator = Box::new(id_generator);
self
}
pub fn with_max_events_per_span(mut self, max_events: u32) -> Self {
self.span_limits.max_events_per_span = max_events;
self
}
pub fn with_max_attributes_per_span(mut self, max_attributes: u32) -> Self {
self.span_limits.max_attributes_per_span = max_attributes;
self
}
pub fn with_max_links_per_span(mut self, max_links: u32) -> Self {
self.span_limits.max_links_per_span = max_links;
self
}
pub fn with_max_attributes_per_event(mut self, max_attributes: u32) -> Self {
self.span_limits.max_attributes_per_event = max_attributes;
self
}
pub fn with_max_attributes_per_link(mut self, max_attributes: u32) -> Self {
self.span_limits.max_attributes_per_link = max_attributes;
self
}
pub fn with_span_limits(mut self, span_limits: SpanLimits) -> Self {
self.span_limits = span_limits;
self
}
pub fn with_resource(mut self, resource: Resource) -> Self {
self.resource = Cow::Owned(resource);
self
}
}
impl Default for Config {
fn default() -> Self {
let mut config = Config {
sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
id_generator: Box::<RandomIdGenerator>::default(),
span_limits: SpanLimits::default(),
resource: Cow::Owned(Resource::default()),
};
if let Some(max_attributes_per_span) = env::var("OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT")
.ok()
.and_then(|count_limit| u32::from_str(&count_limit).ok())
{
config.span_limits.max_attributes_per_span = max_attributes_per_span;
}
if let Some(max_events_per_span) = env::var("OTEL_SPAN_EVENT_COUNT_LIMIT")
.ok()
.and_then(|max_events| u32::from_str(&max_events).ok())
{
config.span_limits.max_events_per_span = max_events_per_span;
}
if let Some(max_links_per_span) = env::var("OTEL_SPAN_LINK_COUNT_LIMIT")
.ok()
.and_then(|max_links| u32::from_str(&max_links).ok())
{
config.span_limits.max_links_per_span = max_links_per_span;
}
let sampler_arg = env::var("OTEL_TRACES_SAMPLER_ARG").ok();
if let Ok(sampler) = env::var("OTEL_TRACES_SAMPLER") {
config.sampler = match sampler.as_str() {
"always_on" => Box::new(Sampler::AlwaysOn),
"always_off" => Box::new(Sampler::AlwaysOff),
"traceidratio" => {
let ratio = sampler_arg.and_then(|r| r.parse::<f64>().ok());
if let Some(r) = ratio {
Box::new(Sampler::TraceIdRatioBased(r))
} else {
handle_error(
Error::Other(String::from(
"Missing or invalid OTEL_TRACES_SAMPLER_ARG value. Falling back to default: 1.0"))
);
Box::new(Sampler::TraceIdRatioBased(1.0))
}
}
"parentbased_always_on" => {
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
}
"parentbased_always_off" => {
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOff)))
}
"parentbased_traceidratio" => {
let ratio = sampler_arg.and_then(|r| r.parse::<f64>().ok());
if let Some(r) = ratio {
Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
r,
))))
} else {
handle_error(
Error::Other(String::from(
"Missing or invalid OTEL_TRACES_SAMPLER_ARG value. Falling back to default: 1.0"
)));
Box::new(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(
1.0,
))))
}
}
"parentbased_jaeger_remote" => {
handle_error(
Error::Other(String::from(
"Unimplemented parentbased_jaeger_remote sampler. Falling back to default: parentbased_always_on"
)));
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
}
"jaeger_remote" => {
handle_error(
Error::Other(String::from("Unimplemented jaeger_remote sampler. Falling back to default: parentbased_always_on")));
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
}
"xray" => {
handle_error(
Error::Other(String::from("Unimplemented xray sampler. Falling back to default: parentbased_always_on")));
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
}
s => {
handle_error(
Error::Other(format!("Unrecognised OTEL_TRACES_SAMPLER value: {}. Falling back to default: parentbased_always_on",
s
)));
Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn)))
}
}
}
config
}
}