Skip to content

Commit

Permalink
Move IdGenerator to SDK, rename RandomIdGenerator (#742)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jtescher committed Feb 23, 2022
1 parent f282659 commit 5696253
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 29 deletions.
10 changes: 0 additions & 10 deletions opentelemetry-api/src/trace/mod.rs
Expand Up @@ -138,7 +138,6 @@

use futures_channel::{mpsc::TrySendError, oneshot::Canceled};
use std::borrow::Cow;
use std::fmt;
use std::time;
use thiserror::Error;

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

/// Interface for generating IDs
pub trait IdGenerator: Send + Sync + fmt::Debug {
/// Generate a new `TraceId`
fn new_trace_id(&self) -> TraceId;

/// Generate a new `SpanId`
fn new_span_id(&self) -> SpanId;
}

/// A `Span` has the ability to add events. Events have a time associated
/// with the moment when they are added to the `Span`.
#[derive(Clone, Debug, PartialEq)]
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-datadog/src/lib.rs
Expand Up @@ -79,7 +79,7 @@
//!
//! ```no_run
//! use opentelemetry::{KeyValue, trace::Tracer};
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::export::trace::ExportResult;
//! use opentelemetry::global::shutdown_tracer_provider;
//! use opentelemetry_datadog::{new_pipeline, ApiVersion, Error};
Expand Down Expand Up @@ -118,7 +118,7 @@
//! .with_trace_config(
//! trace::config()
//! .with_sampler(Sampler::AlwaysOn)
//! .with_id_generator(IdGenerator::default())
//! .with_id_generator(RandomIdGenerator::default())
//! )
//! .install_batch(opentelemetry::runtime::Tokio)?;
//!
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-jaeger/src/lib.rs
Expand Up @@ -141,7 +141,7 @@
//!
//! ```no_run
//! use opentelemetry::{KeyValue, trace::{Tracer, TraceError}};
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
//! use opentelemetry::global;
//!
//! fn main() -> Result<(), TraceError> {
Expand All @@ -153,7 +153,7 @@
//! .with_trace_config(
//! trace::config()
//! .with_sampler(Sampler::AlwaysOn)
//! .with_id_generator(IdGenerator::default())
//! .with_id_generator(RandomIdGenerator::default())
//! .with_max_events_per_span(64)
//! .with_max_attributes_per_span(16)
//! .with_max_events_per_span(16)
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-otlp/src/lib.rs
Expand Up @@ -86,7 +86,7 @@
//!
//! ```no_run
//! use opentelemetry::{KeyValue, trace::Tracer};
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::metrics::{selectors, PushController};
//! use opentelemetry::sdk::util::tokio_interval_stream;
//! use opentelemetry_otlp::{Protocol, WithExportConfig, ExportConfig};
Expand All @@ -112,7 +112,7 @@
//! .with_trace_config(
//! trace::config()
//! .with_sampler(Sampler::AlwaysOn)
//! .with_id_generator(IdGenerator::default())
//! .with_id_generator(RandomIdGenerator::default())
//! .with_max_events_per_span(64)
//! .with_max_attributes_per_span(16)
//! .with_max_events_per_span(16)
Expand Down
5 changes: 2 additions & 3 deletions opentelemetry-sdk/src/trace/config.rs
Expand Up @@ -2,9 +2,8 @@
//!
//! Configuration represents the global tracing configuration, overrides
//! can be set for the default OpenTelemetry limits and Sampler.
use crate::trace::{span_limit::SpanLimits, Sampler, ShouldSample};
use crate::trace::{span_limit::SpanLimits, IdGenerator, RandomIdGenerator, Sampler, ShouldSample};
use opentelemetry_api::global::{handle_error, Error};
use opentelemetry_api::trace::IdGenerator;
use std::env;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -98,7 +97,7 @@ impl Default for Config {
fn default() -> Self {
let mut config = Config {
sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
id_generator: Box::new(crate::trace::IdGenerator::default()),
id_generator: Box::new(RandomIdGenerator::default()),
span_limits: SpanLimits::default(),
resource: None,
};
Expand Down
5 changes: 3 additions & 2 deletions opentelemetry-sdk/src/trace/id_generator/aws.rs
@@ -1,4 +1,5 @@
use opentelemetry_api::trace::{IdGenerator, SpanId, TraceId};
use crate::trace::{IdGenerator, RandomIdGenerator};
use opentelemetry_api::trace::{SpanId, TraceId};
use std::time::{Duration, UNIX_EPOCH};

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

impl IdGenerator for XrayIdGenerator {
Expand Down
19 changes: 14 additions & 5 deletions opentelemetry-sdk/src/trace/id_generator/mod.rs
Expand Up @@ -4,21 +4,30 @@ pub(super) mod aws;
use opentelemetry_api::trace::{SpanId, TraceId};
use rand::{rngs, Rng};
use std::cell::RefCell;
use std::fmt;

/// Default [`crate::trace::IdGenerator`] implementation.
/// Interface for generating IDs
pub trait IdGenerator: Send + Sync + fmt::Debug {
/// Generate a new `TraceId`
fn new_trace_id(&self) -> TraceId;

/// Generate a new `SpanId`
fn new_span_id(&self) -> SpanId;
}

/// Default [`IdGenerator`] implementation.
///
/// Generates Trace and Span ids using a random number generator.
#[derive(Clone, Debug, Default)]
pub struct IdGenerator {
pub struct RandomIdGenerator {
_private: (),
}

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

/// Generate new `SpanId` using thread local rng
fn new_span_id(&self) -> SpanId {
CURRENT_RNG.with(|rng| SpanId::from(rng.borrow_mut().gen::<[u8; 8]>()))
}
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/trace/mod.rs
Expand Up @@ -21,7 +21,7 @@ mod tracer;
pub use config::{config, Config};
pub use evicted_hash_map::EvictedHashMap;
pub use evicted_queue::EvictedQueue;
pub use id_generator::{aws::XrayIdGenerator, IdGenerator};
pub use id_generator::{aws::XrayIdGenerator, IdGenerator, RandomIdGenerator};
pub use provider::{Builder, TracerProvider};
pub use runtime::{TraceRuntime, TrySend};
pub use sampler::{Sampler, ShouldSample};
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-zipkin/src/lib.rs
Expand Up @@ -88,7 +88,7 @@
//!
//! ```no_run
//! use opentelemetry::{KeyValue, trace::Tracer};
//! use opentelemetry::sdk::{trace::{self, IdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::{trace::{self, RandomIdGenerator, Sampler}, Resource};
//! use opentelemetry::sdk::export::trace::ExportResult;
//! use opentelemetry::global;
//! use opentelemetry_http::{HttpClient, HttpError};
Expand Down Expand Up @@ -129,7 +129,7 @@
//! .with_trace_config(
//! trace::config()
//! .with_sampler(Sampler::AlwaysOn)
//! .with_id_generator(IdGenerator::default())
//! .with_id_generator(RandomIdGenerator::default())
//! .with_max_events_per_span(64)
//! .with_max_attributes_per_span(16)
//! .with_max_events_per_span(16)
Expand Down

0 comments on commit 5696253

Please sign in to comment.