Skip to content

Commit

Permalink
opentelememtry: switch MetricsMap to use &'static str
Browse files Browse the repository at this point in the history
This patch updates the `MetricsMap` type to use static strings, this
will reduce the number of allocations needed for the metrics processing.
  • Loading branch information
bryangarza committed Jul 8, 2022
1 parent ac2b3bf commit 3122f7a
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions tracing-opentelemetry/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub(crate) struct Instruments {
f64_value_recorder: MetricsMap<ValueRecorder<f64>>,
}

type MetricsMap<T> = RwLock<HashMap<String, T>>;
type MetricsMap<T> = RwLock<HashMap<&'static str, T>>;

#[derive(Copy, Clone, Debug)]
pub(crate) enum InstrumentType {
Expand All @@ -45,11 +45,11 @@ impl Instruments {
&self,
meter: &Meter,
instrument_type: InstrumentType,
metric_name: &str,
metric_name: &'static str,
) {
fn update_or_insert<T>(
map: &MetricsMap<T>,
name: &str,
name: &'static str,
insert: impl FnOnce() -> T,
update: impl FnOnce(&T),
) {
Expand All @@ -66,7 +66,7 @@ impl Instruments {
let mut lock = map.write().unwrap();
// handle the case where the entry was created while we were waiting to
// acquire the write lock
let metric = lock.entry(name.to_owned()).or_insert_with(insert);
let metric = lock.entry(name).or_insert_with(insert);
update(metric)
}

Expand Down

0 comments on commit 3122f7a

Please sign in to comment.