Skip to content

Commit d069d88

Browse files
committed
feat: Cleanup Views
1 parent f0636b5 commit d069d88

File tree

10 files changed

+31
-305
lines changed

10 files changed

+31
-305
lines changed

examples/metrics-advanced/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::error::Error;
77
fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
88
// for example 1
99
let my_view_rename_and_unit = |i: &Instrument| {
10-
if i.name == "my_histogram" {
10+
if i.name() == "my_histogram" {
1111
Some(
1212
Stream::builder()
1313
.with_name("my_histogram_renamed")
@@ -22,7 +22,7 @@ fn init_meter_provider() -> opentelemetry_sdk::metrics::SdkMeterProvider {
2222

2323
// for example 2
2424
let my_view_change_cardinality = |i: &Instrument| {
25-
if i.name == "my_second_histogram" {
25+
if i.name() == "my_second_histogram" {
2626
// Note: If Stream is invalid, build() will return an error. By
2727
// calling `.ok()`, any such error is ignored and treated as if the
2828
// view does not match the instrument. If this is not the desired

opentelemetry-sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ also modified to suppress telemetry before invoking exporters.
5353
- Added `Stream::builder()` method that returns a new `StreamBuilder`
5454
- `StreamBuilder::build()` returns `Result<Stream, Box<dyn Error>>` enabling
5555
proper validation
56+
- Removed `new_view()` on `View`. Views can be instead added by passing anything
57+
that implements `View` trait to `with_view` method on `MeterProviderBuilder`.
58+
`View` is implemented for `Fn(&Instrument) -> Option<Stream>`, so this can be
59+
used to add views.
5660

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

opentelemetry-sdk/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ futures-executor = { workspace = true }
1818
futures-util = { workspace = true, features = ["std", "sink", "async-await-macro"] }
1919
percent-encoding = { workspace = true, optional = true }
2020
rand = { workspace = true, features = ["std", "std_rng", "small_rng", "os_rng", "thread_rng"], optional = true }
21-
glob = { workspace = true, optional = true }
2221
serde = { workspace = true, features = ["derive", "rc"], optional = true }
2322
serde_json = { workspace = true, optional = true }
2423
thiserror = { workspace = true }
@@ -45,7 +44,7 @@ trace = ["opentelemetry/trace", "rand", "percent-encoding"]
4544
jaeger_remote_sampler = ["trace", "opentelemetry-http", "http", "serde", "serde_json", "url", "experimental_async_runtime"]
4645
logs = ["opentelemetry/logs", "serde_json"]
4746
spec_unstable_logs_enabled = ["logs", "opentelemetry/spec_unstable_logs_enabled"]
48-
metrics = ["opentelemetry/metrics", "glob"]
47+
metrics = ["opentelemetry/metrics"]
4948
testing = ["opentelemetry/testing", "trace", "metrics", "logs", "rt-tokio", "rt-tokio-current-thread", "tokio/macros", "tokio/rt-multi-thread"]
5049
experimental_async_runtime = []
5150
rt-tokio = ["tokio", "tokio-stream", "experimental_async_runtime"]

opentelemetry-sdk/src/metrics/error.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,8 @@ use std::sync::PoisonError;
33
use thiserror::Error;
44

55
/// A specialized `Result` type for metric operations.
6-
#[cfg(feature = "spec_unstable_metrics_views")]
7-
pub type MetricResult<T> = result::Result<T, MetricError>;
8-
#[cfg(not(feature = "spec_unstable_metrics_views"))]
96
pub(crate) type MetricResult<T> = result::Result<T, MetricError>;
107

11-
/// Errors returned by the metrics API.
12-
#[cfg(feature = "spec_unstable_metrics_views")]
13-
#[derive(Error, Debug)]
14-
#[non_exhaustive]
15-
pub enum MetricError {
16-
/// Other errors not covered by specific cases.
17-
#[error("Metrics error: {0}")]
18-
Other(String),
19-
/// Invalid configuration
20-
#[error("Config error {0}")]
21-
Config(String),
22-
/// Invalid instrument configuration such invalid instrument name, invalid instrument description, invalid instrument unit, etc.
23-
/// See [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#general-characteristics)
24-
/// for full list of requirements.
25-
#[error("Invalid instrument configuration: {0}")]
26-
InvalidInstrumentConfiguration(&'static str),
27-
}
28-
29-
#[cfg(not(feature = "spec_unstable_metrics_views"))]
308
#[derive(Error, Debug)]
319
pub(crate) enum MetricError {
3210
/// Other errors not covered by specific cases.

opentelemetry-sdk/src/metrics/instrument.rs

Lines changed: 18 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -84,91 +84,39 @@ impl InstrumentKind {
8484
/// let view = new_view(criteria, mask);
8585
/// # drop(view);
8686
/// ```
87-
#[derive(Clone, Default, Debug, PartialEq)]
88-
#[non_exhaustive]
87+
#[derive(Clone, Debug, PartialEq)]
8988
pub struct Instrument {
9089
/// The human-readable identifier of the instrument.
91-
pub name: Cow<'static, str>,
90+
pub(crate) name: Cow<'static, str>,
9291
/// describes the purpose of the instrument.
93-
pub description: Cow<'static, str>,
92+
pub(crate) description: Cow<'static, str>,
9493
/// The functional group of the instrument.
95-
pub kind: Option<InstrumentKind>,
94+
pub(crate) kind: InstrumentKind,
9695
/// Unit is the unit of measurement recorded by the instrument.
97-
pub unit: Cow<'static, str>,
96+
pub(crate) unit: Cow<'static, str>,
9897
/// The instrumentation that created the instrument.
99-
pub scope: InstrumentationScope,
98+
pub(crate) scope: InstrumentationScope,
10099
}
101100

102-
#[cfg(feature = "spec_unstable_metrics_views")]
103101
impl Instrument {
104-
/// Create a new instrument with default values
105-
pub fn new() -> Self {
106-
Instrument::default()
107-
}
108-
109-
/// Set the instrument name.
110-
pub fn name(mut self, name: impl Into<Cow<'static, str>>) -> Self {
111-
self.name = name.into();
112-
self
113-
}
114-
115-
/// Set the instrument description.
116-
pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
117-
self.description = description.into();
118-
self
119-
}
120-
121-
/// Set the instrument unit.
122-
pub fn unit(mut self, unit: impl Into<Cow<'static, str>>) -> Self {
123-
self.unit = unit.into();
124-
self
125-
}
126-
127-
/// Set the instrument scope.
128-
pub fn scope(mut self, scope: InstrumentationScope) -> Self {
129-
self.scope = scope;
130-
self
131-
}
132-
133-
/// empty returns if all fields of i are their default-value.
134-
pub(crate) fn is_empty(&self) -> bool {
135-
self.name.is_empty()
136-
&& self.description.is_empty()
137-
&& self.kind.is_none()
138-
&& self.unit.is_empty()
139-
&& self.scope == InstrumentationScope::default()
140-
}
141-
142-
pub(crate) fn matches(&self, other: &Instrument) -> bool {
143-
self.matches_name(other)
144-
&& self.matches_description(other)
145-
&& self.matches_kind(other)
146-
&& self.matches_unit(other)
147-
&& self.matches_scope(other)
148-
}
149-
150-
pub(crate) fn matches_name(&self, other: &Instrument) -> bool {
151-
self.name.is_empty() || self.name.as_ref() == other.name.as_ref()
152-
}
153-
154-
pub(crate) fn matches_description(&self, other: &Instrument) -> bool {
155-
self.description.is_empty() || self.description.as_ref() == other.description.as_ref()
102+
/// Instrument name.
103+
pub fn name(&self) -> &str {
104+
self.name.as_ref()
156105
}
157106

158-
pub(crate) fn matches_kind(&self, other: &Instrument) -> bool {
159-
self.kind.is_none() || self.kind == other.kind
107+
/// Instrument kind.
108+
pub fn kind(&self) -> InstrumentKind {
109+
self.kind
160110
}
161111

162-
pub(crate) fn matches_unit(&self, other: &Instrument) -> bool {
163-
self.unit.is_empty() || self.unit.as_ref() == other.unit.as_ref()
112+
/// Instrument unit.
113+
pub fn unit(&self) -> &str {
114+
self.unit.as_ref()
164115
}
165116

166-
pub(crate) fn matches_scope(&self, other: &Instrument) -> bool {
167-
(self.scope.name().is_empty() || self.scope.name() == other.scope.name())
168-
&& (self.scope.version().is_none()
169-
|| self.scope.version().as_ref() == other.scope.version().as_ref())
170-
&& (self.scope.schema_url().is_none()
171-
|| self.scope.schema_url().as_ref() == other.scope.schema_url().as_ref())
117+
/// Instrument scope.
118+
pub fn scope(&self) -> &InstrumentationScope {
119+
&self.scope
172120
}
173121
}
174122

@@ -188,7 +136,6 @@ impl Instrument {
188136
/// .unwrap();
189137
/// ```
190138
#[derive(Default, Debug)]
191-
#[non_exhaustive]
192139
pub struct StreamBuilder {
193140
name: Option<Cow<'static, str>>,
194141
description: Option<Cow<'static, str>>,
@@ -331,8 +278,6 @@ fn validate_bucket_boundaries(boundaries: &[f64]) -> Result<(), String> {
331278
/// # drop(view);
332279
/// ```
333280
#[derive(Default, Debug)]
334-
#[non_exhaustive]
335-
#[allow(unreachable_pub)]
336281
pub struct Stream {
337282
/// The human-readable identifier of the stream.
338283
pub(crate) name: Option<Cow<'static, str>>,

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ where
683683
name,
684684
description: description.unwrap_or_default(),
685685
unit: unit.unwrap_or_default(),
686-
kind: Some(kind),
686+
kind: kind,
687687
scope: self.meter.scope.clone(),
688688
};
689689

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl MeterProviderBuilder {
308308
/// ```
309309
/// # use opentelemetry_sdk::metrics::{Stream, Instrument};
310310
/// let view_rename = |i: &Instrument| {
311-
/// if i.name == "my_counter" {
311+
/// if i.name() == "my_counter" {
312312
/// Some(Stream::builder().with_name("my_counter_renamed").build().expect("Stream should be valid"))
313313
/// } else {
314314
/// None
@@ -324,7 +324,7 @@ impl MeterProviderBuilder {
324324
/// ```
325325
/// # use opentelemetry_sdk::metrics::{Stream, Instrument};
326326
/// let view_change_cardinality = |i: &Instrument| {
327-
/// if i.name == "my_counter" {
327+
/// if i.name() == "my_counter" {
328328
/// Some(
329329
/// Stream::builder()
330330
/// .with_cardinality_limit(100).build().expect("Stream should be valid"),
@@ -343,7 +343,7 @@ impl MeterProviderBuilder {
343343
/// ```
344344
/// # use opentelemetry_sdk::metrics::{Stream, Instrument};
345345
/// let my_view_change_cardinality = |i: &Instrument| {
346-
/// if i.name == "my_second_histogram" {
346+
/// if i.name() == "my_second_histogram" {
347347
/// // Note: If Stream is invalid, build() will return `Error` variant.
348348
/// // By calling `.ok()`, any such error is ignored and treated as if the view does not match
349349
/// // the instrument.

opentelemetry-sdk/src/metrics/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,7 @@ pub use in_memory_exporter::{InMemoryMetricExporter, InMemoryMetricExporterBuild
7373

7474
#[cfg(feature = "spec_unstable_metrics_views")]
7575
pub use aggregation::*;
76-
#[cfg(feature = "spec_unstable_metrics_views")]
77-
pub use error::{MetricError, MetricResult};
76+
7877
#[cfg(feature = "experimental_metrics_custom_reader")]
7978
pub use manual_reader::*;
8079
pub use meter_provider::*;
@@ -84,9 +83,6 @@ pub use pipeline::Pipeline;
8483

8584
pub use instrument::{Instrument, InstrumentKind, Stream, StreamBuilder};
8685

87-
#[cfg(feature = "spec_unstable_metrics_views")]
88-
pub use view::new_view;
89-
9086
pub use view::View;
9187

9288
use std::hash::Hash;

opentelemetry-sdk/src/metrics/pipeline.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,7 @@ where
259259
let mut matched = false;
260260
let mut measures = vec![];
261261
let mut errs = vec![];
262-
let kind = match inst.kind {
263-
Some(kind) => kind,
264-
None => return Err(MetricError::Other("instrument must have a kind".into())),
265-
};
262+
let kind = inst.kind;
266263

267264
// The cache will return the same Aggregator instance. Use stream ids to de duplicate.
268265
let mut seen = HashSet::new();

0 commit comments

Comments
 (0)