mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-01 17:34:38 +08:00
Fix issue when metrics are not available (#1207)
Not all configured metrics are available on all operating systems, added a hasattr call before calling the observer.
This commit is contained in:
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Fix issue when specific metrics are not available in certain OS
|
||||||
|
([#1207](https://github.com/open-telemetry/opentelemetry-python/pull/1207))
|
||||||
|
|
||||||
## Version 0.13b0
|
## Version 0.13b0
|
||||||
|
|
||||||
Released 2020-09-17
|
Released 2020-09-17
|
||||||
|
@ -372,11 +372,12 @@ class SystemMetrics:
|
|||||||
"""
|
"""
|
||||||
for cpu, times in enumerate(psutil.cpu_times(percpu=True)):
|
for cpu, times in enumerate(psutil.cpu_times(percpu=True)):
|
||||||
for metric in self._config["system.cpu.time"]:
|
for metric in self._config["system.cpu.time"]:
|
||||||
self._system_cpu_time_labels["state"] = metric
|
if hasattr(times, metric):
|
||||||
self._system_cpu_time_labels["cpu"] = cpu + 1
|
self._system_cpu_time_labels["state"] = metric
|
||||||
observer.observe(
|
self._system_cpu_time_labels["cpu"] = cpu + 1
|
||||||
getattr(times, metric), self._system_cpu_time_labels
|
observer.observe(
|
||||||
)
|
getattr(times, metric), self._system_cpu_time_labels
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_cpu_utilization(
|
def _get_system_cpu_utilization(
|
||||||
self, observer: metrics.ValueObserver
|
self, observer: metrics.ValueObserver
|
||||||
@ -391,12 +392,13 @@ class SystemMetrics:
|
|||||||
psutil.cpu_times_percent(percpu=True)
|
psutil.cpu_times_percent(percpu=True)
|
||||||
):
|
):
|
||||||
for metric in self._config["system.cpu.utilization"]:
|
for metric in self._config["system.cpu.utilization"]:
|
||||||
self._system_cpu_utilization_labels["state"] = metric
|
if hasattr(times_percent, metric):
|
||||||
self._system_cpu_utilization_labels["cpu"] = cpu + 1
|
self._system_cpu_utilization_labels["state"] = metric
|
||||||
observer.observe(
|
self._system_cpu_utilization_labels["cpu"] = cpu + 1
|
||||||
getattr(times_percent, metric) / 100,
|
observer.observe(
|
||||||
self._system_cpu_utilization_labels,
|
getattr(times_percent, metric) / 100,
|
||||||
)
|
self._system_cpu_utilization_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_memory_usage(
|
def _get_system_memory_usage(
|
||||||
self, observer: metrics.ValueObserver
|
self, observer: metrics.ValueObserver
|
||||||
@ -409,10 +411,11 @@ class SystemMetrics:
|
|||||||
virtual_memory = psutil.virtual_memory()
|
virtual_memory = psutil.virtual_memory()
|
||||||
for metric in self._config["system.memory.usage"]:
|
for metric in self._config["system.memory.usage"]:
|
||||||
self._system_memory_usage_labels["state"] = metric
|
self._system_memory_usage_labels["state"] = metric
|
||||||
observer.observe(
|
if hasattr(virtual_memory, metric):
|
||||||
getattr(virtual_memory, metric),
|
observer.observe(
|
||||||
self._system_memory_usage_labels,
|
getattr(virtual_memory, metric),
|
||||||
)
|
self._system_memory_usage_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_memory_utilization(
|
def _get_system_memory_utilization(
|
||||||
self, observer: metrics.ValueObserver
|
self, observer: metrics.ValueObserver
|
||||||
@ -426,10 +429,11 @@ class SystemMetrics:
|
|||||||
|
|
||||||
for metric in self._config["system.memory.utilization"]:
|
for metric in self._config["system.memory.utilization"]:
|
||||||
self._system_memory_utilization_labels["state"] = metric
|
self._system_memory_utilization_labels["state"] = metric
|
||||||
observer.observe(
|
if hasattr(system_memory, metric):
|
||||||
getattr(system_memory, metric) / system_memory.total,
|
observer.observe(
|
||||||
self._system_memory_utilization_labels,
|
getattr(system_memory, metric) / system_memory.total,
|
||||||
)
|
self._system_memory_utilization_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_swap_usage(self, observer: metrics.ValueObserver) -> None:
|
def _get_system_swap_usage(self, observer: metrics.ValueObserver) -> None:
|
||||||
"""Observer callback for swap usage
|
"""Observer callback for swap usage
|
||||||
@ -441,9 +445,11 @@ class SystemMetrics:
|
|||||||
|
|
||||||
for metric in self._config["system.swap.usage"]:
|
for metric in self._config["system.swap.usage"]:
|
||||||
self._system_swap_usage_labels["state"] = metric
|
self._system_swap_usage_labels["state"] = metric
|
||||||
observer.observe(
|
if hasattr(system_swap, metric):
|
||||||
getattr(system_swap, metric), self._system_swap_usage_labels
|
observer.observe(
|
||||||
)
|
getattr(system_swap, metric),
|
||||||
|
self._system_swap_usage_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_swap_utilization(
|
def _get_system_swap_utilization(
|
||||||
self, observer: metrics.ValueObserver
|
self, observer: metrics.ValueObserver
|
||||||
@ -456,11 +462,12 @@ class SystemMetrics:
|
|||||||
system_swap = psutil.swap_memory()
|
system_swap = psutil.swap_memory()
|
||||||
|
|
||||||
for metric in self._config["system.swap.utilization"]:
|
for metric in self._config["system.swap.utilization"]:
|
||||||
self._system_swap_utilization_labels["state"] = metric
|
if hasattr(system_swap, metric):
|
||||||
observer.observe(
|
self._system_swap_utilization_labels["state"] = metric
|
||||||
getattr(system_swap, metric) / system_swap.total,
|
observer.observe(
|
||||||
self._system_swap_utilization_labels,
|
getattr(system_swap, metric) / system_swap.total,
|
||||||
)
|
self._system_swap_utilization_labels,
|
||||||
|
)
|
||||||
|
|
||||||
# TODO Add _get_system_swap_page_faults
|
# TODO Add _get_system_swap_page_faults
|
||||||
# TODO Add _get_system_swap_page_operations
|
# TODO Add _get_system_swap_page_operations
|
||||||
@ -473,12 +480,13 @@ class SystemMetrics:
|
|||||||
"""
|
"""
|
||||||
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
||||||
for metric in self._config["system.disk.io"]:
|
for metric in self._config["system.disk.io"]:
|
||||||
self._system_disk_io_labels["device"] = device
|
if hasattr(counters, "{}_bytes".format(metric)):
|
||||||
self._system_disk_io_labels["direction"] = metric
|
self._system_disk_io_labels["device"] = device
|
||||||
observer.observe(
|
self._system_disk_io_labels["direction"] = metric
|
||||||
getattr(counters, "{}_bytes".format(metric)),
|
observer.observe(
|
||||||
self._system_disk_io_labels,
|
getattr(counters, "{}_bytes".format(metric)),
|
||||||
)
|
self._system_disk_io_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_disk_operations(
|
def _get_system_disk_operations(
|
||||||
self, observer: metrics.SumObserver
|
self, observer: metrics.SumObserver
|
||||||
@ -490,12 +498,13 @@ class SystemMetrics:
|
|||||||
"""
|
"""
|
||||||
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
||||||
for metric in self._config["system.disk.operations"]:
|
for metric in self._config["system.disk.operations"]:
|
||||||
self._system_disk_operations_labels["device"] = device
|
if hasattr(counters, "{}_count".format(metric)):
|
||||||
self._system_disk_operations_labels["direction"] = metric
|
self._system_disk_operations_labels["device"] = device
|
||||||
observer.observe(
|
self._system_disk_operations_labels["direction"] = metric
|
||||||
getattr(counters, "{}_count".format(metric)),
|
observer.observe(
|
||||||
self._system_disk_operations_labels,
|
getattr(counters, "{}_count".format(metric)),
|
||||||
)
|
self._system_disk_operations_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_disk_time(self, observer: metrics.SumObserver) -> None:
|
def _get_system_disk_time(self, observer: metrics.SumObserver) -> None:
|
||||||
"""Observer callback for disk time
|
"""Observer callback for disk time
|
||||||
@ -505,12 +514,13 @@ class SystemMetrics:
|
|||||||
"""
|
"""
|
||||||
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
||||||
for metric in self._config["system.disk.time"]:
|
for metric in self._config["system.disk.time"]:
|
||||||
self._system_disk_time_labels["device"] = device
|
if hasattr(counters, "{}_time".format(metric)):
|
||||||
self._system_disk_time_labels["direction"] = metric
|
self._system_disk_time_labels["device"] = device
|
||||||
observer.observe(
|
self._system_disk_time_labels["direction"] = metric
|
||||||
getattr(counters, "{}_time".format(metric)) / 1000,
|
observer.observe(
|
||||||
self._system_disk_time_labels,
|
getattr(counters, "{}_time".format(metric)) / 1000,
|
||||||
)
|
self._system_disk_time_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_disk_merged(self, observer: metrics.SumObserver) -> None:
|
def _get_system_disk_merged(self, observer: metrics.SumObserver) -> None:
|
||||||
"""Observer callback for disk merged operations
|
"""Observer callback for disk merged operations
|
||||||
@ -524,12 +534,13 @@ class SystemMetrics:
|
|||||||
|
|
||||||
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
for device, counters in psutil.disk_io_counters(perdisk=True).items():
|
||||||
for metric in self._config["system.disk.time"]:
|
for metric in self._config["system.disk.time"]:
|
||||||
self._system_disk_merged_labels["device"] = device
|
if hasattr(counters, "{}_merged_count".format(metric)):
|
||||||
self._system_disk_merged_labels["direction"] = metric
|
self._system_disk_merged_labels["device"] = device
|
||||||
observer.observe(
|
self._system_disk_merged_labels["direction"] = metric
|
||||||
getattr(counters, "{}_merged_count".format(metric)),
|
observer.observe(
|
||||||
self._system_disk_merged_labels,
|
getattr(counters, "{}_merged_count".format(metric)),
|
||||||
)
|
self._system_disk_merged_labels,
|
||||||
|
)
|
||||||
|
|
||||||
# TODO Add _get_system_filesystem_usage
|
# TODO Add _get_system_filesystem_usage
|
||||||
# TODO Add _get_system_filesystem_utilization
|
# TODO Add _get_system_filesystem_utilization
|
||||||
@ -548,14 +559,17 @@ class SystemMetrics:
|
|||||||
for device, counters in psutil.net_io_counters(pernic=True).items():
|
for device, counters in psutil.net_io_counters(pernic=True).items():
|
||||||
for metric in self._config["system.network.dropped.packets"]:
|
for metric in self._config["system.network.dropped.packets"]:
|
||||||
in_out = {"receive": "in", "transmit": "out"}[metric]
|
in_out = {"receive": "in", "transmit": "out"}[metric]
|
||||||
self._system_network_dropped_packets_labels["device"] = device
|
if hasattr(counters, "drop{}".format(in_out)):
|
||||||
self._system_network_dropped_packets_labels[
|
self._system_network_dropped_packets_labels[
|
||||||
"direction"
|
"device"
|
||||||
] = metric
|
] = device
|
||||||
observer.observe(
|
self._system_network_dropped_packets_labels[
|
||||||
getattr(counters, "drop{}".format(in_out)),
|
"direction"
|
||||||
self._system_network_dropped_packets_labels,
|
] = metric
|
||||||
)
|
observer.observe(
|
||||||
|
getattr(counters, "drop{}".format(in_out)),
|
||||||
|
self._system_network_dropped_packets_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_network_packets(
|
def _get_system_network_packets(
|
||||||
self, observer: metrics.SumObserver
|
self, observer: metrics.SumObserver
|
||||||
@ -569,12 +583,13 @@ class SystemMetrics:
|
|||||||
for device, counters in psutil.net_io_counters(pernic=True).items():
|
for device, counters in psutil.net_io_counters(pernic=True).items():
|
||||||
for metric in self._config["system.network.dropped.packets"]:
|
for metric in self._config["system.network.dropped.packets"]:
|
||||||
recv_sent = {"receive": "recv", "transmit": "sent"}[metric]
|
recv_sent = {"receive": "recv", "transmit": "sent"}[metric]
|
||||||
self._system_network_packets_labels["device"] = device
|
if hasattr(counters, "packets_{}".format(recv_sent)):
|
||||||
self._system_network_packets_labels["direction"] = metric
|
self._system_network_packets_labels["device"] = device
|
||||||
observer.observe(
|
self._system_network_packets_labels["direction"] = metric
|
||||||
getattr(counters, "packets_{}".format(recv_sent)),
|
observer.observe(
|
||||||
self._system_network_packets_labels,
|
getattr(counters, "packets_{}".format(recv_sent)),
|
||||||
)
|
self._system_network_packets_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_network_errors(
|
def _get_system_network_errors(
|
||||||
self, observer: metrics.SumObserver
|
self, observer: metrics.SumObserver
|
||||||
@ -587,12 +602,13 @@ class SystemMetrics:
|
|||||||
for device, counters in psutil.net_io_counters(pernic=True).items():
|
for device, counters in psutil.net_io_counters(pernic=True).items():
|
||||||
for metric in self._config["system.network.errors"]:
|
for metric in self._config["system.network.errors"]:
|
||||||
in_out = {"receive": "in", "transmit": "out"}[metric]
|
in_out = {"receive": "in", "transmit": "out"}[metric]
|
||||||
self._system_network_errors_labels["device"] = device
|
if hasattr(counters, "err{}".format(in_out)):
|
||||||
self._system_network_errors_labels["direction"] = metric
|
self._system_network_errors_labels["device"] = device
|
||||||
observer.observe(
|
self._system_network_errors_labels["direction"] = metric
|
||||||
getattr(counters, "err{}".format(in_out)),
|
observer.observe(
|
||||||
self._system_network_errors_labels,
|
getattr(counters, "err{}".format(in_out)),
|
||||||
)
|
self._system_network_errors_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_network_io(self, observer: metrics.SumObserver) -> None:
|
def _get_system_network_io(self, observer: metrics.SumObserver) -> None:
|
||||||
"""Observer callback for network IO
|
"""Observer callback for network IO
|
||||||
@ -604,12 +620,13 @@ class SystemMetrics:
|
|||||||
for device, counters in psutil.net_io_counters(pernic=True).items():
|
for device, counters in psutil.net_io_counters(pernic=True).items():
|
||||||
for metric in self._config["system.network.dropped.packets"]:
|
for metric in self._config["system.network.dropped.packets"]:
|
||||||
recv_sent = {"receive": "recv", "transmit": "sent"}[metric]
|
recv_sent = {"receive": "recv", "transmit": "sent"}[metric]
|
||||||
self._system_network_io_labels["device"] = device
|
if hasattr(counters, "bytes_{}".format(recv_sent)):
|
||||||
self._system_network_io_labels["direction"] = metric
|
self._system_network_io_labels["device"] = device
|
||||||
observer.observe(
|
self._system_network_io_labels["direction"] = metric
|
||||||
getattr(counters, "bytes_{}".format(recv_sent)),
|
observer.observe(
|
||||||
self._system_network_io_labels,
|
getattr(counters, "bytes_{}".format(recv_sent)),
|
||||||
)
|
self._system_network_io_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_system_network_connections(
|
def _get_system_network_connections(
|
||||||
self, observer: metrics.UpDownSumObserver
|
self, observer: metrics.UpDownSumObserver
|
||||||
@ -662,10 +679,11 @@ class SystemMetrics:
|
|||||||
"""
|
"""
|
||||||
proc_memory = self._proc.memory_info()
|
proc_memory = self._proc.memory_info()
|
||||||
for metric in self._config["runtime.memory"]:
|
for metric in self._config["runtime.memory"]:
|
||||||
self._runtime_memory_labels["type"] = metric
|
if hasattr(proc_memory, metric):
|
||||||
observer.observe(
|
self._runtime_memory_labels["type"] = metric
|
||||||
getattr(proc_memory, metric), self._runtime_memory_labels,
|
observer.observe(
|
||||||
)
|
getattr(proc_memory, metric), self._runtime_memory_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_runtime_cpu_time(self, observer: metrics.SumObserver) -> None:
|
def _get_runtime_cpu_time(self, observer: metrics.SumObserver) -> None:
|
||||||
"""Observer callback for runtime CPU time
|
"""Observer callback for runtime CPU time
|
||||||
@ -675,10 +693,11 @@ class SystemMetrics:
|
|||||||
"""
|
"""
|
||||||
proc_cpu = self._proc.cpu_times()
|
proc_cpu = self._proc.cpu_times()
|
||||||
for metric in self._config["runtime.cpu.time"]:
|
for metric in self._config["runtime.cpu.time"]:
|
||||||
self._runtime_cpu_time_labels["type"] = metric
|
if hasattr(proc_cpu, metric):
|
||||||
observer.observe(
|
self._runtime_cpu_time_labels["type"] = metric
|
||||||
getattr(proc_cpu, metric), self._runtime_cpu_time_labels,
|
observer.observe(
|
||||||
)
|
getattr(proc_cpu, metric), self._runtime_cpu_time_labels,
|
||||||
|
)
|
||||||
|
|
||||||
def _get_runtime_gc_count(self, observer: metrics.SumObserver) -> None:
|
def _get_runtime_gc_count(self, observer: metrics.SumObserver) -> None:
|
||||||
"""Observer callback for garbage collection
|
"""Observer callback for garbage collection
|
||||||
|
Reference in New Issue
Block a user