base
agent_cover.instrumentation.base
Base abstractions and utilities for the agent instrumentation library.
This module defines the foundational classes that allow AgentCover to modify third-party libraries (like LangChain or LlamaIndex) safely.
Core Concepts
- BaseInstrumentor: The blueprint for any strategy that wants to track code execution. It handles dependency checking (versions) and the apply/revert lifecycle.
-
PatchManager: A safety layer ensuring that:
- We never patch the same method twice (idempotency).
- We can always restore the original method (cleanup).
- Wrappers act transparently to the original code.
-
TargetConfig: Defines what to patch (Module + Class + Method).
Classes
BaseInstrumentor
Bases: ABC
Abstract base class for all instrumentation strategies.
Subclasses (e.g., PromptInstrumentor, ToolInstrumentor) implement the
instrument() method to apply specific logic. This base class provides
the infrastructure for:
- Safe Patching: Using _safe_patch to wrap methods without breaking them.
- Version Compatibility: Using VersionChecker to skip instrumentation
if the installed library version is unsupported.
- Module Discovery: Finding the target modules in memory using module_iterator.
Source code in src/agent_cover/instrumentation/base.py
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
Functions
__enter__()
Enters the instrumentation context (used with 'with' statements).
This method calls the instrument method to apply patches.
Returns:
| Type | Description |
|---|---|
|
The instrumentor instance. |
Source code in src/agent_cover/instrumentation/base.py
__exit__(exc_type, exc_val, exc_tb)
Exits the instrumentation context (used with 'with' statements).
This method calls the uninstrument method to restore original functionality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
The exception type (if any). |
required | |
exc_val
|
The exception value (if any). |
required | |
exc_tb
|
The traceback (if any). |
required |
Source code in src/agent_cover/instrumentation/base.py
__init__(registry=None, context_manager=None, patch_manager=None, module_iterator=None, importer_func=None, version_checker=None)
Initializes the BaseInstrumentor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
registry
|
Optional[AgentRegistry]
|
The AgentRegistry instance. Defaults to global registry if None. |
None
|
context_manager
|
Optional[AgentContextManager]
|
The AgentContextManager. Defaults to global manager if None. |
None
|
patch_manager
|
Optional[PatchManager]
|
The PatchManager. Defaults to DefaultPatchManager if None. |
None
|
module_iterator
|
Optional[Callable[[], Dict[str, Any]]]
|
A callable to iterate through modules. Defaults to sys.modules. |
None
|
importer_func
|
Optional[Callable[[str], Any]]
|
A function to import modules. Defaults to importlib.import_module. |
None
|
version_checker
|
Union[VersionChecker, Callable[[str], Optional[str]], None]
|
A VersionChecker instance or a callable that returns a version string. |
None
|
Source code in src/agent_cover/instrumentation/base.py
instrument()
abstractmethod
Abstract method for instrumenting the code.
Subclasses must implement this method to perform the actual instrumentation logic.
uninstrument()
Uninstruments the code by restoring the original methods.
This method iterates through the patched methods and restores them to their original state using the PatchManager.
Source code in src/agent_cover/instrumentation/base.py
DefaultPatchManager
Bases: PatchManager
The default implementation of the PatchManager.
This class applies patches by setting the attribute on the target object to the wrapper function. It also keeps track of patched methods to avoid double patching and to enable restoration.
Methods:
| Name | Description |
|---|---|
apply_patch |
Applies a patch, marking the wrapper to prevent re-patching. |
restore_patch |
Restores the original attribute value. |
Source code in src/agent_cover/instrumentation/base.py
Functions
apply_patch(target_obj, attribute_name, wrapper)
Applies a patch to a target object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_obj
|
Any
|
The object to patch. |
required |
attribute_name
|
str
|
The name of the attribute to patch. |
required |
wrapper
|
Any
|
The wrapper function. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The original method. Returns the original method immediately if |
Any
|
the object is already patched. |
Source code in src/agent_cover/instrumentation/base.py
restore_patch(target_obj, attribute_name, original)
Restores a patch to a target object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_obj
|
Any
|
The object to restore the patch on. |
required |
attribute_name
|
str
|
The name of the attribute. |
required |
original
|
Any
|
The original method. |
required |
Source code in src/agent_cover/instrumentation/base.py
DefaultVersionChecker
Bases: VersionChecker
The default implementation of the VersionChecker.
This class uses importlib.metadata to get the version of a package.
Methods:
| Name | Description |
|---|---|
get_version |
Retrieves the version using importlib.metadata. |
Source code in src/agent_cover/instrumentation/base.py
Functions
get_version(package_name)
Gets the version of a package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The version string, or None if the package is not found. |
Source code in src/agent_cover/instrumentation/base.py
MockVersionChecker
Bases: VersionChecker
A mock implementation of the VersionChecker for testing purposes.
This class allows you to provide a predefined mapping of package names to versions.
Attributes:
| Name | Type | Description |
|---|---|---|
version_map |
A dictionary mapping package names to version strings. |
Methods:
| Name | Description |
|---|---|
get_version |
Retrieves the version from the internal map. |
Source code in src/agent_cover/instrumentation/base.py
Functions
__init__(version_map)
Initializes the MockVersionChecker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version_map
|
Dict[str, str]
|
A dictionary mapping package names to versions. |
required |
get_version(package_name)
Gets the version of a package from the version map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The version string, or None if the package is not found in the map. |
Source code in src/agent_cover/instrumentation/base.py
PatchManager
Bases: ABC
Abstract base class for patch managers.
Patch managers are responsible for applying and restoring patches to methods and classes safely.
Methods:
| Name | Description |
|---|---|
apply_patch |
Applies a patch to a target object. |
restore_patch |
Restores a patch to a target object. |
Source code in src/agent_cover/instrumentation/base.py
Functions
apply_patch(target_obj, attribute_name, wrapper)
abstractmethod
Applies a patch to a target object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_obj
|
Any
|
The object to patch. |
required |
attribute_name
|
str
|
The name of the attribute to patch (e.g., method name). |
required |
wrapper
|
Any
|
The wrapper function to apply. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The original value of the attribute before the patch was applied. |
Source code in src/agent_cover/instrumentation/base.py
restore_patch(target_obj, attribute_name, original)
abstractmethod
Restores a patch to a target object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_obj
|
Any
|
The object to restore the patch on. |
required |
attribute_name
|
str
|
The name of the attribute that was patched. |
required |
original
|
Any
|
The original value of the attribute before the patch was applied. |
required |
Source code in src/agent_cover/instrumentation/base.py
VersionChecker
Abstract base class for version checkers.
Version checkers are used to determine if a package meets certain version requirements before applying instrumentation.
Methods:
| Name | Description |
|---|---|
get_version |
Retrieves the installed version of a package. |
Source code in src/agent_cover/instrumentation/base.py
Functions
get_version(package_name)
Gets the version of a package.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
package_name
|
str
|
The name of the package. |
required |
Returns:
| Type | Description |
|---|---|
Optional[str]
|
The version string, or None if the package is not found. |