Skip to content

Commit bb55188

Browse files
committed
refactor: update MetricsError to be MetricError
this is consistent with LogError and TraceError
1 parent 46c6f62 commit bb55188

File tree

19 files changed

+88
-88
lines changed

19 files changed

+88
-88
lines changed

CONTRIBUTING.md

+1-1
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

+2-2
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, TracerProvider as _},
66
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use once_cell::sync::Lazy;
22
use opentelemetry::global;
33
use opentelemetry::logs::LogError;
4-
use opentelemetry::metrics::MetricsError;
4+
use opentelemetry::metrics::MetricError;
55
use opentelemetry::trace::{TraceError, TracerProvider};
66
use opentelemetry::{
77
trace::{TraceContextExt, Tracer},
@@ -36,7 +36,7 @@ fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
3636
.build())
3737
}
3838

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

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

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

+3-3
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

+1-1
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

+3-3
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

+3-3
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

+6-6
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

+2-2
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

+2-2
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

+12-12
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,
@@ -468,24 +468,24 @@ fn validate_instrument_config(name: &str, unit: &Option<Cow<'static, str>>) -> M
468468

469469
fn validate_instrument_name(name: &str) -> MetricResult<()> {
470470
if name.is_empty() {
471-
return Err(MetricsError::InvalidInstrumentConfiguration(
471+
return Err(MetricError::InvalidInstrumentConfiguration(
472472
INSTRUMENT_NAME_EMPTY,
473473
));
474474
}
475475
if name.len() > INSTRUMENT_NAME_MAX_LENGTH {
476-
return Err(MetricsError::InvalidInstrumentConfiguration(
476+
return Err(MetricError::InvalidInstrumentConfiguration(
477477
INSTRUMENT_NAME_LENGTH,
478478
));
479479
}
480480
if name.starts_with(|c: char| !c.is_ascii_alphabetic()) {
481-
return Err(MetricsError::InvalidInstrumentConfiguration(
481+
return Err(MetricError::InvalidInstrumentConfiguration(
482482
INSTRUMENT_NAME_FIRST_ALPHABETIC,
483483
));
484484
}
485485
if name.contains(|c: char| {
486486
!c.is_ascii_alphanumeric() && !INSTRUMENT_NAME_ALLOWED_NON_ALPHANUMERIC_CHARS.contains(&c)
487487
}) {
488-
return Err(MetricsError::InvalidInstrumentConfiguration(
488+
return Err(MetricError::InvalidInstrumentConfiguration(
489489
INSTRUMENT_NAME_INVALID_CHAR,
490490
));
491491
}
@@ -495,12 +495,12 @@ fn validate_instrument_name(name: &str) -> MetricResult<()> {
495495
fn validate_instrument_unit(unit: &Option<Cow<'static, str>>) -> MetricResult<()> {
496496
if let Some(unit) = unit {
497497
if unit.len() > INSTRUMENT_UNIT_NAME_MAX_LENGTH {
498-
return Err(MetricsError::InvalidInstrumentConfiguration(
498+
return Err(MetricError::InvalidInstrumentConfiguration(
499499
INSTRUMENT_UNIT_LENGTH,
500500
));
501501
}
502502
if unit.contains(|c: char| !c.is_ascii()) {
503-
return Err(MetricsError::InvalidInstrumentConfiguration(
503+
return Err(MetricError::InvalidInstrumentConfiguration(
504504
INSTRUMENT_UNIT_INVALID_CHAR,
505505
));
506506
}
@@ -567,7 +567,7 @@ where
567567
mod tests {
568568
use std::borrow::Cow;
569569

570-
use opentelemetry::metrics::MetricsError;
570+
use opentelemetry::metrics::MetricError;
571571

572572
use super::{
573573
validate_instrument_name, validate_instrument_unit, INSTRUMENT_NAME_FIRST_ALPHABETIC,
@@ -590,13 +590,13 @@ mod tests {
590590
("allow.dots.ok", ""),
591591
];
592592
for (name, expected_error) in instrument_name_test_cases {
593-
let assert = |result: Result<_, MetricsError>| {
593+
let assert = |result: Result<_, MetricError>| {
594594
if expected_error.is_empty() {
595595
assert!(result.is_ok());
596596
} else {
597597
assert!(matches!(
598598
result.unwrap_err(),
599-
MetricsError::InvalidInstrumentConfiguration(msg) if msg == expected_error
599+
MetricError::InvalidInstrumentConfiguration(msg) if msg == expected_error
600600
));
601601
}
602602
};
@@ -621,13 +621,13 @@ mod tests {
621621
];
622622

623623
for (unit, expected_error) in instrument_unit_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
};

opentelemetry-sdk/src/metrics/meter_provider.rs

+2-2
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, MetricError, MetricResult},
1212
otel_debug, otel_error, KeyValue,
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)