Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions opentelemetry-sdk/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::sync::PoisonError;

#[cfg(feature = "logs")]
use crate::logs::LogError;
#[cfg(feature = "metrics")]
use crate::metrics::MetricError;
use opentelemetry::propagation::PropagationError;
#[cfg(feature = "trace")]
use opentelemetry::trace::TraceError;

/// Wrapper for error from both tracing and metrics part of open telemetry.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though this is moving from just one crate to another, I'd like to first confirm that these are indeed required. I understand that it is desirable to return specialized errors from user-facing APIs, but most of the errors here are only for internal logging only. Is there any additional value provided by this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2241 (comment) for prior discussion.
If we don't plan to keep this, then its best to just remove it, instead of moving from api to sdk and then removing it again.
(Note: These are still public APIs in the sdk crate)

#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[cfg(feature = "trace")]
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
#[error(transparent)]
/// Failed to export traces.
Trace(#[from] TraceError),
#[cfg(feature = "metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
#[error(transparent)]
/// An issue raised by the metrics module.
Metric(#[from] MetricError),

#[cfg(feature = "logs")]
#[cfg_attr(docsrs, doc(cfg(feature = "logs")))]
#[error(transparent)]
/// Failed to export logs.
Log(#[from] LogError),

#[error(transparent)]
/// Error happens when injecting and extracting information using propagators.
Propagation(#[from] PropagationError),

#[error("{0}")]
/// Other types of failures not covered by the variants above.
Other(String),
}

impl<T> From<PoisonError<T>> for Error {
fn from(err: PoisonError<T>) -> Self {
Error::Other(err.to_string())
}
}
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/export/logs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! Log exporters
use crate::logs::LogRecord;
use crate::logs::{LogError, LogResult};
use crate::Resource;
use async_trait::async_trait;
#[cfg(feature = "logs_level_enabled")]
use opentelemetry::logs::Severity;
use opentelemetry::logs::{LogError, LogResult};
use opentelemetry::InstrumentationScope;
use std::fmt::Debug;

Expand Down
6 changes: 5 additions & 1 deletion opentelemetry-sdk/src/export/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ pub mod logs;
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
pub mod trace;

pub use opentelemetry::ExportError;
/// Marker trait for errors returned by exporters
pub trait ExportError: std::error::Error + Send + Sync + 'static {
/// The name of exporter that returned this error
fn exporter_name(&self) -> &'static str;
}
2 changes: 2 additions & 0 deletions opentelemetry-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,5 @@ pub mod util;

#[doc(inline)]
pub use resource::Resource;

pub mod error;
63 changes: 63 additions & 0 deletions opentelemetry-sdk/src/logs/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use crate::export::ExportError;

use std::{sync::PoisonError, time::Duration};
use thiserror::Error;

/// Describe the result of operations in log SDK.
pub type LogResult<T> = Result<T, LogError>;

#[derive(Error, Debug)]
#[non_exhaustive]
/// Errors returned by the log SDK.
pub enum LogError {
/// Export failed with the error returned by the exporter.
#[error("Exporter {} encountered the following errors: {0}", .0.exporter_name())]
ExportFailed(Box<dyn ExportError>),

/// Export failed to finish after certain period and processor stopped the export.
#[error("Exporter timed out after {} seconds", .0.as_secs())]
ExportTimedOut(Duration),

/// Processor is already shutdown
#[error("{0} already shutdown")]
AlreadyShutdown(String),

/// Mutex lock poisoning
#[error("mutex lock poisioning for {0}")]
MutexPoisoned(String),

/// Other errors propagated from log SDK that weren't covered above.
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
}

impl<T> From<T> for LogError
where
T: ExportError,
{
fn from(err: T) -> Self {
LogError::ExportFailed(Box::new(err))
}
}

impl From<String> for LogError {
fn from(err_msg: String) -> Self {
LogError::Other(Box::new(Custom(err_msg)))
}
}

impl From<&'static str> for LogError {
fn from(err_msg: &'static str) -> Self {
LogError::Other(Box::new(Custom(err_msg.into())))
}
}

impl<T> From<PoisonError<T>> for LogError {
fn from(err: PoisonError<T>) -> Self {
LogError::Other(err.to_string().into())
}
}
/// Wrap type for string
#[derive(Error, Debug)]
#[error("{0}")]
struct Custom(String);
8 changes: 2 additions & 6 deletions opentelemetry-sdk/src/logs/log_emitter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use super::{BatchLogProcessor, LogProcessor, LogRecord, SimpleLogProcessor, TraceContext};
use crate::{export::logs::LogExporter, runtime::RuntimeChannel, Resource};
use opentelemetry::{
logs::{LogError, LogResult},
otel_debug,
trace::TraceContextExt,
Context, InstrumentationScope,
};
use crate::{logs::LogError, logs::LogResult};
use opentelemetry::{otel_debug, trace::TraceContextExt, Context, InstrumentationScope};

#[cfg(feature = "logs_level_enabled")]
use opentelemetry::logs::Severity;
Expand Down
7 changes: 2 additions & 5 deletions opentelemetry-sdk/src/logs/log_processor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
export::logs::{ExportResult, LogBatch, LogExporter},
logs::LogRecord,
logs::{LogError, LogRecord, LogResult},
runtime::{RuntimeChannel, TrySend},
Resource,
};
Expand All @@ -11,10 +11,7 @@ use futures_util::{
};
#[cfg(feature = "logs_level_enabled")]
use opentelemetry::logs::Severity;
use opentelemetry::{
logs::{LogError, LogResult},
otel_debug, otel_error, otel_warn, InstrumentationScope,
};
use opentelemetry::{otel_debug, otel_error, otel_warn, InstrumentationScope};

use std::sync::atomic::AtomicBool;
use std::{cmp::min, env, sync::Mutex};
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/src/logs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! # OpenTelemetry Log SDK
mod error;
mod log_emitter;
mod log_processor;
pub(crate) mod record;

pub use error::{LogError, LogResult};
pub use log_emitter::{Builder, Logger, LoggerProvider};
pub use log_processor::{
BatchConfig, BatchConfigBuilder, BatchLogProcessor, BatchLogProcessorBuilder, LogProcessor,
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/metrics/aggregation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use crate::metrics::internal::{EXPO_MAX_SCALE, EXPO_MIN_SCALE};
use opentelemetry::metrics::{MetricError, MetricResult};
use crate::metrics::{MetricError, MetricResult};

/// The way recorded measurements are summarized.
#[derive(Clone, Debug, PartialEq)]
Expand Down
40 changes: 40 additions & 0 deletions opentelemetry-sdk/src/metrics/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::result;
use std::sync::PoisonError;
use thiserror::Error;

use crate::export::ExportError;

/// A specialized `Result` type for metric operations.
pub type MetricResult<T> = result::Result<T, MetricError>;

/// Errors returned by the metrics API.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum MetricError {
/// Other errors not covered by specific cases.
#[error("Metrics error: {0}")]
Other(String),
/// Invalid configuration
#[error("Config error {0}")]
Config(String),
/// Fail to export metrics
#[error("Metrics exporter {} failed with {0}", .0.exporter_name())]
ExportErr(Box<dyn ExportError>),
/// Invalid instrument configuration such invalid instrument name, invalid instrument description, invalid instrument unit, etc.
/// See [spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/api.md#general-characteristics)
/// for full list of requirements.
#[error("Invalid instrument configuration: {0}")]
InvalidInstrumentConfiguration(&'static str),
}

impl<T: ExportError> From<T> for MetricError {
fn from(err: T) -> Self {
MetricError::ExportErr(Box::new(err))
}
}

impl<T> From<PoisonError<T>> for MetricError {
fn from(err: PoisonError<T>) -> Self {
MetricError::Other(err.to_string())
}
}
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/metrics/exporter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Interfaces for exporting metrics
use async_trait::async_trait;

use opentelemetry::metrics::MetricResult;
use crate::metrics::MetricResult;

use crate::metrics::data::ResourceMetrics;

Expand Down
7 changes: 3 additions & 4 deletions opentelemetry-sdk/src/metrics/manual_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ use std::{
sync::{Mutex, Weak},
};

use opentelemetry::{
metrics::{MetricError, MetricResult},
otel_debug,
};
use opentelemetry::otel_debug;

use crate::metrics::{MetricError, MetricResult};

use super::{
data::{ResourceMetrics, Temporality},
Expand Down
5 changes: 3 additions & 2 deletions opentelemetry-sdk/src/metrics/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::{borrow::Cow, sync::Arc};
use opentelemetry::{
metrics::{
AsyncInstrumentBuilder, Counter, Gauge, Histogram, HistogramBuilder, InstrumentBuilder,
InstrumentProvider, MetricError, MetricResult, ObservableCounter, ObservableGauge,
ObservableUpDownCounter, UpDownCounter,
InstrumentProvider, ObservableCounter, ObservableGauge, ObservableUpDownCounter,
UpDownCounter,
},
otel_error, InstrumentationScope,
};
Expand All @@ -14,6 +14,7 @@ use crate::metrics::{
instrument::{Instrument, InstrumentKind, Observable, ResolvedMeasures},
internal::{self, Number},
pipeline::{Pipelines, Resolver},
MetricError, MetricResult,
};

use super::noop::NoopSyncInstrument;
Expand Down
3 changes: 2 additions & 1 deletion opentelemetry-sdk/src/metrics/meter_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::{
};

use opentelemetry::{
metrics::{Meter, MeterProvider, MetricError, MetricResult},
metrics::{Meter, MeterProvider},
otel_debug, otel_error, InstrumentationScope,
};

use crate::metrics::{MetricError, MetricResult};
use crate::Resource;

use super::{
Expand Down
2 changes: 2 additions & 0 deletions opentelemetry-sdk/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

pub(crate) mod aggregation;
pub mod data;
mod error;
pub mod exporter;
pub(crate) mod instrument;
pub(crate) mod internal;
Expand All @@ -54,6 +55,7 @@ pub mod reader;
pub(crate) mod view;

pub use aggregation::*;
pub use error::{MetricError, MetricResult};
pub use instrument::*;
pub use manual_reader::*;
pub use meter_provider::*;
Expand Down
7 changes: 2 additions & 5 deletions opentelemetry-sdk/src/metrics/periodic_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ use futures_util::{
stream::{self, FusedStream},
StreamExt,
};
use opentelemetry::{
metrics::{MetricError, MetricResult},
otel_debug, otel_error,
};
use opentelemetry::{otel_debug, otel_error};

use crate::runtime::Runtime;
use crate::{
metrics::{exporter::PushMetricExporter, reader::SdkProducer},
metrics::{exporter::PushMetricExporter, reader::SdkProducer, MetricError, MetricResult},
Resource,
};

Expand Down
6 changes: 2 additions & 4 deletions opentelemetry-sdk/src/metrics/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use std::{
sync::{Arc, Mutex},
};

use opentelemetry::{
metrics::{MetricError, MetricResult},
otel_debug, InstrumentationScope, KeyValue,
};
use opentelemetry::{otel_debug, InstrumentationScope, KeyValue};

use crate::{
metrics::{
Expand All @@ -20,6 +17,7 @@ use crate::{
internal::Number,
reader::{MetricReader, SdkProducer},
view::View,
MetricError, MetricResult,
},
Resource,
};
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/metrics/reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Interfaces for reading and producing metrics
use std::{fmt, sync::Weak};

use opentelemetry::metrics::MetricResult;
use crate::metrics::MetricResult;

use super::{
data::{ResourceMetrics, Temporality},
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/src/metrics/view.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::instrument::{Instrument, Stream};
use crate::metrics::{MetricError, MetricResult};
use glob::Pattern;
use opentelemetry::metrics::{MetricError, MetricResult};

fn empty_view(_inst: &Instrument) -> Option<Stream> {
None
Expand Down
6 changes: 0 additions & 6 deletions opentelemetry/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,6 @@ impl KeyValue {
}
}

/// Marker trait for errors returned by exporters
pub trait ExportError: std::error::Error + Send + Sync + 'static {
/// The name of exporter that returned this error
fn exporter_name(&self) -> &'static str;
}

/// Information about a library or crate providing instrumentation.
///
/// An instrumentation scope should be named to follow any naming conventions
Expand Down
Loading
Loading