OpenTelemetry System Metrics Instrumentation

0.61b0 · active · verified Thu Apr 09

OpenTelemetry System Metrics Instrumentation provides an automated way to collect system-level performance metrics such as CPU, memory, network, and disk I/O. It is part of the `opentelemetry-python-contrib` project, currently in beta version `0.61b0`, and packages are released on a roughly monthly cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the OpenTelemetry SDK with a `MeterProvider` and a `ConsoleMetricExporter` to print collected metrics to the console. It then instruments system metrics using `SystemMetricsInstrumentor().instrument()`, which starts collecting CPU, memory, disk, and network metrics asynchronously.

import time

from opentelemetry.sdk.metrics import MeterProvider, set_meter_provider
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter, PeriodicExportingMetricReader
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor

# Configure MeterProvider
exporter = ConsoleMetricExporter()
reader = PeriodicExportingMetricReader(exporter, export_interval_millis=1000)
set_meter_provider(MeterProvider([reader]))

# Instrument system metrics
SystemMetricsInstrumentor().instrument()

print("Collecting system metrics... Press Ctrl+C to exit.")
try:
    while True:
        time.sleep(5)  # Metrics are collected asynchronously
except KeyboardInterrupt:
    print("Exiting.")

view raw JSON →