class documentation

Exports metrics recorded via a temporalio.runtime.MetricBuffer to an OpenTelemetry opentelemetry.metrics.MeterProvider.

Warning

This class is experimental and may change in future versions. Use with caution in production environments.

This must be started (via run or async with) after the temporalio.runtime.Runtime referencing buffer has been constructed, and it must be kept running for as long as metrics should be exported. It drains the buffer on a fixed interval; per temporalio.runtime.MetricBuffer, updates are dropped (with an error logged by Core) if the buffer is not drained regularly.

Example:

from datetime import timedelta

from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import (
    ConsoleMetricExporter,
    PeriodicExportingMetricReader,
)

from temporalio.client import Client
from temporalio.contrib.opentelemetry import MetricsExporter
from temporalio.runtime import MetricBuffer, Runtime, TelemetryConfig

buffer = MetricBuffer(10_000)
runtime = Runtime(telemetry=TelemetryConfig(metrics=buffer))
meter_provider = MeterProvider(
    metric_readers=[
        PeriodicExportingMetricReader(
            ConsoleMetricExporter(), export_interval_millis=5000
        )
    ]
)

async with MetricsExporter(buffer, meter_provider):
    client = await Client.connect("localhost:7233", runtime=runtime)
    ...
Async Method __aenter__ Start run as a background task.
Async Method __aexit__ Call shutdown and await the background task.
Method __init__ Create an exporter that drains buffer into meter_provider.
Async Method run Drain and export on poll_interval until shutdown is called.
Async Method shutdown Stop run (including a final drain) and wait for it to finish.
Method _apply_update Undocumented
Method _drain_once Undocumented
Method _get_or_create_attributes Undocumented
Method _get_or_create_instrument Undocumented
Method _handle_error Undocumented
Instance Variable _attributes_cache Undocumented
Instance Variable _buffer Undocumented
Instance Variable _instruments Undocumented
Instance Variable _meter Undocumented
Instance Variable _on_error Undocumented
Instance Variable _poll_interval Undocumented
Instance Variable _run_complete_event Undocumented
Instance Variable _run_task Undocumented
Instance Variable _shutdown_event Undocumented
Instance Variable _started Undocumented
async def __aenter__(self) -> MetricsExporter: (source)

Start run as a background task.

async def __aexit__(self, *exc_info: object): (source)

Call shutdown and await the background task.

def __init__(self, buffer: temporalio.runtime.MetricBuffer, meter_provider: opentelemetry.metrics.MeterProvider | None = None, *, meter_name: str = 'temporalio', meter_version: str | None = None, poll_interval: timedelta = timedelta(seconds=1), on_error: Callable[[Exception], None] | None = None): (source)

Create an exporter that drains buffer into meter_provider.

Parameters
buffer:temporalio.runtime.MetricBufferThe buffer to drain. Must already be (or about to be) set as the metrics of a temporalio.runtime.TelemetryConfig on a constructed temporalio.runtime.Runtime.
meter_provider:opentelemetry.metrics.MeterProvider | NoneThe provider to create instruments on. Defaults to opentelemetry.metrics.get_meter_provider (the global provider) if not given.
meter_name:strName passed to get_meter on the provider.
meter_version:str | NoneVersion passed to get_meter on the provider.
poll_interval:timedeltaHow often to drain the buffer. Must be reasonably frequent -- see the warning on temporalio.runtime.MetricBuffer.
on_error:Callable[[Exception], None] | NoneOptional callback invoked (in addition to logging) when draining the buffer or applying an individual update fails. If this callback itself raises, that is logged and ignored.
async def run(self): (source)

Drain and export on poll_interval until shutdown is called.

Raises
RuntimeErrorIf buffer was never attached to a constructed temporalio.runtime.Runtime, or if this is called while already running.
async def shutdown(self): (source)

Stop run (including a final drain) and wait for it to finish.

Undocumented

def _drain_once(self): (source)

Undocumented

def _get_or_create_attributes(self, attributes: temporalio.common.MetricAttributes) -> Mapping[str, opentelemetry.util.types.AttributeValue]: (source)

Undocumented

def _get_or_create_instrument(self, metric: temporalio.runtime.BufferedMetric) -> _Instrument: (source)

Undocumented

def _handle_error(self, err: Exception): (source)

Undocumented

_attributes_cache: dict[int, Mapping[str, opentelemetry.util.types.AttributeValue]] = (source)

Undocumented

Undocumented

_instruments: dict[int, _Instrument] = (source)

Undocumented

Undocumented

_on_error = (source)

Undocumented

_poll_interval = (source)

Undocumented

_run_complete_event = (source)

Undocumented

Undocumented

_shutdown_event = (source)

Undocumented

_started: bool = (source)

Undocumented