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:
alrex
2020-10-06 08:23:27 -07:00
committed by GitHub
parent 629597e883
commit a1bb8772ae
2 changed files with 107 additions and 85 deletions

View File

@ -2,6 +2,9 @@
## 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
Released 2020-09-17

View File

@ -372,6 +372,7 @@ class SystemMetrics:
"""
for cpu, times in enumerate(psutil.cpu_times(percpu=True)):
for metric in self._config["system.cpu.time"]:
if hasattr(times, metric):
self._system_cpu_time_labels["state"] = metric
self._system_cpu_time_labels["cpu"] = cpu + 1
observer.observe(
@ -391,6 +392,7 @@ class SystemMetrics:
psutil.cpu_times_percent(percpu=True)
):
for metric in self._config["system.cpu.utilization"]:
if hasattr(times_percent, metric):
self._system_cpu_utilization_labels["state"] = metric
self._system_cpu_utilization_labels["cpu"] = cpu + 1
observer.observe(
@ -409,6 +411,7 @@ class SystemMetrics:
virtual_memory = psutil.virtual_memory()
for metric in self._config["system.memory.usage"]:
self._system_memory_usage_labels["state"] = metric
if hasattr(virtual_memory, metric):
observer.observe(
getattr(virtual_memory, metric),
self._system_memory_usage_labels,
@ -426,6 +429,7 @@ class SystemMetrics:
for metric in self._config["system.memory.utilization"]:
self._system_memory_utilization_labels["state"] = metric
if hasattr(system_memory, metric):
observer.observe(
getattr(system_memory, metric) / system_memory.total,
self._system_memory_utilization_labels,
@ -441,8 +445,10 @@ class SystemMetrics:
for metric in self._config["system.swap.usage"]:
self._system_swap_usage_labels["state"] = metric
if hasattr(system_swap, metric):
observer.observe(
getattr(system_swap, metric), self._system_swap_usage_labels
getattr(system_swap, metric),
self._system_swap_usage_labels,
)
def _get_system_swap_utilization(
@ -456,6 +462,7 @@ class SystemMetrics:
system_swap = psutil.swap_memory()
for metric in self._config["system.swap.utilization"]:
if hasattr(system_swap, metric):
self._system_swap_utilization_labels["state"] = metric
observer.observe(
getattr(system_swap, metric) / system_swap.total,
@ -473,6 +480,7 @@ class SystemMetrics:
"""
for device, counters in psutil.disk_io_counters(perdisk=True).items():
for metric in self._config["system.disk.io"]:
if hasattr(counters, "{}_bytes".format(metric)):
self._system_disk_io_labels["device"] = device
self._system_disk_io_labels["direction"] = metric
observer.observe(
@ -490,6 +498,7 @@ class SystemMetrics:
"""
for device, counters in psutil.disk_io_counters(perdisk=True).items():
for metric in self._config["system.disk.operations"]:
if hasattr(counters, "{}_count".format(metric)):
self._system_disk_operations_labels["device"] = device
self._system_disk_operations_labels["direction"] = metric
observer.observe(
@ -505,6 +514,7 @@ class SystemMetrics:
"""
for device, counters in psutil.disk_io_counters(perdisk=True).items():
for metric in self._config["system.disk.time"]:
if hasattr(counters, "{}_time".format(metric)):
self._system_disk_time_labels["device"] = device
self._system_disk_time_labels["direction"] = metric
observer.observe(
@ -524,6 +534,7 @@ class SystemMetrics:
for device, counters in psutil.disk_io_counters(perdisk=True).items():
for metric in self._config["system.disk.time"]:
if hasattr(counters, "{}_merged_count".format(metric)):
self._system_disk_merged_labels["device"] = device
self._system_disk_merged_labels["direction"] = metric
observer.observe(
@ -548,7 +559,10 @@ class SystemMetrics:
for device, counters in psutil.net_io_counters(pernic=True).items():
for metric in self._config["system.network.dropped.packets"]:
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[
"device"
] = device
self._system_network_dropped_packets_labels[
"direction"
] = metric
@ -569,6 +583,7 @@ class SystemMetrics:
for device, counters in psutil.net_io_counters(pernic=True).items():
for metric in self._config["system.network.dropped.packets"]:
recv_sent = {"receive": "recv", "transmit": "sent"}[metric]
if hasattr(counters, "packets_{}".format(recv_sent)):
self._system_network_packets_labels["device"] = device
self._system_network_packets_labels["direction"] = metric
observer.observe(
@ -587,6 +602,7 @@ class SystemMetrics:
for device, counters in psutil.net_io_counters(pernic=True).items():
for metric in self._config["system.network.errors"]:
in_out = {"receive": "in", "transmit": "out"}[metric]
if hasattr(counters, "err{}".format(in_out)):
self._system_network_errors_labels["device"] = device
self._system_network_errors_labels["direction"] = metric
observer.observe(
@ -604,6 +620,7 @@ class SystemMetrics:
for device, counters in psutil.net_io_counters(pernic=True).items():
for metric in self._config["system.network.dropped.packets"]:
recv_sent = {"receive": "recv", "transmit": "sent"}[metric]
if hasattr(counters, "bytes_{}".format(recv_sent)):
self._system_network_io_labels["device"] = device
self._system_network_io_labels["direction"] = metric
observer.observe(
@ -662,6 +679,7 @@ class SystemMetrics:
"""
proc_memory = self._proc.memory_info()
for metric in self._config["runtime.memory"]:
if hasattr(proc_memory, metric):
self._runtime_memory_labels["type"] = metric
observer.observe(
getattr(proc_memory, metric), self._runtime_memory_labels,
@ -675,6 +693,7 @@ class SystemMetrics:
"""
proc_cpu = self._proc.cpu_times()
for metric in self._config["runtime.cpu.time"]:
if hasattr(proc_cpu, metric):
self._runtime_cpu_time_labels["type"] = metric
observer.observe(
getattr(proc_cpu, metric), self._runtime_cpu_time_labels,