Skip to content
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

feat: add trace id to responses #28

Merged
merged 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions iroh-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions iroh-gateway/src/constants.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 3 additions & 1 deletion iroh-gateway/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -295,13 +295,15 @@ fn response(
status_code,
body,
headers,
trace_id: get_current_trace_id().to_string(),
})
}

fn error(status_code: StatusCode, message: &str) -> Result<GatewayResponse, GatewayError> {
Err(GatewayError {
status_code,
message: message.to_string(),
trace_id: get_current_trace_id().to_string(),
})
}

Expand Down
5 changes: 3 additions & 2 deletions iroh-gateway/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde_json::json;
pub struct GatewayError {
pub status_code: StatusCode,
pub message: String,
pub trace_id: String,
}

impl IntoResponse for GatewayError {
Expand All @@ -16,9 +17,9 @@ impl IntoResponse for GatewayError {
"code": self.status_code.as_u16(),
"success": false,
"message": self.message,
"trace_id": "some_trace_id",
"trace_id": self.trace_id,
}));

// todo(arqu): add headers
(self.status_code, body).into_response()
}
}
11 changes: 11 additions & 0 deletions iroh-gateway/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use git_version::git_version;
use metrics::{describe_counter, Unit};

use opentelemetry::trace::{TraceContextExt, TraceId};
use tracing_opentelemetry::OpenTelemetrySpanExt;

pub fn metrics_config() -> iroh_metrics::Config {
// compile time configuration
let service_name = env!("CARGO_PKG_NAME").to_string();
Expand All @@ -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()
}
5 changes: 5 additions & 0 deletions iroh-gateway/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct GatewayResponse {
pub status_code: StatusCode,
pub body: BoxBody,
pub headers: HashMap<String, String>,
pub trace_id: String,
}

impl IntoResponse for GatewayResponse {
Expand All @@ -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()
}
}
Expand Down