Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ext/system-metrics: adding instrumentation to collect system metrics #652

Merged
merged 21 commits into from
May 29, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use labels passed into constructor + feedback from review
Alex Boten committed May 7, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1bc093d79e804d0d1a1835e5776d671046eae199
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-system-metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,4 +2,4 @@

## Unreleased

- Initial release
- Initial release (https://github.com/open-telemetry/opentelemetry-python/pull/652)
1 change: 1 addition & 0 deletions ext/opentelemetry-ext-system-metrics/README.rst
Original file line number Diff line number Diff line change
@@ -19,5 +19,6 @@ Installation

References
----------
* `OpenTelemetry System Metrics Instrumentation <https://opentelemetry-python.readthedocs.io/en/latest/ext/system_metrics/system_metrics.html>`_
* `OpenTelemetry Project <https://opentelemetry.io/>`_

codeboten marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -22,7 +22,6 @@

from opentelemetry.ext.system_metrics import SystemMetrics
from opentelemetry.sdk.metrics.export import ConsoleMetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController

exporter = ConsoleMetricsExporter()
SystemMetrics(exporter)
@@ -61,6 +60,18 @@ def __init__(
)
self._proc = psutil.Process(os.getpid())
self._labels = labels
codeboten marked this conversation as resolved.
Show resolved Hide resolved
self._system_memory_labels = {}
self._system_cpu_labels = {}
self._network_bytes_labels = {}
self._runtime_memory_labels = {}
self._runtime_gc_labels = {}
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
# create the label set for each observer once
for key, value in self._labels.items():
self._system_memory_labels[key] = value
self._system_cpu_labels[key] = value
self._network_bytes_labels[key] = value
self._runtime_memory_labels[key] = value
self._runtime_gc_labels[key] = value
ocelotl marked this conversation as resolved.
Show resolved Hide resolved

self.meter.register_observer(
callback=self._get_system_memory,
@@ -109,28 +120,31 @@ def __init__(

def _get_system_memory(self, observer: metrics.Observer) -> None:
"""Observer callback for memory available

Args:
codeboten marked this conversation as resolved.
Show resolved Hide resolved
observer: the observer to update
"""
system_memory = psutil.virtual_memory()
for key, value in system_memory._asdict().items():
if not isinstance(value, int):
continue
labels = {"type": key}
observer.observe(value, labels)
self._system_memory_labels["type"] = key
observer.observe(value, self._system_memory_labels)

def _get_system_cpu(self, observer: metrics.Observer) -> None:
"""Observer callback for system cpu

Args:
observer: the observer to update
"""
cpu_times = psutil.cpu_times()
for key, value in cpu_times._asdict().items():
labels = {"type": key}
observer.observe(value, labels)
self._system_cpu_labels["type"] = key
observer.observe(value, self._system_cpu_labels)

def _get_network_bytes(self, observer: metrics.Observer) -> None:
"""Observer callback for network bytes

Args:
observer: the observer to update
"""
@@ -140,18 +154,21 @@ def _get_network_bytes(self, observer: metrics.Observer) -> None:
"bytes_sent",
]
for metric in _metrics:
labels = {"type": metric}
observer.observe(getattr(net_io, metric), labels)
self._network_bytes_labels["type"] = metric
observer.observe(
getattr(net_io, metric), self._network_bytes_labels
)

def _get_runtime_memory(self, observer: metrics.Observer) -> None:
"""Observer callback for runtime memory

Args:
observer: the observer to update
"""
proc_memory = self._proc.memory_info()
for key, value in proc_memory._asdict().items():
labels = {"type": key}
observer.observe(value, labels)
self._runtime_memory_labels["type"] = key
observer.observe(value, self._runtime_memory_labels)

lzchen marked this conversation as resolved.
Show resolved Hide resolved
def _get_runtime_gc_count(self, observer: metrics.Observer) -> None:
"""Observer callback for garbage collection
@@ -161,5 +178,5 @@ def _get_runtime_gc_count(self, observer: metrics.Observer) -> None:
"""
gc_count = gc.get_count()
for index, count in enumerate(gc_count):
labels = {"count": str(index)}
observer.observe(count, labels)
self._runtime_gc_labels["count"] = str(index)
observer.observe(count, self._runtime_gc_labels)
1 change: 0 additions & 1 deletion tests/util/src/opentelemetry/test/test_base.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@
from opentelemetry import metrics as metrics_api
from opentelemetry import trace as trace_api
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics import export as metrics_export
from opentelemetry.sdk.metrics.export.in_memory_metrics_exporter import (
InMemoryMetricsExporter,
)