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

add execution status for tracing::span #138

Merged
merged 1 commit into from
Nov 21, 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
84 changes: 55 additions & 29 deletions src/extensions/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,22 +379,33 @@ impl Client {
}

pub async fn request(&self, method: &str, params: Vec<JsonValue>) -> Result<JsonValue, ErrorObjectOwned> {
let cx = TRACER.context(method.to_string());
let (tx, rx) = tokio::sync::oneshot::channel();
self.sender
.send(Message::Request {
method: method.into(),
params,
response: tx,
retries: self.retries,
})
.await
.map_err(errors::internal_error)?;
async move {
let (tx, rx) = tokio::sync::oneshot::channel();
self.sender
.send(Message::Request {
method: method.into(),
params,
response: tx,
retries: self.retries,
})
.await
.map_err(errors::internal_error)?;

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

opentelemetry::trace::get_active_span(|span| match result.as_ref() {
Ok(_) => {
span.set_status(opentelemetry::trace::Status::Ok);
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(err.to_string()));
}
});

rx.with_context(cx)
.await
.map_err(errors::internal_error)?
.map_err(errors::map_error)
result
}
.with_context(TRACER.context(method.to_string()))
.await
}

pub async fn subscribe(
Expand All @@ -403,21 +414,36 @@ impl Client {
params: Vec<JsonValue>,
unsubscribe: &str,
) -> Result<Subscription<JsonValue>, Error> {
let cx = TRACER.context(subscribe.to_string());

let (tx, rx) = tokio::sync::oneshot::channel();
self.sender
.send(Message::Subscribe {
subscribe: subscribe.into(),
params,
unsubscribe: unsubscribe.into(),
response: tx,
retries: self.retries,
})
.await
.map_err(errors::failed)?;
async move {
let (tx, rx) = tokio::sync::oneshot::channel();
self.sender
.send(Message::Subscribe {
subscribe: subscribe.into(),
params,
unsubscribe: unsubscribe.into(),
response: tx,
retries: self.retries,
})
.await
.map_err(errors::failed)?;

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

opentelemetry::trace::get_active_span(|span| match result.as_ref() {
Ok(_) => {
span.set_status(opentelemetry::trace::Status::Ok);
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::Error {
description: std::borrow::Cow::from(err.to_string()),
});
}
});

rx.with_context(cx).await.map_err(errors::failed)?
result
}
.with_context(TRACER.context(subscribe.to_string()))
.await
}

pub async fn rotate_endpoint(&self) {
Expand Down
6 changes: 6 additions & 0 deletions src/middlewares/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ 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"));
});
task_handle.abort();
}
_ = &mut task_handle => {
tracing::trace!("middlewares finished: {req}");
opentelemetry::trace::get_active_span(|span| {
span.set_status(opentelemetry::trace::Status::Ok);
});
}
}
}
Expand Down
40 changes: 28 additions & 12 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ pub async fn build(config: Config) -> anyhow::Result<SubwayServerHandle> {

module.register_async_method(method_name, move |params, _| {
let method_middlewares = method_middlewares.clone();

async move {
let cx = tracer.context(method_name);

let parsed = params.parse::<JsonValue>()?;
let params = if parsed == JsonValue::Null {
vec![]
Expand All @@ -89,13 +86,24 @@ pub async fn build(config: Config) -> anyhow::Result<SubwayServerHandle> {

method_middlewares
.call(CallRequest::new(method_name, params), result_tx, timeout)
.with_context(cx)
.await;

result_rx
let result = result_rx
.await
.map_err(|_| errors::map_error(jsonrpsee::core::Error::RequestTimeout))?
.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);
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(err.to_string()));
}
});

result
}
.with_context(tracer.context(method_name))
})?;
}

Expand Down Expand Up @@ -126,10 +134,7 @@ pub async fn build(config: Config) -> anyhow::Result<SubwayServerHandle> {
unsubscribe_name,
move |params, pending_sink, _| {
let subscription_middlewares = subscription_middlewares.clone();

async move {
let cx = tracer.context(name);

let parsed = params.parse::<JsonValue>()?;
let params = if parsed == JsonValue::Null {
vec![]
Expand All @@ -151,13 +156,24 @@ pub async fn build(config: Config) -> anyhow::Result<SubwayServerHandle> {
result_tx,
timeout,
)
.with_context(cx)
.await;

result_rx
let result = result_rx
.await
.map_err(|_| errors::map_error(jsonrpsee::core::Error::RequestTimeout))?
.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);
}
Err(err) => {
span.set_status(opentelemetry::trace::Status::error(format!("{:?}", err)));
}
});

result
}
.with_context(tracer.context(name))
},
)?;
}
Expand Down