Skip to content

Commit

Permalink
Replace once_cell with OnceLock (#152)
Browse files Browse the repository at this point in the history
Co-authored-by: Cijo Thomas <[email protected]>
  • Loading branch information
gruebel and cijothomas authored Jan 16, 2025
1 parent f253035 commit 72a6d4b
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 31 deletions.
1 change: 0 additions & 1 deletion opentelemetry-aws/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ detector-aws-lambda = ["dep:opentelemetry-semantic-conventions"]
internal-logs = ["tracing"]

[dependencies]
once_cell = "1.12"
opentelemetry = { workspace = true }
opentelemetry_sdk = { workspace = true, optional = true }
opentelemetry-semantic-conventions = { workspace = true, optional = true }
Expand Down
11 changes: 8 additions & 3 deletions opentelemetry-aws/src/trace/xray_propagator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
//! }
//! ```
use once_cell::sync::Lazy;
use opentelemetry::{
otel_error,
propagation::{text_map_propagator::FieldIter, Extractor, Injector, TextMapPropagator},
Expand All @@ -47,6 +46,7 @@ use opentelemetry::{
};
use std::borrow::Cow;
use std::convert::TryFrom;
use std::sync::OnceLock;

const AWS_XRAY_TRACE_HEADER: &str = "x-amzn-trace-id";
const AWS_XRAY_VERSION_KEY: &str = "1";
Expand All @@ -60,7 +60,12 @@ const REQUESTED_SAMPLE_DECISION: &str = "?";

const TRACE_FLAG_DEFERRED: TraceFlags = TraceFlags::new(0x02);

static AWS_XRAY_HEADER_FIELD: Lazy<[String; 1]> = Lazy::new(|| [AWS_XRAY_TRACE_HEADER.to_owned()]);
// TODO Replace this with LazyLock when MSRV is 1.80+
static TRACE_CONTEXT_HEADER_FIELDS: OnceLock<[String; 1]> = OnceLock::new();

fn trace_context_header_fields() -> &'static [String; 1] {
TRACE_CONTEXT_HEADER_FIELDS.get_or_init(|| [AWS_XRAY_TRACE_HEADER.to_owned()])
}

/// Extracts and injects `SpanContext`s into `Extractor`s or `Injector`s using AWS X-Ray header format.
///
Expand Down Expand Up @@ -220,7 +225,7 @@ impl TextMapPropagator for XrayPropagator {
}

fn fields(&self) -> FieldIter<'_> {
FieldIter::new(AWS_XRAY_HEADER_FIELD.as_ref())
FieldIter::new(trace_context_header_fields())
}
}

Expand Down
1 change: 0 additions & 1 deletion opentelemetry-contrib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async-trait = { version = "0.1", optional = true }
base64 = { version = "0.13", optional = true }
futures-core = { version = "0.3", optional = true }
futures-util = { version = "0.3", optional = true, default-features = false }
once_cell = "1.17.1"
opentelemetry = { workspace = true }
opentelemetry_sdk = { workspace = true, optional = true }
opentelemetry-semantic-conventions = { workspace = true, optional = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@
//! See the [w3c trace-context docs] for more details.
//!
//! [w3c trace-context docs]: https://w3c.github.io/trace-context/#traceresponse-header
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::sync::OnceLock;

const SUPPORTED_VERSION: u8 = 0;
const MAX_VERSION: u8 = 254;
const TRACERESPONSE_HEADER: &str = "traceresponse";

static TRACE_CONTEXT_HEADER_FIELDS: Lazy<[String; 1]> =
Lazy::new(|| [TRACERESPONSE_HEADER.to_owned()]);
// TODO Replace this with LazyLock when MSRV is 1.80+
static TRACE_CONTEXT_HEADER_FIELDS: OnceLock<[String; 1]> = OnceLock::new();

fn trace_context_header_fields() -> &'static [String; 1] {
TRACE_CONTEXT_HEADER_FIELDS.get_or_init(|| [TRACERESPONSE_HEADER.to_owned()])
}

/// Propagates trace response using the [W3C TraceContext] format
///
Expand Down Expand Up @@ -124,7 +129,7 @@ impl TextMapPropagator for TraceContextResponsePropagator {
}

fn fields(&self) -> FieldIter<'_> {
FieldIter::new(TRACE_CONTEXT_HEADER_FIELDS.as_ref())
FieldIter::new(trace_context_header_fields())
}
}

Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-contrib/src/trace/tracer_source.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Abstracts away details for acquiring a `Tracer` by instrumented libraries.
use once_cell::sync::OnceCell;
use opentelemetry::global::BoxedTracer;
use std::fmt::Debug;
use std::sync::OnceLock;

/// Holds either a borrowed `BoxedTracer` or a factory that can produce one when
/// and if needed.
Expand All @@ -11,7 +11,7 @@ use std::fmt::Debug;
#[derive(Debug)]
pub struct TracerSource<'a> {
variant: Variant<'a>,
tracer: OnceCell<BoxedTracer>,
tracer: OnceLock<BoxedTracer>,
}

enum Variant<'a> {
Expand All @@ -24,7 +24,7 @@ impl<'a> TracerSource<'a> {
pub fn borrowed(tracer: &'a BoxedTracer) -> Self {
Self {
variant: Variant::Borrowed(tracer),
tracer: OnceCell::new(),
tracer: OnceLock::new(),
}
}

Expand All @@ -33,7 +33,7 @@ impl<'a> TracerSource<'a> {
pub fn lazy(factory: &'a dyn Fn() -> BoxedTracer) -> Self {
Self {
variant: Variant::Lazy(factory),
tracer: OnceCell::new(),
tracer: OnceLock::new(),
}
}

Expand Down
1 change: 0 additions & 1 deletion opentelemetry-datadog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ intern-std = []

[dependencies]
indexmap = "2.0"
once_cell = "1.12"
opentelemetry = { workspace = true }
opentelemetry_sdk = { workspace = true, features = ["trace"] }
opentelemetry-http = { workspace = true }
Expand Down
23 changes: 14 additions & 9 deletions opentelemetry-datadog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ pub use exporter::{
pub use propagator::{DatadogPropagator, DatadogTraceState, DatadogTraceStateBuilder};

mod 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::sync::OnceLock;

const DATADOG_TRACE_ID_HEADER: &str = "x-datadog-trace-id";
const DATADOG_PARENT_ID_HEADER: &str = "x-datadog-parent-id";
Expand All @@ -163,13 +163,18 @@ mod propagator {
const TRACE_STATE_TRUE_VALUE: &str = "1";
const TRACE_STATE_FALSE_VALUE: &str = "0";

static DATADOG_HEADER_FIELDS: Lazy<[String; 3]> = Lazy::new(|| {
[
DATADOG_TRACE_ID_HEADER.to_string(),
DATADOG_PARENT_ID_HEADER.to_string(),
DATADOG_SAMPLING_PRIORITY_HEADER.to_string(),
]
});
// TODO Replace this with LazyLock when MSRV is 1.80+
static TRACE_CONTEXT_HEADER_FIELDS: OnceLock<[String; 3]> = OnceLock::new();

fn trace_context_header_fields() -> &'static [String; 3] {
TRACE_CONTEXT_HEADER_FIELDS.get_or_init(|| {
[
DATADOG_TRACE_ID_HEADER.to_owned(),
DATADOG_PARENT_ID_HEADER.to_owned(),
DATADOG_SAMPLING_PRIORITY_HEADER.to_owned(),
]
})
}

#[derive(Default)]
pub struct DatadogTraceStateBuilder {
Expand Down Expand Up @@ -449,7 +454,7 @@ mod propagator {
}

fn fields(&self) -> FieldIter<'_> {
FieldIter::new(DATADOG_HEADER_FIELDS.as_ref())
FieldIter::new(trace_context_header_fields())
}
}

Expand Down
3 changes: 1 addition & 2 deletions opentelemetry-stackdriver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ prost = "0.13"
prost-types = "0.13"
thiserror = "2.0"
tonic = { version = "0.12", default-features = false, features = ["channel", "codegen", "gzip", "prost", "tls"] }
once_cell = { version = "1.19", optional = true }
tracing = { version = "0.1", optional = true }

# Futures
Expand All @@ -34,7 +33,7 @@ default = ["gcp-authorizer", "tls-native-roots", "internal-logs"]
gcp-authorizer = ["dep:gcp_auth"]
tls-native-roots = ["tonic/tls-roots"]
tls-webpki-roots = ["tonic/tls-webpki-roots"]
propagator = ["once_cell"]
propagator = []
internal-logs = ["tracing"]

[dev-dependencies]
Expand Down
15 changes: 9 additions & 6 deletions opentelemetry-stackdriver/src/google_trace_context_propagator.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::str::FromStr;

use once_cell::sync::Lazy;
use opentelemetry::propagation::text_map_propagator::FieldIter;
use opentelemetry::propagation::{Extractor, Injector, TextMapPropagator};
use opentelemetry::trace::{SpanContext, SpanId, TraceContextExt, TraceFlags, TraceId, TraceState};
use opentelemetry::Context;
use std::str::FromStr;
use std::sync::OnceLock;

/// Propagates span context in the Google Cloud Trace format,
/// using the __X-Cloud-Trace-Context__ header.
Expand All @@ -27,8 +26,12 @@ pub struct GoogleTraceContextPropagator {

const CLOUD_TRACE_CONTEXT_HEADER: &str = "X-Cloud-Trace-Context";

static TRACE_CONTEXT_HEADER_FIELDS: Lazy<[String; 1]> =
Lazy::new(|| [CLOUD_TRACE_CONTEXT_HEADER.to_owned()]);
// TODO Replace this with LazyLock when MSRV is 1.80+
static TRACE_CONTEXT_HEADER_FIELDS: OnceLock<[String; 1]> = OnceLock::new();

fn trace_context_header_fields() -> &'static [String; 1] {
TRACE_CONTEXT_HEADER_FIELDS.get_or_init(|| [CLOUD_TRACE_CONTEXT_HEADER.to_owned()])
}

impl GoogleTraceContextPropagator {
fn extract_span_context(&self, extractor: &dyn Extractor) -> Result<SpanContext, ()> {
Expand Down Expand Up @@ -85,7 +88,7 @@ impl TextMapPropagator for GoogleTraceContextPropagator {
}

fn fields(&self) -> FieldIter<'_> {
FieldIter::new(TRACE_CONTEXT_HEADER_FIELDS.as_ref())
FieldIter::new(trace_context_header_fields())
}
}

Expand Down

0 comments on commit 72a6d4b

Please sign in to comment.