opentelemetry_spanprocessor_any/sdk/trace/span_limit.rs
1/// # Span limit
2/// Erroneous code can add unintended attributes, events, and links to a span. If these collections
3/// are unbounded, they can quickly exhaust available memory, resulting in crashes that are
4/// difficult to recover from safely.
5///
6/// To protected against those errors. Users can use span limit to configure
7/// - Maximum allowed span attribute count
8/// - Maximum allowed span event count
9/// - Maximum allowed span link count
10/// - Maximum allowed attribute per span event count
11/// - Maximum allowed attribute per span link count
12///
13/// If the limit has been breached. The attributes, events or links will be dropped based on their
14/// index in the collection. The one added to collections later will be dropped first.
15
16#[cfg(feature = "serialize")]
17use serde::{Deserialize, Serialize};
18
19pub(crate) const DEFAULT_MAX_EVENT_PER_SPAN: u32 = 128;
20pub(crate) const DEFAULT_MAX_ATTRIBUTES_PER_SPAN: u32 = 128;
21pub(crate) const DEFAULT_MAX_LINKS_PER_SPAN: u32 = 128;
22pub(crate) const DEFAULT_MAX_ATTRIBUTES_PER_EVENT: u32 = 128;
23pub(crate) const DEFAULT_MAX_ATTRIBUTES_PER_LINK: u32 = 128;
24
25/// Span limit configuration to keep attributes, events and links to a span in a reasonable number.
26#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
27#[derive(Copy, Clone, Debug)]
28pub struct SpanLimits {
29 /// The max events that can be added to a `Span`.
30 pub max_events_per_span: u32,
31 /// The max attributes that can be added to a `Span`.
32 pub max_attributes_per_span: u32,
33 /// The max links that can be added to a `Span`.
34 pub max_links_per_span: u32,
35 /// The max attributes that can be added into an `Event`
36 pub max_attributes_per_event: u32,
37 /// The max attributes that can be added into a `Link`
38 pub max_attributes_per_link: u32,
39}
40
41impl Default for SpanLimits {
42 fn default() -> Self {
43 SpanLimits {
44 max_events_per_span: DEFAULT_MAX_EVENT_PER_SPAN,
45 max_attributes_per_span: DEFAULT_MAX_ATTRIBUTES_PER_SPAN,
46 max_links_per_span: DEFAULT_MAX_LINKS_PER_SPAN,
47 max_attributes_per_link: DEFAULT_MAX_ATTRIBUTES_PER_LINK,
48 max_attributes_per_event: DEFAULT_MAX_ATTRIBUTES_PER_EVENT,
49 }
50 }
51}