Skip to content

Commit 5696253

Browse files
authored
Move IdGenerator to SDK, rename RandomIdGenerator (open-telemetry#742)
The [spec] defines `IdGenerator` as part of the SDK. This also renames the default generator to match other language implementations in calling the default id generator `RandomIdGenerator`. [spec]: https://github.com/open-telemetry/opentelemetry-specification/blob/v1.9.0/specification/trace/sdk.md#id-generators
1 parent f282659 commit 5696253

File tree

9 files changed

+28
-29
lines changed

9 files changed

+28
-29
lines changed

opentelemetry-api/src/trace/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
139139
use futures_channel::{mpsc::TrySendError, oneshot::Canceled};
140140
use std::borrow::Cow;
141-
use std::fmt;
142141
use std::time;
143142
use thiserror::Error;
144143

@@ -216,15 +215,6 @@ impl From<&'static str> for TraceError {
216215
#[error("{0}")]
217216
struct Custom(String);
218217

219-
/// Interface for generating IDs
220-
pub trait IdGenerator: Send + Sync + fmt::Debug {
221-
/// Generate a new `TraceId`
222-
fn new_trace_id(&self) -> TraceId;
223-
224-
/// Generate a new `SpanId`
225-
fn new_span_id(&self) -> SpanId;
226-
}
227-
228218
/// A `Span` has the ability to add events. Events have a time associated
229219
/// with the moment when they are added to the `Span`.
230220
#[derive(Clone, Debug, PartialEq)]

opentelemetry-datadog/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
//!
8080
//! ```no_run
8181
//! use opentelemetry::{KeyValue, trace::Tracer};
82-
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
82+
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
8383
//! use opentelemetry::sdk::export::trace::ExportResult;
8484
//! use opentelemetry::global::shutdown_tracer_provider;
8585
//! use opentelemetry_datadog::{new_pipeline, ApiVersion, Error};
@@ -118,7 +118,7 @@
118118
//! .with_trace_config(
119119
//! trace::config()
120120
//! .with_sampler(Sampler::AlwaysOn)
121-
//! .with_id_generator(IdGenerator::default())
121+
//! .with_id_generator(RandomIdGenerator::default())
122122
//! )
123123
//! .install_batch(opentelemetry::runtime::Tokio)?;
124124
//!

opentelemetry-jaeger/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
//!
142142
//! ```no_run
143143
//! use opentelemetry::{KeyValue, trace::{Tracer, TraceError}};
144-
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
144+
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
145145
//! use opentelemetry::global;
146146
//!
147147
//! fn main() -> Result<(), TraceError> {
@@ -153,7 +153,7 @@
153153
//! .with_trace_config(
154154
//! trace::config()
155155
//! .with_sampler(Sampler::AlwaysOn)
156-
//! .with_id_generator(IdGenerator::default())
156+
//! .with_id_generator(RandomIdGenerator::default())
157157
//! .with_max_events_per_span(64)
158158
//! .with_max_attributes_per_span(16)
159159
//! .with_max_events_per_span(16)

opentelemetry-otlp/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
//!
8787
//! ```no_run
8888
//! use opentelemetry::{KeyValue, trace::Tracer};
89-
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
89+
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
9090
//! use opentelemetry::sdk::metrics::{selectors, PushController};
9191
//! use opentelemetry::sdk::util::tokio_interval_stream;
9292
//! use opentelemetry_otlp::{Protocol, WithExportConfig, ExportConfig};
@@ -112,7 +112,7 @@
112112
//! .with_trace_config(
113113
//! trace::config()
114114
//! .with_sampler(Sampler::AlwaysOn)
115-
//! .with_id_generator(IdGenerator::default())
115+
//! .with_id_generator(RandomIdGenerator::default())
116116
//! .with_max_events_per_span(64)
117117
//! .with_max_attributes_per_span(16)
118118
//! .with_max_events_per_span(16)

opentelemetry-sdk/src/trace/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
//!
33
//! Configuration represents the global tracing configuration, overrides
44
//! can be set for the default OpenTelemetry limits and Sampler.
5-
use crate::trace::{span_limit::SpanLimits, Sampler, ShouldSample};
5+
use crate::trace::{span_limit::SpanLimits, IdGenerator, RandomIdGenerator, Sampler, ShouldSample};
66
use opentelemetry_api::global::{handle_error, Error};
7-
use opentelemetry_api::trace::IdGenerator;
87
use std::env;
98
use std::str::FromStr;
109
use std::sync::Arc;
@@ -98,7 +97,7 @@ impl Default for Config {
9897
fn default() -> Self {
9998
let mut config = Config {
10099
sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
101-
id_generator: Box::new(crate::trace::IdGenerator::default()),
100+
id_generator: Box::new(RandomIdGenerator::default()),
102101
span_limits: SpanLimits::default(),
103102
resource: None,
104103
};

opentelemetry-sdk/src/trace/id_generator/aws.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use opentelemetry_api::trace::{IdGenerator, SpanId, TraceId};
1+
use crate::trace::{IdGenerator, RandomIdGenerator};
2+
use opentelemetry_api::trace::{SpanId, TraceId};
23
use std::time::{Duration, UNIX_EPOCH};
34

45
/// Generates AWS X-Ray compliant Trace and Span ids.
@@ -34,7 +35,7 @@ use std::time::{Duration, UNIX_EPOCH};
3435
/// [xray-trace-id]: https://docs.aws.amazon.com/xray/latest/devguide/xray-api-sendingdata.html#xray-api-traceids
3536
#[derive(Debug, Default)]
3637
pub struct XrayIdGenerator {
37-
sdk_default_generator: crate::trace::IdGenerator,
38+
sdk_default_generator: RandomIdGenerator,
3839
}
3940

4041
impl IdGenerator for XrayIdGenerator {

opentelemetry-sdk/src/trace/id_generator/mod.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,30 @@ pub(super) mod aws;
44
use opentelemetry_api::trace::{SpanId, TraceId};
55
use rand::{rngs, Rng};
66
use std::cell::RefCell;
7+
use std::fmt;
78

8-
/// Default [`crate::trace::IdGenerator`] implementation.
9+
/// Interface for generating IDs
10+
pub trait IdGenerator: Send + Sync + fmt::Debug {
11+
/// Generate a new `TraceId`
12+
fn new_trace_id(&self) -> TraceId;
13+
14+
/// Generate a new `SpanId`
15+
fn new_span_id(&self) -> SpanId;
16+
}
17+
18+
/// Default [`IdGenerator`] implementation.
19+
///
920
/// Generates Trace and Span ids using a random number generator.
1021
#[derive(Clone, Debug, Default)]
11-
pub struct IdGenerator {
22+
pub struct RandomIdGenerator {
1223
_private: (),
1324
}
1425

15-
impl opentelemetry_api::trace::IdGenerator for IdGenerator {
16-
/// Generate new `TraceId` using thread local rng
26+
impl IdGenerator for RandomIdGenerator {
1727
fn new_trace_id(&self) -> TraceId {
1828
CURRENT_RNG.with(|rng| TraceId::from(rng.borrow_mut().gen::<[u8; 16]>()))
1929
}
2030

21-
/// Generate new `SpanId` using thread local rng
2231
fn new_span_id(&self) -> SpanId {
2332
CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<[u8; 8]>()))
2433
}

opentelemetry-sdk/src/trace/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod tracer;
2121
pub use config::{config, Config};
2222
pub use evicted_hash_map::EvictedHashMap;
2323
pub use evicted_queue::EvictedQueue;
24-
pub use id_generator::{aws::XrayIdGenerator, IdGenerator};
24+
pub use id_generator::{aws::XrayIdGenerator, IdGenerator, RandomIdGenerator};
2525
pub use provider::{Builder, TracerProvider};
2626
pub use runtime::{TraceRuntime, TrySend};
2727
pub use sampler::{Sampler, ShouldSample};

opentelemetry-zipkin/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
//!
8989
//! ```no_run
9090
//! use opentelemetry::{KeyValue, trace::Tracer};
91-
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
91+
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
9292
//! use opentelemetry::sdk::export::trace::ExportResult;
9393
//! use opentelemetry::global;
9494
//! use opentelemetry_http::{HttpClient, HttpError};
@@ -129,7 +129,7 @@
129129
//! .with_trace_config(
130130
//! trace::config()
131131
//! .with_sampler(Sampler::AlwaysOn)
132-
//! .with_id_generator(IdGenerator::default())
132+
//! .with_id_generator(RandomIdGenerator::default())
133133
//! .with_max_events_per_span(64)
134134
//! .with_max_attributes_per_span(16)
135135
//! .with_max_events_per_span(16)

0 commit comments

Comments
 (0)