Start monitoring and instrumenting packages if not already started.
()
| 521 | |
| 522 | |
| 523 | def instrument_all(): |
| 524 | """Start monitoring and instrumenting packages if not already started.""" |
| 525 | # Check if active_instrumentors is empty, as a proxy for not started. |
| 526 | if not _active_instrumentors: |
| 527 | builtins.__import__ = _import_monitor |
| 528 | global _instrumenting_packages, _has_agentic_library |
| 529 | |
| 530 | # If an agentic library is already instrumented, don't instrument anything else |
| 531 | if _has_agentic_library: |
| 532 | return |
| 533 | |
| 534 | for name in list(sys.modules.keys()): |
| 535 | # Stop if an agentic library gets instrumented during the loop |
| 536 | if _has_agentic_library: |
| 537 | break |
| 538 | |
| 539 | module = sys.modules.get(name) |
| 540 | if not isinstance(module, ModuleType): |
| 541 | continue |
| 542 | |
| 543 | # Check for exact matches first (handles package.module like google.adk) |
| 544 | package_to_check = None |
| 545 | if name in TARGET_PACKAGES: |
| 546 | package_to_check = name |
| 547 | else: |
| 548 | # Check if any target package is a prefix of the module name |
| 549 | for target in TARGET_PACKAGES: |
| 550 | if name.startswith(target + ".") or name == target: |
| 551 | package_to_check = target |
| 552 | break |
| 553 | |
| 554 | if ( |
| 555 | package_to_check |
| 556 | and package_to_check not in _instrumenting_packages |
| 557 | and not _is_package_instrumented(package_to_check) |
| 558 | ): |
| 559 | target_module_obj = sys.modules.get(package_to_check) |
| 560 | |
| 561 | if target_module_obj: |
| 562 | is_sdk = _is_installed_package(target_module_obj, package_to_check) |
| 563 | if not is_sdk: |
| 564 | continue |
| 565 | else: |
| 566 | logger.debug( |
| 567 | f"instrument_all: No module object found for '{package_to_check}' in sys.modules during startup scan. Proceeding cautiously." |
| 568 | ) |
| 569 | |
| 570 | _instrumenting_packages.add(package_to_check) |
| 571 | try: |
| 572 | _perform_instrumentation(package_to_check) |
| 573 | except Exception as e: |
| 574 | logger.error(f"Error instrumenting {package_to_check}: {str(e)}") |
| 575 | finally: |
| 576 | _instrumenting_packages.discard(package_to_check) |
| 577 | |
| 578 | |
| 579 | def uninstrument_all(): |
no test coverage detected
searching dependent graphs…