Skip to content

Commit 3e67893

Browse files
authored
Update system metrics callbacks to accept CallbackOptions (#1084)
1 parent fa56c6c commit 3e67893

File tree

2 files changed

+58
-22
lines changed
  • .github/workflows
  • instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics

2 files changed

+58
-22
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66
- 'release/*'
77
pull_request:
88
env:
9-
CORE_REPO_SHA: 2c1cb20543fcc0bcfe5525d8a695e92babec06db
9+
CORE_REPO_SHA: f367ec2045b2588be95dfa11913868c1d4fcbbc2
1010

1111
jobs:
1212
build:
@@ -42,7 +42,7 @@ jobs:
4242
path: |
4343
.tox
4444
~/.cache/pip
45-
key: v4-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }}
45+
key: v5-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }}
4646
- name: run tox
4747
run: tox -f ${{ matrix.python-version }}-${{ matrix.package }} -- --benchmark-json=${{ env.RUN_MATRIX_COMBINATION }}-benchmark.json
4848
# - name: Find and merge ${{ matrix.package }} benchmarks
@@ -118,7 +118,7 @@ jobs:
118118
path: |
119119
.tox
120120
~/.cache/pip
121-
key: v4-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }}
121+
key: v5-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }}
122122
- name: run tox
123123
run: tox -e ${{ matrix.tox-environment }}
124124
- name: Ensure generated code is up to date

instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676

7777
import psutil
7878

79-
from opentelemetry._metrics import Observation, get_meter
79+
from opentelemetry._metrics import CallbackOptions, Observation, get_meter
8080

8181
# FIXME Remove this pyling disabling line when Github issue is cleared
8282
# pylint: disable=no-name-in-module
@@ -320,7 +320,9 @@ def _instrument(self, **kwargs):
320320
def _uninstrument(self, **__):
321321
pass
322322

323-
def _get_system_cpu_time(self) -> Iterable[Observation]:
323+
def _get_system_cpu_time(
324+
self, options: CallbackOptions
325+
) -> Iterable[Observation]:
324326
"""Observer callback for system CPU time"""
325327
for cpu, times in enumerate(psutil.cpu_times(percpu=True)):
326328
for metric in self._config["system.cpu.time"]:
@@ -331,7 +333,9 @@ def _get_system_cpu_time(self) -> Iterable[Observation]:
331333
getattr(times, metric), self._system_cpu_time_labels
332334
)
333335

334-
def _get_system_cpu_utilization(self) -> Iterable[Observation]:
336+
def _get_system_cpu_utilization(
337+
self, options: CallbackOptions
338+
) -> Iterable[Observation]:
335339
"""Observer callback for system CPU utilization"""
336340

337341
for cpu, times_percent in enumerate(
@@ -346,7 +350,9 @@ def _get_system_cpu_utilization(self) -> Iterable[Observation]:
346350
self._system_cpu_utilization_labels,
347351
)
348352

349-
def _get_system_memory_usage(self) -> Iterable[Observation]:
353+
def _get_system_memory_usage(
354+
self, options: CallbackOptions
355+
) -> Iterable[Observation]:
350356
"""Observer callback for memory usage"""
351357
virtual_memory = psutil.virtual_memory()
352358
for metric in self._config["system.memory.usage"]:
@@ -357,7 +363,9 @@ def _get_system_memory_usage(self) -> Iterable[Observation]:
357363
self._system_memory_usage_labels,
358364
)
359365

360-
def _get_system_memory_utilization(self) -> Iterable[Observation]:
366+
def _get_system_memory_utilization(
367+
self, options: CallbackOptions
368+
) -> Iterable[Observation]:
361369
"""Observer callback for memory utilization"""
362370
system_memory = psutil.virtual_memory()
363371

@@ -369,7 +377,9 @@ def _get_system_memory_utilization(self) -> Iterable[Observation]:
369377
self._system_memory_utilization_labels,
370378
)
371379

372-
def _get_system_swap_usage(self) -> Iterable[Observation]:
380+
def _get_system_swap_usage(
381+
self, options: CallbackOptions
382+
) -> Iterable[Observation]:
373383
"""Observer callback for swap usage"""
374384
system_swap = psutil.swap_memory()
375385

@@ -381,7 +391,9 @@ def _get_system_swap_usage(self) -> Iterable[Observation]:
381391
self._system_swap_usage_labels,
382392
)
383393

384-
def _get_system_swap_utilization(self) -> Iterable[Observation]:
394+
def _get_system_swap_utilization(
395+
self, options: CallbackOptions
396+
) -> Iterable[Observation]:
385397
"""Observer callback for swap utilization"""
386398
system_swap = psutil.swap_memory()
387399

@@ -393,7 +405,9 @@ def _get_system_swap_utilization(self) -> Iterable[Observation]:
393405
self._system_swap_utilization_labels,
394406
)
395407

396-
def _get_system_disk_io(self) -> Iterable[Observation]:
408+
def _get_system_disk_io(
409+
self, options: CallbackOptions
410+
) -> Iterable[Observation]:
397411
"""Observer callback for disk IO"""
398412
for device, counters in psutil.disk_io_counters(perdisk=True).items():
399413
for metric in self._config["system.disk.io"]:
@@ -405,7 +419,9 @@ def _get_system_disk_io(self) -> Iterable[Observation]:
405419
self._system_disk_io_labels,
406420
)
407421

408-
def _get_system_disk_operations(self) -> Iterable[Observation]:
422+
def _get_system_disk_operations(
423+
self, options: CallbackOptions
424+
) -> Iterable[Observation]:
409425
"""Observer callback for disk operations"""
410426
for device, counters in psutil.disk_io_counters(perdisk=True).items():
411427
for metric in self._config["system.disk.operations"]:
@@ -417,7 +433,9 @@ def _get_system_disk_operations(self) -> Iterable[Observation]:
417433
self._system_disk_operations_labels,
418434
)
419435

420-
def _get_system_disk_time(self) -> Iterable[Observation]:
436+
def _get_system_disk_time(
437+
self, options: CallbackOptions
438+
) -> Iterable[Observation]:
421439
"""Observer callback for disk time"""
422440
for device, counters in psutil.disk_io_counters(perdisk=True).items():
423441
for metric in self._config["system.disk.time"]:
@@ -429,7 +447,9 @@ def _get_system_disk_time(self) -> Iterable[Observation]:
429447
self._system_disk_time_labels,
430448
)
431449

432-
def _get_system_disk_merged(self) -> Iterable[Observation]:
450+
def _get_system_disk_merged(
451+
self, options: CallbackOptions
452+
) -> Iterable[Observation]:
433453
"""Observer callback for disk merged operations"""
434454

435455
# 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]:
445465
self._system_disk_merged_labels,
446466
)
447467

448-
def _get_system_network_dropped_packets(self) -> Iterable[Observation]:
468+
def _get_system_network_dropped_packets(
469+
self, options: CallbackOptions
470+
) -> Iterable[Observation]:
449471
"""Observer callback for network dropped packets"""
450472

451473
for device, counters in psutil.net_io_counters(pernic=True).items():
@@ -463,7 +485,9 @@ def _get_system_network_dropped_packets(self) -> Iterable[Observation]:
463485
self._system_network_dropped_packets_labels,
464486
)
465487

466-
def _get_system_network_packets(self) -> Iterable[Observation]:
488+
def _get_system_network_packets(
489+
self, options: CallbackOptions
490+
) -> Iterable[Observation]:
467491
"""Observer callback for network packets"""
468492

469493
for device, counters in psutil.net_io_counters(pernic=True).items():
@@ -477,7 +501,9 @@ def _get_system_network_packets(self) -> Iterable[Observation]:
477501
self._system_network_packets_labels,
478502
)
479503

480-
def _get_system_network_errors(self) -> Iterable[Observation]:
504+
def _get_system_network_errors(
505+
self, options: CallbackOptions
506+
) -> Iterable[Observation]:
481507
"""Observer callback for network errors"""
482508
for device, counters in psutil.net_io_counters(pernic=True).items():
483509
for metric in self._config["system.network.errors"]:
@@ -490,7 +516,9 @@ def _get_system_network_errors(self) -> Iterable[Observation]:
490516
self._system_network_errors_labels,
491517
)
492518

493-
def _get_system_network_io(self) -> Iterable[Observation]:
519+
def _get_system_network_io(
520+
self, options: CallbackOptions
521+
) -> Iterable[Observation]:
494522
"""Observer callback for network IO"""
495523

496524
for device, counters in psutil.net_io_counters(pernic=True).items():
@@ -504,7 +532,9 @@ def _get_system_network_io(self) -> Iterable[Observation]:
504532
self._system_network_io_labels,
505533
)
506534

507-
def _get_system_network_connections(self) -> Iterable[Observation]:
535+
def _get_system_network_connections(
536+
self, options: CallbackOptions
537+
) -> Iterable[Observation]:
508538
"""Observer callback for network connections"""
509539
# TODO How to find the device identifier for a particular
510540
# connection?
@@ -542,7 +572,9 @@ def _get_system_network_connections(self) -> Iterable[Observation]:
542572
connection_counter["labels"],
543573
)
544574

545-
def _get_runtime_memory(self) -> Iterable[Observation]:
575+
def _get_runtime_memory(
576+
self, options: CallbackOptions
577+
) -> Iterable[Observation]:
546578
"""Observer callback for runtime memory"""
547579
proc_memory = self._proc.memory_info()
548580
for metric in self._config["runtime.memory"]:
@@ -553,7 +585,9 @@ def _get_runtime_memory(self) -> Iterable[Observation]:
553585
self._runtime_memory_labels,
554586
)
555587

556-
def _get_runtime_cpu_time(self) -> Iterable[Observation]:
588+
def _get_runtime_cpu_time(
589+
self, options: CallbackOptions
590+
) -> Iterable[Observation]:
557591
"""Observer callback for runtime CPU time"""
558592
proc_cpu = self._proc.cpu_times()
559593
for metric in self._config["runtime.cpu.time"]:
@@ -564,7 +598,9 @@ def _get_runtime_cpu_time(self) -> Iterable[Observation]:
564598
self._runtime_cpu_time_labels,
565599
)
566600

567-
def _get_runtime_gc_count(self) -> Iterable[Observation]:
601+
def _get_runtime_gc_count(
602+
self, options: CallbackOptions
603+
) -> Iterable[Observation]:
568604
"""Observer callback for garbage collection"""
569605
for index, count in enumerate(gc.get_count()):
570606
self._runtime_gc_count_labels["count"] = str(index)

0 commit comments

Comments
 (0)