opentelemetry_sdk/metrics/
error.rs

1use std::result;
2use std::sync::PoisonError;
3use thiserror::Error;
4
5use crate::ExportError;
6
7/// A specialized `Result` type for metric operations.
8pub type MetricResult<T> = result::Result<T, MetricError>;
9
10/// Errors returned by the metrics API.
11#[derive(Error, Debug)]
12#[non_exhaustive]
13pub enum MetricError {
14    /// Other errors not covered by specific cases.
15    #[error("Metrics error: {0}")]
16    Other(String),
17    /// Invalid configuration
18    #[error("Config error {0}")]
19    Config(String),
20    /// Fail to export metrics
21    #[error("Metrics exporter {0} failed with {name}", name = .0.exporter_name())]
22    ExportErr(Box<dyn ExportError>),
23    /// Invalid instrument configuration such invalid instrument name, invalid instrument description, invalid instrument unit, etc.
24    /// See [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#general-characteristics)
25    /// for full list of requirements.
26    #[error("Invalid instrument configuration: {0}")]
27    InvalidInstrumentConfiguration(&'static str),
28}
29
30impl<T: ExportError> From<T> for MetricError {
31    fn from(err: T) -> Self {
32        MetricError::ExportErr(Box::new(err))
33    }
34}
35
36impl<T> From<PoisonError<T>> for MetricError {
37    fn from(err: PoisonError<T>) -> Self {
38        MetricError::Other(err.to_string())
39    }
40}