diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index 2df1611562..97156d0569 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -17,6 +17,7 @@ `Error` which contained many variants unrelated to building an exporter, the new one returns specific variants applicable to building an exporter. Some variants might be applicable only on select features. + Also, now unused `Error` enum is removed. - **Breaking** `ExportConfig`'s `timeout` field is now optional(`Option`) - **Breaking** Export configuration done via code is final. ENV variables cannot be used to override the code config. Do not use code based config, if there is desire to control the settings via ENV variables. diff --git a/opentelemetry-otlp/src/exporter/http/mod.rs b/opentelemetry-otlp/src/exporter/http/mod.rs index 77eb5cefb2..417857d206 100644 --- a/opentelemetry-otlp/src/exporter/http/mod.rs +++ b/opentelemetry-otlp/src/exporter/http/mod.rs @@ -287,7 +287,7 @@ impl OtlpHttpClient { fn build_trace_export_body( &self, spans: Vec, - ) -> opentelemetry_sdk::trace::TraceResult<(Vec, &'static str)> { + ) -> Result<(Vec, &'static str), String> { use opentelemetry_proto::tonic::collector::trace::v1::ExportTraceServiceRequest; let resource_spans = group_spans_by_resource_and_scope(spans, &self.resource); @@ -296,7 +296,7 @@ impl OtlpHttpClient { #[cfg(feature = "http-json")] Protocol::HttpJson => match serde_json::to_string_pretty(&req) { Ok(json) => Ok((json.into_bytes(), "application/json")), - Err(e) => Err(opentelemetry_sdk::trace::TraceError::from(e.to_string())), + Err(e) => Err(e.to_string()), }, _ => Ok((req.encode_to_vec(), "application/x-protobuf")), } diff --git a/opentelemetry-otlp/src/lib.rs b/opentelemetry-otlp/src/lib.rs index 748687cfbe..4913c552ca 100644 --- a/opentelemetry-otlp/src/lib.rs +++ b/opentelemetry-otlp/src/lib.rs @@ -362,8 +362,6 @@ pub use crate::exporter::{ OTEL_EXPORTER_OTLP_TIMEOUT_DEFAULT, }; -use opentelemetry_sdk::ExportError; - /// Type to indicate the builder does not have a client set. #[derive(Debug, Default, Clone)] pub struct NoExporterBuilderSet; @@ -391,104 +389,6 @@ pub use crate::exporter::tonic::{TonicConfig, TonicExporterBuilder}; #[cfg(feature = "serialize")] use serde::{Deserialize, Serialize}; -/// Wrap type for errors from this crate. -#[derive(thiserror::Error, Debug)] -pub enum Error { - /// Wrap error from [`tonic::transport::Error`] - #[cfg(feature = "grpc-tonic")] - #[error("transport error {0}")] - Transport(#[from] tonic::transport::Error), - - /// Wrap the [`tonic::codegen::http::uri::InvalidUri`] error - #[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))] - #[error("invalid URI {0}")] - InvalidUri(#[from] http::uri::InvalidUri), - - /// Wrap type for [`tonic::Status`] - #[cfg(feature = "grpc-tonic")] - #[error("the grpc server returns error ({code}): {message}")] - Status { - /// grpc status code - code: tonic::Code, - /// error message - message: String, - }, - - /// Http requests failed because no http client is provided. - #[cfg(any(feature = "http-proto", feature = "http-json"))] - #[error( - "no http client, you must select one from features or provide your own implementation" - )] - NoHttpClient, - - /// Http requests failed. - #[cfg(any(feature = "http-proto", feature = "http-json"))] - #[error("http request failed with {0}")] - RequestFailed(#[from] opentelemetry_http::HttpError), - - /// The provided value is invalid in HTTP headers. - #[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))] - #[error("http header value error {0}")] - InvalidHeaderValue(#[from] http::header::InvalidHeaderValue), - - /// The provided name is invalid in HTTP headers. - #[cfg(any(feature = "grpc-tonic", feature = "http-proto", feature = "http-json"))] - #[error("http header name error {0}")] - InvalidHeaderName(#[from] http::header::InvalidHeaderName), - - /// Prost encode failed - #[cfg(any( - feature = "http-proto", - all(feature = "http-json", not(feature = "trace")) - ))] - #[error("prost encoding error {0}")] - EncodeError(#[from] prost::EncodeError), - - /// The lock in exporters has been poisoned. - #[cfg(feature = "metrics")] - #[error("the lock of the {0} has been poisoned")] - PoisonedLock(&'static str), - - /// Unsupported compression algorithm. - #[error("unsupported compression algorithm '{0}'")] - UnsupportedCompressionAlgorithm(String), - - /// Feature required to use the specified compression algorithm. - #[cfg(any(not(feature = "gzip-tonic"), not(feature = "zstd-tonic")))] - #[error("feature '{0}' is required to use the compression algorithm '{1}'")] - FeatureRequiredForCompressionAlgorithm(&'static str, Compression), -} - -#[cfg(feature = "grpc-tonic")] -impl From for Error { - fn from(status: tonic::Status) -> Error { - Error::Status { - code: status.code(), - message: { - if !status.message().is_empty() { - let mut result = ", detailed error message: ".to_string() + status.message(); - if status.code() == tonic::Code::Unknown { - let source = (&status as &dyn std::error::Error) - .source() - .map(|e| format!("{:?}", e)); - result.push(' '); - result.push_str(source.unwrap_or_default().as_ref()); - } - result - } else { - String::new() - } - }, - } - } -} - -impl ExportError for Error { - fn exporter_name(&self) -> &'static str { - "otlp" - } -} - /// The communication protocol to use when exporting data. #[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))] #[derive(Clone, Copy, Debug, Eq, PartialEq)] diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 6d9a72bce2..28b9901fcd 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -75,7 +75,10 @@ - **Breaking** for custom `LogProcessor` authors: Changed `set_resource` to require mutable ref. `fn set_resource(&mut self, _resource: &Resource) {}` -- **Breaking** Removed deprecated functions and methods related to `trace::Config` +- **Breaking**: InMemoryExporter's return type change. + - `TraceResult>` to `Result, InMemoryExporterError>` + - `MetricResult>` to `Result, InMemoryExporterError>` + - `LogResult>` to `Result, InMemoryExporterError>` ## 0.28.0 diff --git a/opentelemetry-sdk/src/lib.rs b/opentelemetry-sdk/src/lib.rs index 89f2e46c38..8ee470b828 100644 --- a/opentelemetry-sdk/src/lib.rs +++ b/opentelemetry-sdk/src/lib.rs @@ -146,3 +146,24 @@ pub use resource::Resource; pub mod error; pub use error::ExportError; + +#[cfg(any(feature = "testing", test))] +#[derive(thiserror::Error, Debug)] +/// Errors that can occur during when returning telemetry from InMemoryLogExporter +pub enum InMemoryExporterError { + /// Operation failed due to an internal error. + /// + /// The error message is intended for logging purposes only and should not + /// be used to make programmatic decisions. It is implementation-specific + /// and subject to change without notice. Consumers of this error should not + /// rely on its content beyond logging. + #[error("Unable to obtain telemetry. Reason: {0}")] + InternalFailure(String), +} + +#[cfg(any(feature = "testing", test))] +impl From> for InMemoryExporterError { + fn from(err: std::sync::PoisonError) -> Self { + InMemoryExporterError::InternalFailure(format!("Mutex poison error: {}", err)) + } +} diff --git a/opentelemetry-sdk/src/logs/in_memory_exporter.rs b/opentelemetry-sdk/src/logs/in_memory_exporter.rs index eb83033bdb..63737997a9 100644 --- a/opentelemetry-sdk/src/logs/in_memory_exporter.rs +++ b/opentelemetry-sdk/src/logs/in_memory_exporter.rs @@ -1,14 +1,13 @@ use crate::error::{OTelSdkError, OTelSdkResult}; use crate::logs::SdkLogRecord; use crate::logs::{LogBatch, LogExporter}; +use crate::InMemoryExporterError; use crate::Resource; use opentelemetry::InstrumentationScope; use std::borrow::Cow; use std::sync::atomic::AtomicBool; use std::sync::{Arc, Mutex}; -type LogResult = Result; - /// An in-memory logs exporter that stores logs data in memory.. /// /// This exporter is useful for testing and debugging purposes. @@ -157,14 +156,9 @@ impl InMemoryLogExporter { /// let emitted_logs = exporter.get_emitted_logs().unwrap(); /// ``` /// - pub fn get_emitted_logs(&self) -> LogResult> { - let logs_guard = self - .logs - .lock() - .map_err(|e| OTelSdkError::InternalFailure(format!("Failed to lock logs: {}", e)))?; - let resource_guard = self.resource.lock().map_err(|e| { - OTelSdkError::InternalFailure(format!("Failed to lock resource: {}", e)) - })?; + pub fn get_emitted_logs(&self) -> Result, InMemoryExporterError> { + let logs_guard = self.logs.lock().map_err(InMemoryExporterError::from)?; + let resource_guard = self.resource.lock().map_err(InMemoryExporterError::from)?; let logs: Vec = logs_guard .iter() .map(|log_data| LogDataWithResource { diff --git a/opentelemetry-sdk/src/metrics/in_memory_exporter.rs b/opentelemetry-sdk/src/metrics/in_memory_exporter.rs index 50f03edbf9..fe06cadbc2 100644 --- a/opentelemetry-sdk/src/metrics/in_memory_exporter.rs +++ b/opentelemetry-sdk/src/metrics/in_memory_exporter.rs @@ -2,9 +2,8 @@ use crate::error::{OTelSdkError, OTelSdkResult}; use crate::metrics::data::{self, Gauge, Sum}; use crate::metrics::data::{Histogram, Metric, ResourceMetrics, ScopeMetrics}; use crate::metrics::exporter::PushMetricExporter; -use crate::metrics::MetricError; -use crate::metrics::MetricResult; use crate::metrics::Temporality; +use crate::InMemoryExporterError; use std::collections::VecDeque; use std::fmt; use std::sync::{Arc, Mutex}; @@ -143,11 +142,13 @@ impl InMemoryMetricExporter { /// let exporter = InMemoryMetricExporter::default(); /// let finished_metrics = exporter.get_finished_metrics().unwrap(); /// ``` - pub fn get_finished_metrics(&self) -> MetricResult> { - self.metrics + pub fn get_finished_metrics(&self) -> Result, InMemoryExporterError> { + let metrics = self + .metrics .lock() .map(|metrics_guard| metrics_guard.iter().map(Self::clone_metrics).collect()) - .map_err(MetricError::from) + .map_err(InMemoryExporterError::from)?; + Ok(metrics) } /// Clears the internal storage of finished metrics. diff --git a/opentelemetry-sdk/src/trace/in_memory_exporter.rs b/opentelemetry-sdk/src/trace/in_memory_exporter.rs index 9f4f38bb4b..099f666a41 100644 --- a/opentelemetry-sdk/src/trace/in_memory_exporter.rs +++ b/opentelemetry-sdk/src/trace/in_memory_exporter.rs @@ -1,7 +1,7 @@ use crate::error::{OTelSdkError, OTelSdkResult}; use crate::resource::Resource; -use crate::trace::error::{TraceError, TraceResult}; use crate::trace::{SpanData, SpanExporter}; +use crate::InMemoryExporterError; use std::sync::{Arc, Mutex}; /// An in-memory span exporter that stores span data in memory. @@ -104,11 +104,13 @@ impl InMemorySpanExporter { /// let exporter = InMemorySpanExporter::default(); /// let finished_spans = exporter.get_finished_spans().unwrap(); /// ``` - pub fn get_finished_spans(&self) -> TraceResult> { - self.spans + pub fn get_finished_spans(&self) -> Result, InMemoryExporterError> { + let spans = self + .spans .lock() .map(|spans_guard| spans_guard.iter().cloned().collect()) - .map_err(TraceError::from) + .map_err(InMemoryExporterError::from)?; + Ok(spans) } /// Clears the internal storage of finished spans.