Skip to content

Commit 15b5fa4

Browse files
authored
Move traceid spanid traceflags to common (#2609)
1 parent dfeeac7 commit 15b5fa4

File tree

7 files changed

+268
-259
lines changed

7 files changed

+268
-259
lines changed

opentelemetry/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ mod context;
250250

251251
pub use context::{Context, ContextGuard};
252252

253+
mod trace_context;
254+
pub use trace_context::{SpanId, TraceFlags, TraceId};
255+
253256
mod common;
254257

255258
#[cfg(any(feature = "testing", test))]

opentelemetry/src/logs/record.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{Key, StringValue};
22

3-
#[cfg(feature = "trace")]
4-
use crate::trace::{SpanId, TraceFlags, TraceId};
3+
use crate::{SpanId, TraceFlags, TraceId};
54

65
use std::{borrow::Cow, collections::HashMap, time::SystemTime};
76

@@ -47,7 +46,6 @@ pub trait LogRecord {
4746
V: Into<AnyValue>;
4847

4948
/// Sets the trace context of the log.
50-
#[cfg(feature = "trace")]
5149
fn set_trace_context(
5250
&mut self,
5351
trace_id: TraceId,

opentelemetry/src/testing/trace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
trace::{Span, SpanContext, SpanId, Status, TraceId},
3-
KeyValue,
2+
trace::{Span, SpanContext, Status},
3+
KeyValue, SpanId, TraceId,
44
};
55
use std::borrow::Cow;
66

opentelemetry/src/trace/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,12 @@ pub use self::{
182182
get_active_span, mark_span_as_active, FutureExt, SpanRef, TraceContextExt, WithContext,
183183
},
184184
span::{Span, SpanKind, Status},
185-
span_context::{SpanContext, SpanId, TraceFlags, TraceId, TraceState},
185+
span_context::{SpanContext, TraceState},
186186
tracer::{SamplingDecision, SamplingResult, SpanBuilder, Tracer},
187187
tracer_provider::TracerProvider,
188188
};
189189
use crate::KeyValue;
190+
pub use crate::{SpanId, TraceFlags, TraceId};
190191
use std::sync::PoisonError;
191192

192193
// TODO - Move ExportError and TraceError to opentelemetry-sdk

opentelemetry/src/trace/span_context.rs

Lines changed: 1 addition & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -1,218 +1,10 @@
11
use crate::trace::{TraceError, TraceResult};
2+
use crate::{SpanId, TraceFlags, TraceId};
23
use std::collections::VecDeque;
3-
use std::fmt;
44
use std::hash::Hash;
5-
use std::num::ParseIntError;
6-
use std::ops::{BitAnd, BitOr, Not};
75
use std::str::FromStr;
86
use thiserror::Error;
97

10-
/// Flags that can be set on a [`SpanContext`].
11-
///
12-
/// The current version of the specification only supports a single flag
13-
/// [`TraceFlags::SAMPLED`].
14-
///
15-
/// See the W3C TraceContext specification's [trace-flags] section for more
16-
/// details.
17-
///
18-
/// [trace-flags]: https://www.w3.org/TR/trace-context/#trace-flags
19-
#[derive(Clone, Debug, Default, PartialEq, Eq, Copy, Hash)]
20-
pub struct TraceFlags(u8);
21-
22-
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-
31-
/// Trace flags with the `sampled` flag set to `1`.
32-
///
33-
/// Spans that are not sampled will be ignored by most tracing tools.
34-
/// See the `sampled` section of the [W3C TraceContext specification] for details.
35-
///
36-
/// [W3C TraceContext specification]: https://www.w3.org/TR/trace-context/#sampled-flag
37-
pub const SAMPLED: TraceFlags = TraceFlags(0x01);
38-
39-
/// Construct new trace flags
40-
pub const fn new(flags: u8) -> Self {
41-
TraceFlags(flags)
42-
}
43-
44-
/// Returns `true` if the `sampled` flag is set
45-
pub fn is_sampled(&self) -> bool {
46-
(*self & TraceFlags::SAMPLED) == TraceFlags::SAMPLED
47-
}
48-
49-
/// Returns copy of the current flags with the `sampled` flag set.
50-
pub fn with_sampled(&self, sampled: bool) -> Self {
51-
if sampled {
52-
*self | TraceFlags::SAMPLED
53-
} else {
54-
*self & !TraceFlags::SAMPLED
55-
}
56-
}
57-
58-
/// Returns the flags as a `u8`
59-
pub fn to_u8(self) -> u8 {
60-
self.0
61-
}
62-
}
63-
64-
impl BitAnd for TraceFlags {
65-
type Output = Self;
66-
67-
fn bitand(self, rhs: Self) -> Self::Output {
68-
Self(self.0 & rhs.0)
69-
}
70-
}
71-
72-
impl BitOr for TraceFlags {
73-
type Output = Self;
74-
75-
fn bitor(self, rhs: Self) -> Self::Output {
76-
Self(self.0 | rhs.0)
77-
}
78-
}
79-
80-
impl Not for TraceFlags {
81-
type Output = Self;
82-
83-
fn not(self) -> Self::Output {
84-
Self(!self.0)
85-
}
86-
}
87-
88-
impl fmt::LowerHex for TraceFlags {
89-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90-
fmt::LowerHex::fmt(&self.0, f)
91-
}
92-
}
93-
94-
/// A 16-byte value which identifies a given trace.
95-
///
96-
/// The id is valid if it contains at least one non-zero byte.
97-
#[derive(Clone, PartialEq, Eq, Copy, Hash)]
98-
pub struct TraceId(u128);
99-
100-
impl TraceId {
101-
/// Invalid trace id
102-
pub const INVALID: TraceId = TraceId(0);
103-
104-
/// Create a trace id from its representation as a byte array.
105-
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
106-
TraceId(u128::from_be_bytes(bytes))
107-
}
108-
109-
/// Return the representation of this trace id as a byte array.
110-
pub const fn to_bytes(self) -> [u8; 16] {
111-
self.0.to_be_bytes()
112-
}
113-
114-
/// Converts a string in base 16 to a trace id.
115-
///
116-
/// # Examples
117-
///
118-
/// ```
119-
/// use opentelemetry::trace::TraceId;
120-
///
121-
/// assert!(TraceId::from_hex("42").is_ok());
122-
/// assert!(TraceId::from_hex("58406520a006649127e371903a2de979").is_ok());
123-
///
124-
/// assert!(TraceId::from_hex("not_hex").is_err());
125-
/// ```
126-
pub fn from_hex(hex: &str) -> Result<Self, ParseIntError> {
127-
u128::from_str_radix(hex, 16).map(TraceId)
128-
}
129-
}
130-
131-
impl From<u128> for TraceId {
132-
fn from(value: u128) -> Self {
133-
TraceId(value)
134-
}
135-
}
136-
137-
impl fmt::Debug for TraceId {
138-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139-
f.write_fmt(format_args!("{:032x}", self.0))
140-
}
141-
}
142-
143-
impl fmt::Display for TraceId {
144-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145-
f.write_fmt(format_args!("{:032x}", self.0))
146-
}
147-
}
148-
149-
impl fmt::LowerHex for TraceId {
150-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151-
fmt::LowerHex::fmt(&self.0, f)
152-
}
153-
}
154-
155-
/// An 8-byte value which identifies a given span.
156-
///
157-
/// The id is valid if it contains at least one non-zero byte.
158-
#[derive(Clone, PartialEq, Eq, Copy, Hash)]
159-
pub struct SpanId(u64);
160-
161-
impl SpanId {
162-
/// Invalid span id
163-
pub const INVALID: SpanId = SpanId(0);
164-
165-
/// Create a span id from its representation as a byte array.
166-
pub const fn from_bytes(bytes: [u8; 8]) -> Self {
167-
SpanId(u64::from_be_bytes(bytes))
168-
}
169-
170-
/// Return the representation of this span id as a byte array.
171-
pub const fn to_bytes(self) -> [u8; 8] {
172-
self.0.to_be_bytes()
173-
}
174-
175-
/// Converts a string in base 16 to a span id.
176-
///
177-
/// # Examples
178-
///
179-
/// ```
180-
/// use opentelemetry::trace::SpanId;
181-
///
182-
/// assert!(SpanId::from_hex("42").is_ok());
183-
/// assert!(SpanId::from_hex("58406520a0066491").is_ok());
184-
///
185-
/// assert!(SpanId::from_hex("not_hex").is_err());
186-
/// ```
187-
pub fn from_hex(hex: &str) -> Result<Self, ParseIntError> {
188-
u64::from_str_radix(hex, 16).map(SpanId)
189-
}
190-
}
191-
192-
impl From<u64> for SpanId {
193-
fn from(value: u64) -> Self {
194-
SpanId(value)
195-
}
196-
}
197-
198-
impl fmt::Debug for SpanId {
199-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200-
f.write_fmt(format_args!("{:016x}", self.0))
201-
}
202-
}
203-
204-
impl fmt::Display for SpanId {
205-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
206-
f.write_fmt(format_args!("{:016x}", self.0))
207-
}
208-
}
209-
210-
impl fmt::LowerHex for SpanId {
211-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212-
fmt::LowerHex::fmt(&self.0, f)
213-
}
214-
}
215-
2168
/// TraceState carries system-specific configuration data, represented as a list
2179
/// of key-value pairs. TraceState allows multiple tracing systems to
21810
/// participate in the same trace.
@@ -546,24 +338,6 @@ mod tests {
546338
use super::*;
547339
use crate::{trace::TraceContextExt, Context};
548340

549-
#[rustfmt::skip]
550-
fn trace_id_test_data() -> Vec<(TraceId, &'static str, [u8; 16])> {
551-
vec![
552-
(TraceId(0), "00000000000000000000000000000000", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
553-
(TraceId(42), "0000000000000000000000000000002a", [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 42]),
554-
(TraceId(126642714606581564793456114182061442190), "5f467fe7bf42676c05e20ba4a90e448e", [95, 70, 127, 231, 191, 66, 103, 108, 5, 226, 11, 164, 169, 14, 68, 142])
555-
]
556-
}
557-
558-
#[rustfmt::skip]
559-
fn span_id_test_data() -> Vec<(SpanId, &'static str, [u8; 8])> {
560-
vec![
561-
(SpanId(0), "0000000000000000", [0, 0, 0, 0, 0, 0, 0, 0]),
562-
(SpanId(42), "000000000000002a", [0, 0, 0, 0, 0, 0, 0, 42]),
563-
(SpanId(5508496025762705295), "4c721bf33e3caf8f", [76, 114, 27, 243, 62, 60, 175, 143])
564-
]
565-
}
566-
567341
#[rustfmt::skip]
568342
fn trace_state_test_data() -> Vec<(TraceState, &'static str, &'static str)> {
569343
vec![
@@ -573,30 +347,6 @@ mod tests {
573347
]
574348
}
575349

576-
#[test]
577-
fn test_trace_id() {
578-
for test_case in trace_id_test_data() {
579-
assert_eq!(format!("{}", test_case.0), test_case.1);
580-
assert_eq!(format!("{:032x}", test_case.0), test_case.1);
581-
assert_eq!(test_case.0.to_bytes(), test_case.2);
582-
583-
assert_eq!(test_case.0, TraceId::from_hex(test_case.1).unwrap());
584-
assert_eq!(test_case.0, TraceId::from_bytes(test_case.2));
585-
}
586-
}
587-
588-
#[test]
589-
fn test_span_id() {
590-
for test_case in span_id_test_data() {
591-
assert_eq!(format!("{}", test_case.0), test_case.1);
592-
assert_eq!(format!("{:016x}", test_case.0), test_case.1);
593-
assert_eq!(test_case.0.to_bytes(), test_case.2);
594-
595-
assert_eq!(test_case.0, SpanId::from_hex(test_case.1).unwrap());
596-
assert_eq!(test_case.0, SpanId::from_bytes(test_case.2));
597-
}
598-
}
599-
600350
#[test]
601351
fn test_trace_state() {
602352
for test_case in trace_state_test_data() {

opentelemetry/src/trace/tracer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
trace::{Event, Link, Span, SpanId, SpanKind, Status, TraceContextExt, TraceId, TraceState},
3-
Context, KeyValue,
2+
trace::{Event, Link, Span, SpanKind, Status, TraceContextExt, TraceState},
3+
Context, KeyValue, SpanId, TraceId,
44
};
55
use std::borrow::Cow;
66
use std::time::SystemTime;

0 commit comments

Comments
 (0)