OpenTelemetry System Metrics Instrumentation
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
- deprecated Out-of-spec `process.runtime` prefixed metrics are deprecated and will be removed in future versions. Users are encouraged to migrate to standard process metrics.
- gotcha The instrumentation library is still in beta, indicating that its API and collected telemetry might undergo breaking changes in future releases as the OpenTelemetry specification evolves.
- gotcha Specific system and process metrics can be excluded from collection using the `OTEL_PYTHON_SYSTEM_METRICS_EXCLUDED_METRICS` environment variable. This is useful for reducing telemetry cardinality or focusing on specific metrics.
- gotcha In versions prior to `v0.48b0`, the `process.runtime.cpu.utilization` metric might report values outside the expected range of 0 to 1.
Install
-
pip install opentelemetry-instrumentation-system-metrics opentelemetry-sdk psutil
Imports
- SystemMetricsInstrumentor
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
- MeterProvider
from opentelemetry.sdk.metrics import MeterProvider
- PeriodicExportingMetricReader
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
- ConsoleMetricExporter
from opentelemetry.sdk.metrics.export import ConsoleMetricExporter
- set_meter_provider
from opentelemetry.sdk.metrics import set_meter_provider
Quickstart
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.")