Skip to content

Commit 1fc2ccb

Browse files
authored
add x-ray trace id header in lambda-http (#557)
* fix for Github issue #552 * Github issue #552 fmt check * github issue #552 apply clippy fix * remove unnessecory comment
1 parent f8706e3 commit 1fc2ccb

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

lambda-http/src/request.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ use aws_lambda_events::apigw::{ApiGatewayV2httpRequest, ApiGatewayV2httpRequestC
1616
use aws_lambda_events::apigw::{ApiGatewayWebsocketProxyRequest, ApiGatewayWebsocketProxyRequestContext};
1717
use aws_lambda_events::{encodings::Body, query_map::QueryMap};
1818
use http::header::HeaderName;
19-
use http::HeaderMap;
19+
use http::{HeaderMap, HeaderValue};
2020
use serde::Deserialize;
2121
use serde_json::error::Error as JsonError;
2222
use std::future::Future;
2323
use std::pin::Pin;
24-
use std::{io::Read, mem};
24+
use std::{env, io::Read, mem};
2525
use url::Url;
2626

2727
/// Internal representation of an Lambda http event from
@@ -119,8 +119,9 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
119119
.extension(RequestContext::ApiGatewayV2(ag.request_context));
120120

121121
let mut headers = ag.headers;
122+
update_xray_trace_id_header(&mut headers);
122123
if let Some(cookies) = ag.cookies {
123-
if let Ok(header_value) = http::header::HeaderValue::from_str(&cookies.join(";")) {
124+
if let Ok(header_value) = HeaderValue::from_str(&cookies.join(";")) {
124125
headers.append(http::header::COOKIE, header_value);
125126
}
126127
}
@@ -142,6 +143,13 @@ fn into_api_gateway_v2_request(ag: ApiGatewayV2httpRequest) -> http::Request<Bod
142143
req
143144
}
144145

146+
fn update_xray_trace_id_header(headers: &mut HeaderMap) {
147+
if let Ok(xray_trace_id) = env::var("_X_AMZN_TRACE_ID") {
148+
if let Ok(header_value) = HeaderValue::from_str(&xray_trace_id) {
149+
headers.insert(HeaderName::from_static("x-amzn-trace-id"), header_value);
150+
}
151+
}
152+
}
145153
#[cfg(feature = "apigw_rest")]
146154
fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
147155
let http_method = ag.http_method;
@@ -179,6 +187,7 @@ fn into_proxy_request(ag: ApiGatewayProxyRequest) -> http::Request<Body> {
179187
// multi-value_headers our cannoncial source of request headers
180188
let mut headers = ag.multi_value_headers;
181189
headers.extend(ag.headers);
190+
update_xray_trace_id_header(&mut headers);
182191

183192
let base64 = ag.is_base64_encoded.unwrap_or_default();
184193
let mut req = builder
@@ -229,6 +238,7 @@ fn into_alb_request(alb: AlbTargetGroupRequest) -> http::Request<Body> {
229238
// multi-value_headers our cannoncial source of request headers
230239
let mut headers = alb.multi_value_headers;
231240
headers.extend(alb.headers);
241+
update_xray_trace_id_header(&mut headers);
232242

233243
let base64 = alb.is_base64_encoded;
234244

@@ -291,6 +301,7 @@ fn into_websocket_request(ag: ApiGatewayWebsocketProxyRequest) -> http::Request<
291301
// multi-value_headers our cannoncial source of request headers
292302
let mut headers = ag.multi_value_headers;
293303
headers.extend(ag.headers);
304+
update_xray_trace_id_header(&mut headers);
294305

295306
let base64 = ag.is_base64_encoded.unwrap_or_default();
296307
let mut req = builder

lambda-runtime/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ where
134134
let request_id = &ctx.request_id.clone();
135135

136136
let xray_trace_id = &ctx.xray_trace_id.clone();
137-
env::set_var("_X_AMZN_TRACE_ID", xray_trace_id);
138-
137+
match xray_trace_id {
138+
Some(trace_id) => env::set_var("_X_AMZN_TRACE_ID", trace_id),
139+
None => env::remove_var("_X_AMZN_TRACE_ID"),
140+
}
139141
let body = match serde_json::from_slice(&body) {
140142
Ok(body) => body,
141143
Err(err) => {

lambda-runtime/src/types.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub struct Context {
9090
/// The ARN of the Lambda function being invoked.
9191
pub invoked_function_arn: String,
9292
/// The X-Ray trace ID for the current invocation.
93-
pub xray_trace_id: String,
93+
pub xray_trace_id: Option<String>,
9494
/// The client context object sent by the AWS mobile SDK. This field is
9595
/// empty unless the function is invoked using an AWS mobile SDK.
9696
pub client_context: Option<ClientContext>,
@@ -139,9 +139,7 @@ impl TryFrom<HeaderMap> for Context {
139139
.to_owned(),
140140
xray_trace_id: headers
141141
.get("lambda-runtime-trace-id")
142-
.unwrap_or(&HeaderValue::from_static(""))
143-
.to_str()?
144-
.to_owned(),
142+
.map(|v| String::from_utf8_lossy(v.as_bytes()).to_string()),
145143
client_context,
146144
identity,
147145
..Default::default()

0 commit comments

Comments
 (0)