Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dropped attribute counts to events and links #529

Merged
merged 1 commit into from Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions opentelemetry-jaeger/src/exporter/thrift/mod.rs
Expand Up @@ -42,6 +42,14 @@ impl From<Event> for jaeger::Log {
fields.push(Key::new("event").string(event.name).into());
}

if event.dropped_attributes_count != 0 {
fields.push(
Key::new("otel.event.dropped_attributes_count")
.i64(i64::from(event.dropped_attributes_count))
.into(),
);
}

jaeger::Log::new(timestamp, fields)
}
}
Expand Down
12 changes: 6 additions & 6 deletions opentelemetry-otlp/src/transform/traces.rs
Expand Up @@ -50,7 +50,7 @@ mod tonic {
.to_vec(),
trace_state: link.span_context().trace_state().header(),
attributes: Attributes::from(link.attributes().clone()).0,
dropped_attributes_count: 0,
dropped_attributes_count: link.dropped_attributes_count(),
}
}
}
Expand Down Expand Up @@ -103,7 +103,7 @@ mod tonic {
time_unix_nano: to_nanos(event.timestamp),
name: event.name.into(),
attributes: Attributes::from(event.attributes).0,
dropped_attributes_count: 0,
dropped_attributes_count: event.dropped_attributes_count,
})
.collect(),
dropped_links_count: source_span.links.dropped_count(),
Expand Down Expand Up @@ -179,7 +179,7 @@ mod prost {
.to_vec(),
trace_state: link.span_context().trace_state().header(),
attributes: Attributes::from(link.attributes().clone()).0,
dropped_attributes_count: 0,
dropped_attributes_count: link.dropped_attributes_count(),
}
}
}
Expand Down Expand Up @@ -232,7 +232,7 @@ mod prost {
time_unix_nano: to_nanos(event.timestamp),
name: event.name.into(),
attributes: Attributes::from(event.attributes).0,
dropped_attributes_count: 0,
dropped_attributes_count: event.dropped_attributes_count,
})
.collect(),
dropped_links_count: source_span.links.dropped_count(),
Expand Down Expand Up @@ -311,7 +311,7 @@ mod grpcio {
.to_vec(),
trace_state: link.span_context().trace_state().header(),
attributes: Attributes::from(link.attributes().clone()).0,
dropped_attributes_count: 0,
dropped_attributes_count: link.dropped_attributes_count(),
..Default::default()
}
}
Expand Down Expand Up @@ -367,7 +367,7 @@ mod grpcio {
time_unix_nano: to_nanos(event.timestamp),
name: event.name.into(),
attributes: Attributes::from(event.attributes).0,
dropped_attributes_count: 0,
dropped_attributes_count: event.dropped_attributes_count,
..Default::default()
})
.collect(),
Expand Down
28 changes: 14 additions & 14 deletions opentelemetry/src/sdk/trace/config.rs
Expand Up @@ -20,8 +20,8 @@ pub struct Config {
pub sampler: Box<dyn sdk::trace::ShouldSample>,
/// The id generator that the sdk should use
pub id_generator: Box<dyn IdGenerator>,
/// span limit
pub span_limit: SpanLimits,
/// span limits
pub span_limits: SpanLimits,
/// Contains attributes representing an entity that produces telemetry.
pub resource: Option<Arc<sdk::Resource>>,
}
Expand All @@ -41,37 +41,37 @@ impl Config {

/// Specify the number of events to be recorded per span.
pub fn with_max_events_per_span(mut self, max_events: u32) -> Self {
self.span_limit.max_events_per_span = max_events;
self.span_limits.max_events_per_span = max_events;
self
}

/// Specify the number of attributes to be recorded per span.
pub fn with_max_attributes_per_span(mut self, max_attributes: u32) -> Self {
self.span_limit.max_attributes_per_span = max_attributes;
self.span_limits.max_attributes_per_span = max_attributes;
self
}

/// Specify the number of events to be recorded per span.
pub fn with_max_links_per_span(mut self, max_links: u32) -> Self {
self.span_limit.max_links_per_span = max_links;
self.span_limits.max_links_per_span = max_links;
self
}

/// Specify the number of attributes one event can have.
pub fn with_max_attributes_per_event(mut self, max_attributes: u32) -> Self {
self.span_limit.max_attributes_per_event = max_attributes;
self.span_limits.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.span_limit.max_attributes_per_link = max_attributes;
self.span_limits.max_attributes_per_link = max_attributes;
self
}

/// Specify all limit via the span_limit
pub fn with_span_limit(mut self, span_limit: SpanLimits) -> Self {
self.span_limit = span_limit;
/// Specify all limit via the span_limits
pub fn with_span_limits(mut self, span_limits: SpanLimits) -> Self {
self.span_limits = span_limits;
self
}

Expand All @@ -88,29 +88,29 @@ 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()),
span_limit: SpanLimits::default(),
span_limits: SpanLimits::default(),
resource: None,
};

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_limit.max_attributes_per_span = max_attributes_per_span;
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_limit.max_events_per_span = max_events_per_span;
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_limit.max_links_per_span = max_links_per_span;
config.span_limits.max_links_per_span = max_links_per_span;
}

config
Expand Down
29 changes: 16 additions & 13 deletions opentelemetry/src/sdk/trace/span.rs
Expand Up @@ -21,7 +21,7 @@ pub struct Span {
span_context: SpanContext,
data: Option<SpanData>,
tracer: sdk::trace::Tracer,
span_limit: SpanLimits,
span_limits: SpanLimits,
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -59,7 +59,7 @@ impl Span {
span_context,
data,
tracer,
span_limit,
span_limits: span_limit,
}
}

Expand All @@ -84,14 +84,17 @@ impl crate::trace::Span for Span {
timestamp: SystemTime,
mut attributes: Vec<KeyValue>,
) {
let max_attributes_per_event = self.span_limit.max_attributes_per_event as usize;
let event_attributes_limit = self.span_limits.max_attributes_per_event as usize;
self.with_data(|data| {
if attributes.len() > max_attributes_per_event {
attributes.truncate(max_attributes_per_event);
}

data.message_events
.push_back(Event::new(name, timestamp, attributes))
let dropped_attributes_count = attributes.len().saturating_sub(event_attributes_limit);
attributes.truncate(event_attributes_limit);

data.message_events.push_back(Event::new(
name,
timestamp,
attributes,
dropped_attributes_count as u32,
))
});
}

Expand Down Expand Up @@ -231,11 +234,11 @@ mod tests {
start_time: crate::time::now(),
end_time: crate::time::now(),
attributes: sdk::trace::EvictedHashMap::new(
config.span_limit.max_attributes_per_span,
config.span_limits.max_attributes_per_span,
0,
),
message_events: sdk::trace::EvictedQueue::new(config.span_limit.max_events_per_span),
links: sdk::trace::EvictedQueue::new(config.span_limit.max_links_per_span),
message_events: sdk::trace::EvictedQueue::new(config.span_limits.max_events_per_span),
links: sdk::trace::EvictedQueue::new(config.span_limits.max_links_per_span),
status_code: StatusCode::Unset,
status_message: "".into(),
};
Expand Down Expand Up @@ -531,7 +534,7 @@ mod tests {
Vec::new(),
);
for i in 0..(DEFAULT_MAX_ATTRIBUTES_PER_LINK * 2) {
link.attributes_mut()
link.attributes
.push(KeyValue::new(format!("key {}", i), i.to_string()));
}

Expand Down
31 changes: 17 additions & 14 deletions opentelemetry/src/sdk/trace/tracer.rs
Expand Up @@ -178,7 +178,7 @@ impl crate::trace::Tracer for Tracer {

let provider = provider.unwrap();
let config = provider.config();
let span_limit = config.span_limit;
let span_limits = config.span_limits;
let span_id = builder
.span_id
.take()
Expand Down Expand Up @@ -265,30 +265,33 @@ impl crate::trace::Tracer for Tracer {
span_trace_state = trace_state;
attribute_options.append(&mut extra_attrs);
let mut attributes =
EvictedHashMap::new(span_limit.max_attributes_per_span, attribute_options.len());
EvictedHashMap::new(span_limits.max_attributes_per_span, attribute_options.len());
for attribute in attribute_options {
attributes.insert(attribute);
}
let mut links = EvictedQueue::new(span_limit.max_links_per_span);
let mut links = EvictedQueue::new(span_limits.max_links_per_span);
if let Some(link_options) = &mut link_options {
let link_attributes_limit = span_limits.max_attributes_per_link as usize;
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() > span_limit.max_attributes_per_link as usize {
attributes.truncate(span_limit.max_attributes_per_link as usize);
}
let dropped_attributes_count =
link.attributes.len().saturating_sub(link_attributes_limit);
link.attributes.truncate(link_attributes_limit);
link.dropped_attributes_count = dropped_attributes_count as u32;
}
links.append_vec(link_options);
}
let start_time = start_time.unwrap_or_else(crate::time::now);
let end_time = end_time.unwrap_or(start_time);
let mut message_events_queue = EvictedQueue::new(span_limit.max_events_per_span);
let mut message_events_queue = EvictedQueue::new(span_limits.max_events_per_span);
if let Some(mut events) = message_events {
let event_attributes_limit = span_limits.max_attributes_per_event as usize;
for event in events.iter_mut() {
let attributes = &mut event.attributes;
if attributes.len() > span_limit.max_attributes_per_event as usize {
attributes.truncate(span_limit.max_attributes_per_event as usize);
}
let dropped_attributes_count = event
.attributes
.len()
.saturating_sub(event_attributes_limit);
event.attributes.truncate(event_attributes_limit);
event.dropped_attributes_count = dropped_attributes_count as u32;
}
message_events_queue.append_vec(&mut events);
}
Expand All @@ -310,7 +313,7 @@ impl crate::trace::Tracer for Tracer {
});

let span_context = SpanContext::new(trace_id, span_id, flags, false, span_trace_state);
let span = Span::new(span_context, inner, self.clone(), span_limit);
let span = Span::new(span_context, inner, self.clone(), span_limits);

// Call `on_start` for all processors
for processor in provider.span_processors() {
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry/src/testing/trace.rs
Expand Up @@ -46,9 +46,9 @@ pub fn new_test_export_span_data() -> SpanData {
name: "opentelemetry".into(),
start_time: crate::time::now(),
end_time: crate::time::now(),
attributes: EvictedHashMap::new(config.span_limit.max_attributes_per_span, 0),
message_events: EvictedQueue::new(config.span_limit.max_events_per_span),
links: EvictedQueue::new(config.span_limit.max_links_per_span),
attributes: EvictedHashMap::new(config.span_limits.max_attributes_per_span, 0),
message_events: EvictedQueue::new(config.span_limits.max_events_per_span),
links: EvictedQueue::new(config.span_limits.max_links_per_span),
status_code: StatusCode::Unset,
status_message: "".into(),
resource: config.resource,
Expand Down
5 changes: 5 additions & 0 deletions opentelemetry/src/trace/event.rs
Expand Up @@ -17,6 +17,8 @@ pub struct Event {
pub timestamp: SystemTime,
/// Event attributes
pub attributes: Vec<KeyValue>,
/// Number of dropped attributes
pub dropped_attributes_count: u32,
}

impl Event {
Expand All @@ -25,11 +27,13 @@ impl Event {
name: T,
timestamp: SystemTime,
attributes: Vec<KeyValue>,
dropped_attributes_count: u32,
) -> Self {
Event {
name: name.into(),
timestamp,
attributes,
dropped_attributes_count,
}
}

Expand All @@ -39,6 +43,7 @@ impl Event {
name: name.into(),
timestamp: crate::time::now(),
attributes: Vec::new(),
dropped_attributes_count: 0,
}
}
}
10 changes: 6 additions & 4 deletions opentelemetry/src/trace/link.rs
Expand Up @@ -9,7 +9,8 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq)]
pub struct Link {
span_context: SpanContext,
attributes: Vec<KeyValue>,
pub(crate) attributes: Vec<KeyValue>,
pub(crate) dropped_attributes_count: u32,
}

impl Link {
Expand All @@ -18,6 +19,7 @@ impl Link {
Link {
span_context,
attributes,
dropped_attributes_count: 0,
}
}

Expand All @@ -31,8 +33,8 @@ impl Link {
&self.attributes
}

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