@@ -266,7 +266,7 @@ def bugtool_filenamer():
266266 tracers = list (map (create_tracer_from_config , configs ))
267267 debug ("tracers=%s" , tracers )
268268
269- def span_of_tracers (wrapped = None , span_name_prefix = "" ):
269+ def span_of_tracers (wrapped = None , span_name_prefix = "" , parent_context = None ):
270270 """
271271 Public decorator that creates a trace around a function.
272272
@@ -289,7 +289,7 @@ def span_of_tracers(wrapped=None, span_name_prefix=""):
289289 that the function is decorated properly on the second pass.
290290 """
291291 if wrapped is None : # handle decorators with parameters
292- return functools .partial (span_of_tracers , span_name_prefix = span_name_prefix )
292+ return functools .partial (span_of_tracers , span_name_prefix = span_name_prefix , parent_context = parent_context )
293293
294294 @wrapt .decorator
295295 def instrument_function (wrapped , _ , args , kwargs ):
@@ -352,11 +352,10 @@ def autoinstrument_class(aclass):
352352 traceback .format_exc (),
353353 )
354354
355-
356355 def autoinstrument_module (amodule ):
357356 """Autoinstrument the classes and functions in a module."""
358357
359- with tracers [0 ].start_as_current_span (f"auto_instrumentation.add_module: { amodule } " ):
358+ with tracers [0 ].start_as_current_span (f"auto_instrumentation.add_module: { amodule } " , context = parent_context ):
360359 # Instrument the methods of the classes in the module
361360 for _ , aclass in inspect .getmembers (amodule , inspect .isclass ):
362361 try :
@@ -373,21 +372,26 @@ def autoinstrument_module(amodule):
373372
374373 return instrument_function (wrapped )
375374
376- def _patch_module (module_name ):
375+ def _patch_module (module_name , parent_context = None ):
377376 wrapt .importer .discover_post_import_hooks (module_name )
378377 wrapt .importer .when_imported (module_name )(
379- lambda hook : span_of_tracers (wrapped = hook )
378+ lambda hook : span_of_tracers (wrapped = hook , parent_context = parent_context )
380379 )
381380
382- for m in module_names :
383- _patch_module (m )
381+ def _patch_modules (parent_context ):
382+ for m in module_names :
383+ _patch_module (m , parent_context = parent_context )
384384
385385 # Create spans to track observer.py's setup duration
386386 t = tracers [0 ]
387387 with t .start_as_current_span ("observer.py:init_tracing" , start_time = observer_ts_start ):
388388 import_span = t .start_span ("observer.py:imports" , start_time = import_ts_start )
389389 import_span .end (end_time = import_ts_end )
390390
391+ # Set a parent span in the add_module spans' context so that they are kept together
392+ with t .start_span ("auto_instrumentation" ) as aspan :
393+ _patch_modules (trace .set_span_in_context (aspan ))
394+
391395 return span_of_tracers , _patch_module
392396
393397
0 commit comments