Skip to content

Commit

Permalink
add TODO
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Nov 25, 2024
1 parent 3b56ddd commit 2b1e9bd
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 67 deletions.
26 changes: 15 additions & 11 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ use std::{
borrow::Cow,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
Arc, OnceLock,
},
};

use std::sync::LazyLock;

// a no nop logger provider used as placeholder when the provider is shutdown
static NOOP_LOGGER_PROVIDER: LazyLock<LoggerProvider> = LazyLock::new(|| LoggerProvider {
inner: Arc::new(LoggerProviderInner {
processors: Vec::new(),
resource: Resource::empty(),
is_shutdown: AtomicBool::new(true),
}),
});
// TODO - replace it with LazyLock once it is stable
static NOOP_LOGGER_PROVIDER: OnceLock<LoggerProvider> = OnceLock::new();

#[inline]
fn noop_logger_provider() -> &'static LoggerProvider {
NOOP_LOGGER_PROVIDER.get_or_init(|| LoggerProvider {
inner: Arc::new(LoggerProviderInner {
processors: Vec::new(),
resource: Resource::empty(),
is_shutdown: AtomicBool::new(true),
}),
})
}

#[derive(Debug, Clone)]
/// Handles the creation and coordination of [`Logger`]s.
Expand Down Expand Up @@ -55,7 +59,7 @@ impl opentelemetry::logs::LoggerProvider for LoggerProvider {
fn logger_with_scope(&self, scope: InstrumentationScope) -> Self::Logger {
// If the provider is shutdown, new logger will refer a no-op logger provider.
if self.inner.is_shutdown.load(Ordering::Relaxed) {
return Logger::new(scope, NOOP_LOGGER_PROVIDER.clone());
return Logger::new(scope, noop_logger_provider().clone());
}
if scope.name().is_empty() {
otel_info!(name: "LoggerNameEmpty", message = "Logger name is empty; consider providing a meaningful name. Logger will function normally and the provided name will be used as-is.");
Expand Down
62 changes: 34 additions & 28 deletions opentelemetry-sdk/src/metrics/internal/exponential_histogram.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{f64::consts::LOG2_E, mem::replace, ops::DerefMut, sync::Mutex, time::SystemTime};

use opentelemetry::{otel_debug, KeyValue};
use std::sync::LazyLock;
use std::sync::OnceLock;

use crate::metrics::{
data::{self, Aggregation},
Expand Down Expand Up @@ -131,7 +131,7 @@ impl<T: Number> ExpoHistogramDataPoint<T> {
}
return (exp - correction) >> -self.scale;
}
(exp << self.scale) + (frac.ln() * SCALE_FACTORS[self.scale as usize]) as i32 - 1
(exp << self.scale) + (frac.ln() * scale_factors()[self.scale as usize]) as i32 - 1
}
}

Expand Down Expand Up @@ -165,32 +165,38 @@ fn scale_change(max_size: i32, bin: i32, start_bin: i32, length: i32) -> u32 {
count
}

/// Constants used in calculating the logarithm index.
static SCALE_FACTORS: LazyLock<[f64; 21]> = LazyLock::new(|| {
[
LOG2_E * 2f64.powi(0),
LOG2_E * 2f64.powi(1),
LOG2_E * 2f64.powi(2),
LOG2_E * 2f64.powi(3),
LOG2_E * 2f64.powi(4),
LOG2_E * 2f64.powi(5),
LOG2_E * 2f64.powi(6),
LOG2_E * 2f64.powi(7),
LOG2_E * 2f64.powi(8),
LOG2_E * 2f64.powi(9),
LOG2_E * 2f64.powi(10),
LOG2_E * 2f64.powi(11),
LOG2_E * 2f64.powi(12),
LOG2_E * 2f64.powi(13),
LOG2_E * 2f64.powi(14),
LOG2_E * 2f64.powi(15),
LOG2_E * 2f64.powi(16),
LOG2_E * 2f64.powi(17),
LOG2_E * 2f64.powi(18),
LOG2_E * 2f64.powi(19),
LOG2_E * 2f64.powi(20),
]
});
// TODO - replace it with LazyLock once it is stable
static SCALE_FACTORS: OnceLock<[f64; 21]> = OnceLock::new();

/// returns constants used in calculating the logarithm index.
#[inline]
fn scale_factors() -> &'static [f64; 21] {
SCALE_FACTORS.get_or_init(|| {
[
LOG2_E * 2f64.powi(0),
LOG2_E * 2f64.powi(1),
LOG2_E * 2f64.powi(2),
LOG2_E * 2f64.powi(3),
LOG2_E * 2f64.powi(4),
LOG2_E * 2f64.powi(5),
LOG2_E * 2f64.powi(6),
LOG2_E * 2f64.powi(7),
LOG2_E * 2f64.powi(8),
LOG2_E * 2f64.powi(9),
LOG2_E * 2f64.powi(10),
LOG2_E * 2f64.powi(11),
LOG2_E * 2f64.powi(12),
LOG2_E * 2f64.powi(13),
LOG2_E * 2f64.powi(14),
LOG2_E * 2f64.powi(15),
LOG2_E * 2f64.powi(16),
LOG2_E * 2f64.powi(17),
LOG2_E * 2f64.powi(18),
LOG2_E * 2f64.powi(19),
LOG2_E * 2f64.powi(20),
]
})
}

/// Breaks the number into a normalized fraction and a base-2 exponent.
///
Expand Down
14 changes: 9 additions & 5 deletions opentelemetry-sdk/src/metrics/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ use aggregate::is_under_cardinality_limit;
pub(crate) use aggregate::{AggregateBuilder, ComputeAggregation, Measure};
pub(crate) use exponential_histogram::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
use opentelemetry::{otel_warn, KeyValue};
use std::sync::LazyLock;
use std::sync::OnceLock;

pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: LazyLock<Vec<KeyValue>> =
LazyLock::new(|| vec![KeyValue::new("otel.metric.overflow", "true")]);
pub(crate) static STREAM_OVERFLOW_ATTRIBUTES: OnceLock<Vec<KeyValue>> = OnceLock::new();

#[inline]
fn stream_overflow_attributes() -> &'static Vec<KeyValue> {
STREAM_OVERFLOW_ATTRIBUTES.get_or_init(|| vec![KeyValue::new("otel.metric.overflow", "true")])
}

pub(crate) trait Aggregator {
/// A static configuration that is needed in order to initialize aggregator.
Expand Down Expand Up @@ -121,12 +125,12 @@ where
trackers.insert(sorted_attrs, new_tracker);

self.count.fetch_add(1, Ordering::SeqCst);
} else if let Some(overflow_value) = trackers.get(STREAM_OVERFLOW_ATTRIBUTES.as_slice()) {
} else if let Some(overflow_value) = trackers.get(stream_overflow_attributes().as_slice()) {
overflow_value.update(value);
} else {
let new_tracker = A::create(&self.config);
new_tracker.update(value);
trackers.insert(STREAM_OVERFLOW_ATTRIBUTES.clone(), Arc::new(new_tracker));
trackers.insert(stream_overflow_attributes().clone(), Arc::new(new_tracker));
otel_warn!( name: "ValueMap.measure",
message = "Maximum data points for metric stream exceeded. Entry added to overflow. Subsequent overflows to same metric until next collect will not be logged."
);
Expand Down
12 changes: 9 additions & 3 deletions opentelemetry-sdk/src/propagation/baggage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ use opentelemetry::{
};
use percent_encoding::{percent_decode_str, utf8_percent_encode, AsciiSet, CONTROLS};
use std::iter;
use std::sync::LazyLock;
use std::sync::OnceLock;

static BAGGAGE_HEADER: &str = "baggage";
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b';').add(b',').add(b'=');
static BAGGAGE_FIELDS: LazyLock<[String; 1]> = LazyLock::new(|| [BAGGAGE_HEADER.to_owned()]);

// TODO Replace this with LazyLock once it is stable.
static BAGGAGE_FIELDS: OnceLock<[String; 1]> = OnceLock::new();
#[inline]
fn baggage_fields() -> &'static [String; 1] {
BAGGAGE_FIELDS.get_or_init(|| [BAGGAGE_HEADER.to_owned()])
}

/// Propagates name-value pairs in [W3C Baggage] format.
///
Expand Down Expand Up @@ -149,7 +155,7 @@ impl TextMapPropagator for BaggagePropagator {
}

fn fields(&self) -> FieldIter<'_> {
FieldIter::new(BAGGAGE_FIELDS.as_ref())
FieldIter::new(baggage_fields())
}
}

Expand Down
13 changes: 9 additions & 4 deletions opentelemetry-sdk/src/propagation/trace_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ use opentelemetry::{
Context,
};
use std::str::FromStr;
use std::sync::LazyLock;
use std::sync::OnceLock;

const SUPPORTED_VERSION: u8 = 0;
const MAX_VERSION: u8 = 254;
const TRACEPARENT_HEADER: &str = "traceparent";
const TRACESTATE_HEADER: &str = "tracestate";

static TRACE_CONTEXT_HEADER_FIELDS: LazyLock<[String; 2]> =
LazyLock::new(|| [TRACEPARENT_HEADER.to_owned(), TRACESTATE_HEADER.to_owned()]);
// TODO Replace this with LazyLock once it is stable.
static TRACE_CONTEXT_HEADER_FIELDS: OnceLock<[String; 2]> = OnceLock::new();

fn trace_context_header_fields() -> &'static [String; 2] {
TRACE_CONTEXT_HEADER_FIELDS
.get_or_init(|| [TRACEPARENT_HEADER.to_owned(), TRACESTATE_HEADER.to_owned()])
}

/// Propagates `SpanContext`s in [W3C TraceContext] format under `traceparent` and `tracestate` header.
///
Expand Down Expand Up @@ -146,7 +151,7 @@ impl TextMapPropagator for TraceContextPropagator {
}

fn fields(&self) -> FieldIter<'_> {
FieldIter::new(TRACE_CONTEXT_HEADER_FIELDS.as_ref())
FieldIter::new(trace_context_header_fields())
}
}

Expand Down
39 changes: 23 additions & 16 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,34 @@ use opentelemetry::InstrumentationScope;
use opentelemetry::{otel_debug, trace::TraceResult};
use std::borrow::Cow;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::sync::{LazyLock, OnceLock};
use std::sync::{Arc, OnceLock};

use super::IdGenerator;

// TODO Replace with LazyLock once it is stable
static PROVIDER_RESOURCE: OnceLock<Resource> = OnceLock::new();

// a no nop tracer provider used as placeholder when the provider is shutdown
static NOOP_TRACER_PROVIDER: LazyLock<TracerProvider> = LazyLock::new(|| TracerProvider {
inner: Arc::new(TracerProviderInner {
processors: Vec::new(),
config: Config {
// cannot use default here as the default resource is not empty
sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
id_generator: Box::<RandomIdGenerator>::default(),
span_limits: SpanLimits::default(),
resource: Cow::Owned(Resource::empty()),
},
is_shutdown: AtomicBool::new(true),
}),
});
// TODO Replace with LazyLock once it is stable
static NOOP_TRACER_PROVIDER: OnceLock<TracerProvider> = OnceLock::new();
#[inline]
fn noop_tracer_provider() -> &'static TracerProvider {
NOOP_TRACER_PROVIDER.get_or_init(|| {
TracerProvider {
inner: Arc::new(TracerProviderInner {
processors: Vec::new(),
config: Config {
// cannot use default here as the default resource is not empty
sampler: Box::new(Sampler::ParentBased(Box::new(Sampler::AlwaysOn))),
id_generator: Box::<RandomIdGenerator>::default(),
span_limits: SpanLimits::default(),
resource: Cow::Owned(Resource::empty()),
},
is_shutdown: AtomicBool::new(true),
}),
}
})
}

/// TracerProvider inner type
#[derive(Debug)]
Expand Down Expand Up @@ -269,7 +276,7 @@ impl opentelemetry::trace::TracerProvider for TracerProvider {

fn tracer_with_scope(&self, scope: InstrumentationScope) -> Self::Tracer {
if self.inner.is_shutdown.load(Ordering::Relaxed) {
return Tracer::new(scope, NOOP_TRACER_PROVIDER.clone());
return Tracer::new(scope, noop_tracer_provider().clone());
}
Tracer::new(scope, self.clone())
}
Expand Down

0 comments on commit 2b1e9bd

Please sign in to comment.