Skip to content

Commit

Permalink
Merge pull request #5839 from snwoods/private/stevenwo/CP-49875
Browse files Browse the repository at this point in the history
CP-49875: observer.py: Group the auto_instrumentation spans by module
  • Loading branch information
snwoods authored Jul 17, 2024
2 parents b8b5fd0 + bd69fbe commit 2aa27d7
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions python3/packages/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,15 @@ def autoinstrument_class(aclass):
"""Auto-instrument a class."""

t = tracers[0]
module_name = f"{aclass.__module__}:{aclass.__qualname__}"
class_name = f"{aclass.__module__}:{aclass.__qualname__}"

with t.start_as_current_span(f"auto_instrumentation.add: {module_name}"):
with t.start_as_current_span(f"auto_instrumentation.add_class: {class_name}"):
for method_name, method in aclass.__dict__.items():
if not callable(getattr(aclass, method_name)):
continue

with t.start_as_current_span(
f"class.instrument:{module_name}.{method_name}"
f"class.instrument:{class_name}.{method_name}"
):
# Avoid RecursionError:
# 'maximum recursion depth exceeded in comparison'
Expand All @@ -333,16 +333,17 @@ def autoinstrument_class(aclass):
def autoinstrument_module(amodule):
"""Autoinstrument the classes and functions in a module."""

# Instrument the methods of the classes in the module
for _, aclass in inspect.getmembers(amodule, inspect.isclass):
try:
autoinstrument_class(aclass)
except Exception:
debug("instrument_function: Exception %s", traceback.format_exc())

# Instrument the module-level functions of the module
for fname, afunction in inspect.getmembers(amodule, inspect.isfunction):
setattr(amodule, fname, instrument_function(afunction))
with tracers[0].start_as_current_span(f"auto_instrumentation.add_module: {amodule}"):
# Instrument the methods of the classes in the module
for _, aclass in inspect.getmembers(amodule, inspect.isclass):
try:
autoinstrument_class(aclass)
except Exception:
debug("instrument_function: Exception %s", traceback.format_exc())

# Instrument the module-level functions of the module
for fname, afunction in inspect.getmembers(amodule, inspect.isfunction):
setattr(amodule, fname, instrument_function(afunction))

if inspect.ismodule(wrapped):
autoinstrument_module(wrapped)
Expand Down

0 comments on commit 2aa27d7

Please sign in to comment.