Skip to content

Commit aeb38a0

Browse files
authored
feat: Promote subset of Metric Views to stable (#2982)
1 parent 857a38b commit aeb38a0

File tree

9 files changed

+83
-129
lines changed

9 files changed

+83
-129
lines changed

examples/metrics-advanced/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ bench = false
1313

1414
[dependencies]
1515
opentelemetry = { path = "../../opentelemetry", features = ["metrics"] }
16-
opentelemetry_sdk = { path = "../../opentelemetry-sdk", features = ["spec_unstable_metrics_views"] }
16+
opentelemetry_sdk = { path = "../../opentelemetry-sdk" }
1717
opentelemetry-stdout = { workspace = true, features = ["metrics"] }
1818
tokio = { workspace = true, features = ["full"] }

examples/metrics-advanced/src/main.rs

Lines changed: 20 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use opentelemetry::global;
2-
use opentelemetry::Key;
32
use opentelemetry::KeyValue;
4-
use opentelemetry_sdk::metrics::{Aggregation, Instrument, SdkMeterProvider, Stream, Temporality};
3+
use opentelemetry_sdk::metrics::{Instrument, SdkMeterProvider, Stream, Temporality};
54
use opentelemetry_sdk::Resource;
65
use std::error::Error;
76

@@ -20,23 +19,9 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
2019
};
2120

2221
// for example 2
23-
let my_view_drop_attributes = |i: &Instrument| {
24-
if i.name == "my_counter" {
25-
Some(Stream::new().allowed_attribute_keys(vec![Key::from("mykey1")]))
26-
} else {
27-
None
28-
}
29-
};
30-
31-
// for example 3
32-
let my_view_change_aggregation = |i: &Instrument| {
22+
let my_view_change_cardinality = |i: &Instrument| {
3323
if i.name == "my_second_histogram" {
34-
Some(
35-
Stream::new().aggregation(Aggregation::ExplicitBucketHistogram {
36-
boundaries: vec![0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5],
37-
record_min_max: false,
38-
}),
39-
)
24+
Some(Stream::new().cardinality_limit(2))
4025
} else {
4126
None
4227
}
@@ -55,8 +40,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
5540
.with_periodic_exporter(exporter)
5641
.with_resource(resource)
5742
.with_view(my_view_rename_and_unit)
58-
.with_view(my_view_drop_attributes)
59-
.with_view(my_view_change_aggregation)
43+
.with_view(my_view_change_cardinality)
6044
.build();
6145
global::set_meter_provider(provider.clone());
6246
provider
@@ -88,65 +72,31 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
8872
],
8973
);
9074

91-
// Example 2 - Drop unwanted attributes using view.
92-
let counter = meter.u64_counter("my_counter").build();
93-
94-
// Record measurements using the Counter instrument.
95-
// Though we are passing 4 attributes here, only 1 will be used
96-
// for aggregation as view is configured to use only "mykey1"
97-
// attribute.
98-
counter.add(
99-
10,
100-
&[
101-
KeyValue::new("mykey1", "myvalue1"),
102-
KeyValue::new("mykey2", "myvalue2"),
103-
KeyValue::new("mykey3", "myvalue3"),
104-
KeyValue::new("mykey4", "myvalue4"),
105-
],
106-
);
107-
108-
// Example 3 - Change Aggregation configuration using View.
109-
// Histograms are by default aggregated using ExplicitBucketHistogram
110-
// with default buckets. The configured view will change the aggregation to
111-
// use a custom set of boundaries, and min/max values will not be recorded.
75+
// Example 2 - Change cardinality using View.
11276
let histogram2 = meter
11377
.f64_histogram("my_second_histogram")
11478
.with_unit("ms")
11579
.with_description("My histogram example description")
11680
.build();
11781

11882
// Record measurements using the histogram instrument.
119-
// The values recorded are in the range of 1.2 to 1.5, warranting
120-
// the change of boundaries.
121-
histogram2.record(
122-
1.5,
123-
&[
124-
KeyValue::new("mykey1", "myvalue1"),
125-
KeyValue::new("mykey2", "myvalue2"),
126-
KeyValue::new("mykey3", "myvalue3"),
127-
KeyValue::new("mykey4", "myvalue4"),
128-
],
129-
);
83+
// This metric will have a cardinality limit of 2,
84+
// as set in the view. Because of this, only the first two
85+
// measurements will be recorded, and the rest will be folded
86+
// into the overflow attribute.
87+
histogram2.record(1.5, &[KeyValue::new("mykey1", "v1")]);
13088

131-
histogram2.record(
132-
1.2,
133-
&[
134-
KeyValue::new("mykey1", "myvalue1"),
135-
KeyValue::new("mykey2", "myvalue2"),
136-
KeyValue::new("mykey3", "myvalue3"),
137-
KeyValue::new("mykey4", "myvalue4"),
138-
],
139-
);
89+
histogram2.record(1.2, &[KeyValue::new("mykey1", "v2")]);
14090

141-
histogram2.record(
142-
1.23,
143-
&[
144-
KeyValue::new("mykey1", "myvalue1"),
145-
KeyValue::new("mykey2", "myvalue2"),
146-
KeyValue::new("mykey3", "myvalue3"),
147-
KeyValue::new("mykey4", "myvalue4"),
148-
],
149-
);
91+
histogram2.record(1.23, &[KeyValue::new("mykey1", "v3")]);
92+
93+
histogram2.record(1.4, &[KeyValue::new("mykey1", "v4")]);
94+
95+
histogram2.record(1.6, &[KeyValue::new("mykey1", "v5")]);
96+
97+
histogram2.record(1.7, &[KeyValue::new("mykey1", "v6")]);
98+
99+
histogram2.record(1.8, &[KeyValue::new("mykey1", "v7")]);
150100

151101
// Metrics are exported by default every 30 seconds when using stdout exporter,
152102
// however shutting down the MeterProvider here instantly flushes

opentelemetry-sdk/CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,13 @@ also modified to suppress telemetry before invoking exporters.
4343
[#2928](https://github.com/open-telemetry/opentelemetry-rust/pull/2928)
4444

4545
- TODO: Placeholder for View related changelog. Polish this after all
46-
changes are done.
47-
Hide public fields from `Stream` struct.
46+
- The `Stream` struct now has its public fields hidden.
47+
- Core view functionality is now available by default—users can change the
48+
name, unit, description, and cardinality limit of a metric via views without
49+
enabling the `spec_unstable_metrics_views` feature flag. Advanced view
50+
features, such as custom aggregation or attribute filtering, still require
51+
the `spec_unstable_metrics_views` feature.
52+
- TODO: Add Stream::builder() pattern change, validation when done.
4853

4954
- *Breaking* `Aggregation` enum moved behind feature flag
5055
"spec_unstable_metrics_views". This was only required when using Views.

opentelemetry-sdk/src/metrics/aggregation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ impl Aggregation {
150150

151151
#[cfg(test)]
152152
mod tests {
153+
use super::Aggregation;
153154
use crate::metrics::error::{MetricError, MetricResult};
154155
use crate::metrics::internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
155-
use crate::metrics::Aggregation;
156156

157157
#[test]
158158
fn validate_aggregation() {

opentelemetry-sdk/src/metrics/instrument.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ impl InstrumentKind {
8383
/// ```
8484
#[derive(Clone, Default, Debug, PartialEq)]
8585
#[non_exhaustive]
86-
#[allow(unreachable_pub)]
8786
pub struct Instrument {
8887
/// The human-readable identifier of the instrument.
8988
pub name: Cow<'static, str>,
@@ -208,7 +207,6 @@ pub struct Stream {
208207
pub(crate) cardinality_limit: Option<usize>,
209208
}
210209

211-
#[cfg(feature = "spec_unstable_metrics_views")]
212210
impl Stream {
213211
/// Create a new stream with empty values.
214212
pub fn new() -> Self {
@@ -233,12 +231,14 @@ impl Stream {
233231
self
234232
}
235233

234+
#[cfg(feature = "spec_unstable_metrics_views")]
236235
/// Set the stream aggregation.
237236
pub fn aggregation(mut self, aggregation: Aggregation) -> Self {
238237
self.aggregation = Some(aggregation);
239238
self
240239
}
241240

241+
#[cfg(feature = "spec_unstable_metrics_views")]
242242
/// Set the stream allowed attribute keys.
243243
///
244244
/// Any attribute recorded for the stream with a key not in this set will be

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,6 @@ impl MeterProviderBuilder {
287287
self
288288
}
289289

290-
#[cfg(feature = "spec_unstable_metrics_views")]
291290
/// Associates a [View] with a [MeterProvider].
292291
///
293292
/// [View]s are appended to existing ones in a [MeterProvider] if this option is

0 commit comments

Comments
 (0)