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
59 changes: 42 additions & 17 deletions bottlecap/src/lifecycle/invocation/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,7 @@ impl Processor {
pub fn on_invocation_start(&mut self, headers: HashMap<String, String>, payload: Vec<u8>) {
self.tracer_detected = true;

let payload_value = match serde_json::from_slice::<Value>(&payload) {
Ok(value) => value,
Err(_) => json!({}),
};
let payload_value = serde_json::from_slice::<Value>(&payload).unwrap_or_else(|_| json!({}));

// Tag the invocation span with the request payload
if self.config.capture_lambda_payload {
Expand Down Expand Up @@ -474,10 +471,7 @@ impl Processor {
/// Given trace context information, set it to the current span.
///
pub fn on_invocation_end(&mut self, headers: HashMap<String, String>, payload: Vec<u8>) {
let payload_value = match serde_json::from_slice::<Value>(&payload) {
Ok(value) => value,
Err(_) => json!({}),
};
let payload_value = serde_json::from_slice::<Value>(&payload).unwrap_or_else(|_| json!({}));

// Tag the invocation span with the request payload
if self.config.capture_lambda_payload {
Expand All @@ -490,17 +484,19 @@ impl Processor {
);
}

if let Some(status_code) = payload_value.get("statusCode").and_then(Value::as_str) {
self.span
.meta
.insert("http.status_code".to_string(), status_code.to_string());
if let Some(status_code) = payload_value.get("statusCode").and_then(Value::as_i64) {
let status_code_as_string = status_code.to_string();
self.span.meta.insert(
"http.status_code".to_string(),
status_code_as_string.clone(),
);

if status_code.len() == 3 && status_code.starts_with('5') {
if status_code_as_string.len() == 3 && status_code_as_string.starts_with('5') {
self.span.error = 1;
}

// If we have an inferred span, set the status code to it
self.inferrer.set_status_code(status_code.to_string());
self.inferrer.set_status_code(status_code_as_string);
}

self.update_span_context_from_headers(&headers);
Expand Down Expand Up @@ -738,7 +734,7 @@ mod tests {

assert_eq!(p.span.trace_id, 999);
assert_eq!(p.span.parent_id, 1000);
let priority = p.span.metrics.get(TAG_SAMPLING_PRIORITY).cloned();
let priority = p.span.metrics.get(TAG_SAMPLING_PRIORITY).copied();
assert_eq!(priority, Some(-1.0));
}

Expand All @@ -756,7 +752,7 @@ mod tests {

p.update_span_context_from_headers(&headers);

assert!(p.span.metrics.get(TAG_SAMPLING_PRIORITY).is_none());
assert!(!p.span.metrics.contains_key(TAG_SAMPLING_PRIORITY));
assert_eq!(p.span.trace_id, 888);
assert_eq!(p.span.parent_id, 999);
}
Expand All @@ -771,8 +767,37 @@ mod tests {

p.update_span_context_from_headers(&headers);

assert!(p.span.metrics.get(TAG_SAMPLING_PRIORITY).is_none());
assert!(!p.span.metrics.contains_key(TAG_SAMPLING_PRIORITY));
assert_eq!(p.span.trace_id, 111);
assert_eq!(p.span.parent_id, 222);
}

#[test]
fn parsing_status_code() {
let mut p = setup();

let response = r#"
{
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"isBase64Encoded": false,
"multiValueHeaders": {
"X-Custom-Header": ["My value", "My other value"]
},
"body": "{\n \"TotalCodeSize\": 104330022,\n \"FunctionCount\": 26\n}"
}
"#;

p.on_invocation_end(HashMap::new(), response.as_bytes().to_vec());

assert_eq!(
p.span
.meta
.get("http.status_code")
.expect("Status code not parsed!"),
"200"
);
}
}
19 changes: 9 additions & 10 deletions bottlecap/src/tags/lambda/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,16 +402,15 @@ mod tests {
let tags = Lambda::new_from_config(config, &metadata);
let function_tags = tags.get_function_tags_map();
assert_eq!(function_tags.len(), 1);
let fn_tags_map: hash_map::HashMap<String, String> = hash_map::HashMap::from_iter(
function_tags
.get(FUNCTION_TAGS_KEY)
.unwrap()
.split(',')
.map(|tag| {
let parts = tag.split(':').collect::<Vec<&str>>();
(parts[0].to_string(), parts[1].to_string())
}),
);
let fn_tags_map: hash_map::HashMap<String, String> = function_tags
.get(FUNCTION_TAGS_KEY)
.unwrap()
.split(',')
.map(|tag| {
let parts = tag.split(':').collect::<Vec<&str>>();
(parts[0].to_string(), parts[1].to_string())
})
.collect();
assert_eq!(fn_tags_map.len(), 14);
assert_eq!(fn_tags_map.get("key1").unwrap(), "value1");
assert_eq!(fn_tags_map.get("key2").unwrap(), "value2");
Expand Down
1 change: 1 addition & 0 deletions bottlecap/src/traces/span_pointers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod tests {
}

#[test]
#[allow(clippy::too_many_lines)]
fn test_attach_span_pointers_to_span() {
let test_cases = vec![
SpanPointerTestCase {
Expand Down
Loading