Skip to content

refactor: Replace OnceCell with std::sync::OnceLock #2899

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion opentelemetry-prometheus/examples/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use hyper::{
};
use hyper_util::rt::{TokioExecutor, TokioIo};
use once_cell::sync::Lazy;
use opentelemetry::time::now;
use opentelemetry::{
metrics::{Counter, Histogram, MeterProvider as _},
KeyValue,
Expand All @@ -15,7 +16,6 @@ use opentelemetry_sdk::metrics::SdkMeterProvider;
use prometheus::{Encoder, Registry, TextEncoder};
use std::net::SocketAddr;
use std::sync::Arc;
use opentelemetry::time::now;
use tokio::net::TcpListener;

static HANDLER_ALL: Lazy<[KeyValue; 1]> = Lazy::new(|| [KeyValue::new("handler", "all")]);
Expand Down
7 changes: 3 additions & 4 deletions opentelemetry-prometheus/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::fmt;
use once_cell::sync::OnceCell;
use opentelemetry_sdk::metrics::{ManualReaderBuilder, MetricError, MetricResult};
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, OnceLock};

use crate::{Collector, PrometheusExporter, ResourceSelector};

Expand Down Expand Up @@ -124,11 +123,11 @@ impl ExporterBuilder {
without_units: self.without_units,
without_counter_suffixes: self.without_counter_suffixes,
disable_scope_info: self.disable_scope_info,
create_target_info_once: OnceCell::new(),
create_target_info_once: OnceLock::new(),
namespace: self.namespace,
inner: Mutex::new(Default::default()),
resource_selector: self.resource_selector,
resource_labels_once: OnceCell::new(),
resource_labels_once: OnceLock::new(),
};

let registry = self.registry.unwrap_or_default();
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-prometheus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
)]
#![cfg_attr(test, deny(warnings))]

use once_cell::sync::{Lazy, OnceCell};
use once_cell::sync::Lazy;
use opentelemetry::{otel_error, otel_warn, InstrumentationScope, Key, Value};
use opentelemetry_sdk::{
error::OTelSdkResult,
Expand All @@ -125,7 +125,7 @@ use std::{
any::TypeId,
borrow::Cow,
collections::{BTreeMap, HashMap},
sync::{Arc, Mutex},
sync::{Arc, Mutex, OnceLock},
};
use std::{fmt, sync::Weak};

Expand Down Expand Up @@ -189,8 +189,8 @@ struct Collector {
without_units: bool,
without_counter_suffixes: bool,
disable_scope_info: bool,
create_target_info_once: OnceCell<MetricFamily>,
resource_labels_once: OnceCell<Vec<LabelPair>>,
create_target_info_once: OnceLock<MetricFamily>,
resource_labels_once: OnceLock<Vec<LabelPair>>,
namespace: Option<String>,
inner: Mutex<CollectorInner>,
resource_selector: ResourceSelector,
Expand Down