Skip to content

Commit

Permalink
Fix lint and use metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Sep 3, 2020
1 parent 3824fa3 commit 680a54d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@

from opentelemetry import metrics
from opentelemetry.sdk.metrics import (
SumObserver, ValueObserver, UpDownSumObserver
SumObserver,
UpDownSumObserver,
ValueObserver,
)
from opentelemetry.sdk.util import get_dict_as_key
from opentelemetry.sdk.metrics.export import MetricsExporter
from opentelemetry.sdk.metrics.export.controller import PushController
from opentelemetry.sdk.util import get_dict_as_key


class SystemMetrics:
Expand Down Expand Up @@ -616,6 +618,9 @@ def _get_system_network_connections(
self._system_network_connections_labels[
"state"
] = net_connection.status
self._system_network_connections_labels[metric] = getattr(
net_connection, metric
)

connection_counters_key = get_dict_as_key(
self._system_network_connections_labels
Expand All @@ -626,13 +631,12 @@ def _get_system_network_connections(
else:
connection_counters[connection_counters_key] = {
"counter": 1,
"labels": self._system_network_connections_labels.copy()
"labels": self._system_network_connections_labels.copy(),
}

for connection_counter in connection_counters.values():
observer.observe(
connection_counter["counter"],
connection_counter["labels"],
connection_counter["counter"], connection_counter["labels"],
)

def _get_runtime_memory(self, observer: metrics.SumObserver) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _assert_metrics(self, observer_name, system_metrics, expected):
) in (
self.memory_metrics_exporter._exported_metrics # pylint: disable=protected-access
):
if (
if (
metric.labels in expected
and metric.instrument.name == observer_name
):
Expand All @@ -93,6 +93,7 @@ def _test_metrics(self, observer_name, expected):
# When this test case is executed, _get_system_cpu_utilization gets run
# too because of the controller thread which runs all observers. This patch
# is added here to stop a warning that would otherwise be raised.
# pylint: disable=unused-argument
@mock.patch("psutil.cpu_times_percent")
@mock.patch("psutil.cpu_times")
def test_system_cpu_time(self, mock_cpu_times, mock_cpu_times_percent):
Expand Down Expand Up @@ -633,21 +634,23 @@ def test_system_network_connections(self, mock_net_connections):
)
Type = namedtuple("Type", ["value"])
mock_net_connections.return_value = [
NetConnection(
family=1,
status="ESTABLISHED",
type=Type(value=2),
),
NetConnection(
family=1,
status="ESTABLISHED",
type=Type(value=1),
),
NetConnection(family=1, status="ESTABLISHED", type=Type(value=2),),
NetConnection(family=1, status="ESTABLISHED", type=Type(value=1),),
]

expected = {
(("protocol", "udp"), ("state", "ESTABLISHED"),): 1,
(("protocol", "tcp"), ("state", "ESTABLISHED"),): 1,
(
("family", 1),
("protocol", "udp"),
("state", "ESTABLISHED"),
("type", Type(value=2)),
): 1,
(
("family", 1),
("protocol", "tcp"),
("state", "ESTABLISHED"),
("type", Type(value=1)),
): 1,
}
self._test_metrics("system.network.connections", expected)

Expand Down

0 comments on commit 680a54d

Please sign in to comment.