Skip to content

CP-53472: Create parent for add_module spans #6390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions python3/packages/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
tracers = list(map(create_tracer_from_config, configs))
debug("tracers=%s", tracers)

def span_of_tracers(wrapped=None, span_name_prefix=""):
def span_of_tracers(wrapped=None, span_name_prefix="", parent_context=None):
"""
Public decorator that creates a trace around a function.

Expand All @@ -289,7 +289,7 @@
that the function is decorated properly on the second pass.
"""
if wrapped is None: # handle decorators with parameters
return functools.partial(span_of_tracers, span_name_prefix=span_name_prefix)
return functools.partial(span_of_tracers, span_name_prefix=span_name_prefix, parent_context=parent_context)

Check warning on line 292 in python3/packages/observer.py

View workflow job for this annotation

GitHub Actions / Python tests (3.11)

[pylint] reported by reviewdog 🐶 C0301: Line too long (119/100) (line-too-long) Raw Output: python3/packages/observer.py:292:0: C0301: Line too long (119/100) (line-too-long)

@wrapt.decorator
def instrument_function(wrapped, _, args, kwargs):
Expand Down Expand Up @@ -352,11 +352,10 @@
traceback.format_exc(),
)


def autoinstrument_module(amodule):
"""Autoinstrument the classes and functions in a module."""

with tracers[0].start_as_current_span(f"auto_instrumentation.add_module: {amodule}"):
with tracers[0].start_as_current_span(f"auto_instrumentation.add_module: {amodule}", context=parent_context):

Check warning on line 358 in python3/packages/observer.py

View workflow job for this annotation

GitHub Actions / Python tests (3.11)

[pylint] reported by reviewdog 🐶 C0301: Line too long (121/100) (line-too-long) Raw Output: python3/packages/observer.py:358:0: C0301: Line too long (121/100) (line-too-long)
# Instrument the methods of the classes in the module
for _, aclass in inspect.getmembers(amodule, inspect.isclass):
try:
Expand All @@ -373,21 +372,26 @@

return instrument_function(wrapped)

def _patch_module(module_name):
def _patch_module(module_name, parent_context=None):
wrapt.importer.discover_post_import_hooks(module_name)
wrapt.importer.when_imported(module_name)(
lambda hook: span_of_tracers(wrapped=hook)
lambda hook: span_of_tracers(wrapped=hook, parent_context=parent_context)
)

for m in module_names:
_patch_module(m)
def _patch_modules(parent_context):
for m in module_names:
_patch_module(m, parent_context=parent_context)

# Create spans to track observer.py's setup duration
t = tracers[0]
with t.start_as_current_span("observer.py:init_tracing", start_time=observer_ts_start):
import_span = t.start_span("observer.py:imports", start_time=import_ts_start)
import_span.end(end_time=import_ts_end)

# Set a parent span in the add_module spans' context so that they are kept together
with t.start_span("auto_instrumentation") as aspan:
_patch_modules(trace.set_span_in_context(aspan))

return span_of_tracers, _patch_module


Expand Down
Loading