Instrumentor#
This modules contains the OtelInstrumentor
class that implements
Opentelemetry’s BaseInstrumentor
interface and allows for automated
instrumentation of Chatsky applications,
e.g. for automated logging and log export.
- class OtelInstrumentor(logger_provider=None, tracer_provider=None, meter_provider=None)[source]#
Bases:
BaseInstrumentor
Utility class for instrumenting Chatsky-related functions that implements the
BaseInstrumentor
interface._instrument()
and_uninstrument()
methods are available to apply and revert the instrumentation effects, e.g. enable and disable logging at runtime.chatsky_instrumentor = OtelInstrumentor() chatsky_instrumentor._instrument() chatsky_instrumentor._uninstrument()
Opentelemetry provider instances can be optionally passed to the class constructor. Otherwise, the global logger, tracer and meter providers are leveraged.
The class implements the
__call__()
method, so that regular functions can be decorated using the class instance.@chatsky_instrumentor async def function(context, pipeline, runtime_info): ...
- Parameters:
logger_provider – Opentelemetry logger provider. Used to construct a logger instance.
tracer_provider – Opentelemetry tracer provider. Used to construct a tracer instance.
- Parame meter_provider:
Opentelemetry meter provider. Used to construct a meter instance.
- classmethod from_url(url, insecure=True, timeout=None)[source]#
Construct an instrumentor instance using only the url of the OTLP Collector. Inherently modifies the global provider instances adding an export destination for the target url.
instrumentor = OtelInstrumentor.from_url("grpc://localhost:4317")
- Parameters:
url (
str
) – Url of the running Otel Collector server. Due to limited support of HTTP protocol by the Opentelemetry Python extension, GRPC protocol is preferred.insecure (
bool
) – Whether non-SSL-protected connection is allowed. Defaults to True.timeout (
Optional
[int
]) – Connection timeout in seconds, optional.
- instrumentation_dependencies()[source]#
Required libraries. Implements the Python Opentelemetry instrumentor interface.
- Return type:
Collection
[str
]
- _instrument(logger_provider=None, tracer_provider=None, meter_provider=None)[source]#
Instrument the library
- __call__(enabled=None, adapter=None, proxy=<class 'FunctionWrapper'>)[source]#
Regular functions that match the
ExtraHandlerFunction
signature can be decorated with the class instance to log the returned value. This method implements the logging procedure. The returned value is assumed to be dict or NoneType. Logging non-atomic values is discouraged, as they cannot be translated using the Protobuf protocol. Logging is ignored if the application is in ‘uninstrumented’ state.- Parameters:
wrapped – Function to decorate.
args – Positional arguments of the decorated function.
kwargs – Keyword arguments of the decorated function.