Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update from Acala subway #1

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
6 changes: 3 additions & 3 deletions src/middlewares/subscriptions/merge_subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ impl Middleware<SubscriptionRequest, Result<(), StringError>> for MergeSubscript
}
}
Err(e) => {
// this should never happen
tracing::error!("subscription stream error {e:?}");
unreachable!("subscription stream error {e:?}");
// remote upstream subscription failed, drop subscription
tracing::trace!("subscription stream error {e}");
break;
}
}
}
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));
});
}
}
}