-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Init tracing * Document ReqId::new * Update kanin/Cargo.toml Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/app/task.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/app/task.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/extract/req_id.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/extract/req_id.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/extract/req_id.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/extract/req_id.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/extract/req_id.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/tests/send_recv.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Add req_id to Request * Move 'response' log * Update kanin/src/request.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Update kanin/src/request.rs Co-authored-by: Victor Nordam Suadicani <[email protected]> * Remove instrumentation + From<_> --> from_delivery * Better log messages + elapsed time for req * Update versions --------- Co-authored-by: Victor Nordam Suadicani <[email protected]>
- Loading branch information
1 parent
68c5977
commit 1a39461
Showing
13 changed files
with
183 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "kanin" | ||
version = "0.23.1" | ||
version = "0.24.0" | ||
edition = "2021" | ||
authors = ["Victor Nordam Suadicani <[email protected]>"] | ||
description = "An RPC microservice framework for AMQP, protobuf and Rust built on lapin (https://github.com/amqp-rs/lapin)." | ||
|
@@ -13,13 +13,16 @@ readme = "../README.md" | |
|
||
[dependencies] | ||
# Derive macros for traits in kanin. | ||
kanin_derive = "0.5.0" | ||
kanin_derive = "0.5.2" | ||
|
||
# Lower level AMQP framework. | ||
lapin = "2.1.0" | ||
|
||
# Generalized logging framework. | ||
log = "0.4" | ||
# Generalized tracing framework. | ||
tracing = "0.1.37" | ||
|
||
# Used to create unique request IDs. | ||
uuid = { version = "1.4.1", features = ["v4"] } | ||
|
||
# Asynchronous runtime. | ||
tokio = { version = "1.18.0", features = ["rt", "rt-multi-thread", "macros"] } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
/// Create a [`ReqId`] from an AMQP Delivery. If no `req_id` is found in the headers of the | ||
/// message then a new one is created. | ||
pub(crate) fn from_delivery(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()) | ||
} | ||
} | ||
|
||
/// [`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 { | ||
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"), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl Extract for ReqId { | ||
type Error = Infallible; | ||
|
||
async fn extract(req: &mut Request) -> Result<Self, Self::Error> { | ||
Ok(req.req_id.clone()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.