Skip to content

Commit f7684b0

Browse files
authored
Prefer use of const to static where possible (#1270)
- Changed NOOP_SPAN from a lazy static to a const - Remove branch in build_with_context for the noop tracer. The current context's span() method already returns &NOOP_SPAN in the case when no active span is present, which already has the right span context value to use. - Changed other constituents of span context to use const default values too.
1 parent ab9972f commit f7684b0

File tree

4 files changed

+34
-37
lines changed

4 files changed

+34
-37
lines changed

opentelemetry/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
- Bump MSRV to 1.64 [#1203](https://github.com/open-telemetry/opentelemetry-rust/pull/1203)
88
- `opentelemetry` crate now only carries the API types #1186. Use the `opentelemetry_sdk` crate for the SDK types.
9+
- `trace::noop::NoopSpan` no longer implements `Default` and instead exposes
10+
a `const DEFAULT` value. [#1270](https://github.com/open-telemetry/opentelemetry-rust/pull/1270)
911

1012
## [v0.20.0](https://github.com/open-telemetry/opentelemetry-rust/compare/v0.19.0...v0.20.0)
1113
This release should been seen as 1.0-rc3 following 1.0-rc2 in v0.19.0. Refer to CHANGELOG.md in individual creates for details on changes made in different creates.

opentelemetry/src/trace/context.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::{
66
};
77
use futures_core::stream::Stream;
88
use futures_sink::Sink;
9-
use once_cell::sync::Lazy;
109
use pin_project_lite::pin_project;
1110
use std::{
1211
borrow::Cow,
@@ -16,10 +15,10 @@ use std::{
1615
task::{Context as TaskContext, Poll},
1716
};
1817

19-
static NOOP_SPAN: Lazy<SynchronizedSpan> = Lazy::new(|| SynchronizedSpan {
20-
span_context: SpanContext::empty_context(),
18+
const NOOP_SPAN: SynchronizedSpan = SynchronizedSpan {
19+
span_context: SpanContext::NONE,
2120
inner: None,
22-
});
21+
};
2322

2423
/// A reference to the currently active span in this context.
2524
#[derive(Debug)]

opentelemetry/src/trace/noop.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! to have minimal resource utilization and runtime impact.
66
use crate::{
77
propagation::{text_map_propagator::FieldIter, Extractor, Injector, TextMapPropagator},
8-
trace::{self, TraceContextExt, TraceFlags, TraceState},
8+
trace::{self, TraceContextExt as _},
99
Context, InstrumentationLibrary, KeyValue,
1010
};
1111
use std::{borrow::Cow, sync::Arc, time::SystemTime};
@@ -38,25 +38,11 @@ pub struct NoopSpan {
3838
span_context: trace::SpanContext,
3939
}
4040

41-
impl Default for NoopSpan {
42-
fn default() -> Self {
43-
NoopSpan::new()
44-
}
45-
}
46-
4741
impl NoopSpan {
48-
/// Creates a new `NoopSpan` instance.
49-
pub fn new() -> Self {
50-
NoopSpan {
51-
span_context: trace::SpanContext::new(
52-
trace::TraceId::INVALID,
53-
trace::SpanId::INVALID,
54-
TraceFlags::default(),
55-
false,
56-
TraceState::default(),
57-
),
58-
}
59-
}
42+
/// The default `NoopSpan`, as a constant
43+
pub const DEFAULT: NoopSpan = NoopSpan {
44+
span_context: trace::SpanContext::NONE,
45+
};
6046
}
6147

6248
impl trace::Span for NoopSpan {
@@ -135,12 +121,8 @@ impl trace::Tracer for NoopTracer {
135121
/// If the span builder or the context's current span contains a valid span context, it is
136122
/// propagated.
137123
fn build_with_context(&self, _builder: trace::SpanBuilder, parent_cx: &Context) -> Self::Span {
138-
if parent_cx.has_active_span() {
139-
NoopSpan {
140-
span_context: parent_cx.span().span_context().clone(),
141-
}
142-
} else {
143-
NoopSpan::new()
124+
NoopSpan {
125+
span_context: parent_cx.span().span_context().clone(),
144126
}
145127
}
146128
}
@@ -178,7 +160,7 @@ impl TextMapPropagator for NoopTextMapPropagator {
178160
mod tests {
179161
use super::*;
180162
use crate::testing::trace::TestSpan;
181-
use crate::trace::{self, Span, Tracer};
163+
use crate::trace::{self, Span, TraceState, Tracer};
182164

183165
fn valid_span_context() -> trace::SpanContext {
184166
trace::SpanContext::new(

opentelemetry/src/trace/span_context.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ use thiserror::Error;
2020
pub struct TraceFlags(u8);
2121

2222
impl TraceFlags {
23+
/// Trace flags with the `sampled` flag set to `0`.
24+
///
25+
/// Spans that are not sampled will be ignored by most tracing tools.
26+
/// See the `sampled` section of the [W3C TraceContext specification] for details.
27+
///
28+
/// [W3C TraceContext specification]: https://www.w3.org/TR/trace-context/#sampled-flag
29+
pub const NOT_SAMPLED: TraceFlags = TraceFlags(0x00);
30+
2331
/// Trace flags with the `sampled` flag set to `1`.
2432
///
2533
/// Spans that are not sampled will be ignored by most tracing tools.
@@ -216,6 +224,9 @@ impl fmt::LowerHex for SpanId {
216224
pub struct TraceState(Option<VecDeque<(String, String)>>);
217225

218226
impl TraceState {
227+
/// The default `TraceState`, as a constant
228+
pub const NONE: TraceState = TraceState(None);
229+
219230
/// Validates that the given `TraceState` list-member key is valid per the [W3 Spec].
220231
///
221232
/// [W3 Spec]: https://www.w3.org/TR/trace-context/#key
@@ -457,15 +468,18 @@ pub struct SpanContext {
457468
}
458469

459470
impl SpanContext {
471+
/// An invalid span context
472+
pub const NONE: SpanContext = SpanContext {
473+
trace_id: TraceId::INVALID,
474+
span_id: SpanId::INVALID,
475+
trace_flags: TraceFlags::NOT_SAMPLED,
476+
is_remote: false,
477+
trace_state: TraceState::NONE,
478+
};
479+
460480
/// Create an invalid empty span context
461481
pub fn empty_context() -> Self {
462-
SpanContext::new(
463-
TraceId::INVALID,
464-
SpanId::INVALID,
465-
TraceFlags::default(),
466-
false,
467-
TraceState::default(),
468-
)
482+
SpanContext::NONE
469483
}
470484

471485
/// Construct a new `SpanContext`

0 commit comments

Comments
 (0)