Skip to content

Commit 3883ffd

Browse files
fix: negative duration on aws.apigateway.authorizer span (#782)
1 parent c43d2b2 commit 3883ffd

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

datadog_lambda/tracing.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -977,16 +977,12 @@ def process_injected_data(event, request_time_epoch_ms, args, tags):
977977
start_time_ns = int(
978978
injected_authorizer_data.get(Headers.Parent_Span_Finish_Time)
979979
)
980-
finish_time_ns = (
981-
request_time_epoch_ms
982-
+ (
983-
int(
984-
event["requestContext"]["authorizer"].get(
985-
"integrationLatency", 0
986-
)
987-
)
988-
)
989-
) * 1e6
980+
integration_latency = int(
981+
event["requestContext"]["authorizer"].get("integrationLatency", 0)
982+
)
983+
finish_time_ns = max(
984+
start_time_ns, (request_time_epoch_ms + integration_latency) * 1e6
985+
)
990986
upstream_authorizer_span = insert_upstream_authorizer_span(
991987
args, tags, start_time_ns, finish_time_ns
992988
)

tests/event_samples/authorizer-request-api-gateway-v1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"requestContext": {
5656
"resourceId": "0et54l",
5757
"authorizer": {
58-
"_datadog": "eyJ4LWRhdGFkb2ctdHJhY2UtaWQiOiAiMTM0Nzg3MDU5OTU3OTcyMjEyMDkiLCAieC1kYXRhZG9nLXBhcmVudC1pZCI6ICI4NDcxMjg4MjYzMzg0MjE2ODk2IiwgIngtZGF0YWRvZy1zYW1wbGluZy1wcmlvcml0eSI6ICIxIiwgIngtZGF0YWRvZy1wYXJlbnQtc3Bhbi1maW5pc2gtdGltZSI6IDE2NjMyOTUwMjE4MjcuNTIxLCAieC1kYXRhZG9nLWF1dGhvcml6aW5nLXJlcXVlc3RpZCI6ICJhYmMxMjMifQ==",
58+
"_datadog": "eyJ4LWRhdGFkb2ctdHJhY2UtaWQiOiAiMTM0Nzg3MDU5OTU3OTcyMjEyMDkiLCAieC1kYXRhZG9nLXBhcmVudC1pZCI6ICI4NDcxMjg4MjYzMzg0MjE2ODk2IiwgIngtZGF0YWRvZy1zYW1wbGluZy1wcmlvcml0eSI6ICIxIiwgIngtZGF0YWRvZy1wYXJlbnQtc3Bhbi1maW5pc2gtdGltZSI6IDE2NjMyOTUwMjE4Mjc1MjEwMDAsICJ4LWRhdGFkb2ctYXV0aG9yaXppbmctcmVxdWVzdGlkIjogImFiYzEyMyJ9",
5959
"scope": "this is just a string",
6060
"principalId": "foo",
6161
"integrationLatency": 1897

tests/test_tracing.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,6 +2408,38 @@ def test_create_inferred_span(mock_span_finish, source, expect):
24082408
assert mock_span_finish.call_count == 0
24092409

24102410

2411+
@patch("ddtrace.trace.Span.finish", autospec=True)
2412+
def test_authorizer_span_no_negative_duration_when_clock_skew(mock_span_finish):
2413+
"""
2414+
Simulate the case where requestTimeEpoch + integrationLatency < parentSpanFinishTime
2415+
(i.e., API Gateway's reported end is before the authorizer lambda's self-reported finish).
2416+
This 1ms discrepancy would produce a negative duration.
2417+
"""
2418+
with open(f"{event_samples}authorizer-request-api-gateway-v1.json") as f:
2419+
event = json.load(f)
2420+
2421+
authorizer_ctx = json.loads(
2422+
base64.b64decode(event["requestContext"]["authorizer"]["_datadog"])
2423+
)
2424+
parent_finish_time_ns = authorizer_ctx["x-datadog-parent-span-finish-time"]
2425+
request_time_epoch_ms = event["requestContext"]["requestTimeEpoch"]
2426+
# Set integrationLatency 1ms below the parent span's finish time to simulate clock skew.
2427+
event["requestContext"]["authorizer"]["integrationLatency"] = (
2428+
parent_finish_time_ns // 1_000_000 - request_time_epoch_ms - 1
2429+
)
2430+
2431+
ctx = get_mock_context(aws_request_id="123")
2432+
create_inferred_span(event, ctx)
2433+
2434+
args = mock_span_finish.call_args_list[0].args
2435+
authorizer_span, finish_time = args[0], args[1]
2436+
2437+
assert finish_time >= authorizer_span.start, (
2438+
f"Authorizer span has negative duration: "
2439+
f"finish_time={finish_time} < start={authorizer_span.start}"
2440+
)
2441+
2442+
24112443
class TestInferredSpans(unittest.TestCase):
24122444
@patch("datadog_lambda.tracing.submit_errors_metric")
24132445
def test_mark_trace_as_error_for_5xx_responses_getting_400_response_code(

0 commit comments

Comments
 (0)