Skip to content

Commit 2c0e00f

Browse files
committed
refactor: update MetricsError to be MetricError
this is consistent with LogError and TraceError
1 parent bd55114 commit 2c0e00f

File tree

19 files changed

+85
-85
lines changed

19 files changed

+85
-85
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ For a deeper discussion, see:
140140

141141
Currently, the Opentelemetry Rust SDK has two ways to handle errors. In the situation where errors are not allowed to return. One should call global error handler to process the errors. Otherwise, one should return the errors.
142142

143-
The Opentelemetry Rust SDK comes with an error type `opentelemetry::Error`. For different function, one error has been defined. All error returned by trace module MUST be wrapped in `opentelemetry::trace::TraceError`. All errors returned by metrics module MUST be wrapped in `opentelemetry::metrics::MetricsError`. All errors returned by logs module MUST be wrapped in `opentelemetry::logs::LogsError`.
143+
The Opentelemetry Rust SDK comes with an error type `opentelemetry::Error`. For different function, one error has been defined. All error returned by trace module MUST be wrapped in `opentelemetry::trace::TraceError`. All errors returned by metrics module MUST be wrapped in `opentelemetry::metrics::MetricError`. All errors returned by logs module MUST be wrapped in `opentelemetry::logs::LogsError`.
144144

145145
For users that want to implement their own exporters. It's RECOMMENDED to wrap all errors from the exporter into a crate-level error type, and implement `ExporterError` trait.
146146

opentelemetry-otlp/examples/basic-otlp-http/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use once_cell::sync::Lazy;
22
use opentelemetry::{
33
global,
4-
metrics::MetricsError,
4+
metrics::MetricError,
55
trace::{TraceContextExt, TraceError, Tracer},
66
InstrumentationScope, KeyValue,
77
};
@@ -65,7 +65,7 @@ fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
6565
.build())
6666
}
6767

68-
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
68+
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricError> {
6969
let exporter = MetricsExporter::builder()
7070
.with_http()
7171
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format

opentelemetry-otlp/examples/basic-otlp/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use once_cell::sync::Lazy;
22
use opentelemetry::logs::LogError;
3-
use opentelemetry::metrics::MetricsError;
3+
use opentelemetry::metrics::MetricError;
44
use opentelemetry::trace::{TraceContextExt, TraceError, Tracer};
55
use opentelemetry::KeyValue;
66
use opentelemetry::{global, InstrumentationScope};
@@ -33,7 +33,7 @@ fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
3333
.build())
3434
}
3535

36-
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
36+
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricError> {
3737
let exporter = MetricsExporter::builder().with_tonic().build()?;
3838

3939
let reader = PeriodicReader::builder(exporter, runtime::Tokio).build();

opentelemetry-otlp/src/exporter/http/metrics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::sync::Arc;
22

33
use async_trait::async_trait;
44
use http::{header::CONTENT_TYPE, Method};
5-
use opentelemetry::metrics::{MetricResult, MetricsError};
5+
use opentelemetry::metrics::{MetricError, MetricResult};
66
use opentelemetry_sdk::metrics::data::ResourceMetrics;
77

88
use crate::{metric::MetricsClient, Error};
@@ -18,7 +18,7 @@ impl MetricsClient for OtlpHttpClient {
1818
.map_err(Into::into)
1919
.and_then(|g| match &*g {
2020
Some(client) => Ok(Arc::clone(client)),
21-
_ => Err(MetricsError::Other("exporter is already shut down".into())),
21+
_ => Err(MetricError::Other("exporter is already shut down".into())),
2222
})?;
2323

2424
let (body, content_type) = self.build_metrics_export_body(metrics)?;
@@ -36,7 +36,7 @@ impl MetricsClient for OtlpHttpClient {
3636
client
3737
.send(request)
3838
.await
39-
.map_err(|e| MetricsError::ExportErr(Box::new(Error::RequestFailed(e))))?;
39+
.map_err(|e| MetricError::ExportErr(Box::new(Error::RequestFailed(e))))?;
4040

4141
Ok(())
4242
}

opentelemetry-otlp/src/exporter/http/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ impl OtlpHttpClient {
320320
#[cfg(feature = "http-json")]
321321
Protocol::HttpJson => match serde_json::to_string_pretty(&req) {
322322
Ok(json) => Ok((json.into(), "application/json")),
323-
Err(e) => Err(opentelemetry::metrics::MetricsError::Other(e.to_string())),
323+
Err(e) => Err(opentelemetry::metrics::MetricError::Other(e.to_string())),
324324
},
325325
_ => Ok((req.encode_to_vec(), "application/x-protobuf")),
326326
}

opentelemetry-otlp/src/exporter/tonic/metrics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::fmt;
22
use std::sync::Mutex;
33

44
use async_trait::async_trait;
5-
use opentelemetry::metrics::{MetricResult, MetricsError};
5+
use opentelemetry::metrics::{MetricError, MetricResult};
66
use opentelemetry_proto::tonic::collector::metrics::v1::{
77
metrics_service_client::MetricsServiceClient, ExportMetricsServiceRequest,
88
};
@@ -62,14 +62,14 @@ impl MetricsClient for TonicMetricsClient {
6262
.interceptor
6363
.call(Request::new(()))
6464
.map_err(|e| {
65-
MetricsError::Other(format!(
65+
MetricError::Other(format!(
6666
"unexpected status while exporting {e:?}"
6767
))
6868
})?
6969
.into_parts();
7070
Ok((inner.client.clone(), m, e))
7171
}
72-
None => Err(MetricsError::Other("exporter is already shut down".into())),
72+
None => Err(MetricError::Other("exporter is already shut down".into())),
7373
})?;
7474

7575
client

opentelemetry-proto/src/transform/metrics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod tonic {
88
use std::any::Any;
99
use std::fmt;
1010

11-
use opentelemetry::{global, metrics::MetricsError, Key, Value};
11+
use opentelemetry::{global, metrics::MetricError, Key, Value};
1212
use opentelemetry_sdk::metrics::data::{
1313
self, Exemplar as SdkExemplar, ExponentialHistogram as SdkExponentialHistogram,
1414
Gauge as SdkGauge, Histogram as SdkHistogram, Metric as SdkMetric,
@@ -97,7 +97,7 @@ pub mod tonic {
9797
Temporality::Cumulative => AggregationTemporality::Cumulative,
9898
Temporality::Delta => AggregationTemporality::Delta,
9999
other => {
100-
opentelemetry::global::handle_error(MetricsError::Other(format!(
100+
opentelemetry::global::handle_error(MetricError::Other(format!(
101101
"Unknown temporality {:?}, using default instead.",
102102
other
103103
)));
@@ -184,7 +184,7 @@ pub mod tonic {
184184
} else if let Some(gauge) = data.downcast_ref::<SdkGauge<f64>>() {
185185
Ok(TonicMetricData::Gauge(gauge.into()))
186186
} else {
187-
global::handle_error(MetricsError::Other("unknown aggregator".into()));
187+
global::handle_error(MetricError::Other("unknown aggregator".into()));
188188
Err(())
189189
}
190190
}

opentelemetry-sdk/src/metrics/aggregation.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fmt;
22

33
use crate::metrics::internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
4-
use opentelemetry::metrics::{MetricResult, MetricsError};
4+
use opentelemetry::metrics::{MetricError, MetricResult};
55

66
/// The way recorded measurements are summarized.
77
#[derive(Clone, Debug, PartialEq)]
@@ -118,7 +118,7 @@ impl Aggregation {
118118
Aggregation::ExplicitBucketHistogram { boundaries, .. } => {
119119
for x in boundaries.windows(2) {
120120
if x[0] >= x[1] {
121-
return Err(MetricsError::Config(format!(
121+
return Err(MetricError::Config(format!(
122122
"aggregation: explicit bucket histogram: non-monotonic boundaries: {:?}",
123123
boundaries,
124124
)));
@@ -129,13 +129,13 @@ impl Aggregation {
129129
}
130130
Aggregation::Base2ExponentialHistogram { max_scale, .. } => {
131131
if *max_scale > EXPO_MAX_SCALE {
132-
return Err(MetricsError::Config(format!(
132+
return Err(MetricError::Config(format!(
133133
"aggregation: exponential histogram: max scale ({}) is greater than 20",
134134
max_scale,
135135
)));
136136
}
137137
if *max_scale < EXPO_MIN_SCALE {
138-
return Err(MetricsError::Config(format!(
138+
return Err(MetricError::Config(format!(
139139
"aggregation: exponential histogram: max scale ({}) is less than -10",
140140
max_scale,
141141
)));
@@ -153,7 +153,7 @@ mod tests {
153153
internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE},
154154
Aggregation,
155155
};
156-
use opentelemetry::metrics::{MetricResult, MetricsError};
156+
use opentelemetry::metrics::{MetricError, MetricResult};
157157

158158
#[test]
159159
fn validate_aggregation() {
@@ -163,7 +163,7 @@ mod tests {
163163
check: Box<dyn Fn(MetricResult<()>) -> bool>,
164164
}
165165
let ok = Box::new(|result: MetricResult<()>| result.is_ok());
166-
let config_error = Box::new(|result| matches!(result, Err(MetricsError::Config(_))));
166+
let config_error = Box::new(|result| matches!(result, Err(MetricError::Config(_))));
167167

168168
let test_cases: Vec<TestCase> = vec![
169169
TestCase {

opentelemetry-sdk/src/metrics/internal/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use aggregate::is_under_cardinality_limit;
1616
pub(crate) use aggregate::{AggregateBuilder, ComputeAggregation, Measure};
1717
pub(crate) use exponential_histogram::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
1818
use once_cell::sync::Lazy;
19-
use opentelemetry::metrics::MetricsError;
19+
use opentelemetry::metrics::MetricError;
2020
use opentelemetry::{global, otel_warn, KeyValue};
2121

2222
use crate::metrics::AttributeSet;
@@ -146,7 +146,7 @@ impl<AU: AtomicallyUpdate<T>, T: Number, O: Operation> ValueMap<AU, T, O> {
146146
let new_tracker = AU::new_atomic_tracker(self.buckets_count);
147147
O::update_tracker(&new_tracker, measurement, index);
148148
trackers.insert(STREAM_OVERFLOW_ATTRIBUTES.clone(), Arc::new(new_tracker));
149-
global::handle_error(MetricsError::Other("Warning: Maximum data points for metric stream exceeded. Entry added to overflow. Subsequent overflows to same metric until next collect will not be logged.".into()));
149+
global::handle_error(MetricError::Other("Warning: Maximum data points for metric stream exceeded. Entry added to overflow. Subsequent overflows to same metric until next collect will not be logged.".into()));
150150
otel_warn!( name: "ValueMap.measure",
151151
message = "Maximum data points for metric stream exceeded. Entry added to overflow. Subsequent overflows to same metric until next collect will not be logged."
152152
);

opentelemetry-sdk/src/metrics/manual_reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
};
55

66
use opentelemetry::{
7-
metrics::{MetricResult, MetricsError},
7+
metrics::{MetricError, MetricResult},
88
otel_debug,
99
};
1010

@@ -93,7 +93,7 @@ impl MetricReader for ManualReader {
9393
match &inner.sdk_producer.as_ref().and_then(|w| w.upgrade()) {
9494
Some(producer) => producer.produce(rm)?,
9595
None => {
96-
return Err(MetricsError::Other(
96+
return Err(MetricError::Other(
9797
"reader is shut down or not registered".into(),
9898
))
9999
}

opentelemetry-sdk/src/metrics/meter.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use opentelemetry::{
55
global,
66
metrics::{
77
AsyncInstrumentBuilder, Counter, Gauge, Histogram, HistogramBuilder, InstrumentBuilder,
8-
InstrumentProvider, MetricResult, MetricsError, ObservableCounter, ObservableGauge,
8+
InstrumentProvider, MetricError, MetricResult, ObservableCounter, ObservableGauge,
99
ObservableUpDownCounter, UpDownCounter,
1010
},
1111
otel_error, InstrumentationScope,
@@ -499,24 +499,24 @@ fn validate_instrument_config(name: &str, unit: &Option<Cow<'static, str>>) -> M
499499

500500
fn validate_instrument_name(name: &str) -> MetricResult<()> {
501501
if name.is_empty() {
502-
return Err(MetricsError::InvalidInstrumentConfiguration(
502+
return Err(MetricError::InvalidInstrumentConfiguration(
503503
INSTRUMENT_NAME_EMPTY,
504504
));
505505
}
506506
if name.len() > INSTRUMENT_NAME_MAX_LENGTH {
507-
return Err(MetricsError::InvalidInstrumentConfiguration(
507+
return Err(MetricError::InvalidInstrumentConfiguration(
508508
INSTRUMENT_NAME_LENGTH,
509509
));
510510
}
511511
if name.starts_with(|c: char| !c.is_ascii_alphabetic()) {
512-
return Err(MetricsError::InvalidInstrumentConfiguration(
512+
return Err(MetricError::InvalidInstrumentConfiguration(
513513
INSTRUMENT_NAME_FIRST_ALPHABETIC,
514514
));
515515
}
516516
if name.contains(|c: char| {
517517
!c.is_ascii_alphanumeric() && !INSTRUMENT_NAME_ALLOWED_NON_ALPHANUMERIC_CHARS.contains(&c)
518518
}) {
519-
return Err(MetricsError::InvalidInstrumentConfiguration(
519+
return Err(MetricError::InvalidInstrumentConfiguration(
520520
INSTRUMENT_NAME_INVALID_CHAR,
521521
));
522522
}
@@ -526,12 +526,12 @@ fn validate_instrument_name(name: &str) -> MetricResult<()> {
526526
fn validate_instrument_unit(unit: &Option<Cow<'static, str>>) -> MetricResult<()> {
527527
if let Some(unit) = unit {
528528
if unit.len() > INSTRUMENT_UNIT_NAME_MAX_LENGTH {
529-
return Err(MetricsError::InvalidInstrumentConfiguration(
529+
return Err(MetricError::InvalidInstrumentConfiguration(
530530
INSTRUMENT_UNIT_LENGTH,
531531
));
532532
}
533533
if unit.contains(|c: char| !c.is_ascii()) {
534-
return Err(MetricsError::InvalidInstrumentConfiguration(
534+
return Err(MetricError::InvalidInstrumentConfiguration(
535535
INSTRUMENT_UNIT_INVALID_CHAR,
536536
));
537537
}
@@ -598,7 +598,7 @@ where
598598
mod tests {
599599
use std::borrow::Cow;
600600

601-
use opentelemetry::metrics::MetricsError;
601+
use opentelemetry::metrics::MetricError;
602602

603603
use super::{
604604
validate_instrument_name, validate_instrument_unit, INSTRUMENT_NAME_FIRST_ALPHABETIC,
@@ -621,13 +621,13 @@ mod tests {
621621
("allow.dots.ok", ""),
622622
];
623623
for (name, expected_error) in instrument_name_test_cases {
624-
let assert = |result: Result<_, MetricsError>| {
624+
let assert = |result: Result<_, MetricError>| {
625625
if expected_error.is_empty() {
626626
assert!(result.is_ok());
627627
} else {
628628
assert!(matches!(
629629
result.unwrap_err(),
630-
MetricsError::InvalidInstrumentConfiguration(msg) if msg == expected_error
630+
MetricError::InvalidInstrumentConfiguration(msg) if msg == expected_error
631631
));
632632
}
633633
};
@@ -652,13 +652,13 @@ mod tests {
652652
];
653653

654654
for (unit, expected_error) in instrument_unit_test_cases {
655-
let assert = |result: Result<_, MetricsError>| {
655+
let assert = |result: Result<_, MetricError>| {
656656
if expected_error.is_empty() {
657657
assert!(result.is_ok());
658658
} else {
659659
assert!(matches!(
660660
result.unwrap_err(),
661-
MetricsError::InvalidInstrumentConfiguration(msg) if msg == expected_error
661+
MetricError::InvalidInstrumentConfiguration(msg) if msg == expected_error
662662
));
663663
}
664664
};

opentelemetry-sdk/src/metrics/meter_provider.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
};
99

1010
use opentelemetry::{
11-
metrics::{Meter, MeterProvider, MetricResult, MetricsError},
11+
metrics::{Meter, MeterProvider, MetricResult, MetricError},
1212
otel_debug, otel_error, InstrumentationScope,
1313
};
1414

@@ -125,7 +125,7 @@ impl SdkMeterProviderInner {
125125
{
126126
self.pipes.shutdown()
127127
} else {
128-
Err(MetricsError::Other(
128+
Err(MetricError::Other(
129129
"metrics provider already shut down".into(),
130130
))
131131
}

0 commit comments

Comments
 (0)