-
Notifications
You must be signed in to change notification settings - Fork 0
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
Init tracing #28
Init tracing #28
Changes from 12 commits
ca6d357
c0e3622
7f6ee5a
bbe02d0
b3e0dfc
f5b2c56
7498c5d
ab04384
6e11997
d4e3a1a
efde0fe
84d7ed8
6e7b555
1038334
3fbf25d
02b6025
0a798c9
3ee9e77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ use lapin::{ | |
types::FieldTable, | ||
BasicProperties, Channel, Connection, Consumer, | ||
}; | ||
use log::{debug, error, trace, warn}; | ||
use tracing::{debug, error, info, info_span, trace, warn, Instrument}; | ||
|
||
use crate::{Handler, HandlerConfig, Request, Respond}; | ||
|
||
|
@@ -25,6 +25,7 @@ use super::StateMap; | |
pub(super) type HandlerTask = Pin<Box<dyn Future<Output = String> + Send>>; | ||
|
||
/// Creates the handler task for the given handler and routing key. See [`HandlerTask`]. | ||
#[tracing::instrument(skip_all, field(routing_key = routing_key))] | ||
fn handler_task<H, Args, Res>( | ||
routing_key: String, | ||
handler: H, | ||
|
@@ -47,7 +48,7 @@ where | |
let delivery = tokio::select! { | ||
// Listen on new deliveries. | ||
delivery = consumer.next() => match delivery { | ||
// Received a delivery succesfully, just unwrap it from the option. | ||
// Received a delivery successfully, just unwrap it from the option. | ||
Some(delivery) => delivery, | ||
// We should only ever get to this point if the consumer is cancelled. | ||
// We'll just return the routing key - might be a help for the user to see which | ||
|
@@ -79,7 +80,11 @@ where | |
// Requests are handled and replied to concurrently. | ||
// This allows each handler task to process multiple requests at once. | ||
tasks.push(tokio::spawn(async move { | ||
handle_request(req, handler, channel, should_reply).await; | ||
let span = info_span!("request", req_id = %req.req_id); | ||
|
||
handle_request(req, handler, channel, should_reply) | ||
.instrument(span) | ||
.await; | ||
})); | ||
} | ||
}) | ||
|
@@ -100,12 +105,14 @@ async fn handle_request<H, Args, Res>( | |
let properties = req.properties().cloned(); | ||
let reply_to = properties.as_ref().and_then(|p| p.reply_to().clone()); | ||
let correlation_id = properties.as_ref().and_then(|p| p.correlation_id().clone()); | ||
let app_id = req.app_id().unwrap_or("<unknown>"); | ||
|
||
let handler_name = std::any::type_name::<H>(); | ||
info!("Received request on handler {handler_name:?} from {app_id}"); | ||
|
||
// Call the handler with the request. | ||
let response = handler.call(&mut req).await; | ||
debug!( | ||
"Handler {:?} produced response: {response:?}", | ||
std::any::type_name::<H>() | ||
); | ||
info!("Handler produced response: {response:?}"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the name of the handler here (it feels weird that it's printed twice when you can just use the But I have a concern about printing the full response of every single request. For small responses it's no problem; but it doesn't seem right thing to do as the default for every service. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. info!("Handler produced response of {} bytes that will be published on {reply_to}", bytes_response.len()); could be an option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There won't always be a reply_to. But maybe you can log in the different scenarios of reply_to/no reply_to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this? :) |
||
|
||
let bytes_response = response.respond(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! Request IDs. | ||
|
||
use core::fmt; | ||
use std::convert::Infallible; | ||
|
||
use async_trait::async_trait; | ||
use lapin::{ | ||
message::Delivery, | ||
types::{AMQPValue, LongString}, | ||
}; | ||
use uuid::Uuid; | ||
|
||
use crate::{Extract, Request}; | ||
|
||
/// Request IDs allow concurrent logs to be associated with a unique request. It can also enable requests | ||
/// to be traced between different services by propagating the request IDs when calling other services. | ||
/// This type implements [`Extract`], so it can be used in handlers. | ||
#[derive(Debug, Clone)] | ||
pub struct ReqId(AMQPValue); | ||
|
||
impl ReqId { | ||
/// Create a new [`ReqId`] as a random UUID. | ||
fn new() -> Self { | ||
let uuid = Uuid::new_v4(); | ||
let amqp_value = AMQPValue::LongString(LongString::from(uuid.to_string())); | ||
Self(amqp_value) | ||
} | ||
} | ||
|
||
/// [`AMQPValue`] does not implement `Display` but we provide a `Display` implementation for | ||
/// `ReqId` to allow it to be used in tracing spans (see the `tracing` crate). | ||
impl fmt::Display for ReqId { | ||
gorm-issuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match &self.0 { | ||
AMQPValue::LongString(req_id) => req_id.fmt(f), | ||
AMQPValue::Boolean(b) => b.fmt(f), | ||
AMQPValue::ShortShortInt(v) => v.fmt(f), | ||
AMQPValue::ShortShortUInt(v) => v.fmt(f), | ||
AMQPValue::ShortInt(v) => v.fmt(f), | ||
AMQPValue::ShortUInt(v) => v.fmt(f), | ||
AMQPValue::LongInt(v) => v.fmt(f), | ||
AMQPValue::LongUInt(v) => v.fmt(f), | ||
AMQPValue::LongLongInt(v) => v.fmt(f), | ||
AMQPValue::Float(v) => v.fmt(f), | ||
AMQPValue::Double(v) => v.fmt(f), | ||
AMQPValue::DecimalValue(v) => write!(f, "{v:?}"), | ||
AMQPValue::ShortString(v) => write!(f, "{v:?}"), | ||
AMQPValue::FieldArray(v) => write!(f, "{v:?}"), | ||
AMQPValue::Timestamp(v) => write!(f, "{v:?}"), | ||
AMQPValue::FieldTable(v) => write!(f, "{v:?}"), | ||
AMQPValue::ByteArray(v) => write!(f, "{v:?}"), | ||
AMQPValue::Void => write!(f, "Void"), | ||
} | ||
} | ||
} | ||
|
||
impl From<&Delivery> for ReqId { | ||
fn from(delivery: &Delivery) -> Self { | ||
let Some(headers) = delivery.properties.headers() else { | ||
return Self::new(); | ||
}; | ||
|
||
let Some(req_id) = headers.inner().get("req_id") else { | ||
return Self::new(); | ||
}; | ||
|
||
Self(req_id.clone()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Extract for ReqId { | ||
type Error = Infallible; | ||
|
||
async fn extract(req: &mut Request) -> Result<Self, Self::Error> { | ||
Ok(req.req_id.clone()) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ use lapin::protocol::basic::AMQPProperties; | |
use lapin::{message::Delivery, Channel}; | ||
|
||
use crate::app::StateMap; | ||
use crate::extract::ReqId; | ||
|
||
/// An AMQP request. | ||
#[derive(Debug)] | ||
|
@@ -15,6 +16,9 @@ pub struct Request { | |
state: Arc<StateMap>, | ||
/// The channel the message was received on. | ||
channel: Channel, | ||
/// Request ID. This is a unique ID for every request. Either a newly created uuid or whatever | ||
/// is found in the header of the incoming amqp message. | ||
gorm-issuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub(crate) req_id: ReqId, | ||
/// The message delivery. | ||
pub(crate) delivery: Option<Delivery>, | ||
} | ||
|
@@ -25,6 +29,7 @@ impl Request { | |
Self { | ||
state, | ||
channel, | ||
req_id: ReqId::from(&delivery), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the usage of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am actually not a super big fan of these impl's (as with the example with |
||
delivery: Some(delivery), | ||
} | ||
} | ||
|
@@ -44,4 +49,11 @@ impl Request { | |
pub fn properties(&self) -> Option<&AMQPProperties> { | ||
self.delivery.as_ref().map(|d| &d.properties) | ||
} | ||
|
||
/// Return `app_id` of the sender of the request. | ||
gorm-issuu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub fn app_id(&self) -> Option<&str> { | ||
self.properties() | ||
.and_then(|p| p.app_id().as_ref()) | ||
.map(|app_id| app_id.as_str()) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't see this before. Won't this add yet another layer of a span? How does it look when printed? Is it too much?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh! I think this was just me debugging/exploring. It doesn't do anything because.. yeah.. I'm not sure tbh.