Skip to content

Commit

Permalink
enhancement: honor span limits when collecting events
Browse files Browse the repository at this point in the history
  • Loading branch information
mladedav committed Feb 15, 2024
1 parent bd90c86 commit 451abd5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,20 @@ where
let mut otel_data = span.extensions_mut().remove::<OtelData>();
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(),
Expand Down
10 changes: 10 additions & 0 deletions src/tracer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<opentelemetry_sdk::trace::SpanLimits> {
None
}
}

impl PreSampledTracer for noop::NoopTracer {
Expand Down Expand Up @@ -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<opentelemetry_sdk::trace::SpanLimits> {
self.provider()
.map(|provider| provider.config().span_limits)
}
}

fn current_trace_state(
Expand Down

0 comments on commit 451abd5

Please sign in to comment.