Skip to content

Commit

Permalink
fix: address comments
Browse files Browse the repository at this point in the history
* Fix typo and naming
* Use thiserror for stdout exporter and datadog exporter
* Implement Error for opentelemetry::Error, renamed from OpenTelemetryError
  • Loading branch information
TommyCpp committed Nov 28, 2020
1 parent eea7237 commit 74e683d
Showing 13 changed files with 47 additions and 93 deletions.
4 changes: 2 additions & 2 deletions examples/actix-http/src/main.rs
Original file line number Diff line number Diff line change
@@ -6,11 +6,11 @@ use opentelemetry::{
trace::{FutureExt, TraceContextExt, Tracer},
Key,
};
use std::error::Error;
use opentelemetry::trace::TraceError;

fn init_tracer() -> Result<
(sdktrace::Tracer, opentelemetry_jaeger::Uninstall),
Box<dyn Error + Send + Sync + 'static>,
TraceError
> {
opentelemetry_jaeger::new_pipeline()
.with_collector_endpoint("http://127.0.0.1:14268/api/traces")
4 changes: 2 additions & 2 deletions examples/actix-udp/src/main.rs
Original file line number Diff line number Diff line change
@@ -6,11 +6,11 @@ use opentelemetry::{
trace::{FutureExt, TraceContextExt, Tracer},
Key,
};
use std::error::Error;
use opentelemetry::trace::TraceError;

fn init_tracer() -> Result<
(sdktrace::Tracer, opentelemetry_jaeger::Uninstall),
Box<dyn Error + Send + Sync + 'static>,
TraceError
> {
opentelemetry_jaeger::new_pipeline()
.with_agent_endpoint("localhost:6831")
3 changes: 2 additions & 1 deletion examples/async/src/main.rs
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ use opentelemetry::{
use std::{error::Error, io, net::SocketAddr};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
use opentelemetry::trace::TraceError;

async fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
let tracer = global::tracer("connector");
@@ -54,7 +55,7 @@ async fn run(addr: &SocketAddr) -> io::Result<usize> {

fn init_tracer() -> Result<
(sdktrace::Tracer, opentelemetry_jaeger::Uninstall),
Box<dyn Error + Send + Sync + 'static>,
TraceError
> {
opentelemetry_jaeger::new_pipeline()
.with_service_name("trace-demo")
4 changes: 2 additions & 2 deletions examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
@@ -10,13 +10,13 @@ use opentelemetry::{
use opentelemetry::{global, sdk::trace as sdktrace};
use std::error::Error;
use std::time::Duration;
use opentelemetry::trace::TraceError;

fn init_tracer(
) -> Result<(sdktrace::Tracer, opentelemetry_otlp::Uninstall), Box<dyn Error + Send + Sync + 'static>>
) -> Result<(sdktrace::Tracer, opentelemetry_otlp::Uninstall), TraceError>
{
opentelemetry_otlp::new_pipeline()
.install()
.map_err::<Box<dyn Error + Send + Sync + 'static>, _>(|err| Box::new(err))
}

// Skip first immediate tick from tokio, not needed for async_std.
3 changes: 2 additions & 1 deletion examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -10,10 +10,11 @@ use opentelemetry::{
};
use std::error::Error;
use std::time::Duration;
use opentelemetry::trace::TraceError;

fn init_tracer() -> Result<
(sdktrace::Tracer, opentelemetry_jaeger::Uninstall),
Box<dyn Error + Send + Sync + 'static>,
TraceError,
> {
opentelemetry_jaeger::new_pipeline()
.with_service_name("trace-demo")
4 changes: 2 additions & 2 deletions examples/grpc/src/client.rs
Original file line number Diff line number Diff line change
@@ -6,14 +6,14 @@ use opentelemetry::{
trace::{TraceContextExt, Tracer},
Context, KeyValue,
};
use std::error::Error;
use opentelemetry::trace::TraceError;

pub mod hello_world {
tonic::include_proto!("helloworld");
}

fn tracing_init(
) -> Result<(impl Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error + Send + Sync + 'static>>
) -> Result<(impl Tracer, opentelemetry_jaeger::Uninstall),TraceError>
{
global::set_text_map_propagator(TraceContextPropagator::new());
opentelemetry_jaeger::new_pipeline()
3 changes: 2 additions & 1 deletion examples/grpc/src/server.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ use opentelemetry::{
KeyValue,
};
use std::error::Error;
use opentelemetry::trace::TraceError;

pub mod hello_world {
tonic::include_proto!("helloworld"); // The string specified here must match the proto package name.
@@ -37,7 +38,7 @@ impl Greeter for MyGreeter {
}

fn tracing_init(
) -> Result<(impl Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error + Send + Sync + 'static>>
) -> Result<(impl Tracer, opentelemetry_jaeger::Uninstall), TraceError>
{
global::set_text_map_propagator(TraceContextPropagator::new());
opentelemetry_jaeger::new_pipeline()
43 changes: 10 additions & 33 deletions opentelemetry-contrib/src/trace/exporter/datadog/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,35 @@
use http::uri::InvalidUri;
use opentelemetry::exporter::trace;
use opentelemetry::exporter::trace::ExportError;
use std::fmt;

mod v03;
mod v05;

/// Wrap type for errors from opentelemetry datadog exporter
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Message pack error
#[error("message pack error")]
MessagePackError,
/// No http client founded. User should provide one or enbale features
/// No http client founded. User should provide one or enable features
#[error("http client must be set, users can enable reqwest or surf feature to use http client implementation within create")]
NoHttpClient,
/// Http requests failed with following errors
RequestError(http::Error),
/// The Uri was invalid.
InvalidUri(http::uri::InvalidUri),
#[error(transparent)]
RequestError(#[from] http::Error),
/// The Uri was invalid
#[error(transparent)]
InvalidUri(#[from] http::uri::InvalidUri),
/// Other errors
#[error("{0}")]
Other(String),
}

impl std::error::Error for Error {}

impl ExportError for Error {
fn exporter_name(&self) -> &'static str {
"datadog"
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::MessagePackError => write!(f, "message pack error"),
Error::NoHttpClient => write!(f, "http client must be set, users can enable reqwest or surf feature to use http client implementation within create"),
Error::RequestError(err) => write!(f, "{}", err),
Error::InvalidUri(err) => write!(f, "{}", err),
Error::Other(msg) => write!(f, "{}", msg)
}
}
}

impl From<http::uri::InvalidUri> for Error {
fn from(err: InvalidUri) -> Self {
Error::InvalidUri(err)
}
}

impl From<http::Error> for Error {
fn from(err: http::Error) -> Self {
Error::RequestError(err)
}
}

impl From<rmp::encode::ValueWriteError> for Error {
fn from(_: rmp::encode::ValueWriteError) -> Self {
Self::MessagePackError
3 changes: 2 additions & 1 deletion opentelemetry-jaeger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -206,6 +206,7 @@ use std::{
time::{Duration, SystemTime},
};
use uploader::BatchUploader;
use opentelemetry::trace::TraceError;

/// Default service name if no service is configured.
const DEFAULT_SERVICE_NAME: &str = "OpenTelemetry";
@@ -409,7 +410,7 @@ impl PipelineBuilder {
/// Install a Jaeger pipeline with the recommended defaults.
pub fn install(
self,
) -> Result<(sdk::trace::Tracer, Uninstall), Box<dyn std::error::Error + Send + Sync + 'static>>
) -> Result<(sdk::trace::Tracer, Uninstall), TraceError>
{
let tracer_provider = self.build()?;
let tracer =
2 changes: 1 addition & 1 deletion opentelemetry-otlp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -246,6 +246,6 @@ pub enum Error {

impl ExportError for Error {
fn exporter_name(&self) -> &'static str {
"otel"
"otlp"
}
}
31 changes: 9 additions & 22 deletions opentelemetry/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -19,35 +19,22 @@ pub mod propagation;
pub mod trace;

/// Wrapper for error from both tracing and metrics part of open telemetry.
#[derive(Debug)]
pub enum OpenTelemetryError {
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[cfg(feature = "trace")]
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
TraceErr(TraceError),
#[error(transparent)]
Trace(#[from] TraceError),
#[cfg(feature = "metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
MetricErr(MetricsError),
#[error(transparent)]
Metric(#[from] MetricsError),
#[error("{0}")]
Other(String),
}

#[cfg(feature = "trace")]
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
impl From<TraceError> for OpenTelemetryError {
fn from(err: TraceError) -> Self {
OpenTelemetryError::TraceErr(err)
}
}

#[cfg(feature = "metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
impl From<MetricsError> for OpenTelemetryError {
fn from(err: MetricsError) -> Self {
OpenTelemetryError::MetricErr(err)
}
}

impl<T> From<PoisonError<T>> for OpenTelemetryError {
impl<T> From<PoisonError<T>> for Error {
fn from(err: PoisonError<T>) -> Self {
OpenTelemetryError::Other(err.to_string())
Error::Other(err.to_string())
}
}
20 changes: 3 additions & 17 deletions opentelemetry/src/exporter/trace/stdout.rs
Original file line number Diff line number Diff line change
@@ -31,7 +31,6 @@ use crate::{
trace::TracerProvider,
};
use async_trait::async_trait;
use std::fmt::{Debug, Display, Formatter};
use std::io::{stdout, Stdout, Write};

/// Pipeline builder
@@ -150,22 +149,9 @@ where
pub struct Uninstall(global::TracerProviderGuard);

/// Stdout exporter's error
#[derive(Debug)]
struct Error(std::io::Error);

impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(self.0.to_string().as_str())
}
}

impl std::error::Error for Error {}

impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error(err)
}
}
#[derive(thiserror::Error, Debug)]
#[error(transparent)]
struct Error(#[from] std::io::Error);

impl ExportError for Error {
fn exporter_name(&self) -> &'static str {
16 changes: 8 additions & 8 deletions opentelemetry/src/global/error_handler.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
use crate::api::OpenTelemetryError;
use crate::api::Error;
use std::sync::RwLock;

lazy_static::lazy_static! {
/// The global error handler.
static ref GLOBAL_ERROR_HANDLER: RwLock<Option<ErrorHandler>> = RwLock::new(None);
}

struct ErrorHandler(Box<dyn Fn(OpenTelemetryError) + Send + Sync>);
struct ErrorHandler(Box<dyn Fn(Error) + Send + Sync>);

/// Handle error using the globally configured error handler.
///
/// Writes to stderr if unset.
pub fn handle_error<T: Into<OpenTelemetryError>>(err: T) {
pub fn handle_error<T: Into<Error>>(err: T) {
match GLOBAL_ERROR_HANDLER.read() {
Ok(handler) if handler.is_some() => (handler.as_ref().unwrap().0)(err.into()),
_ => match err.into() {
#[cfg(feature = "metrics")]
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
OpenTelemetryError::MetricErr(err) => {
Error::Metric(err) => {
eprintln!("OpenTelemetry metrics error occurred {:?}", err)
}
#[cfg(feature = "trace")]
#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
OpenTelemetryError::TraceErr(err) => {
Error::Trace(err) => {
eprintln!("OpenTelemetry trace error occurred {:?}", err)
}
OpenTelemetryError::Other(err_msg) => {
Error::Other(err_msg) => {
println!("OpenTelemetry error occurred {}", err_msg)
}
},
}
}

/// Set global error handler.
pub fn set_error_handler<F>(f: F) -> std::result::Result<(), OpenTelemetryError>
pub fn set_error_handler<F>(f: F) -> std::result::Result<(), Error>
where
F: Fn(OpenTelemetryError) + Send + Sync + 'static,
F: Fn(Error) + Send + Sync + 'static,
{
GLOBAL_ERROR_HANDLER
.write()

0 comments on commit 74e683d

Please sign in to comment.