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

Implement cardinality limits for metrics streams #1889

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## vNext

- Implement function to set cardinality limits for metrics streams using `set_stream_cardinality_limit` function.
- Add "metrics", "logs" to default features. With this, default feature list is
"trace", "metrics" and "logs".
- Add `with_resource` on Builder for LoggerProvider, replacing the `with_config`
Expand Down
11 changes: 8 additions & 3 deletions opentelemetry-sdk/src/metrics/internal/aggregate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{marker, sync::Arc};
use std::{marker, sync::atomic::AtomicU64, sync::Arc};

use once_cell::sync::Lazy;
use opentelemetry::KeyValue;
Expand All @@ -16,15 +16,20 @@
Number,
};

const STREAM_CARDINALITY_LIMIT: u32 = 2000;
static STREAM_CARDINALITY_LIMIT: AtomicU64 = AtomicU64::new(2000);
pub(crate) static STREAM_OVERFLOW_ATTRIBUTE_SET: Lazy<AttributeSet> = Lazy::new(|| {
let key_values: [KeyValue; 1] = [KeyValue::new("otel.metric.overflow", "true")];
AttributeSet::from(&key_values[..])
});

/// Checks whether aggregator has hit cardinality limit for metric streams
pub(crate) fn is_under_cardinality_limit(size: usize) -> bool {
size < STREAM_CARDINALITY_LIMIT as usize
size < STREAM_CARDINALITY_LIMIT.load(std::sync::atomic::Ordering::Relaxed) as usize
}

/// Set cardinality limit for metric streams
pub fn set_stream_cardinality_limit(size: u64) {
STREAM_CARDINALITY_LIMIT.store(size, std::sync::atomic::Ordering::Relaxed)

Check warning on line 32 in opentelemetry-sdk/src/metrics/internal/aggregate.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/internal/aggregate.rs#L31-L32

Added lines #L31 - L32 were not covered by tests
}

/// Receives measurements to be aggregated.
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/src/metrics/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::ops::{Add, AddAssign, Sub};
use std::sync::atomic::{AtomicI64, AtomicU64, Ordering};
use std::sync::Mutex;

pub use aggregate::set_stream_cardinality_limit;

pub(crate) use aggregate::{AggregateBuilder, ComputeAggregation, Measure};
pub(crate) use exponential_histogram::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};

Expand Down
1 change: 1 addition & 0 deletions opentelemetry-sdk/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub(crate) mod view;

pub use aggregation::*;
pub use instrument::*;
pub use internal::set_stream_cardinality_limit;
pub use manual_reader::*;
pub use meter::*;
pub use meter_provider::*;
Expand Down
Loading