Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use InstrumentationLibrary in metrics. #393

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion opentelemetry-prometheus/tests/integration_test.rs
Expand Up @@ -13,7 +13,7 @@ fn test_add() {
.with_resource(Resource::new(vec![KeyValue::new("R", "V")]))
.init();

let meter = exporter.provider().unwrap().meter("test");
let meter = exporter.provider().unwrap().meter("test", None);

let up_down_counter = meter.f64_up_down_counter("updowncounter").init();
let counter = meter.f64_counter("counter").init();
Expand Down
6 changes: 4 additions & 2 deletions opentelemetry/benches/ddsketch.rs
Expand Up @@ -28,7 +28,8 @@ fn ddsketch(data: Vec<f64>) {
DDSKetchAggregator::new(&DDSketchConfig::new(0.001, 2048, 1e-9), NumberKind::F64);
let descriptor = Descriptor::new(
"test".to_string(),
"test".to_string(),
"test",
None,
InstrumentKind::ValueRecorder,
NumberKind::F64,
);
Expand All @@ -53,7 +54,8 @@ fn array(data: Vec<f64>) {
let aggregator = ArrayAggregator::default();
let descriptor = Descriptor::new(
"test".to_string(),
"test".to_string(),
"test",
None,
InstrumentKind::ValueRecorder,
NumberKind::F64,
);
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry/benches/metric.rs
Expand Up @@ -131,7 +131,7 @@ impl Processor for BenchProcessor {
fn build_meter() -> Meter {
let processor = Arc::new(BenchProcessor::default());
let core = accumulator(processor).build();
Meter::new("benches", Arc::new(core))
Meter::new("benches", None, Arc::new(core))
}

criterion_group!(benches, counters);
Expand Down
32 changes: 18 additions & 14 deletions opentelemetry/src/api/metrics/config.rs
@@ -1,35 +1,39 @@
use crate::sdk::InstrumentationLibrary;
use crate::Unit;

/// Config contains some options for metrics of any kind.
#[derive(Clone, Debug, PartialEq, Hash)]
pub struct InstrumentConfig {
pub(crate) description: Option<String>,
pub(crate) unit: Option<Unit>,
pub(crate) instrumentation_name: String,
pub(crate) instrumentation_version: Option<String>,
pub(crate) instrumentation_library: InstrumentationLibrary,
}

impl InstrumentConfig {
/// Create a new config from instrumentation name
pub fn with_instrumentation_name(instrumentation_name: String) -> Self {
pub fn with_instrumentation_name(instrumentation_name: &'static str) -> Self {
InstrumentConfig {
description: None,
unit: None,
instrumentation_name,
instrumentation_version: None,
instrumentation_library: InstrumentationLibrary {
name: instrumentation_name,
version: None,
},
}
}

/// Create a new config with instrumentation name and version
/// Create a new config with instrumentation name and optional version
pub fn with_instrumentation(
instrumentation_name: String,
instrumentation_version: String,
instrumentation_name: &'static str,
instrumentation_version: Option<&'static str>,
) -> Self {
InstrumentConfig {
description: None,
unit: None,
instrumentation_name,
instrumentation_version: Some(instrumentation_version),
instrumentation_library: InstrumentationLibrary {
name: instrumentation_name,
version: instrumentation_version,
},
}
}

Expand All @@ -44,12 +48,12 @@ impl InstrumentConfig {
}

/// Instrumentation name is the name given to the Meter that created this instrument.
pub fn instrumentation_name(&self) -> &String {
&self.instrumentation_name
pub fn instrumentation_name(&self) -> &'static str {
self.instrumentation_library.name
}

/// Instrumentation version returns the version of instrumentation
pub fn instrumentation_version(&self) -> Option<&String> {
self.instrumentation_version.as_ref()
pub fn instrumentation_version(&self) -> Option<&'static str> {
self.instrumentation_library.version
}
}
3 changes: 2 additions & 1 deletion opentelemetry/src/api/metrics/counter.rs
Expand Up @@ -69,7 +69,8 @@ impl<'a, T> CounterBuilder<'a, T> {
meter,
descriptor: Descriptor::new(
name,
meter.instrumentation_name().to_string(),
meter.instrumentation_library().name,
meter.instrumentation_library().version,
InstrumentKind::Counter,
number_kind,
),
Expand Down
13 changes: 9 additions & 4 deletions opentelemetry/src/api/metrics/descriptor.rs
Expand Up @@ -17,21 +17,26 @@ impl Descriptor {
/// Create a new descriptor
pub fn new(
name: String,
instrumentation_name: String,
instrumentation_name: &'static str,
instrumentation_version: Option<&'static str>,
instrument_kind: InstrumentKind,
number_kind: NumberKind,
) -> Self {
let mut hasher = FnvHasher::default();
name.hash(&mut hasher);
instrumentation_name.hash(&mut hasher);
instrumentation_version.hash(&mut hasher);
instrument_kind.hash(&mut hasher);
number_kind.hash(&mut hasher);

Descriptor {
name,
instrument_kind,
number_kind,
config: InstrumentConfig::with_instrumentation_name(instrumentation_name),
config: InstrumentConfig::with_instrumentation(
instrumentation_name,
instrumentation_version,
),
attribute_hash: hasher.finish(),
}
}
Expand Down Expand Up @@ -68,8 +73,8 @@ impl Descriptor {
}

/// The name of the library that provided instrumentation for this instrument.
pub fn instrumentation_name(&self) -> &str {
self.config.instrumentation_name.as_str()
pub fn instrumentation_name(&self) -> &'static str {
self.config.instrumentation_name()
}

/// The pre-computed hash of the descriptor data
Expand Down
25 changes: 20 additions & 5 deletions opentelemetry/src/api/metrics/meter.rs
@@ -1,3 +1,4 @@
use crate::sdk::InstrumentationLibrary;
use crate::{
metrics::{
sdk_api, AsyncRunner, BatchObserver, BatchObserverCallback, CounterBuilder, Descriptor,
Expand All @@ -18,31 +19,45 @@ pub trait MeterProvider: fmt::Debug {
/// empty, then a implementation defined default name will be used instead.
///
/// [`Meter`]: struct.Meter.html
fn meter(&self, instrumentation_name: &str) -> Meter;
fn meter(
&self,
instrumentation_name: &'static str,
instrumentation_version: Option<&'static str>,
) -> Meter;
}

/// Meter is the OpenTelemetry metric API, based on a sdk-defined `MeterCore`
/// implementation and the `Meter` library name.
#[derive(Debug)]
pub struct Meter {
instrumentation_name: String,
instrumentation_library: InstrumentationLibrary,
core: Arc<dyn sdk_api::MeterCore + Send + Sync>,
}

impl Meter {
/// Create a new named meter from a sdk implemented core
pub fn new<T: Into<String>>(
pub fn new<T: Into<&'static str>>(
instrumentation_name: T,
instrumentation_version: Option<T>,
core: Arc<dyn sdk_api::MeterCore + Send + Sync>,
) -> Self {
Meter {
instrumentation_name: instrumentation_name.into(),
instrumentation_library: InstrumentationLibrary::new(
instrumentation_name.into(),
instrumentation_version.map(Into::into),
),
core,
}
}

#[deprecated(note = "use instrumentation_library() instead")]
#[allow(dead_code)]
pub(crate) fn instrumentation_name(&self) -> &str {
self.instrumentation_name.as_str()
self.instrumentation_library.name
}

pub(crate) fn instrumentation_library(&self) -> InstrumentationLibrary {
self.instrumentation_library
}

/// Creates a new floating point `ValueObserverBuilder` instrument with the
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry/src/api/metrics/noop.rs
Expand Up @@ -18,7 +18,7 @@ use std::any::Any;
use std::sync::Arc;

lazy_static::lazy_static! {
static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop".to_string(), InstrumentKind::Counter, NumberKind::U64);
static ref NOOP_DESCRIPTOR: Descriptor = Descriptor::new(String::new(), "noop", None, InstrumentKind::Counter, NumberKind::U64);
}

/// A no-op instance of a `MetricProvider`
Expand All @@ -35,8 +35,8 @@ impl NoopMeterProvider {
}

impl MeterProvider for NoopMeterProvider {
fn meter(&self, name: &str) -> Meter {
Meter::new(name, Arc::new(NoopMeterCore::new()))
fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
Meter::new(name, version, Arc::new(NoopMeterCore::new()))
}
}

Expand Down
9 changes: 6 additions & 3 deletions opentelemetry/src/api/metrics/observer.rs
Expand Up @@ -56,7 +56,8 @@ impl<'a, T> SumObserverBuilder<'a, T> {
meter,
descriptor: Descriptor::new(
name,
meter.instrumentation_name().to_string(),
meter.instrumentation_library().name,
meter.instrumentation_library().version,
InstrumentKind::SumObserver,
number_kind,
),
Expand Down Expand Up @@ -142,7 +143,8 @@ impl<'a, T> UpDownSumObserverBuilder<'a, T> {
meter,
descriptor: Descriptor::new(
name,
meter.instrumentation_name().to_string(),
meter.instrumentation_library().name,
meter.instrumentation_library().version,
InstrumentKind::UpDownSumObserver,
number_kind,
),
Expand Down Expand Up @@ -227,7 +229,8 @@ impl<'a, T> ValueObserverBuilder<'a, T> {
meter,
descriptor: Descriptor::new(
name,
meter.instrumentation_name().to_string(),
meter.instrumentation_library().name,
meter.instrumentation_library().version,
InstrumentKind::ValueObserver,
number_kind,
),
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry/src/api/metrics/registry.rs
Expand Up @@ -20,8 +20,8 @@ pub fn meter_provider(core: Arc<dyn MeterCore + Send + Sync>) -> RegistryMeterPr
pub struct RegistryMeterProvider(Arc<dyn MeterCore + Send + Sync>);

impl MeterProvider for RegistryMeterProvider {
fn meter(&self, name: &str) -> Meter {
Meter::new(name, self.0.clone())
fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
Meter::new(name, version, self.0.clone())
}
}

Expand Down
3 changes: 2 additions & 1 deletion opentelemetry/src/api/metrics/up_down_counter.rs
Expand Up @@ -69,7 +69,8 @@ impl<'a, T> UpDownCounterBuilder<'a, T> {
meter,
descriptor: Descriptor::new(
name,
meter.instrumentation_name().to_string(),
meter.instrumentation_library().name,
meter.instrumentation_library().version,
InstrumentKind::UpDownCounter,
number_kind,
),
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry/src/api/metrics/value_recorder.rs
Expand Up @@ -69,7 +69,8 @@ impl<'a, T> ValueRecorderBuilder<'a, T> {
meter,
descriptor: Descriptor::new(
name,
meter.instrumentation_name().to_string(),
meter.instrumentation_library().name,
meter.instrumentation_library().version,
InstrumentKind::ValueRecorder,
number_kind,
),
Expand Down
15 changes: 11 additions & 4 deletions opentelemetry/src/global/metrics.rs
Expand Up @@ -16,8 +16,8 @@ pub struct GlobalMeterProvider {
}

impl MeterProvider for GlobalMeterProvider {
fn meter(&self, name: &str) -> Meter {
self.provider.meter(name)
fn meter(&self, name: &'static str, version: Option<&'static str>) -> Meter {
self.provider.meter(name, version)
}
}

Expand Down Expand Up @@ -67,6 +67,13 @@ pub fn meter_provider() -> GlobalMeterProvider {
///
/// [`Meter`]: ../../api/metrics/meter/struct.Meter.html
/// [`GlobalMeterProvider`]: struct.GlobalMeterProvider.html
pub fn meter(name: &str) -> Meter {
meter_provider().meter(name)
pub fn meter(name: &'static str) -> Meter {
meter_provider().meter(name, None)
}

/// Creates a [`Meter`] with the name and version.
///
/// [`Meter`]: ../../api/metrics/meter/struct.Meter.html
pub fn meter_with_version(name: &'static str, version: &'static str) -> Meter {
meter_provider().meter(name, Some(version))
}
4 changes: 3 additions & 1 deletion opentelemetry/src/global/mod.rs
Expand Up @@ -140,7 +140,9 @@ mod trace;
pub use error_handler::{handle_error, set_error_handler};
#[cfg(feature = "metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
pub use metrics::{meter, meter_provider, set_meter_provider, GlobalMeterProvider};
pub use metrics::{
meter, meter_provider, meter_with_version, set_meter_provider, GlobalMeterProvider,
};
#[cfg(feature = "trace")]
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
pub use propagation::{get_text_map_propagator, set_text_map_propagator};
Expand Down
6 changes: 4 additions & 2 deletions opentelemetry/src/sdk/metrics/aggregators/ddsketch.rs
Expand Up @@ -811,7 +811,8 @@ mod tests {
);
let descriptor = Descriptor::new(
"test".to_string(),
"test".to_string(),
"test",
None,
InstrumentKind::ValueRecorder,
kind.clone(),
);
Expand Down Expand Up @@ -976,7 +977,8 @@ mod tests {
);
let descriptor = Descriptor::new(
"test".to_string(),
"test".to_string(),
"test",
None,
InstrumentKind::ValueRecorder,
kind.clone(),
);
Expand Down