From 3bb440b150dae7e73fab092465a6971effb36d05 Mon Sep 17 00:00:00 2001 From: "zhongyang.wu" Date: Wed, 14 Apr 2021 20:21:36 -0400 Subject: [PATCH] feat: add max_attributes_per_event and max_attributes_per_link. --- opentelemetry/src/sdk/trace/config.rs | 30 ++++++++++++++++++++++++--- opentelemetry/src/sdk/trace/span.rs | 14 ++++++++++++- opentelemetry/src/sdk/trace/tracer.rs | 9 ++++++++ opentelemetry/src/trace/link.rs | 5 +++++ 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/opentelemetry/src/sdk/trace/config.rs b/opentelemetry/src/sdk/trace/config.rs index 04a4e711c2..b6a299a34b 100644 --- a/opentelemetry/src/sdk/trace/config.rs +++ b/opentelemetry/src/sdk/trace/config.rs @@ -7,6 +7,12 @@ use std::env; use std::str::FromStr; use std::sync::Arc; +pub(crate) const DEFAULT_MAX_EVENT_PER_SPAN: u32 = 128; +pub(crate) const DEFAULT_MAX_ATTRIBUTES_PER_SPAN: u32 = 128; +pub(crate) const DEFAULT_MAX_LINKS_PER_SPAN: u32 = 128; +pub(crate) const DEFAULT_MAX_ATTRIBUTES_PER_EVENT: u32 = 128; +pub(crate) const DEFAULT_MAX_ATTRIBUTES_PER_LINK: u32 = 128; + /// Default trace configuration pub fn config() -> Config { Config::default() @@ -25,6 +31,10 @@ pub struct Config { pub max_attributes_per_span: u32, /// The max links that can be added to a `Span`. pub max_links_per_span: u32, + /// The max attributes that can be added into an `Event` + pub max_attributes_per_event: u32, + /// The max attributes that can be added into a `Link` + pub max_attributes_per_link: u32, /// Contains attributes representing an entity that produces telemetry. pub resource: Option>, } @@ -60,6 +70,18 @@ impl Config { self } + /// Specify the number of attributes one event can have. + pub fn with_max_attributes_per_event(mut self, max_attributes: u32) -> Self { + self.max_attributes_per_event = max_attributes; + self + } + + /// Specify the number of attributes one link can have. + pub fn with_max_attributes_per_link(mut self, max_attributes: u32) -> Self { + self.max_attributes_per_link = max_attributes; + self + } + /// Specify the attributes representing the entity that produces telemetry pub fn with_resource(mut self, resource: sdk::Resource) -> Self { self.resource = Some(Arc::new(resource)); @@ -73,9 +95,11 @@ impl Default for Config { let mut config = Config { sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))), id_generator: Box::new(sdk::trace::IdGenerator::default()), - max_events_per_span: 128, - max_attributes_per_span: 128, - max_links_per_span: 128, + max_events_per_span: DEFAULT_MAX_EVENT_PER_SPAN, + max_attributes_per_span: DEFAULT_MAX_ATTRIBUTES_PER_SPAN, + max_links_per_span: DEFAULT_MAX_LINKS_PER_SPAN, + max_attributes_per_link: DEFAULT_MAX_ATTRIBUTES_PER_LINK, + max_attributes_per_event: DEFAULT_MAX_ATTRIBUTES_PER_EVENT, resource: None, }; diff --git a/opentelemetry/src/sdk/trace/span.rs b/opentelemetry/src/sdk/trace/span.rs index 06942afb4c..b3ffb38c07 100644 --- a/opentelemetry/src/sdk/trace/span.rs +++ b/opentelemetry/src/sdk/trace/span.rs @@ -8,6 +8,7 @@ //! start time is set to the current time on span creation. After the `Span` is created, it //! is possible to change its name, set its `Attributes`, and add `Links` and `Events`. //! These cannot be changed after the `Span`'s end time has been set. +use crate::sdk::trace::config::DEFAULT_MAX_ATTRIBUTES_PER_EVENT; use crate::trace::{Event, SpanContext, SpanId, SpanKind, StatusCode}; use crate::{sdk, trace, KeyValue}; use std::borrow::Cow; @@ -78,9 +79,20 @@ impl crate::trace::Span for Span { &mut self, name: String, timestamp: SystemTime, - attributes: Vec, + mut attributes: Vec, ) { + let max_attributes_per_event = + self.tracer + .provider() + .map(|provider| provider.config().max_attributes_per_event) + .unwrap_or(DEFAULT_MAX_ATTRIBUTES_PER_EVENT) as usize; self.with_data(|data| { + if attributes.len() > max_attributes_per_event { + let _dropped: Vec<_> = attributes + .drain((max_attributes_per_event + 1)..) + .collect(); + } + data.message_events .push_back(Event::new(name, timestamp, attributes)) }); diff --git a/opentelemetry/src/sdk/trace/tracer.rs b/opentelemetry/src/sdk/trace/tracer.rs index 1ba8cb01d4..7a16cb985e 100644 --- a/opentelemetry/src/sdk/trace/tracer.rs +++ b/opentelemetry/src/sdk/trace/tracer.rs @@ -259,6 +259,15 @@ impl crate::trace::Tracer for Tracer { } let mut links = EvictedQueue::new(config.max_links_per_span); if let Some(link_options) = &mut link_options { + for link in link_options.iter_mut() { + // make sure the attributes is less than max_attribute_per_link + let attributes = link.attributes_mut(); + if attributes.len() > config.max_attributes_per_link as usize { + let _dropped: Vec<_> = attributes + .drain((config.max_attributes_per_link as usize + 1)..) + .collect(); + } + } links.append_vec(link_options); } let start_time = start_time.unwrap_or_else(crate::time::now); diff --git a/opentelemetry/src/trace/link.rs b/opentelemetry/src/trace/link.rs index 8405785e82..465ac7a9d6 100644 --- a/opentelemetry/src/trace/link.rs +++ b/opentelemetry/src/trace/link.rs @@ -30,4 +30,9 @@ impl Link { pub fn attributes(&self) -> &Vec { &self.attributes } + + /// Mutable attributes of the link + pub(crate) fn attributes_mut(&mut self) -> &mut Vec { + self.attributes.as_mut() + } }