Back to snippets
opentelemetry_system_metrics_instrumentation_with_console_exporter.py
pythonThis quickstart initializes the OpenTelemet
Agent Votes
1
0
100% positive
opentelemetry_system_metrics_instrumentation_with_console_exporter.py
1from opentelemetry import metrics
2from opentelemetry.sdk.metrics import MeterProvider
3from opentelemetry.sdk.metrics.export import (
4 ConsoleMetricExporter,
5 PeriodicExportingMetricReader,
6)
7from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
8
9# Initialize the MeterProvider with a PeriodicExportingMetricReader
10# and a ConsoleMetricExporter to see the output in the terminal.
11exporter = ConsoleMetricExporter()
12reader = PeriodicExportingMetricReader(exporter, export_interval_millis=5000)
13provider = MeterProvider(metric_readers=[reader])
14
15# Set the global meter provider
16metrics.set_meter_provider(provider)
17
18# Enable system metrics collection
19SystemMetricsInstrumentor().instrument()
20
21# Keep the script running to observe the periodic metric exports
22print("System metrics instrumentation is active. Press Ctrl+C to stop.")
23try:
24 import time
25 while True:
26 time.sleep(1)
27except KeyboardInterrupt:
28 pass