-
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.
Merge pull request #125 from xsnippet/request-id
Generate a unique id for each request and return it in the X-Request-Id header
- Loading branch information
Showing
10 changed files
with
154 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod content; | ||
mod tracing; | ||
|
||
pub use content::{Input, NegotiatedContentType, Output}; | ||
pub use tracing::{RequestId, RequestIdHeader}; |
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,48 @@ | ||
use rocket::fairing::{Fairing, Info, Kind}; | ||
use rocket::request::{self, FromRequest, Request}; | ||
use rocket::{Outcome, Response}; | ||
|
||
/// Unique identifier assigned to a given API request. | ||
pub struct RequestId(pub String); | ||
|
||
impl RequestId { | ||
pub fn new() -> Self { | ||
RequestId(uuid::Uuid::new_v4().to_hyphenated().to_string()) | ||
} | ||
} | ||
|
||
impl Default for RequestId { | ||
fn default() -> Self { | ||
RequestId::new() | ||
} | ||
} | ||
|
||
impl<'a, 'r> FromRequest<'a, 'r> for &'a RequestId { | ||
type Error = (); | ||
|
||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> { | ||
// on first access, generate a new value and store it in the request cache | ||
Outcome::Success(request.local_cache(RequestId::default)) | ||
} | ||
} | ||
|
||
/// Rocket fairing that returns the identifier of the API request in the | ||
/// X-Request-Id response header. | ||
pub struct RequestIdHeader; | ||
|
||
impl Fairing for RequestIdHeader { | ||
fn info(&self) -> Info { | ||
Info { | ||
name: "X-Request-Id response header", | ||
kind: Kind::Response, | ||
} | ||
} | ||
|
||
fn on_response(&self, request: &Request, response: &mut Response) { | ||
if let Some(request_id) = request.guard::<&RequestId>().succeeded() { | ||
response.set_raw_header("X-Request-Id", request_id.0.clone()); | ||
} else { | ||
eprintln!("Failed to generate a request id"); | ||
} | ||
} | ||
} |
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.