diff --git a/opentelemetry-sdk/Cargo.toml b/opentelemetry-sdk/Cargo.toml index bf03c8d34a..39b5fddfa2 100644 --- a/opentelemetry-sdk/Cargo.toml +++ b/opentelemetry-sdk/Cargo.toml @@ -17,7 +17,6 @@ async-trait = { workspace = true, optional = true } futures-channel = "0.3" futures-executor = { workspace = true } futures-util = { workspace = true, features = ["std", "sink", "async-await-macro"] } -once_cell = { workspace = true } percent-encoding = { version = "2.0", optional = true } rand = { workspace = true, features = ["std", "std_rng","small_rng"], optional = true } glob = { version = "0.3.1", optional =true} diff --git a/opentelemetry-sdk/src/logs/log_emitter.rs b/opentelemetry-sdk/src/logs/log_emitter.rs index 1ebe76de9a..83670c3efc 100644 --- a/opentelemetry-sdk/src/logs/log_emitter.rs +++ b/opentelemetry-sdk/src/logs/log_emitter.rs @@ -15,10 +15,10 @@ use std::{ }, }; -use once_cell::sync::Lazy; +use std::sync::LazyLock; // a no nop logger provider used as placeholder when the provider is shutdown -static NOOP_LOGGER_PROVIDER: Lazy = Lazy::new(|| LoggerProvider { +static NOOP_LOGGER_PROVIDER: LazyLock = LazyLock::new(|| LoggerProvider { inner: Arc::new(LoggerProviderInner { processors: Vec::new(), resource: Resource::empty(), diff --git a/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs b/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs index c0d4263946..43c36726af 100644 --- a/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs +++ b/opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs @@ -1,7 +1,7 @@ use std::{f64::consts::LOG2_E, mem::replace, ops::DerefMut, sync::Mutex, time::SystemTime}; -use once_cell::sync::Lazy; use opentelemetry::{otel_debug, KeyValue}; +use std::sync::LazyLock; use crate::metrics::{ data::{self, Aggregation}, @@ -166,7 +166,7 @@ fn scale_change(max_size: i32, bin: i32, start_bin: i32, length: i32) -> u32 { } /// Constants used in calculating the logarithm index. -static SCALE_FACTORS: Lazy<[f64; 21]> = Lazy::new(|| { +static SCALE_FACTORS: LazyLock<[f64; 21]> = LazyLock::new(|| { [ LOG2_E * 2f64.powi(0), LOG2_E * 2f64.powi(1), diff --git a/opentelemetry-sdk/src/metrics/internal/mod.rs b/opentelemetry-sdk/src/metrics/internal/mod.rs index 4eaea7972c..fc64799b05 100644 --- a/opentelemetry-sdk/src/metrics/internal/mod.rs +++ b/opentelemetry-sdk/src/metrics/internal/mod.rs @@ -15,11 +15,11 @@ use std::sync::{Arc, RwLock}; use aggregate::is_under_cardinality_limit; pub(crate) use aggregate::{AggregateBuilder, ComputeAggregation, Measure}; pub(crate) use exponential_histogram::{EXPO_MAX_SCALE, EXPO_MIN_SCALE}; -use once_cell::sync::Lazy; use opentelemetry::{otel_warn, KeyValue}; +use std::sync::LazyLock; -pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: Lazy> = - Lazy::new(|| vec![KeyValue::new("otel.metric.overflow", "true")]); +pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: LazyLock> = + LazyLock::new(|| vec![KeyValue::new("otel.metric.overflow", "true")]); pub(crate) trait Aggregator { /// A static configuration that is needed in order to initialize aggregator. diff --git a/opentelemetry-sdk/src/propagation/baggage.rs b/opentelemetry-sdk/src/propagation/baggage.rs index 35567b4b4b..a76547e6a9 100644 --- a/opentelemetry-sdk/src/propagation/baggage.rs +++ b/opentelemetry-sdk/src/propagation/baggage.rs @@ -1,4 +1,3 @@ -use once_cell::sync::Lazy; use opentelemetry::{ baggage::{BaggageExt, KeyValueMetadata}, otel_warn, @@ -7,10 +6,11 @@ use opentelemetry::{ }; use percent_encoding::{percent_decode_str, utf8_percent_encode, AsciiSet, CONTROLS}; use std::iter; +use std::sync::LazyLock; static BAGGAGE_HEADER: &str = "baggage"; const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b';').add(b',').add(b'='); -static BAGGAGE_FIELDS: Lazy<[String; 1]> = Lazy::new(|| [BAGGAGE_HEADER.to_owned()]); +static BAGGAGE_FIELDS: LazyLock<[String; 1]> = LazyLock::new(|| [BAGGAGE_HEADER.to_owned()]); /// Propagates name-value pairs in [W3C Baggage] format. /// diff --git a/opentelemetry-sdk/src/propagation/trace_context.rs b/opentelemetry-sdk/src/propagation/trace_context.rs index ced269c2d2..ad9039769e 100644 --- a/opentelemetry-sdk/src/propagation/trace_context.rs +++ b/opentelemetry-sdk/src/propagation/trace_context.rs @@ -1,21 +1,21 @@ //! # W3C Trace Context Propagator //! -use once_cell::sync::Lazy; use opentelemetry::{ propagation::{text_map_propagator::FieldIter, Extractor, Injector, TextMapPropagator}, trace::{SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState}, Context, }; use std::str::FromStr; +use std::sync::LazyLock; const SUPPORTED_VERSION: u8 = 0; const MAX_VERSION: u8 = 254; const TRACEPARENT_HEADER: &str = "traceparent"; const TRACESTATE_HEADER: &str = "tracestate"; -static TRACE_CONTEXT_HEADER_FIELDS: Lazy<[String; 2]> = - Lazy::new(|| [TRACEPARENT_HEADER.to_owned(), TRACESTATE_HEADER.to_owned()]); +static TRACE_CONTEXT_HEADER_FIELDS: LazyLock<[String; 2]> = + LazyLock::new(|| [TRACEPARENT_HEADER.to_owned(), TRACESTATE_HEADER.to_owned()]); /// Propagates `SpanContext`s in [W3C TraceContext] format under `traceparent` and `tracestate` header. /// diff --git a/opentelemetry-sdk/src/trace/provider.rs b/opentelemetry-sdk/src/trace/provider.rs index dcb8f55fd8..eb14c62c82 100644 --- a/opentelemetry-sdk/src/trace/provider.rs +++ b/opentelemetry-sdk/src/trace/provider.rs @@ -68,20 +68,20 @@ use crate::trace::{ }; use crate::Resource; use crate::{export::trace::SpanExporter, trace::SpanProcessor}; -use once_cell::sync::{Lazy, OnceCell}; use opentelemetry::trace::TraceError; use opentelemetry::InstrumentationScope; use opentelemetry::{otel_debug, trace::TraceResult}; use std::borrow::Cow; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use std::sync::{LazyLock, OnceLock}; use super::IdGenerator; -static PROVIDER_RESOURCE: OnceCell = OnceCell::new(); +static PROVIDER_RESOURCE: OnceLock = OnceLock::new(); // a no nop tracer provider used as placeholder when the provider is shutdown -static NOOP_TRACER_PROVIDER: Lazy = Lazy::new(|| TracerProvider { +static NOOP_TRACER_PROVIDER: LazyLock = LazyLock::new(|| TracerProvider { inner: Arc::new(TracerProviderInner { processors: Vec::new(), config: Config { @@ -392,16 +392,8 @@ impl Builder { // For the uncommon case where there are multiple tracer providers with different resource // configurations, users can optionally provide their own borrowed static resource. if matches!(config.resource, Cow::Owned(_)) { - config.resource = match PROVIDER_RESOURCE.try_insert(config.resource.into_owned()) { - Ok(static_resource) => Cow::Borrowed(static_resource), - Err((prev, new)) => { - if prev == &new { - Cow::Borrowed(prev) - } else { - Cow::Owned(new) - } - } - } + let owned_resource = config.resource.clone().into_owned(); + config.resource = Cow::Borrowed(PROVIDER_RESOURCE.get_or_init(|| owned_resource)); } // Create a new vector to hold the modified processors