Skip to content

Commit

Permalink
Minor cleanups in Metrics module (open-telemetry#2155)
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas committed Oct 4, 2024
1 parent 5003dd3 commit 29ec004
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
13 changes: 8 additions & 5 deletions opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{

use opentelemetry::{
global,
metrics::{noop::NoopMeterCore, Meter, MeterProvider, MetricsError, Result},
metrics::{noop::NoopMeter, Meter, MeterProvider, MetricsError, Result},
KeyValue,
};

Expand All @@ -21,8 +21,11 @@ use super::{meter::SdkMeter, pipeline::Pipelines, reader::MetricReader, view::Vi
///
/// All `Meter`s created by a `MeterProvider` will be associated with the same
/// [Resource], have the same [View]s applied to them, and have their produced
/// metric telemetry passed to the configured [MetricReader]s.
///
/// metric telemetry passed to the configured [MetricReader]s. This is a
/// clonable handle to the MeterProvider implementation itself, and cloning it
/// will create a new reference, not a new instance of a MeterProvider. Dropping
/// the last reference to it will trigger shutdown of the provider. Shutdown can
/// also be triggered manually by calling the `shutdown` method.
/// [Meter]: opentelemetry::metrics::Meter
#[derive(Clone, Debug)]
pub struct SdkMeterProvider {
Expand Down Expand Up @@ -149,7 +152,7 @@ impl MeterProvider for SdkMeterProvider {
attributes: Option<Vec<KeyValue>>,
) -> Meter {
if self.inner.is_shutdown.load(Ordering::Relaxed) {
return Meter::new(Arc::new(NoopMeterCore::new()));
return Meter::new(Arc::new(NoopMeter::new()));
}

let mut builder = Scope::builder(name);
Expand All @@ -175,7 +178,7 @@ impl MeterProvider for SdkMeterProvider {
.clone();
Meter::new(meter)
} else {
Meter::new(Arc::new(NoopMeterCore::new()))
Meter::new(Arc::new(NoopMeter::new()))
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

- **Modified**: `MeterProvider.meter()` and `MeterProvider.versioned_meter()` argument types have been updated to `&'static str` instead of `impl Into<Cow<'static, str>>>` [#2112](https://github.com/open-telemetry/opentelemetry-rust/pull/2112). These APIs were modified to enforce the Meter `name`, `version`, and `schema_url` to be `&'static str`.

- **Renamed**: `NoopMeterCore` to `NoopMeter`

- Added `with_boundaries` API to allow users to provide custom bounds for Histogram instruments. [#2135](https://github.com/open-telemetry/opentelemetry-rust/pull/2135)

## v0.25.0
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ categories = [
"api-bindings",
"asynchronous",
]
keywords = ["opentelemetry", "logging", "tracing", "metrics", "async"]
keywords = ["opentelemetry", "logging", "tracing", "metrics"]
license = "Apache-2.0"
edition = "2021"
rust-version = "1.65"
Expand Down
39 changes: 34 additions & 5 deletions opentelemetry/src/metrics/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ pub trait MeterProvider {
/// your application's processing logic. For example, you might use a Counter
/// to record the number of HTTP requests received.
///
/// - **Asynchronous Instruments** (e.g., Gauge): These allow you to register a
/// callback function that is invoked during export. For instance, you could
/// use an asynchronous gauge to monitor temperature from a sensor every time
/// metrics are exported.
/// - **Asynchronous Instruments** (e.g., ObservableGauge): These allow you to
/// register a callback function that is invoked during export. For instance,
/// you could use an asynchronous gauge to monitor temperature from a sensor
/// every time metrics are exported.
///
/// # Example Usage
///
Expand Down Expand Up @@ -105,7 +105,6 @@ pub trait MeterProvider {
/// ],
/// );
///
/// // Asynchronous Instruments
///
/// // u64 Observable Counter
/// let _observable_u64_counter = meter
Expand Down Expand Up @@ -191,6 +190,36 @@ pub trait MeterProvider {
/// })
/// .init();
///
/// // i64 Gauge
/// let gauge = meter.i64_gauge("my_gauge").init();
/// gauge.record(
/// -10,
/// &[
/// KeyValue::new("mykey1", "myvalue1"),
/// KeyValue::new("mykey2", "myvalue2"),
/// ],
/// );
///
/// // u64 Gauge
/// let gauge = meter.u64_gauge("my_gauge").init();
/// gauge.record(
/// 101,
/// &[
/// KeyValue::new("mykey1", "myvalue1"),
/// KeyValue::new("mykey2", "myvalue2"),
/// ],
/// );
///
/// // f64 Gauge
/// let gauge = meter.f64_gauge("my_gauge").init();
/// gauge.record(
/// 12.5,
/// &[
/// KeyValue::new("mykey1", "myvalue1"),
/// KeyValue::new("mykey2", "myvalue2"),
/// ],
/// );
///
/// // u64 Observable Gauge
/// let _observable_u64_gauge = meter
/// .u64_observable_gauge("my_u64_gauge")
Expand Down
10 changes: 5 additions & 5 deletions opentelemetry/src/metrics/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ impl MeterProvider for NoopMeterProvider {
_schema_url: Option<&'static str>,
_attributes: Option<Vec<KeyValue>>,
) -> Meter {
Meter::new(Arc::new(NoopMeterCore::new()))
Meter::new(Arc::new(NoopMeter::new()))
}
}

/// A no-op instance of a `Meter`
#[derive(Debug, Default)]
pub struct NoopMeterCore {
pub struct NoopMeter {
_private: (),
}

impl NoopMeterCore {
impl NoopMeter {
/// Create a new no-op meter core.
pub fn new() -> Self {
NoopMeterCore { _private: () }
NoopMeter { _private: () }
}
}

impl InstrumentProvider for NoopMeterCore {}
impl InstrumentProvider for NoopMeter {}

/// A no-op sync instrument
#[derive(Debug, Default)]
Expand Down

0 comments on commit 29ec004

Please sign in to comment.