From 451abd56e7a33af1016cb13e2f3d0c828c70fcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ml=C3=A1dek?= Date: Fri, 16 Feb 2024 00:31:48 +0100 Subject: [PATCH] enhancement: honor span limits when collecting events --- src/layer.rs | 14 ++++++++++++++ src/tracer.rs | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/layer.rs b/src/layer.rs index 2a16d47..376dc58 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -1022,6 +1022,20 @@ where let mut otel_data = span.extensions_mut().remove::(); let span_builder = otel_data.as_mut().map(|data| &mut data.builder); + let limits = self.tracer.span_limits(); + + if span_builder + .as_ref() + .and_then(|span_builder| span_builder.events.as_ref().map(|events| events.len())) + .zip(limits) + .map_or(false, |(current_length, limits)| { + current_length >= limits.max_events_per_span as usize + }) + { + // We have reached the configured limit for events so there is no point in storing any more. + return; + } + let mut otel_event = otel::Event::new( String::new(), crate::time::now(), diff --git a/src/tracer.rs b/src/tracer.rs index c37966b..b45ec58 100644 --- a/src/tracer.rs +++ b/src/tracer.rs @@ -47,6 +47,11 @@ pub trait PreSampledTracer { /// Generate a new span id. fn new_span_id(&self) -> otel::SpanId; + + /// Gets the current span limits. + fn span_limits(&self) -> Option { + None + } } impl PreSampledTracer for noop::NoopTracer { @@ -111,6 +116,11 @@ impl PreSampledTracer for SdkTracer { .map(|provider| provider.config().id_generator.new_span_id()) .unwrap_or(otel::SpanId::INVALID) } + + fn span_limits(&self) -> Option { + self.provider() + .map(|provider| provider.config().span_limits) + } } fn current_trace_state(