|
| 1 | +import os |
1 | 2 | import time
|
2 | 3 | import unittest
|
3 |
| -import datadog_lambda.cold_start as cold_start |
| 4 | + |
4 | 5 | from sys import modules, meta_path
|
5 |
| -import os |
6 | 6 | from unittest.mock import MagicMock
|
7 | 7 |
|
| 8 | +import datadog_lambda.cold_start as cold_start |
| 9 | +import datadog_lambda.wrapper as wrapper |
| 10 | + |
8 | 11 |
|
9 | 12 | class TestColdStartTracingSetup(unittest.TestCase):
|
10 | 13 | def test_proactive_init(self):
|
@@ -234,3 +237,42 @@ def test_trace_ignore_libs(self):
|
234 | 237 | self.cold_start_tracer.trace(nodes)
|
235 | 238 | self.mock_activate.assert_called_once_with(self.mock_trace_ctx)
|
236 | 239 | self.assertEqual(self.output_spans, ["node_0", "unittest_cold_start"])
|
| 240 | + |
| 241 | + |
| 242 | +def test_lazy_loaded_package_imports(monkeypatch): |
| 243 | + |
| 244 | + spans = [] |
| 245 | + |
| 246 | + def finish(span): |
| 247 | + spans.append(span) |
| 248 | + |
| 249 | + monkeypatch.setattr(wrapper.tracer, "_on_span_finish", finish) |
| 250 | + monkeypatch.setattr(wrapper, "is_new_sandbox", lambda: True) |
| 251 | + monkeypatch.setattr("datadog_lambda.wrapper.dd_tracing_enabled", True) |
| 252 | + monkeypatch.setenv( |
| 253 | + "DD_COLD_START_TRACE_SKIP_LIB", "ddtrace.contrib.logging,datadog_lambda.wrapper" |
| 254 | + ) |
| 255 | + monkeypatch.setenv("DD_MIN_COLD_START_DURATION", "0") |
| 256 | + |
| 257 | + @wrapper.datadog_lambda_wrapper |
| 258 | + def handler(event, context): |
| 259 | + import tabnanny |
| 260 | + |
| 261 | + lambda_context = MagicMock() |
| 262 | + lambda_context.invoked_function_arn = ( |
| 263 | + "arn:aws:lambda:us-west-1:123457598159:function:python-layer-test:1" |
| 264 | + ) |
| 265 | + |
| 266 | + handler.cold_start_tracing = True |
| 267 | + handler({}, lambda_context) |
| 268 | + |
| 269 | + function_span = import_span = None |
| 270 | + for span in spans: |
| 271 | + if span.resource == "tabnanny": |
| 272 | + import_span = span |
| 273 | + elif span.name == "aws.lambda": |
| 274 | + function_span = span |
| 275 | + |
| 276 | + assert function_span is not None |
| 277 | + assert import_span is not None |
| 278 | + assert import_span.parent_id == function_span.span_id |
0 commit comments