Skip to content

Commit 167baf8

Browse files
authored
Minor cleanups in Metrics module (#2155)
1 parent 380a709 commit 167baf8

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99

1010
use opentelemetry::{
1111
global,
12-
metrics::{noop::NoopMeterCore, Meter, MeterProvider, MetricsError, Result},
12+
metrics::{noop::NoopMeter, Meter, MeterProvider, MetricsError, Result},
1313
KeyValue,
1414
};
1515

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

154157
let mut builder = Scope::builder(name);
@@ -174,7 +177,7 @@ impl MeterProvider for SdkMeterProvider {
174177
.clone();
175178
Meter::new(meter)
176179
} else {
177-
Meter::new(Arc::new(NoopMeterCore::new()))
180+
Meter::new(Arc::new(NoopMeter::new()))
178181
}
179182
}
180183
}

opentelemetry/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
- **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`.
1111

12+
- **Renamed**: `NoopMeterCore` to `NoopMeter`
13+
1214
- Added `with_boundaries` API to allow users to provide custom bounds for Histogram instruments. [#2135](https://github.com/open-telemetry/opentelemetry-rust/pull/2135)
1315

1416
## v0.25.0

opentelemetry/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ categories = [
1111
"api-bindings",
1212
"asynchronous",
1313
]
14-
keywords = ["opentelemetry", "logging", "tracing", "metrics", "async"]
14+
keywords = ["opentelemetry", "logging", "tracing", "metrics"]
1515
license = "Apache-2.0"
1616
edition = "2021"
1717
rust-version = "1.65"

opentelemetry/src/metrics/meter.rs

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ pub trait MeterProvider {
7171
/// your application's processing logic. For example, you might use a Counter
7272
/// to record the number of HTTP requests received.
7373
///
74-
/// - **Asynchronous Instruments** (e.g., Gauge): These allow you to register a
75-
/// callback function that is invoked during export. For instance, you could
76-
/// use an asynchronous gauge to monitor temperature from a sensor every time
77-
/// metrics are exported.
74+
/// - **Asynchronous Instruments** (e.g., ObservableGauge): These allow you to
75+
/// register a callback function that is invoked during export. For instance,
76+
/// you could use an asynchronous gauge to monitor temperature from a sensor
77+
/// every time metrics are exported.
7878
///
7979
/// # Example Usage
8080
///
@@ -105,7 +105,6 @@ pub trait MeterProvider {
105105
/// ],
106106
/// );
107107
///
108-
/// // Asynchronous Instruments
109108
///
110109
/// // u64 Observable Counter
111110
/// let _observable_u64_counter = meter
@@ -191,6 +190,36 @@ pub trait MeterProvider {
191190
/// })
192191
/// .init();
193192
///
193+
/// // i64 Gauge
194+
/// let gauge = meter.i64_gauge("my_gauge").init();
195+
/// gauge.record(
196+
/// -10,
197+
/// &[
198+
/// KeyValue::new("mykey1", "myvalue1"),
199+
/// KeyValue::new("mykey2", "myvalue2"),
200+
/// ],
201+
/// );
202+
///
203+
/// // u64 Gauge
204+
/// let gauge = meter.u64_gauge("my_gauge").init();
205+
/// gauge.record(
206+
/// 101,
207+
/// &[
208+
/// KeyValue::new("mykey1", "myvalue1"),
209+
/// KeyValue::new("mykey2", "myvalue2"),
210+
/// ],
211+
/// );
212+
///
213+
/// // f64 Gauge
214+
/// let gauge = meter.f64_gauge("my_gauge").init();
215+
/// gauge.record(
216+
/// 12.5,
217+
/// &[
218+
/// KeyValue::new("mykey1", "myvalue1"),
219+
/// KeyValue::new("mykey2", "myvalue2"),
220+
/// ],
221+
/// );
222+
///
194223
/// // u64 Observable Gauge
195224
/// let _observable_u64_gauge = meter
196225
/// .u64_observable_gauge("my_u64_gauge")

opentelemetry/src/metrics/noop.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ impl MeterProvider for NoopMeterProvider {
3333
_schema_url: Option<&'static str>,
3434
_attributes: Option<Vec<KeyValue>>,
3535
) -> Meter {
36-
Meter::new(Arc::new(NoopMeterCore::new()))
36+
Meter::new(Arc::new(NoopMeter::new()))
3737
}
3838
}
3939

4040
/// A no-op instance of a `Meter`
4141
#[derive(Debug, Default)]
42-
pub struct NoopMeterCore {
42+
pub struct NoopMeter {
4343
_private: (),
4444
}
4545

46-
impl NoopMeterCore {
46+
impl NoopMeter {
4747
/// Create a new no-op meter core.
4848
pub fn new() -> Self {
49-
NoopMeterCore { _private: () }
49+
NoopMeter { _private: () }
5050
}
5151
}
5252

53-
impl InstrumentProvider for NoopMeterCore {}
53+
impl InstrumentProvider for NoopMeter {}
5454

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

0 commit comments

Comments
 (0)