Skip to content

Commit d28b42a

Browse files
authored
Merge branch 'main' into chore/metrics-advanced
2 parents d3754eb + 42685e8 commit d28b42a

File tree

10 files changed

+40
-114
lines changed

10 files changed

+40
-114
lines changed

opentelemetry-sdk/src/metrics/instrument.rs

+3-27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{borrow::Cow, collections::HashSet, sync::Arc};
22

33
use opentelemetry::{
4-
metrics::{AsyncInstrument, SyncCounter, SyncGauge, SyncHistogram, SyncUpDownCounter},
4+
metrics::{AsyncInstrument, SyncInstrument},
55
Key, KeyValue,
66
};
77

@@ -252,32 +252,8 @@ pub(crate) struct ResolvedMeasures<T> {
252252
pub(crate) measures: Vec<Arc<dyn Measure<T>>>,
253253
}
254254

255-
impl<T: Copy + 'static> SyncCounter<T> for ResolvedMeasures<T> {
256-
fn add(&self, val: T, attrs: &[KeyValue]) {
257-
for measure in &self.measures {
258-
measure.call(val, attrs)
259-
}
260-
}
261-
}
262-
263-
impl<T: Copy + 'static> SyncUpDownCounter<T> for ResolvedMeasures<T> {
264-
fn add(&self, val: T, attrs: &[KeyValue]) {
265-
for measure in &self.measures {
266-
measure.call(val, attrs)
267-
}
268-
}
269-
}
270-
271-
impl<T: Copy + 'static> SyncGauge<T> for ResolvedMeasures<T> {
272-
fn record(&self, val: T, attrs: &[KeyValue]) {
273-
for measure in &self.measures {
274-
measure.call(val, attrs)
275-
}
276-
}
277-
}
278-
279-
impl<T: Copy + 'static> SyncHistogram<T> for ResolvedMeasures<T> {
280-
fn record(&self, val: T, attrs: &[KeyValue]) {
255+
impl<T: Copy + 'static> SyncInstrument<T> for ResolvedMeasures<T> {
256+
fn measure(&self, val: T, attrs: &[KeyValue]) {
281257
for measure in &self.measures {
282258
measure.call(val, attrs)
283259
}

opentelemetry-sdk/src/metrics/noop.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use opentelemetry::{
2-
metrics::{
3-
AsyncInstrument, InstrumentProvider, SyncCounter, SyncGauge, SyncHistogram,
4-
SyncUpDownCounter,
5-
},
2+
metrics::{AsyncInstrument, InstrumentProvider, SyncInstrument},
63
KeyValue,
74
};
85

@@ -34,26 +31,8 @@ impl NoopSyncInstrument {
3431
}
3532
}
3633

37-
impl<T> SyncCounter<T> for NoopSyncInstrument {
38-
fn add(&self, _value: T, _attributes: &[KeyValue]) {
39-
// Ignored
40-
}
41-
}
42-
43-
impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
44-
fn add(&self, _value: T, _attributes: &[KeyValue]) {
45-
// Ignored
46-
}
47-
}
48-
49-
impl<T> SyncHistogram<T> for NoopSyncInstrument {
50-
fn record(&self, _value: T, _attributes: &[KeyValue]) {
51-
// Ignored
52-
}
53-
}
54-
55-
impl<T> SyncGauge<T> for NoopSyncInstrument {
56-
fn record(&self, _value: T, _attributes: &[KeyValue]) {
34+
impl<T> SyncInstrument<T> for NoopSyncInstrument {
35+
fn measure(&self, _value: T, _attributes: &[KeyValue]) {
5736
// Ignored
5837
}
5938
}

opentelemetry/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
- Bump MSRV to 1.70 [#2179](https://github.com/open-telemetry/opentelemetry-rust/pull/2179)
66
- Add `LogRecord::set_trace_context`; an optional method conditional on the `trace` feature for setting trace context on a log record.
7-
- Remove unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/issues/2187)
7+
- Removed unnecessary public methods named `as_any` from `AsyncInstrument` trait and the implementing instruments: `ObservableCounter`, `ObservableGauge`, and `ObservableUpDownCounter` [#2187](https://github.com/open-telemetry/opentelemetry-rust/issues/2187)
8+
- Introduced `SyncInstrument` trait to replace the individual synchronous instrument traits (`SyncCounter`, `SyncGauge`, `SyncHistogram`, `SyncUpDownCounter`) which are meant for SDK implementation. [#2207](https://github.com/open-telemetry/opentelemetry-rust/issues/2207)
89

910
## v0.26.0
1011
Released 2024-Sep-30

opentelemetry/src/metrics/instruments/counter.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ use crate::{metrics::AsyncInstrument, KeyValue};
22
use core::fmt;
33
use std::sync::Arc;
44

5-
/// An SDK implemented instrument that records increasing values.
6-
pub trait SyncCounter<T> {
7-
/// Records an increment to the counter.
8-
fn add(&self, value: T, attributes: &[KeyValue]);
9-
}
5+
use super::SyncInstrument;
106

117
/// An instrument that records increasing values.
128
#[derive(Clone)]
139
#[non_exhaustive]
14-
pub struct Counter<T>(Arc<dyn SyncCounter<T> + Send + Sync>);
10+
pub struct Counter<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
1511

1612
impl<T> fmt::Debug for Counter<T>
1713
where
@@ -24,13 +20,13 @@ where
2420

2521
impl<T> Counter<T> {
2622
/// Create a new counter.
27-
pub fn new(inner: Arc<dyn SyncCounter<T> + Send + Sync>) -> Self {
23+
pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
2824
Counter(inner)
2925
}
3026

3127
/// Records an increment to the counter.
3228
pub fn add(&self, value: T, attributes: &[KeyValue]) {
33-
self.0.add(value, attributes)
29+
self.0.measure(value, attributes)
3430
}
3531
}
3632

opentelemetry/src/metrics/instruments/gauge.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ use crate::{metrics::AsyncInstrument, KeyValue};
22
use core::fmt;
33
use std::sync::Arc;
44

5-
/// An SDK implemented instrument that records independent values
6-
pub trait SyncGauge<T> {
7-
/// Records an independent value.
8-
fn record(&self, value: T, attributes: &[KeyValue]);
9-
}
5+
use super::SyncInstrument;
106

117
/// An instrument that records independent values
128
#[derive(Clone)]
139
#[non_exhaustive]
14-
pub struct Gauge<T>(Arc<dyn SyncGauge<T> + Send + Sync>);
10+
pub struct Gauge<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
1511

1612
impl<T> fmt::Debug for Gauge<T>
1713
where
@@ -24,13 +20,13 @@ where
2420

2521
impl<T> Gauge<T> {
2622
/// Create a new gauge.
27-
pub fn new(inner: Arc<dyn SyncGauge<T> + Send + Sync>) -> Self {
23+
pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
2824
Gauge(inner)
2925
}
3026

3127
/// Records an independent value.
3228
pub fn record(&self, value: T, attributes: &[KeyValue]) {
33-
self.0.record(value, attributes)
29+
self.0.measure(value, attributes)
3430
}
3531
}
3632

opentelemetry/src/metrics/instruments/histogram.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ use crate::KeyValue;
22
use core::fmt;
33
use std::sync::Arc;
44

5-
/// An SDK implemented instrument that records a distribution of values.
6-
pub trait SyncHistogram<T> {
7-
/// Adds an additional value to the distribution.
8-
fn record(&self, value: T, attributes: &[KeyValue]);
9-
}
5+
use super::SyncInstrument;
106

117
/// An instrument that records a distribution of values.
128
#[derive(Clone)]
139
#[non_exhaustive]
14-
pub struct Histogram<T>(Arc<dyn SyncHistogram<T> + Send + Sync>);
10+
pub struct Histogram<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
1511

1612
impl<T> fmt::Debug for Histogram<T>
1713
where
@@ -24,12 +20,12 @@ where
2420

2521
impl<T> Histogram<T> {
2622
/// Create a new histogram.
27-
pub fn new(inner: Arc<dyn SyncHistogram<T> + Send + Sync>) -> Self {
23+
pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
2824
Histogram(inner)
2925
}
3026

3127
/// Adds an additional value to the distribution.
3228
pub fn record(&self, value: T, attributes: &[KeyValue]) {
33-
self.0.record(value, attributes)
29+
self.0.measure(value, attributes)
3430
}
3531
}

opentelemetry/src/metrics/instruments/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ pub trait AsyncInstrument<T>: Send + Sync {
2424
fn observe(&self, measurement: T, attributes: &[KeyValue]);
2525
}
2626

27+
/// An SDK implemented instrument that records measurements synchronously.
28+
pub trait SyncInstrument<T>: Send + Sync {
29+
/// Records a measurement synchronously.
30+
fn measure(&self, measurement: T, attributes: &[KeyValue]);
31+
}
32+
2733
/// Configuration for building a Histogram.
2834
#[non_exhaustive] // We expect to add more configuration fields in the future
2935
pub struct HistogramBuilder<'a, T> {

opentelemetry/src/metrics/instruments/up_down_counter.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ use crate::KeyValue;
22
use core::fmt;
33
use std::sync::Arc;
44

5-
use super::AsyncInstrument;
6-
7-
/// An SDK implemented instrument that records increasing or decreasing values.
8-
pub trait SyncUpDownCounter<T> {
9-
/// Records an increment or decrement to the counter.
10-
fn add(&self, value: T, attributes: &[KeyValue]);
11-
}
5+
use super::{AsyncInstrument, SyncInstrument};
126

137
/// An instrument that records increasing or decreasing values.
148
#[derive(Clone)]
159
#[non_exhaustive]
16-
pub struct UpDownCounter<T>(Arc<dyn SyncUpDownCounter<T> + Send + Sync>);
10+
pub struct UpDownCounter<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
1711

1812
impl<T> fmt::Debug for UpDownCounter<T>
1913
where
@@ -29,13 +23,13 @@ where
2923

3024
impl<T> UpDownCounter<T> {
3125
/// Create a new up down counter.
32-
pub fn new(inner: Arc<dyn SyncUpDownCounter<T> + Send + Sync>) -> Self {
26+
pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
3327
UpDownCounter(inner)
3428
}
3529

3630
/// Records an increment or decrement to the counter.
3731
pub fn add(&self, value: T, attributes: &[KeyValue]) {
38-
self.0.add(value, attributes)
32+
self.0.measure(value, attributes)
3933
}
4034
}
4135

opentelemetry/src/metrics/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ pub(crate) mod noop;
1313

1414
use crate::{Array, ExportError, KeyValue, Value};
1515
pub use instruments::{
16-
counter::{Counter, ObservableCounter, SyncCounter},
17-
gauge::{Gauge, ObservableGauge, SyncGauge},
18-
histogram::{Histogram, SyncHistogram},
19-
up_down_counter::{ObservableUpDownCounter, SyncUpDownCounter, UpDownCounter},
16+
counter::{Counter, ObservableCounter},
17+
gauge::{Gauge, ObservableGauge},
18+
histogram::Histogram,
19+
up_down_counter::{ObservableUpDownCounter, UpDownCounter},
2020
AsyncInstrument, AsyncInstrumentBuilder, Callback, HistogramBuilder, InstrumentBuilder,
21+
SyncInstrument,
2122
};
2223
pub use meter::{Meter, MeterProvider};
2324

opentelemetry/src/metrics/noop.rs

+5-24
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
//! has been set. It is expected to have minimal resource utilization and
55
//! runtime impact.
66
use crate::{
7-
metrics::{
8-
AsyncInstrument, InstrumentProvider, Meter, MeterProvider, SyncCounter, SyncGauge,
9-
SyncHistogram, SyncUpDownCounter,
10-
},
7+
metrics::{AsyncInstrument, InstrumentProvider, Meter, MeterProvider},
118
KeyValue,
129
};
1310
use std::sync::Arc;
1411

12+
use super::instruments::SyncInstrument;
13+
1514
/// A no-op instance of a `MetricProvider`
1615
#[derive(Debug, Default)]
1716
pub(crate) struct NoopMeterProvider {
@@ -65,26 +64,8 @@ impl NoopSyncInstrument {
6564
}
6665
}
6766

68-
impl<T> SyncCounter<T> for NoopSyncInstrument {
69-
fn add(&self, _value: T, _attributes: &[KeyValue]) {
70-
// Ignored
71-
}
72-
}
73-
74-
impl<T> SyncUpDownCounter<T> for NoopSyncInstrument {
75-
fn add(&self, _value: T, _attributes: &[KeyValue]) {
76-
// Ignored
77-
}
78-
}
79-
80-
impl<T> SyncHistogram<T> for NoopSyncInstrument {
81-
fn record(&self, _value: T, _attributes: &[KeyValue]) {
82-
// Ignored
83-
}
84-
}
85-
86-
impl<T> SyncGauge<T> for NoopSyncInstrument {
87-
fn record(&self, _value: T, _attributes: &[KeyValue]) {
67+
impl<T> SyncInstrument<T> for NoopSyncInstrument {
68+
fn measure(&self, _value: T, _attributes: &[KeyValue]) {
8869
// Ignored
8970
}
9071
}

0 commit comments

Comments
 (0)