File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,11 +20,37 @@ use crate::attestation::{
2020use crate :: state:: AppState ;
2121use crate :: webhook:: { parse_merge_event, verify_github_signature} ;
2222
23+ // Request ID middleware for trace propagation across services.
24+ async fn request_id ( req : Request < Body > , next : Next ) -> Response {
25+ use axum:: body:: Body ;
26+ use axum:: http:: Request ;
27+ use axum:: middleware:: Next ;
28+ use axum:: response:: Response ;
29+
30+ let request_id = req
31+ . headers ( )
32+ . get ( "x-request-id" )
33+ . and_then ( |v| v. to_str ( ) . ok ( ) )
34+ . map ( str:: to_string)
35+ . unwrap_or_else ( || uuid:: Uuid :: new_v4 ( ) . to_string ( ) ) ;
36+
37+ tracing:: Span :: current ( ) . record ( "request_id" , & request_id) ;
38+
39+ let mut response = next. run ( req) . await ;
40+ response. headers_mut ( ) . insert (
41+ "x-request-id" ,
42+ request_id. parse ( ) . unwrap ( ) ,
43+ ) ;
44+ response
45+ }
46+
47+
2348pub fn router ( state : AppState ) -> Router {
2449 Router :: new ( )
2550 . route ( "/health" , get ( health) )
2651 . route ( "/ready" , get ( ready) )
2752 . route ( "/webhooks/github" , post ( github_webhook) )
53+ . layer ( axum:: middleware:: from_fn ( request_id) )
2854 . with_state ( state)
2955}
3056
You can’t perform that action at this time.
0 commit comments