Telemetry

mt5cli.telemetry

Optional OpenTelemetry metrics for MT5 history and snapshot observability.

logger module-attribute

logger = getLogger(__name__)

configure_metrics

configure_metrics(meter: Any) -> None

Configure MT5 metrics using the provided meter.

Parameters:

Name Type Description Default
meter Any

An OpenTelemetry Meter or duck-typed compatible object.

required
Source code in mt5cli/telemetry.py
def configure_metrics(meter: Any) -> None:  # noqa: ANN401
    """Configure MT5 metrics using the provided meter.

    Args:
        meter: An OpenTelemetry ``Meter`` or duck-typed compatible object.
    """
    _metrics.configure(meter)

enable_otel_metrics

enable_otel_metrics(
    service_name: str = "mt5cli",
    readers: list[Any] | None = None,
) -> None

Enable OTel metrics by wiring up an SDK MeterProvider pipeline.

Requires the otel optional dependency group: pip install "mt5cli[otel]".

Parameters:

Name Type Description Default
service_name str

OTel meter/service name used for the Resource and the meter itself.

'mt5cli'
readers list[Any] | None

Optional list of metric readers. When None (the default), a :class:~opentelemetry.sdk.metrics.export.PeriodicExportingMetricReader backed by an OTLP HTTP exporter is created automatically (reads the endpoint from OTEL_EXPORTER_OTLP_ENDPOINT). Pass a custom list (e.g. InMemoryMetricReader for tests) to override.

None

Raises:

Type Description
ImportError

If opentelemetry-api is not installed, or if readers is None and opentelemetry-exporter-otlp-proto-http is not installed.

Source code in mt5cli/telemetry.py
def enable_otel_metrics(
    service_name: str = "mt5cli",
    readers: list[Any] | None = None,
) -> None:
    """Enable OTel metrics by wiring up an SDK ``MeterProvider`` pipeline.

    Requires the ``otel`` optional dependency group:
    ``pip install "mt5cli[otel]"``.

    Args:
        service_name: OTel meter/service name used for the ``Resource`` and
            the meter itself.
        readers: Optional list of metric readers. When *None* (the default),
            a :class:`~opentelemetry.sdk.metrics.export.PeriodicExportingMetricReader`
            backed by an OTLP HTTP exporter is created automatically
            (reads the endpoint from ``OTEL_EXPORTER_OTLP_ENDPOINT``).
            Pass a custom list (e.g. ``InMemoryMetricReader`` for tests)
            to override.

    Raises:
        ImportError: If ``opentelemetry-api`` is not installed, or if
            ``readers`` is *None* and
            ``opentelemetry-exporter-otlp-proto-http`` is not installed.
    """
    if not _OTEL_AVAILABLE:
        msg = (
            "opentelemetry-api is not installed. "
            'Install it with: pip install "mt5cli[otel]"'
        )
        raise ImportError(msg)
    if readers is None:
        if _OtelOTLPExporter is None:
            msg = (
                "opentelemetry-exporter-otlp-proto-http is required for the "
                "default OTLP export pipeline. "
                'Install it with: pip install "mt5cli[otel]" or pass a '
                "custom readers list."
            )
            raise ImportError(msg)
        readers = [_OtelPeriodicReader(_OtelOTLPExporter())]  # type: ignore[misc]
    resource = _OtelResource.create({"service.name": service_name})  # type: ignore[union-attr]
    provider = _OtelMeterProvider(resource=resource, metric_readers=readers)  # type: ignore[misc]
    _otel_metrics_mod.set_meter_provider(provider)  # type: ignore[union-attr]
    meter = provider.get_meter(service_name)
    configure_metrics(meter)

get_metrics

get_metrics() -> _Mt5Metrics

Return the global :class:_Mt5Metrics instance.

Returns:

Type Description
_Mt5Metrics

The global metric registry (no-op until :func:configure_metrics is

_Mt5Metrics

called).

Source code in mt5cli/telemetry.py
def get_metrics() -> _Mt5Metrics:
    """Return the global :class:`_Mt5Metrics` instance.

    Returns:
        The global metric registry (no-op until :func:`configure_metrics` is
        called).
    """
    return _metrics

Enabling OpenTelemetry metrics

Install the optional exporter dependencies with:

uv add 'mt5cli[otel]'

Then enable the default OTLP HTTP pipeline:

from mt5cli.telemetry import enable_otel_metrics

enable_otel_metrics(service_name="mt5cli")

When readers=None, enable_otel_metrics() builds a PeriodicExportingMetricReader backed by the OTLP HTTP exporter and reads the endpoint from OTEL_EXPORTER_OTLP_ENDPOINT.

If your application already owns an OpenTelemetry Meter, wire mt5cli into it directly with configure_metrics(meter).

Emitted metric names

enable_otel_metrics() / configure_metrics() register these instruments:

  • mt5_history_update_duration_seconds
  • mt5_history_update_rows_total
  • mt5_history_update_failures_total
  • mt5_snapshot_update_duration_seconds
  • mt5_snapshot_update_failures_total
  • mt5_account_balance
  • mt5_account_equity
  • mt5_account_margin
  • mt5_account_margin_free
  • mt5_account_margin_level
  • mt5_position_profit
  • mt5_position_volume
  • mt5_terminal_connected
  • mt5_terminal_trade_allowed
  • mt5_terminal_trade_expert
  • mt5_last_successful_update_timestamp

The history metrics use a dataset attribute. Account and position gauges add labels such as login, server, and symbol where applicable.