Skip to content

Commit f0e8c5e

Browse files
committed
fix duration
1 parent 64af962 commit f0e8c5e

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

datadog_lambda/handler.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from importlib import import_module
88

99
import os
10-
import time
10+
from time import time_ns
1111

1212
from datadog_lambda.wrapper import datadog_lambda_wrapper, error_fallback_handler
1313
from datadog_lambda.module_name import modify_module_name
@@ -31,9 +31,11 @@ class HandlerError(Exception):
3131
modified_mod_name = modify_module_name(mod_name)
3232

3333
try:
34-
start_time_ns = time.time_ns()
34+
handler_load_start_time_ns = time_ns()
3535
handler_module = import_module(modified_mod_name)
3636
handler_func = getattr(handler_module, handler_name)
3737
handler = datadog_lambda_wrapper(handler_func)
3838
except Exception as e:
39-
handler = error_fallback_handler(e, modified_mod_name, start_time_ns)
39+
handler = error_fallback_handler(
40+
e, modified_mod_name, time_ns() - handler_load_start_time_ns
41+
)

datadog_lambda/tracing.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import traceback
1010
import ujson as json
1111
from datetime import datetime, timezone
12+
from time import time_ns
1213
from typing import Optional, Dict
1314

1415
from datadog_lambda.metric import submit_errors_metric
@@ -1324,7 +1325,7 @@ def is_async(span: Span) -> bool:
13241325

13251326

13261327
def emit_telemetry_on_exception_outside_of_handler(
1327-
context, exception, resource_name, start_time_ns
1328+
context, exception, resource_name, handler_load_duration
13281329
):
13291330
"""
13301331
Emit an enhanced error metric and create a span for exceptions occuring outside of the handler
@@ -1337,7 +1338,8 @@ def emit_telemetry_on_exception_outside_of_handler(
13371338
resource=resource_name,
13381339
span_type="serverless",
13391340
)
1340-
span.start_ns = start_time_ns
1341+
span.start_ns = time_ns() - handler_load_duration
1342+
13411343
tags = {
13421344
"error.status": 500,
13431345
"error.type": type(exception).__name__,

datadog_lambda/wrapper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,17 +389,17 @@ class _ErrorFallbackHandler(object):
389389
Emits telemetry and re-raises the exception.
390390
"""
391391

392-
def __init__(self, exception, modified_mod_name, start_time_ns):
392+
def __init__(self, exception, modified_mod_name, handler_load_duration_ns):
393393
self.exception = exception
394394
self.modified_mod_name = modified_mod_name
395-
self.start_time_ns = start_time_ns
395+
self.handler_load_duration_ns = handler_load_duration_ns
396396

397397
def __call__(self, event, context, **kwargs):
398398
emit_telemetry_on_exception_outside_of_handler(
399399
context,
400400
self.exception,
401401
self.modified_mod_name,
402-
self.start_time_ns,
402+
self.handler_load_duration_ns,
403403
)
404404
raise self.exception
405405

tests/test_handler.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_exception_importing_module(self, mock_time, mock_emit_telemetry):
3939
lambda_context = get_mock_context()
4040
datadog_lambda.handler.handler.__call__(None, lambda_context)
4141
mock_emit_telemetry.assert_called_once_with(
42-
lambda_context, test_context.exception, "nonsense", 42
42+
lambda_context, test_context.exception, "nonsense", 0
4343
)
4444

4545
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True)
@@ -55,16 +55,15 @@ def test_exception_getting_handler_func(
5555
lambda_context = get_mock_context()
5656
datadog_lambda.handler.handler.__call__(None, lambda_context)
5757
mock_emit_telemetry.assert_called_once_with(
58-
lambda_context, test_context.exception, "nonsense", 42
58+
lambda_context, test_context.exception, "nonsense", 0
5959
)
6060

6161
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True)
6262
@patch("importlib.import_module")
6363
@patch("datadog_lambda.wrapper.emit_telemetry_on_exception_outside_of_handler")
64-
@patch("time.time_ns", return_value=42)
6564
@patch("datadog_lambda.wrapper.datadog_lambda_wrapper")
6665
def test_handler_success(
67-
self, mock_lambda_wrapper, mock_time, mock_emit_telemetry, mock_import
66+
self, mock_lambda_wrapper, mock_emit_telemetry, mock_import
6867
):
6968
def nonsense():
7069
pass

tests/test_tracing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2005,7 +2005,8 @@ def test_deterministic_m5_hash__always_leading_with_zero(self):
20052005

20062006
class TestExceptionOutsideHandler(unittest.TestCase):
20072007
@patch("datadog_lambda.tracing.submit_errors_metric")
2008-
def test_exception_outside_handler(self, mock_submit_errors_metric):
2008+
@patch("datadog_lambda.tracing.time_ns", return_value=100)
2009+
def test_exception_outside_handler(self, mock_time, mock_submit_errors_metric):
20092010
fake_error = ValueError("Some error message")
20102011
resource_name = "my_handler"
20112012
span_type = "aws.lambda"
@@ -2045,4 +2046,4 @@ def test_exception_outside_handler(self, mock_submit_errors_metric):
20452046
)
20462047
mock_span.finish.assert_called_once()
20472048
assert mock_span.error == 1
2048-
assert mock_span.start_ns == 42
2049+
assert mock_span.start_ns == 58

0 commit comments

Comments
 (0)