Skip to content
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

- Add optional `metrics-use-foldhash` feature flag to use [foldhash](https://github.com/orlp/foldhash) for `ValueMap` HashMaps in the metrics hot path, providing ~30% improvement over the default std `SipHash` hasher. [#3388](https://github.com/open-telemetry/opentelemetry-rust/pull/3388)
- Add 32-bit platform support by using `portable-atomic` for `AtomicI64` and `AtomicU64` in the metrics module. This enables compilation on 32-bit ARM targets (e.g., `armv5te-unknown-linux-gnueabi`, `armv7-unknown-linux-gnueabihf`).
- `Aggregation` enum and `StreamBuilder::with_aggregation()` are now stable and no longer require the `spec_unstable_metrics_views` feature flag.
- Fix `service.name` Resource attribute fallback to follow OpenTelemetry
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ autobenches = false

[dependencies]
opentelemetry = { workspace = true }
foldhash = { version = "0.1", optional = true }
opentelemetry-http = { workspace = true, optional = true }
futures-channel = { workspace = true }
futures-executor = { workspace = true }
Expand Down Expand Up @@ -59,6 +60,7 @@ experimental_logs_batch_log_processor_with_async_runtime = ["logs", "experimenta
experimental_logs_concurrent_log_processor = ["logs"]
experimental_trace_batch_span_processor_with_async_runtime = ["tokio/sync", "trace", "experimental_async_runtime"]
experimental_metrics_disable_name_validation = ["metrics"]
metrics-use-foldhash = ["metrics", "foldhash"]
bench_profiling = []

[[bench]]
Expand Down
23 changes: 20 additions & 3 deletions opentelemetry-sdk/src/metrics/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::fmt;
#[cfg(not(target_has_atomic = "64"))]
use portable_atomic::{AtomicI64, AtomicU64};
use std::cmp::min;
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use std::mem::swap;
use std::ops::{Add, AddAssign, DerefMut, Sub};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
Expand All @@ -24,6 +24,23 @@ use opentelemetry::{otel_warn, KeyValue};
use super::data::{AggregatedMetrics, MetricData};
use super::pipeline::DEFAULT_CARDINALITY_LIMIT;

#[cfg(feature = "metrics-use-foldhash")]
type HashMap<K, V> = std::collections::HashMap<K, V, foldhash::fast::RandomState>;
#[cfg(not(feature = "metrics-use-foldhash"))]
type HashMap<K, V> = std::collections::HashMap<K, V>;

#[cfg(feature = "metrics-use-foldhash")]
fn new_hashmap<K, V>(capacity: usize) -> HashMap<K, V> {
std::collections::HashMap::with_capacity_and_hasher(
capacity,
foldhash::fast::RandomState::default(),
)
}
#[cfg(not(feature = "metrics-use-foldhash"))]
fn new_hashmap<K, V>(capacity: usize) -> HashMap<K, V> {
std::collections::HashMap::with_capacity(capacity)
}

// TODO Replace it with LazyLock once it is stable
pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: OnceLock<Vec<KeyValue>> = OnceLock::new();

Expand Down Expand Up @@ -85,7 +102,7 @@ where
{
fn new(config: A::InitConfig, cardinality_limit: usize) -> Self {
ValueMap {
trackers: RwLock::new(HashMap::with_capacity(
trackers: RwLock::new(new_hashmap(
1 + min(DEFAULT_CARDINALITY_LIMIT, cardinality_limit),
)),
trackers_for_collect: OnceLock::new(),
Expand All @@ -100,7 +117,7 @@ where
#[inline]
fn trackers_for_collect(&self) -> &RwLock<HashMap<Vec<KeyValue>, Arc<A>>> {
self.trackers_for_collect.get_or_init(|| {
RwLock::new(HashMap::with_capacity(
RwLock::new(new_hashmap(
1 + min(DEFAULT_CARDINALITY_LIMIT, self.cardinality_limit),
))
})
Expand Down