MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / _perform_instrumentation

Function _perform_instrumentation

agentops/instrumentation/__init__.py:291–372  ·  view source on GitHub ↗

Helper function to perform instrumentation for a given package.

(package_name: str)

Source from the content-addressed store, hash-verified

289
290
291def _perform_instrumentation(package_name: str):
292 """Helper function to perform instrumentation for a given package."""
293 global _instrumenting_packages, _active_instrumentors, _has_agentic_library
294 if not _should_instrument_package(package_name):
295 return
296
297 # Get the appropriate configuration for the package
298 # Ensure package_name is a key in either PROVIDERS or AGENTIC_LIBRARIES
299 if package_name not in PROVIDERS and package_name not in AGENTIC_LIBRARIES:
300 logger.debug(
301 f"_perform_instrumentation: Package '{package_name}' not found in PROVIDERS or AGENTIC_LIBRARIES. Skipping."
302 )
303 return
304
305 config = PROVIDERS.get(package_name) or AGENTIC_LIBRARIES.get(package_name)
306 loader = InstrumentorLoader(**config)
307
308 # instrument_one already checks loader.should_activate
309 instrumentor_instance = instrument_one(loader)
310 if instrumentor_instance is not None:
311 # Check if it was *actually* instrumented by instrument_one by seeing if the instrument method was called successfully.
312 # This relies on instrument_one returning None if its internal .instrument() call failed (if we revert that, this needs adjustment)
313 # For now, assuming instrument_one returns instance only on full success.
314 # User request was to return instrumentor even if .instrument() fails. So, we check if _agentops_instrumented_package_key was set by us.
315
316 # Let's assume instrument_one might return an instance whose .instrument() failed.
317 # The key is set before _active_instrumentors.append, so if it's already there and matches, it means it's a re-attempt on the same package.
318 # The _is_package_instrumented check at the start of _should_instrument_package should prevent most re-entry for the same package_name.
319
320 # Store the package key this instrumentor is for, to aid _is_package_instrumented
321 instrumentor_instance._agentops_instrumented_package_key = package_name
322
323 # Add to active_instrumentors only if it's not a duplicate in terms of package_key being instrumented
324 # This is a safeguard, _is_package_instrumented should catch this earlier.
325 is_newly_added = True
326 for existing_inst in _active_instrumentors:
327 if (
328 hasattr(existing_inst, "_agentops_instrumented_package_key")
329 and existing_inst._agentops_instrumented_package_key == package_name
330 ):
331 is_newly_added = False
332 logger.debug(
333 f"_perform_instrumentation: Instrumentor for '{package_name}' already in _active_instrumentors. Not adding again."
334 )
335 break
336 if is_newly_added:
337 _active_instrumentors.append(instrumentor_instance)
338
339 # If this was an agentic library AND it's newly effectively instrumented.
340 if (
341 package_name in AGENTIC_LIBRARIES and not _has_agentic_library
342 ): # Check _has_agentic_library to ensure this is the *first* one.
343 # _uninstrument_providers() was already called in _should_instrument_package for the first agentic library.
344 _has_agentic_library = True
345
346 # Special case: If mem0 is instrumented, also instrument concurrent.futures
347 if (package_name == "mem0" or package_name == "autogen") and is_newly_added:
348 try:

Callers 2

_import_monitorFunction · 0.85
instrument_allFunction · 0.85

Calls 5

InstrumentorLoaderClass · 0.85
instrument_oneFunction · 0.85
InstrumentorConfigClass · 0.70
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…