From 3e67893cbab3d920dcb80de7e1d2af84f8576b06 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Tue, 10 May 2022 12:22:58 -0400 Subject: [PATCH] Update system metrics callbacks to accept CallbackOptions (#1084) --- .github/workflows/test.yml | 6 +- .../system_metrics/__init__.py | 74 ++++++++++++++----- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fe968fe909..03d0047c5b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,7 +6,7 @@ on: - 'release/*' pull_request: env: - CORE_REPO_SHA: 2c1cb20543fcc0bcfe5525d8a695e92babec06db + CORE_REPO_SHA: f367ec2045b2588be95dfa11913868c1d4fcbbc2 jobs: build: @@ -42,7 +42,7 @@ jobs: path: | .tox ~/.cache/pip - key: v4-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }} + key: v5-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }} - name: run tox run: tox -f ${{ matrix.python-version }}-${{ matrix.package }} -- --benchmark-json=${{ env.RUN_MATRIX_COMBINATION }}-benchmark.json # - name: Find and merge ${{ matrix.package }} benchmarks @@ -118,7 +118,7 @@ jobs: path: | .tox ~/.cache/pip - key: v4-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }} + key: v5-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }} - name: run tox run: tox -e ${{ matrix.tox-environment }} - name: Ensure generated code is up to date diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py index 0b6e232360..8d76e5fcc3 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py @@ -76,7 +76,7 @@ import psutil -from opentelemetry._metrics import Observation, get_meter +from opentelemetry._metrics import CallbackOptions, Observation, get_meter # FIXME Remove this pyling disabling line when Github issue is cleared # pylint: disable=no-name-in-module @@ -320,7 +320,9 @@ def _instrument(self, **kwargs): def _uninstrument(self, **__): pass - def _get_system_cpu_time(self) -> Iterable[Observation]: + def _get_system_cpu_time( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for system CPU time""" for cpu, times in enumerate(psutil.cpu_times(percpu=True)): for metric in self._config["system.cpu.time"]: @@ -331,7 +333,9 @@ def _get_system_cpu_time(self) -> Iterable[Observation]: getattr(times, metric), self._system_cpu_time_labels ) - def _get_system_cpu_utilization(self) -> Iterable[Observation]: + def _get_system_cpu_utilization( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for system CPU utilization""" for cpu, times_percent in enumerate( @@ -346,7 +350,9 @@ def _get_system_cpu_utilization(self) -> Iterable[Observation]: self._system_cpu_utilization_labels, ) - def _get_system_memory_usage(self) -> Iterable[Observation]: + def _get_system_memory_usage( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for memory usage""" virtual_memory = psutil.virtual_memory() for metric in self._config["system.memory.usage"]: @@ -357,7 +363,9 @@ def _get_system_memory_usage(self) -> Iterable[Observation]: self._system_memory_usage_labels, ) - def _get_system_memory_utilization(self) -> Iterable[Observation]: + def _get_system_memory_utilization( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for memory utilization""" system_memory = psutil.virtual_memory() @@ -369,7 +377,9 @@ def _get_system_memory_utilization(self) -> Iterable[Observation]: self._system_memory_utilization_labels, ) - def _get_system_swap_usage(self) -> Iterable[Observation]: + def _get_system_swap_usage( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for swap usage""" system_swap = psutil.swap_memory() @@ -381,7 +391,9 @@ def _get_system_swap_usage(self) -> Iterable[Observation]: self._system_swap_usage_labels, ) - def _get_system_swap_utilization(self) -> Iterable[Observation]: + def _get_system_swap_utilization( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for swap utilization""" system_swap = psutil.swap_memory() @@ -393,7 +405,9 @@ def _get_system_swap_utilization(self) -> Iterable[Observation]: self._system_swap_utilization_labels, ) - def _get_system_disk_io(self) -> Iterable[Observation]: + def _get_system_disk_io( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for disk IO""" for device, counters in psutil.disk_io_counters(perdisk=True).items(): for metric in self._config["system.disk.io"]: @@ -405,7 +419,9 @@ def _get_system_disk_io(self) -> Iterable[Observation]: self._system_disk_io_labels, ) - def _get_system_disk_operations(self) -> Iterable[Observation]: + def _get_system_disk_operations( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for disk operations""" for device, counters in psutil.disk_io_counters(perdisk=True).items(): for metric in self._config["system.disk.operations"]: @@ -417,7 +433,9 @@ def _get_system_disk_operations(self) -> Iterable[Observation]: self._system_disk_operations_labels, ) - def _get_system_disk_time(self) -> Iterable[Observation]: + def _get_system_disk_time( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for disk time""" for device, counters in psutil.disk_io_counters(perdisk=True).items(): for metric in self._config["system.disk.time"]: @@ -429,7 +447,9 @@ def _get_system_disk_time(self) -> Iterable[Observation]: self._system_disk_time_labels, ) - def _get_system_disk_merged(self) -> Iterable[Observation]: + def _get_system_disk_merged( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for disk merged operations""" # FIXME The units in the spec is 1, it seems like it should be @@ -445,7 +465,9 @@ def _get_system_disk_merged(self) -> Iterable[Observation]: self._system_disk_merged_labels, ) - def _get_system_network_dropped_packets(self) -> Iterable[Observation]: + def _get_system_network_dropped_packets( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for network dropped packets""" for device, counters in psutil.net_io_counters(pernic=True).items(): @@ -463,7 +485,9 @@ def _get_system_network_dropped_packets(self) -> Iterable[Observation]: self._system_network_dropped_packets_labels, ) - def _get_system_network_packets(self) -> Iterable[Observation]: + def _get_system_network_packets( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for network packets""" for device, counters in psutil.net_io_counters(pernic=True).items(): @@ -477,7 +501,9 @@ def _get_system_network_packets(self) -> Iterable[Observation]: self._system_network_packets_labels, ) - def _get_system_network_errors(self) -> Iterable[Observation]: + def _get_system_network_errors( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for network errors""" for device, counters in psutil.net_io_counters(pernic=True).items(): for metric in self._config["system.network.errors"]: @@ -490,7 +516,9 @@ def _get_system_network_errors(self) -> Iterable[Observation]: self._system_network_errors_labels, ) - def _get_system_network_io(self) -> Iterable[Observation]: + def _get_system_network_io( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for network IO""" for device, counters in psutil.net_io_counters(pernic=True).items(): @@ -504,7 +532,9 @@ def _get_system_network_io(self) -> Iterable[Observation]: self._system_network_io_labels, ) - def _get_system_network_connections(self) -> Iterable[Observation]: + def _get_system_network_connections( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for network connections""" # TODO How to find the device identifier for a particular # connection? @@ -542,7 +572,9 @@ def _get_system_network_connections(self) -> Iterable[Observation]: connection_counter["labels"], ) - def _get_runtime_memory(self) -> Iterable[Observation]: + def _get_runtime_memory( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for runtime memory""" proc_memory = self._proc.memory_info() for metric in self._config["runtime.memory"]: @@ -553,7 +585,9 @@ def _get_runtime_memory(self) -> Iterable[Observation]: self._runtime_memory_labels, ) - def _get_runtime_cpu_time(self) -> Iterable[Observation]: + def _get_runtime_cpu_time( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for runtime CPU time""" proc_cpu = self._proc.cpu_times() for metric in self._config["runtime.cpu.time"]: @@ -564,7 +598,9 @@ def _get_runtime_cpu_time(self) -> Iterable[Observation]: self._runtime_cpu_time_labels, ) - def _get_runtime_gc_count(self) -> Iterable[Observation]: + def _get_runtime_gc_count( + self, options: CallbackOptions + ) -> Iterable[Observation]: """Observer callback for garbage collection""" for index, count in enumerate(gc.get_count()): self._runtime_gc_count_labels["count"] = str(index)