Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ classes (sync and async):
Common actions like `launch`, `new_page`, `goto`, `click`, `type`, `screenshot`,
and many more are traced with relevant attributes.

See the [`targets.py`](src/opentelemetry/instrumentation/playwright/targets.py)
file for a full list of the methods that are instrumented, along with the
parameters that will be attached to the Open Telemetry span as parameters.
You can find the full list of methods that are instrumented with the
`annotated_methods()` function from the
`opentelemetry.instrumentation.playwright.targets` module.

## License

Expand Down
19 changes: 10 additions & 9 deletions src/opentelemetry/instrumentation/playwright/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import inspect
import time
from collections.abc import Callable, Collection
from collections.abc import Callable, Collection, Mapping
from contextlib import ExitStack
from functools import wraps
from typing import Any, ParamSpec, Type, TypedDict, TypeVar, Unpack, override
Expand All @@ -19,7 +19,7 @@
from playwright._impl._async_base import AsyncContextManager
from playwright._impl._sync_base import SyncContextManager

from .targets import METHODS, AttrConstructor
from .targets import AttrConstructor, annotated_methods

__all__ = ["PlaywrightInstrumentor"]
__version__ = "0.0.0"
Expand Down Expand Up @@ -79,9 +79,9 @@ def instrumentation_dependencies(self) -> Collection[str]:

@override
def _instrument(self, **kwargs: Any):
for parent, methods in METHODS.items():
for method, attrs in methods:
self._patch(parent, method, attrs)
for parent, methods in annotated_methods().items():
for m in methods:
self._patch(parent, m.name, m.attrs)

self._patch_context_manager(SyncContextManager)
self._patch_async_context_manager(AsyncContextManager)
Expand All @@ -92,8 +92,9 @@ def _uninstrument(self, **kwargs: Any):
# instrumentor back to a usable state.
self._exit_stack.pop_all().close()

def _patch(self, parent: Type, method: str, attrs: dict[str, AttrConstructor]):
"""Patches a method on a Playwright class to add OpenTelemetry instrumentation.
def _patch(self, parent: Type, method: str, attrs: Mapping[str, AttrConstructor]):
"""
Patches a method on a Playwright class to add OpenTelemetry instrumentation.

This is the core instrumentation function that:
1. Gets the original method from the parent class
Expand Down Expand Up @@ -286,7 +287,7 @@ async def aexit_wrapper(self_, exc_type, exc_val, exc_tb):


def _attr_maker(
func: Callable, attributes: dict[str, AttrConstructor]
func: Callable, attributes: Mapping[str, AttrConstructor]
) -> Callable[..., Attributes]:
"""
Get a callable which, when called with the same arguments as the original
Expand All @@ -300,7 +301,7 @@ def _attr_maker(
for attr_name in attributes:
assert (
attr_name in signature.parameters
), f"Argument '{attr_name}' not found in {func.__name__}{signature}"
), f"Argument '{attr_name}' not found in {func.__qualname__}{signature}"

def maker(*args: Any, **kwargs: Any) -> Attributes:
bound = signature.bind(*args, **kwargs)
Expand Down
Loading
Loading