@@ -325,3 +325,45 @@ def handler(event, context):
325325 assert import_span .trace_id == function_span .trace_id
326326 assert load_span is not None
327327 assert load_span .trace_id == function_span .trace_id
328+
329+
330+ def test_lazy_loaded_package_imports_inside_client_span (monkeypatch ):
331+ spans = []
332+
333+ def finish (span ):
334+ spans .append (span )
335+
336+ monkeypatch .setattr (wrapper .tracer , "_on_span_finish" , finish )
337+ monkeypatch .setattr (wrapper , "is_new_sandbox" , lambda : True )
338+ monkeypatch .setattr ("datadog_lambda.config.Config.trace_enabled" , True )
339+ monkeypatch .setenv (
340+ "DD_COLD_START_TRACE_SKIP_LIB" , "ddtrace.contrib.logging,datadog_lambda.wrapper"
341+ )
342+ monkeypatch .setenv ("DD_MIN_COLD_START_DURATION" , "0" )
343+ # ensure the import below is not served from the module cache
344+ monkeypatch .delitem (modules , "colorsys" , raising = False )
345+
346+ @wrapper .datadog_lambda_wrapper
347+ def handler (event , context ):
348+ # e.g. botocore lazily importing modules on the first DynamoDB call
349+ with wrapper .tracer .trace ("dynamodb.command" , service = "aws.dynamodb" ) as span :
350+ span .set_tag ("span.kind" , "client" )
351+ import colorsys # noqa: F401
352+
353+ handler ({}, get_mock_context ())
354+
355+ function_span = client_span = import_span = None
356+ for span in spans :
357+ if span .resource == "colorsys" :
358+ import_span = span
359+ elif span .name == "aws.lambda" :
360+ function_span = span
361+ elif span .name == "dynamodb.command" :
362+ client_span = span
363+
364+ assert function_span is not None
365+ assert client_span is not None
366+ assert import_span is not None
367+ assert import_span .parent_id == function_span .span_id
368+ assert import_span .parent_id != client_span .span_id
369+ assert import_span .trace_id == function_span .trace_id
0 commit comments