Skip to content

Commit 81ceae7

Browse files
Fix negative duration on aws.apigateway.authorizer span
1 parent c43d2b2 commit 81ceae7

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

datadog_lambda/tracing.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -974,19 +974,11 @@ def process_injected_data(event, request_time_epoch_ms, args, tags):
974974
injected_authorizer_data = get_injected_authorizer_data(event, False)
975975
if injected_authorizer_data:
976976
try:
977-
start_time_ns = int(
978-
injected_authorizer_data.get(Headers.Parent_Span_Finish_Time)
977+
start_time_ns = int(injected_authorizer_data.get(Headers.Parent_Span_Finish_Time))
978+
integration_latency = int(
979+
event["requestContext"]["authorizer"].get("integrationLatency", 0)
979980
)
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
981+
finish_time_ns = max(start_time_ns, (request_time_epoch_ms + integration_latency) * 1e6)
990982
upstream_authorizer_span = insert_upstream_authorizer_span(
991983
args, tags, start_time_ns, finish_time_ns
992984
)

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)