Skip to content

Commit 6b4da4a

Browse files
authored
fix: remove Lazy for thread_local static (#215)
As `thread_local` statics are already lazily initialized on first access, there is no need to use a Lazy initializer on top of that. This PR removes that usage and the associated dependency on `once_cell`.
1 parent 0bb71d1 commit 6b4da4a

File tree

2 files changed

+3
-5
lines changed

2 files changed

+3
-5
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ tracing = { version = "0.1.35", default-features = false, features = ["std"] }
2727
tracing-core = "0.1.28"
2828
tracing-subscriber = { version = "0.3.0", default-features = false, features = ["registry", "std"] }
2929
tracing-log = { version = "0.2.0", default-features = false, optional = true }
30-
once_cell = "1.13.0"
3130
rustversion = "1.0.9"
3231
smallvec = { version = "1.0", optional = true }
3332

src/layer.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::{OtelData, PreSampledTracer};
2-
use once_cell::unsync;
32
use opentelemetry::{
43
trace::{self as otel, noop, SpanBuilder, SpanKind, Status, TraceContextExt},
54
Context as OtelContext, Key, KeyValue, StringValue, Value,
@@ -848,7 +847,7 @@ where
848847
}
849848

850849
thread_local! {
851-
static THREAD_ID: unsync::Lazy<u64> = unsync::Lazy::new(|| {
850+
static THREAD_ID: u64 = {
852851
// OpenTelemetry's semantic conventions require the thread ID to be
853852
// recorded as an integer, but `std::thread::ThreadId` does not expose
854853
// the integer value on stable, so we have to convert it to a `usize` by
@@ -857,7 +856,7 @@ thread_local! {
857856
// TODO(eliza): once `std::thread::ThreadId::as_u64` is stabilized
858857
// (https://github.com/rust-lang/rust/issues/67939), just use that.
859858
thread_id_integer(thread::current().id())
860-
});
859+
};
861860
}
862861

863862
impl<S, T> Layer<S> for OpenTelemetryLayer<S, T>
@@ -911,7 +910,7 @@ where
911910
}
912911

913912
if self.with_threads {
914-
THREAD_ID.with(|id| builder_attrs.push(KeyValue::new("thread.id", **id as i64)));
913+
THREAD_ID.with(|id| builder_attrs.push(KeyValue::new("thread.id", *id as i64)));
915914
if let Some(name) = std::thread::current().name() {
916915
// TODO(eliza): it's a bummer that we have to allocate here, but
917916
// we can't easily get the string as a `static`. it would be

0 commit comments

Comments
 (0)