Skip to content

Commit

Permalink
Use default names for SDK provider implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-fabs committed Oct 23, 2024
1 parent a1d9091 commit afeb1aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
25 changes: 21 additions & 4 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ use opentelemetry::{
#[cfg(feature = "logs_level_enabled")]
use opentelemetry::logs::Severity;

use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
};
use std::time::SystemTime;
use std::{
borrow::Cow,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

use once_cell::sync::Lazy;

Expand Down Expand Up @@ -45,9 +48,23 @@ pub struct LoggerProvider {
inner: Arc<LoggerProviderInner>,
}

/// Default logger name if empty string is provided.
const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/logger";

impl opentelemetry::logs::LoggerProvider for LoggerProvider {
type Logger = Logger;

fn logger(&self, name: impl Into<Cow<'static, str>>) -> Self::Logger {
let mut name = name.into();

if name.is_empty() {
name = Cow::Borrowed(DEFAULT_COMPONENT_NAME)

Check warning on line 61 in opentelemetry-sdk/src/logs/log_emitter.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/logs/log_emitter.rs#L61

Added line #L61 was not covered by tests
};

let scope = InstrumentationScope::builder(name).build();
self.logger_with_scope(scope)
}

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) {
Expand Down
13 changes: 13 additions & 0 deletions opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,20 @@ impl Drop for SdkMeterProviderInner {
}
}
}

/// Default logger name if empty string is provided.
const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/meter";

impl MeterProvider for SdkMeterProvider {
fn meter(&self, mut name: &'static str) -> Meter {
if name.is_empty() {
name = DEFAULT_COMPONENT_NAME

Check warning on line 154 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L154

Added line #L154 was not covered by tests
};

let scope = InstrumentationScope::builder(name).build();
self.meter_with_scope(scope)
}

fn meter_with_scope(&self, scope: InstrumentationScope) -> Meter {
if self.inner.is_shutdown.load(Ordering::Relaxed) {
return Meter::new(Arc::new(NoopMeter::new()));

Check warning on line 163 in opentelemetry-sdk/src/metrics/meter_provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/metrics/meter_provider.rs#L163

Added line #L163 was not covered by tests
Expand Down
14 changes: 14 additions & 0 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,24 @@ impl TracerProvider {
}
}

/// Default tracer name if empty string is provided.
const DEFAULT_COMPONENT_NAME: &str = "rust.opentelemetry.io/sdk/tracer";

impl opentelemetry::trace::TracerProvider for TracerProvider {
/// This implementation of `TracerProvider` produces `Tracer` instances.
type Tracer = Tracer;

fn tracer(&self, name: impl Into<Cow<'static, str>>) -> Self::Tracer {
let mut name = name.into();

if name.is_empty() {
name = Cow::Borrowed(DEFAULT_COMPONENT_NAME)

Check warning on line 261 in opentelemetry-sdk/src/trace/provider.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/provider.rs#L261

Added line #L261 was not covered by tests
};

let scope = InstrumentationScope::builder(name).build();
self.tracer_with_scope(scope)
}

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());
Expand Down

0 comments on commit afeb1aa

Please sign in to comment.