Skip to content

Commit d739445

Browse files
authored
Panic when X-Ray header not present (#91)
Changed the X-Ray trace id header to be optional since X-Ray is not support in GovCloud. This addresses issue #89. Also bumped version for patch release.
1 parent 3b796f3 commit d739445

File tree

7 files changed

+77
-15
lines changed

7 files changed

+77
-15
lines changed

Cargo.lock

Lines changed: 8 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lambda-runtime-client/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lambda_runtime_client"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = ["Stefano Buliani", "David Barsky"]
55
edition = "2018"
66
description = "Client SDK for AWS Lambda's runtime APIs"
@@ -24,4 +24,7 @@ serde_json = "^1"
2424
serde_derive = "^1"
2525
log = "0.4"
2626
lambda_runtime_errors = { path = "../lambda-runtime-errors", version = "^0.1" }
27-
failure = "^0.1"
27+
failure = "^0.1"
28+
29+
[dev-dependencies]
30+
chrono = "^0.4"

lambda-runtime-client/src/client.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub struct EventContext {
109109
/// The AWS request ID generated by the Lambda service.
110110
pub aws_request_id: String,
111111
/// The X-Ray trace ID for the current invocation.
112-
pub xray_trace_id: String,
112+
pub xray_trace_id: Option<String>,
113113
/// The execution deadline for the current invocation in milliseconds.
114114
pub deadline: i64,
115115
/// The client context object sent by the AWS mobile SDK. This field is
@@ -404,7 +404,17 @@ impl<'ev> RuntimeClient {
404404
headers.get(LambdaHeaders::FunctionArn.as_str()),
405405
&LambdaHeaders::FunctionArn,
406406
)?;
407-
let xray_trace_id = header_string(headers.get(LambdaHeaders::TraceId.as_str()), &LambdaHeaders::TraceId)?;
407+
let xray_trace_id = match headers.get(LambdaHeaders::TraceId.as_str()) {
408+
Some(trace_id) => match trace_id.to_str() {
409+
Ok(trace_str) => Some(trace_str.to_owned()),
410+
Err(e) => {
411+
// we do not fail on this error.
412+
error!("Could not parse X-Ray trace id as string: {}", e);
413+
None
414+
}
415+
},
416+
None => None,
417+
};
408418
let deadline = header_string(headers.get(LambdaHeaders::Deadline.as_str()), &LambdaHeaders::Deadline)?
409419
.parse::<i64>()
410420
.context(ApiErrorKind::Recoverable(
@@ -461,3 +471,49 @@ fn header_string(value: Option<&HeaderValue>, header_type: &LambdaHeaders) -> Re
461471
}
462472
}
463473
}
474+
475+
#[cfg(test)]
476+
pub(crate) mod tests {
477+
use super::*;
478+
use chrono::{Duration, Utc};
479+
480+
fn get_headers() -> HeaderMap<HeaderValue> {
481+
let mut headers: HeaderMap<HeaderValue> = HeaderMap::new();
482+
headers.insert(
483+
LambdaHeaders::RequestId.as_str(),
484+
HeaderValue::from_str("req_id").unwrap(),
485+
);
486+
headers.insert(
487+
LambdaHeaders::FunctionArn.as_str(),
488+
HeaderValue::from_str("func_arn").unwrap(),
489+
);
490+
headers.insert(LambdaHeaders::TraceId.as_str(), HeaderValue::from_str("trace").unwrap());
491+
let deadline = Utc::now() + Duration::seconds(10);
492+
headers.insert(
493+
LambdaHeaders::Deadline.as_str(),
494+
HeaderValue::from_str(&deadline.timestamp_millis().to_string()).unwrap(),
495+
);
496+
headers
497+
}
498+
499+
#[test]
500+
fn get_event_context_with_empty_trace_id() {
501+
let client = RuntimeClient::new("localhost:8081", None, None).expect("Could not initialize runtime client");
502+
let mut headers = get_headers();
503+
headers.remove(LambdaHeaders::TraceId.as_str());
504+
let headers_result = client.get_event_context(&headers);
505+
assert_eq!(false, headers_result.is_err());
506+
let ok_result = headers_result.unwrap();
507+
assert_eq!(None, ok_result.xray_trace_id);
508+
assert_eq!("req_id", ok_result.aws_request_id);
509+
}
510+
511+
#[test]
512+
fn get_event_context_populates_trace_id_when_present() {
513+
let client = RuntimeClient::new("localhost:8081", None, None).expect("Could not initialize runtime client");
514+
let headers = get_headers();
515+
let headers_result = client.get_event_context(&headers);
516+
assert_eq!(false, headers_result.is_err());
517+
assert_eq!(Some("trace".to_owned()), headers_result.unwrap().xray_trace_id);
518+
}
519+
}

lambda-runtime-client/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl ErrorResponse {
6060
err.stack_trace = Some(
6161
format!("{:?}", stack)
6262
.lines()
63-
.map(|s| s.to_string())
63+
.map(std::string::ToString::to_string)
6464
.collect::<Vec<String>>(),
6565
);
6666
trace!("Completed backtrace collection");

lambda-runtime-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lambda_runtime_core"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["Stefano Buliani", "David Barsky"]
55
description = "Rust runtime for AWS Lambda"
66
keywords = ["AWS", "Lambda", "Runtime", "Rust"]

lambda-runtime-core/src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub struct Context {
3232
/// by the Lambda Runtime APIs as a header. Developers can use this value
3333
/// with the AWS SDK to create new, custom sub-segments to the current
3434
/// invocation.
35-
pub xray_trace_id: String,
35+
pub xray_trace_id: Option<String>,
3636
/// The name of the CloudWatch log stream for the current execution
3737
/// environment. This value is extracted from the `AWS_LAMBDA_LOG_STREAM_NAME`
3838
/// environment variable set by the Lambda service.
@@ -72,7 +72,7 @@ impl Context {
7272
/// A new, populated `Context` object.
7373
pub(super) fn new(local_settings: lambda_env::FunctionSettings) -> Context {
7474
Context {
75-
xray_trace_id: String::from(""),
75+
xray_trace_id: None,
7676
memory_limit_in_mb: local_settings.memory_size,
7777
function_name: local_settings.function_name,
7878
function_version: local_settings.version,
@@ -107,7 +107,7 @@ pub(crate) mod tests {
107107
function_version: "$LATEST".to_string(),
108108
invoked_function_arn: "arn:aws:lambda".to_string(),
109109
aws_request_id: "123".to_string(),
110-
xray_trace_id: "123".to_string(),
110+
xray_trace_id: Some("123".to_string()),
111111
log_stream_name: "logStream".to_string(),
112112
log_group_name: "logGroup".to_string(),
113113
client_context: Option::default(),

lambda-runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub(crate) mod tests {
142142
function_version: "$LATEST".to_string(),
143143
invoked_function_arn: "arn:aws:lambda".to_string(),
144144
aws_request_id: "123".to_string(),
145-
xray_trace_id: "123".to_string(),
145+
xray_trace_id: Some("123".to_string()),
146146
log_stream_name: "logStream".to_string(),
147147
log_group_name: "logGroup".to_string(),
148148
client_context: Option::default(),

0 commit comments

Comments
 (0)