Skip to content

Commit 4e283ca

Browse files
committed
Expand X-Ray unit coverage
1 parent 9af125b commit 4e283ca

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

scripts/test-unit.sh

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ bootstrap="$root/runtime/bootstrap"
66

77
case_name=${1:-}
88
if [ -z "$case_name" ]; then
9-
printf '%s\n' "Usage: $0 <missing-handler-file|missing-handler-function|unreadable-handler|handler-exit|handler-exit-stderr|handler-exit-escape|response-post-failure|error-post-failure|large-payload|env-var-cleanup|xray-segment-log|xray-segment-log-disabled|streaming-response>" >&2
9+
printf '%s\n' "Usage: $0 <missing-handler-file|missing-handler-function|unreadable-handler|handler-exit|handler-exit-stderr|handler-exit-escape|response-post-failure|error-post-failure|large-payload|env-var-cleanup|xray-trace-env|xray-segment-log|xray-segment-unsampled|xray-segment-log-disabled|streaming-response>" >&2
1010
exit 2
1111
fi
1212

@@ -436,6 +436,40 @@ HANDLER
436436
exit 1
437437
fi
438438
;;
439+
xray-trace-env)
440+
cat <<'HANDLER' > "$workdir/function.sh"
441+
handler() {
442+
log_file="${LAMBDA_TASK_ROOT}/env-log"
443+
if [ "${_X_AMZN_TRACE_ID+x}" = "x" ]; then
444+
printf '%s\n' "$_X_AMZN_TRACE_ID" >> "$log_file"
445+
else
446+
printf '%s\n' "UNSET" >> "$log_file"
447+
fi
448+
printf '%s\n' '{"ok":true}'
449+
}
450+
HANDLER
451+
printf '{"message":"ok"}' > "$event_file"
452+
trace_id="Root=1-abcdef01-234567890abcdef01234567;Parent=1234;Sampled=1"
453+
MOCK_NEXT_TRACE_ID="$trace_id"
454+
MOCK_NEXT_TRACE_ID_ONCE=1
455+
MOCK_NEXT_COUNT_FILE="$workdir/next-count"
456+
run_bootstrap "function.handler"
457+
env_log="$workdir/env-log"
458+
if ! wait_for_lines "$env_log" 2; then
459+
printf '%s\n' "Expected env log to contain two invocations" >&2
460+
exit 1
461+
fi
462+
line1=$(sed -n '1p' "$env_log")
463+
line2=$(sed -n '2p' "$env_log")
464+
if [ "$line1" != "$trace_id" ]; then
465+
printf '%s\n' "Expected first invocation _X_AMZN_TRACE_ID to be set" >&2
466+
exit 1
467+
fi
468+
if [ "$line2" != "UNSET" ]; then
469+
printf '%s\n' "Expected _X_AMZN_TRACE_ID to be unset on second invocation" >&2
470+
exit 1
471+
fi
472+
;;
439473
xray-segment-log)
440474
cat <<'HANDLER' > "$workdir/function.sh"
441475
handler() {
@@ -451,6 +485,23 @@ HANDLER
451485
assert_log_contains "X-Ray segment:"
452486
assert_log_contains "1-abcdef01-234567890abcdef01234567"
453487
;;
488+
xray-segment-unsampled)
489+
cat <<'HANDLER' > "$workdir/function.sh"
490+
handler() {
491+
printf '%s\n' '{"ok":true}'
492+
}
493+
HANDLER
494+
printf '{"message":"ok"}' > "$event_file"
495+
trace_id="Root=1-abcdef01-234567890abcdef01234567;Parent=1234;Sampled=0"
496+
MOCK_NEXT_TRACE_ID="$trace_id"
497+
MOCK_XRAY_DAEMON_ADDRESS="127.0.0.1:2000"
498+
run_bootstrap "function.handler"
499+
wait_for_file "$response_file"
500+
if grep -F "X-Ray segment:" "$log_file" >/dev/null 2>&1; then
501+
printf '%s\n' "Expected X-Ray segment log to be skipped for unsampled trace" >&2
502+
exit 1
503+
fi
504+
;;
454505
xray-segment-log-disabled)
455506
cat <<'HANDLER' > "$workdir/function.sh"
456507
handler() {

spec/test-unit_spec.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,21 @@ Describe 'unit tests'
4949
The status should be success
5050
End
5151

52+
It 'propagates _X_AMZN_TRACE_ID per invocation'
53+
When run ./scripts/test-unit.sh xray-trace-env
54+
The status should be success
55+
End
56+
5257
It 'logs X-Ray segment when sampled'
5358
When run ./scripts/test-unit.sh xray-segment-log
5459
The status should be success
5560
End
5661

62+
It 'skips X-Ray segment log when unsampled'
63+
When run ./scripts/test-unit.sh xray-segment-unsampled
64+
The status should be success
65+
End
66+
5767
It 'skips X-Ray segment log without daemon address'
5868
When run ./scripts/test-unit.sh xray-segment-log-disabled
5969
The status should be success

0 commit comments

Comments
 (0)