-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect exceptions raised outside of the handler (#475)
* emit enhanced error metric and create span when an exception is raised outside of the handler function * rename handler * move fallback handler to wrapper.py * fix duration * respect DD_TRACE_ENABLED * emit telemetry and raise during init * Update datadog_lambda/tags.py Co-authored-by: jordan gonzález <30836115+duncanista@users.noreply.github.com> --------- Co-authored-by: jordan gonzález <30836115+duncanista@users.noreply.github.com>
- Loading branch information
1 parent
3c79531
commit a95341e
Showing
7 changed files
with
198 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import os | ||
import sys | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from tests.utils import get_mock_context | ||
|
||
|
||
class TestHandler(unittest.TestCase): | ||
def tearDown(self): | ||
for mod in sys.modules.copy(): | ||
if mod.startswith("datadog_lambda.handler"): | ||
del sys.modules[mod] | ||
|
||
def test_dd_lambda_handler_env_var_none(self): | ||
with self.assertRaises(Exception) as context: | ||
import datadog_lambda.handler as handler | ||
|
||
assert context.exception == handler.HandlerError( | ||
"DD_LAMBDA_HANDLER is not defined. Can't use prebuilt datadog handler" | ||
) | ||
|
||
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "malformed"}, clear=True) | ||
def test_dd_lambda_handler_env_var_malformed(self): | ||
with self.assertRaises(Exception) as context: | ||
import datadog_lambda.handler as handler | ||
|
||
assert context.exception == handler.HandlerError( | ||
"Value malformed for DD_LAMBDA_HANDLER has invalid format." | ||
) | ||
|
||
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True) | ||
@patch("datadog_lambda.tracing.emit_telemetry_on_exception_outside_of_handler") | ||
@patch("time.time_ns", return_value=42) | ||
def test_exception_importing_module(self, mock_time, mock_emit_telemetry): | ||
with self.assertRaises(ModuleNotFoundError) as test_context: | ||
import datadog_lambda.handler | ||
|
||
mock_emit_telemetry.assert_called_once_with( | ||
test_context.exception, "nonsense", 42 | ||
) | ||
|
||
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True) | ||
@patch("importlib.import_module", return_value=None) | ||
@patch("datadog_lambda.tracing.emit_telemetry_on_exception_outside_of_handler") | ||
@patch("time.time_ns", return_value=42) | ||
def test_exception_getting_handler_func( | ||
self, mock_time, mock_emit_telemetry, mock_import | ||
): | ||
with self.assertRaises(AttributeError) as test_context: | ||
import datadog_lambda.handler | ||
|
||
mock_emit_telemetry.assert_called_once_with( | ||
test_context.exception, "nonsense", 42 | ||
) | ||
|
||
@patch.dict(os.environ, {"DD_LAMBDA_HANDLER": "nonsense.nonsense"}, clear=True) | ||
@patch("importlib.import_module") | ||
@patch("datadog_lambda.tracing.emit_telemetry_on_exception_outside_of_handler") | ||
@patch("datadog_lambda.wrapper.datadog_lambda_wrapper") | ||
def test_handler_success( | ||
self, mock_lambda_wrapper, mock_emit_telemetry, mock_import | ||
): | ||
def nonsense(): | ||
pass | ||
|
||
mock_import.nonsense.return_value = nonsense | ||
|
||
import datadog_lambda.handler | ||
|
||
mock_emit_telemetry.assert_not_called() | ||
mock_lambda_wrapper.assert_called_once_with(mock_import().nonsense) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters