diff --git a/iroh-gateway/Cargo.toml b/iroh-gateway/Cargo.toml index 50ca8572c3..df2b9647ef 100644 --- a/iroh-gateway/Cargo.toml +++ b/iroh-gateway/Cargo.toml @@ -21,6 +21,8 @@ tracing = "0.1.33" metrics = "0.18.1" names = { version = "0.13.0", default-features = false } git-version = "0.3.5" +tracing-opentelemetry = "0.17.2" +opentelemetry = { version = "0.17.0", features = ["rt-tokio"] } [dev-dependencies] axum-macros = "0.2.0" # use #[axum_macros::debug_handler] for better error messages on handlers \ No newline at end of file diff --git a/iroh-gateway/src/constants.rs b/iroh-gateway/src/constants.rs index 96745e0bbd..1096fc1e4c 100644 --- a/iroh-gateway/src/constants.rs +++ b/iroh-gateway/src/constants.rs @@ -1,6 +1,7 @@ // Headers pub const HEADER_X_IPFS_PATH: &str = "X-Ipfs-Path"; pub const HEADER_X_CONTENT_TYPE_OPTIONS: &str = "X-Content-Type-Options"; +pub const HEADER_X_TRACE_ID: &str = "X-Trace-Id"; // Common Header Values pub const VALUE_XCTO_NOSNIFF: &str = "nosniff"; diff --git a/iroh-gateway/src/core.rs b/iroh-gateway/src/core.rs index a3b3970240..8153bb6499 100644 --- a/iroh-gateway/src/core.rs +++ b/iroh-gateway/src/core.rs @@ -18,7 +18,7 @@ use crate::{ config::Config, constants::*, error::GatewayError, - metrics::METRICS_CNT_REQUESTS_TOTAL, + metrics::{get_current_trace_id, METRICS_CNT_REQUESTS_TOTAL}, response::{GatewayResponse, ResponseFormat}, }; @@ -295,6 +295,7 @@ fn response( status_code, body, headers, + trace_id: get_current_trace_id().to_string(), }) } @@ -302,6 +303,7 @@ fn error(status_code: StatusCode, message: &str) -> Result iroh_metrics::Config { // compile time configuration let service_name = env!("CARGO_PKG_NAME").to_string(); @@ -23,3 +26,11 @@ pub fn register_counters() { "Total number of requests received by the gateway" ); } + +pub fn get_current_trace_id() -> TraceId { + tracing::Span::current() + .context() + .span() + .span_context() + .trace_id() +} diff --git a/iroh-gateway/src/response.rs b/iroh-gateway/src/response.rs index c120dda88c..d3b7591447 100644 --- a/iroh-gateway/src/response.rs +++ b/iroh-gateway/src/response.rs @@ -67,6 +67,7 @@ pub struct GatewayResponse { pub status_code: StatusCode, pub body: BoxBody, pub headers: HashMap, + pub trace_id: String, } impl IntoResponse for GatewayResponse { @@ -77,6 +78,10 @@ impl IntoResponse for GatewayResponse { let header_name = HeaderName::from_str(key).unwrap(); headers.insert(header_name, HeaderValue::from_str(value).unwrap()); } + headers.insert( + HEADER_X_TRACE_ID, + HeaderValue::from_str(&self.trace_id).unwrap(), + ); rb.body(self.body).unwrap() } }