Skip to content
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: 1 addition & 1 deletion NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ By [@garypen](https://github.com/garypen) in https://github.com/apollographql/ro

### Add `trace_id` in logs to identify all logs related to a specific request ([Issue #1981](https://github.com/apollographql/router/issues/1981))

It automatically adds a `trace_id` on logs to identify which log is related to a specific request. Also adds `apollo_trace_id` in response headers to help the client to identify logs for this request.
It automatically adds a `trace_id` on logs to identify which log is related to a specific request.

Example of logs in text:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,17 +427,18 @@ impl Exporter {
.map(|(header_name, value)| (header_name.to_lowercase(), Values { value }))
.collect();
// For now, only trace_id
let mut response_headers = HashMap::with_capacity(1);
response_headers.insert(
String::from("apollo_trace_id"),
Values {
value: vec![span.span_context.trace_id().to_string()],
},
);
// let mut response_headers = HashMap::with_capacity(1);
// FIXME: uncomment later
// response_headers.insert(
// String::from("apollo_trace_id"),
// Values {
// value: vec![span.span_context.trace_id().to_string()],
// },
// );
Http {
method: method.into(),
request_headers,
response_headers,
response_headers: HashMap::new(),
status_code: 0,
}
}
Expand Down
32 changes: 15 additions & 17 deletions apollo-router/src/services/supergraph_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
use std::sync::Arc;
use std::task::Poll;

use axum::headers::HeaderName;
use futures::future::BoxFuture;
use futures::stream::StreamExt;
use futures::TryFutureExt;
use http::HeaderValue;
use http::StatusCode;
use indexmap::IndexMap;
use multimap::MultiMap;
Expand Down Expand Up @@ -40,7 +38,6 @@ use crate::query_planner::CachingQueryPlanner;
use crate::router_factory::Endpoint;
use crate::router_factory::SupergraphServiceFactory;
use crate::services::layers::ensure_query_presence::EnsureQueryPresence;
use crate::tracer::TraceId;
use crate::Configuration;
use crate::Context;
use crate::ExecutionRequest;
Expand Down Expand Up @@ -103,8 +100,8 @@ where
let schema = self.schema.clone();

let context_cloned = req.context.clone();
let fut = service_call(planning, execution, schema, req)
.or_else(|error: BoxError| async move {
let fut =
service_call(planning, execution, schema, req).or_else(|error: BoxError| async move {
let errors = vec![crate::error::Error {
message: error.to_string(),
extensions: serde_json_bytes::json!({
Expand All @@ -122,19 +119,20 @@ where
.context(context_cloned)
.build()
.expect("building a response like this should not fail"))
})
.and_then(|mut res| async move {
if let Some(trace_id) = TraceId::maybe_new().map(|t| t.to_string()) {
let header_value = HeaderValue::from_str(trace_id.as_str());
if let Ok(header_value) = header_value {
res.response
.headers_mut()
.insert(HeaderName::from_static("apollo_trace_id"), header_value);
}
}

Ok(res)
});
// FIXME: Enable it later
// .and_then(|mut res| async move {
// if let Some(trace_id) = TraceId::maybe_new().map(|t| t.to_string()) {
// let header_value = HeaderValue::from_str(trace_id.as_str());
// if let Ok(header_value) = header_value {
// res.response
// .headers_mut()
// .insert(HeaderName::from_static("apollo_trace_id"), header_value);
// }
// }

// Ok(res)
// });

Box::pin(fut)
}
Expand Down