Skip to content

Commit

Permalink
feat: use Cow<'static, str> instead of &'static str
Browse files Browse the repository at this point in the history
  • Loading branch information
tqwewe committed Apr 3, 2023
1 parent 3597479 commit c6f5b7b
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 49 deletions.
19 changes: 11 additions & 8 deletions opentelemetry-api/src/global/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::metrics::{self, Meter, MeterProvider};
use core::fmt;
use once_cell::sync::Lazy;
use std::sync::{Arc, RwLock};
use std::{
borrow::Cow,
sync::{Arc, RwLock},
};

/// The global `Meter` provider singleton.
static GLOBAL_METER_PROVIDER: Lazy<RwLock<GlobalMeterProvider>> = Lazy::new(|| {
Expand All @@ -26,9 +29,9 @@ impl fmt::Debug for GlobalMeterProvider {
impl MeterProvider for GlobalMeterProvider {
fn versioned_meter(
&self,
name: &'static str,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Meter {
self.provider.versioned_meter(name, version, schema_url)
}
Expand Down Expand Up @@ -72,7 +75,7 @@ pub fn meter_provider() -> GlobalMeterProvider {
/// If the name is an empty string, the provider will use a default name.
///
/// This is a more convenient way of expressing `global::meter_provider().meter(name, None, None)`.
pub fn meter(name: &'static str) -> Meter {
pub fn meter(name: Cow<'static, str>) -> Meter {
meter_provider().versioned_meter(name, None, None)
}

Expand All @@ -90,9 +93,9 @@ pub fn meter(name: &'static str) -> Meter {
/// ```
///
pub fn meter_with_version(
name: &'static str,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Meter {
meter_provider().versioned_meter(name, version, schema_url)
}
16 changes: 8 additions & 8 deletions opentelemetry-api/src/global/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ pub trait ObjectSafeTracerProvider {
fn versioned_tracer_boxed(
&self,
name: Cow<'static, str>,
version: Option<&'static str>,
schema_url: Option<&'static str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Box<dyn ObjectSafeTracer + Send + Sync>;
}

Expand All @@ -306,8 +306,8 @@ where
fn versioned_tracer_boxed(
&self,
name: Cow<'static, str>,
version: Option<&'static str>,
schema_url: Option<&'static str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Box<dyn ObjectSafeTracer + Send + Sync> {
Box::new(self.versioned_tracer(name, version, schema_url))
}
Expand Down Expand Up @@ -349,13 +349,13 @@ impl trace::TracerProvider for GlobalTracerProvider {
/// Create a versioned tracer using the global provider.
fn versioned_tracer(
&self,
name: impl Into<Cow<'static, str>>,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Self::Tracer {
BoxedTracer(
self.provider
.versioned_tracer_boxed(name.into(), version, schema_url),
.versioned_tracer_boxed(name, version, schema_url),
)
}
}
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-api/src/metrics/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait MeterProvider {
///
/// If the name is empty, then an implementation defined default name will
/// be used instead.
fn meter(&self, name: &'static str) -> Meter {
fn meter(&self, name: Cow<'static, str>) -> Meter {
self.versioned_meter(name, None, None)
}

Expand All @@ -34,9 +34,9 @@ pub trait MeterProvider {
/// default name will be used instead.
fn versioned_meter(
&self,
name: &'static str,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Meter;
}

Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-api/src/metrics/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
},
Context, KeyValue,
};
use std::{any::Any, sync::Arc};
use std::{any::Any, borrow::Cow, sync::Arc};

/// A no-op instance of a `MetricProvider`
#[derive(Debug, Default)]
Expand All @@ -28,9 +28,9 @@ impl NoopMeterProvider {
impl MeterProvider for NoopMeterProvider {
fn versioned_meter(
&self,
_name: &'static str,
_version: Option<&'static str>,
_schema_url: Option<&'static str>,
_name: Cow<'static, str>,
_version: Option<Cow<'static, str>>,
_schema_url: Option<Cow<'static, str>>,
) -> Meter {
Meter::new(Arc::new(NoopMeterCore::new()))
}
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-api/src/trace/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ impl trace::TracerProvider for NoopTracerProvider {
/// Returns a new `NoopTracer` instance.
fn versioned_tracer(
&self,
_name: impl Into<Cow<'static, str>>,
_version: Option<&'static str>,
_schema_url: Option<&'static str>,
_name: Cow<'static, str>,
_version: Option<Cow<'static, str>>,
_schema_url: Option<Cow<'static, str>>,
) -> Self::Tracer {
NoopTracer::new()
}
Expand Down
8 changes: 4 additions & 4 deletions opentelemetry-api/src/trace/tracer_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait TracerProvider {
/// Some("https://opentelemetry.io/schema/1.0.0")
/// );
/// ```
fn tracer(&self, name: impl Into<Cow<'static, str>>) -> Self::Tracer {
fn tracer(&self, name: Cow<'static, str>) -> Self::Tracer {
self.versioned_tracer(name, None, None)
}

Expand Down Expand Up @@ -63,8 +63,8 @@ pub trait TracerProvider {
/// ```
fn versioned_tracer(
&self,
name: impl Into<Cow<'static, str>>,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Self::Tracer;
}
4 changes: 2 additions & 2 deletions opentelemetry-jaeger/src/exporter/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ pub(crate) fn install_tracer_provider_and_get_tracer(
tracer_provider: sdk::trace::TracerProvider,
) -> Result<sdk::trace::Tracer, TraceError> {
let tracer = tracer_provider.versioned_tracer(
"opentelemetry-jaeger",
Some(env!("CARGO_PKG_VERSION")),
"opentelemetry-jaeger".into(),
Some(env!("CARGO_PKG_VERSION").into()),
None,
);
let _ = global::set_tracer_provider(tracer_provider);
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/export/trace/stdout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ where
let provider = provider_builder.build();

let tracer =
provider.versioned_tracer("opentelemetry", Some(env!("CARGO_PKG_VERSION")), None);
provider.versioned_tracer("opentelemetry".into(), Some(env!("CARGO_PKG_VERSION").into()), None);
let _ = global::set_tracer_provider(provider);

tracer
Expand Down
15 changes: 9 additions & 6 deletions opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use core::fmt;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
use std::{
borrow::Cow,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
};

use opentelemetry_api::{
Expand Down Expand Up @@ -73,9 +76,9 @@ impl MeterProvider {
impl opentelemetry_api::metrics::MeterProvider for MeterProvider {
fn versioned_meter(
&self,
name: &'static str,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> ApiMeter {
let inst_provider: Arc<dyn InstrumentProvider + Send + Sync> =
if !self.is_shutdown.load(Ordering::Relaxed) {
Expand Down
13 changes: 4 additions & 9 deletions opentelemetry-sdk/src/trace/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,17 @@ impl opentelemetry_api::trace::TracerProvider for TracerProvider {
/// Create a new versioned `Tracer` instance.
fn versioned_tracer(
&self,
name: impl Into<Cow<'static, str>>,
version: Option<&'static str>,
schema_url: Option<&'static str>,
name: Cow<'static, str>,
version: Option<Cow<'static, str>>,
schema_url: Option<Cow<'static, str>>,
) -> Self::Tracer {
let name = name.into();
// Use default value if name is invalid empty string
let component_name = if name.is_empty() {
Cow::Borrowed(DEFAULT_COMPONENT_NAME)
} else {
name
};
let instrumentation_lib = InstrumentationLibrary::new(
component_name,
version.map(Into::into),
schema_url.map(Into::into),
);
let instrumentation_lib = InstrumentationLibrary::new(component_name, version, schema_url);

Tracer::new(instrumentation_lib, Arc::downgrade(&self.inner))
}
Expand Down

0 comments on commit c6f5b7b

Please sign in to comment.