Skip to content

Commit 165abcf

Browse files
authored
Always export the _X_AMZN_TRACE_ID env variable. (#850)
This variable is expected across runtimes regardless of other features. Signed-off-by: David Calavera <david.calavera@gmail.com>
1 parent e0a3827 commit 165abcf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lambda-runtime/src/layers/trace.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::env;
21
use tower::{Layer, Service};
32
use tracing::{instrument::Instrumented, Instrument};
43

@@ -53,15 +52,13 @@ where
5352
fn request_span(ctx: &Context) -> tracing::Span {
5453
match &ctx.xray_trace_id {
5554
Some(trace_id) => {
56-
env::set_var("_X_AMZN_TRACE_ID", trace_id);
5755
tracing::info_span!(
5856
"Lambda runtime invoke",
5957
requestId = &ctx.request_id,
6058
xrayTraceId = trace_id
6159
)
6260
}
6361
None => {
64-
env::remove_var("_X_AMZN_TRACE_ID");
6562
tracing::info_span!("Lambda runtime invoke", requestId = &ctx.request_id)
6663
}
6764
}

lambda-runtime/src/runtime.rs

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use http_body_util::BodyExt;
66
use lambda_runtime_api_client::BoxError;
77
use lambda_runtime_api_client::Client as ApiClient;
88
use serde::{Deserialize, Serialize};
9+
use std::env;
910
use std::fmt::Debug;
1011
use std::future::Future;
1112
use std::sync::Arc;
@@ -176,6 +177,9 @@ where
176177
let context = Context::new(invoke_request_id(&parts.headers)?, config.clone(), &parts.headers)?;
177178
let invocation = LambdaInvocation { parts, body, context };
178179

180+
// Setup Amazon's default tracing data
181+
amzn_trace_env(&invocation.context);
182+
179183
// Wait for service to be ready
180184
let ready = service.ready().await?;
181185

@@ -232,6 +236,13 @@ fn incoming(
232236
}
233237
}
234238

239+
fn amzn_trace_env(ctx: &Context) {
240+
match &ctx.xray_trace_id {
241+
Some(trace_id) => env::set_var("_X_AMZN_TRACE_ID", trace_id),
242+
None => env::remove_var("_X_AMZN_TRACE_ID"),
243+
}
244+
}
245+
235246
/* --------------------------------------------------------------------------------------------- */
236247
/* TESTS */
237248
/* --------------------------------------------------------------------------------------------- */

0 commit comments

Comments
 (0)