Skip to content

Commit

Permalink
feat: add max_attributes_per_event and max_attributes_per_link.
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyCpp committed Apr 15, 2021
1 parent 1cdf725 commit 3bb440b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
30 changes: 27 additions & 3 deletions opentelemetry/src/sdk/trace/config.rs
Expand Up @@ -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()
Expand All @@ -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<Arc<sdk::Resource>>,
}
Expand Down Expand Up @@ -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));
Expand All @@ -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,
};

Expand Down
14 changes: 13 additions & 1 deletion opentelemetry/src/sdk/trace/span.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -78,9 +79,20 @@ impl crate::trace::Span for Span {
&mut self,
name: String,
timestamp: SystemTime,
attributes: Vec<KeyValue>,
mut attributes: Vec<KeyValue>,
) {
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))
});
Expand Down
9 changes: 9 additions & 0 deletions opentelemetry/src/sdk/trace/tracer.rs
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions opentelemetry/src/trace/link.rs
Expand Up @@ -30,4 +30,9 @@ impl Link {
pub fn attributes(&self) -> &Vec<KeyValue> {
&self.attributes
}

/// Mutable attributes of the link
pub(crate) fn attributes_mut(&mut self) -> &mut Vec<KeyValue> {
self.attributes.as_mut()
}
}

0 comments on commit 3bb440b

Please sign in to comment.