Skip to content

Commit 949c002

Browse files
fix(lambda-http): source X-Ray trace id from context
- Replace env-var based X-Ray header injection with Context.xray_trace_id - Set x-amzn-trace-id in HTTP and streaming adapters before handler call - Remove legacy update_xray_trace_id_header helper from request parsing Tests: - cargo +nightly fmt --all - cargo clippy --workspace --all-features -- -D warnings - cargo +stable test --all-features -p lambda_runtime_api_client -p lambda_runtime -p lambda_http
1 parent cc4a440 commit 949c002

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

lambda-http/src/lib.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ where
192192
}
193193

194194
fn call(&mut self, req: LambdaEvent<LambdaRequest>) -> Self::Future {
195-
let request_origin = req.payload.request_origin();
196-
let event: Request = req.payload.into();
197-
let fut = Box::pin(self.service.call(event.with_lambda_context(req.context)));
195+
let LambdaEvent { payload, context } = req;
196+
let request_origin = payload.request_origin();
197+
let mut event: Request = payload.into();
198+
update_xray_trace_id_header_from_context(event.headers_mut(), &context);
199+
let fut = Box::pin(self.service.call(event.with_lambda_context(context)));
198200

199201
TransformResponse::Request(request_origin, fut)
200202
}
@@ -233,6 +235,15 @@ where
233235
lambda_runtime::run_concurrent(Adapter::from(handler)).await
234236
}
235237

238+
// Replaces update_xray_trace_id_header (env var), now set from Context
239+
fn update_xray_trace_id_header_from_context(headers: &mut http::HeaderMap, context: &Context) {
240+
if let Some(trace_id) = context.xray_trace_id.as_deref() {
241+
if let Ok(header_value) = http::HeaderValue::from_str(trace_id) {
242+
headers.insert(http::header::HeaderName::from_static("x-amzn-trace-id"), header_value);
243+
}
244+
}
245+
}
246+
236247
#[cfg(test)]
237248
mod test_adapter {
238249
use std::task::{Context, Poll};

lambda-http/src/request.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
146146
.extension(RequestContext::ApiGatewayV2(ag.request_context));
147147

148148
let mut headers = ag.headers;
149-
update_xray_trace_id_header(&mut headers);
150149
if let Some(cookies) = ag.cookies {
151150
if let Ok(header_value) = HeaderValue::from_str(&cookies.join(";")) {
152151
headers.insert(http::header::COOKIE, header_value);
@@ -170,13 +169,6 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
170169
req
171170
}
172171

173-
fn update_xray_trace_id_header(headers: &mut HeaderMap) {
174-
if let Ok(xray_trace_id) = env::var("_X_AMZN_TRACE_ID") {
175-
if let Ok(header_value) = HeaderValue::from_str(&xray_trace_id) {
176-
headers.insert(HeaderName::from_static("x-amzn-trace-id"), header_value);
177-
}
178-
}
179-
}
180172
#[cfg(feature = "apigw_rest")]
181173
fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
182174
let http_method = ag.http_method;
@@ -214,7 +206,6 @@ fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
214206
// multi-value_headers our cannoncial source of request headers
215207
let mut headers = ag.multi_value_headers;
216208
headers.extend(ag.headers);
217-
update_xray_trace_id_header(&mut headers);
218209

219210
let base64 = ag.is_base64_encoded;
220211
let mut req = builder
@@ -265,7 +256,6 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
265256
// multi-value_headers our cannoncial source of request headers
266257
let mut headers = alb.multi_value_headers;
267258
headers.extend(alb.headers);
268-
update_xray_trace_id_header(&mut headers);
269259

270260
let base64 = alb.is_base64_encoded;
271261

@@ -330,7 +320,6 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<
330320
// multi-value_headers our canonical source of request headers
331321
let mut headers = ag.multi_value_headers;
332322
headers.extend(ag.headers);
333-
update_xray_trace_id_header(&mut headers);
334323

335324
let base64 = ag.is_base64_encoded;
336325
let mut req = builder
@@ -355,8 +344,6 @@ fn into_pass_through_request(data: String) -> http::Request<Body> {
355344
let headers = builder.headers_mut().unwrap();
356345
headers.insert("Content-Type", "application/json".parse().unwrap());
357346

358-
update_xray_trace_id_header(headers);
359-
360347
let raw_path = "/events";
361348

362349
builder

lambda-http/src/streaming.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::{http::header::SET_COOKIE, request::LambdaRequest, Request, RequestExt};
1+
use crate::{
2+
http::header::SET_COOKIE, request::LambdaRequest, update_xray_trace_id_header_from_context, Request, RequestExt,
3+
};
24
use bytes::Bytes;
35
use core::{
46
fmt::Debug,
@@ -60,10 +62,12 @@ where
6062
}
6163

6264
fn call(&mut self, req: LambdaEvent<LambdaRequest>) -> Self::Future {
63-
let event: Request = req.payload.into();
65+
let LambdaEvent { payload, context } = req;
66+
let mut event: Request = payload.into();
67+
update_xray_trace_id_header_from_context(event.headers_mut(), &context);
6468
Box::pin(
6569
self.service
66-
.call(event.with_lambda_context(req.context))
70+
.call(event.with_lambda_context(context))
6771
.map_ok(into_stream_response),
6872
)
6973
}
@@ -148,8 +152,10 @@ where
148152
}
149153

150154
fn event_to_request(req: LambdaEvent<LambdaRequest>) -> Request {
151-
let event: Request = req.payload.into();
152-
event.with_lambda_context(req.context)
155+
let LambdaEvent { payload, context } = req;
156+
let mut event: Request = payload.into();
157+
update_xray_trace_id_header_from_context(event.headers_mut(), &context);
158+
event.with_lambda_context(context)
153159
}
154160

155161
/// Runs the Lambda runtime with a handler that returns **streaming** HTTP

0 commit comments

Comments
 (0)