Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lalitb committed Nov 1, 2024
1 parent a4381ce commit 9cf5898
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::sync::PoisonError;
use std::sync::RwLock;

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

/// Wrapper for error from both tracing and metrics part of open telemetry.
#[derive(thiserror::Error, Debug)]
Expand Down
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
2 changes: 0 additions & 2 deletions opentelemetry/src/global/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,13 @@
//! [`MeterProvider`]: crate::metrics::MeterProvider
//! [`set_meter_provider`]: crate::global::set_meter_provider

mod error_handler;
mod internal_logging;
#[cfg(feature = "metrics")]
mod metrics;
#[cfg(feature = "trace")]
mod propagation;
#[cfg(feature = "trace")]
mod trace;
pub use error_handler::Error;

#[cfg(feature = "metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
Expand Down
3 changes: 1 addition & 2 deletions opentelemetry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ mod common;
pub mod testing;

pub use common::{
Array, ExportError, InstrumentationScope, InstrumentationScopeBuilder, Key, KeyValue,
StringValue, Value,
Array, InstrumentationScope, InstrumentationScopeBuilder, Key, KeyValue, StringValue, Value,
};

#[cfg(feature = "metrics")]
Expand Down
Loading

0 comments on commit 9cf5898

Please sign in to comment.