Skip to content

Commit

Permalink
set attr error.message (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci authored Nov 24, 2023
1 parent d743620 commit dd4fb16
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
18 changes: 8 additions & 10 deletions src/extensions/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,14 @@ impl Client {

let result = rx.await.map_err(errors::internal_error)?.map_err(errors::map_error);

opentelemetry::trace::get_active_span(|span| match result.as_ref() {
match result.as_ref() {
Ok(_) => {
span.set_status(opentelemetry::trace::Status::Ok);
TRACER.span_ok();
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(err.to_string()));
span.record_error(&err);
TRACER.span_error(format!("{}", err));
}
});
}

result
}
Expand Down Expand Up @@ -430,15 +429,14 @@ impl Client {

let result = rx.await.map_err(errors::failed)?;

opentelemetry::trace::get_active_span(|span| match result.as_ref() {
match result.as_ref() {
Ok(_) => {
span.set_status(opentelemetry::trace::Status::Ok);
TRACER.span_ok();
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(err.to_string()));
span.record_error(&err);
TRACER.span_error(format!("{}", err));
}
});
};

result
}
Expand Down
5 changes: 1 addition & 4 deletions src/middlewares/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ impl<Request: Debug + Send + 'static, Result: Send + 'static> Middlewares<Reques
tokio::select! {
_ = sleep => {
tracing::error!("middlewares timeout: {req}");
opentelemetry::trace::get_active_span(|span| {
span.set_status(opentelemetry::trace::Status::error("middlewares timeout"));
span.record_error(&jsonrpsee::core::Error::Custom("middlewares timeout".to_string()));
});
TRACER.span_error("middlewares timeout");
task_handle.abort();
}
_ = &mut task_handle => {
Expand Down
20 changes: 8 additions & 12 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,12 @@ pub async fn build(config: Config) -> anyhow::Result<SubwayServerHandle> {
.await
.map_err(|_| errors::map_error(jsonrpsee::core::Error::RequestTimeout))?;

opentelemetry::trace::get_active_span(|span| match result.as_ref() {
Ok(_) => {
span.set_status(opentelemetry::trace::Status::Ok);
}
match result.as_ref() {
Ok(_) => tracer.span_ok(),
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(err.to_string()));
span.record_error(&err);
tracer.span_error(format!("{}", err));
}
});
};

result
}
Expand Down Expand Up @@ -163,15 +160,14 @@ pub async fn build(config: Config) -> anyhow::Result<SubwayServerHandle> {
.await
.map_err(|_| errors::map_error(jsonrpsee::core::Error::RequestTimeout))?;

opentelemetry::trace::get_active_span(|span| match result.as_ref() {
match result.as_ref() {
Ok(_) => {
span.set_status(opentelemetry::trace::Status::Ok);
tracer.span_ok();
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(format!("{:?}", err)));
span.record_error(&jsonrpsee::core::Error::Custom(format!("{:?}", err)));
tracer.span_error(format!("{:?}", err));
}
});
};

result
}
Expand Down
18 changes: 16 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub mod errors {
pub mod telemetry {
use opentelemetry::{
global::{self, BoxedSpan},
trace::{TraceContextExt, Tracer as _},
Context,
trace::{get_active_span, Status, TraceContextExt, Tracer as _},
Context, KeyValue,
};
use std::borrow::Cow;

Expand All @@ -62,5 +62,19 @@ pub mod telemetry {
let span = self.span(span_name);
Context::current_with_span(span)
}

pub fn span_ok(&self) {
get_active_span(|span| {
span.set_status(Status::Ok);
});
}

pub fn span_error(&self, err: impl Into<Cow<'static, str>>) {
get_active_span(|span| {
let err_msg = err.into();
span.set_status(Status::error(format!("{}", err_msg)));
span.set_attribute(KeyValue::new("error.message", err_msg));
});
}
}
}

0 comments on commit dd4fb16

Please sign in to comment.