Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Boten committed Apr 4, 2022
1 parent b9e2d7d commit 32687f8
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 414 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"system.disk.io": ["read", "write"],
"system.disk.operations": ["read", "write"],
"system.disk.time": ["read", "write"],
"system.disk.merged": ["read", "write"],
"system.network.dropped.packets": ["transmit", "receive"],
"system.network.packets": ["transmit", "receive"],
"system.network.errors": ["transmit", "receive"],
Expand Down Expand Up @@ -64,37 +63,37 @@
"runtime.memory": ["rss", "vms"],
"runtime.cpu.time": ["user", "system"],
}
SystemMetrics(exporter, config=configuration)
SystemMetrics(config=configuration)
API
---
"""

from typing import Collection

import gc
import os
from typing import Dict, List, Iterable, Optional
from platform import python_implementation

import psutil

from opentelemetry._metrics import get_meter_provider
from opentelemetry._metrics import get_meter
from opentelemetry._metrics.measurement import Measurement
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.instrumentation.system_metrics.version import __version__
from opentelemetry.instrumentation.system_metrics.package import _instruments
from opentelemetry.sdk.util import get_dict_as_key


class SystemMetrics:
class SystemMetrics(BaseInstrumentor):
# pylint: disable=too-many-statements
def __init__(
self,
interval: int = 30,
labels: Optional[Dict[str, str]] = None,
config: Optional[Dict[str, List[str]]] = None,
):
self._labels = {} if labels is None else labels
self.meter = get_meter_provider().get_meter(
"io.otel.python.instrumentation.system-metrics"
) # TODO: find a better name here
self._python_implementation = python_implementation().lower()
super().__init__()
if config is None:
self._config = {
"system.cpu.time": ["idle", "user", "system", "irq"],
Expand All @@ -108,7 +107,6 @@ def __init__(
"system.disk.io": ["read", "write"],
"system.disk.operations": ["read", "write"],
"system.disk.time": ["read", "write"],
"system.disk.merged": ["read", "write"],
# "system.filesystem.usage": [],
# "system.filesystem.utilization": [],
"system.network.dropped.packets": ["transmit", "receive"],
Expand All @@ -121,6 +119,20 @@ def __init__(
}
else:
self._config = config
self._labels = {} if labels is None else labels
self._meter = None

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
meter_provider = kwargs.get("meter_provider")
self._meter = get_meter(
"io.otel.python.instrumentation.system_metrics",
__version__,
meter_provider,
)
self._python_implementation = python_implementation().lower()

self._proc = psutil.Process(os.getpid())

Expand Down Expand Up @@ -153,92 +165,84 @@ def __init__(
self._runtime_cpu_time_labels = self._labels.copy()
self._runtime_gc_count_labels = self._labels.copy()

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_cpu_time,
name="system.cpu.time",
description="System CPU time",
unit="seconds",
)

self.meter.create_observable_gauge(
self._meter.create_observable_gauge(
callback=self._get_system_cpu_utilization,
name="system.cpu.utilization",
description="System CPU utilization",
unit="1",
)

self.meter.create_observable_gauge(
self._meter.create_observable_gauge(
callback=self._get_system_memory_usage,
name="system.memory.usage",
description="System memory usage",
unit="bytes",
)

self.meter.create_observable_gauge(
self._meter.create_observable_gauge(
callback=self._get_system_memory_utilization,
name="system.memory.utilization",
description="System memory utilization",
unit="1",
)

self.meter.create_observable_gauge(
self._meter.create_observable_gauge(
callback=self._get_system_swap_usage,
name="system.swap.usage",
description="System swap usage",
unit="pages",
)

self.meter.create_observable_gauge(
self._meter.create_observable_gauge(
callback=self._get_system_swap_utilization,
name="system.swap.utilization",
description="System swap utilization",
unit="1",
)

# # self.meter.create_observable_counter(
# # self._meter.create_observable_counter(
# # callback=self._get_system_swap_page_faults,
# # name="system.swap.page_faults",
# # description="System swap page faults",
# # unit="faults",
# # value_type=int,
# # )

# # self.meter.create_observable_counter(
# # self._meter.create_observable_counter(
# # callback=self._get_system_swap_page_operations,
# # name="system.swap.page_operations",
# # description="System swap page operations",
# # unit="operations",
# # value_type=int,
# # )

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_disk_io,
name="system.disk.io",
description="System disk IO",
unit="bytes",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_disk_operations,
name="system.disk.operations",
description="System disk operations",
unit="operations",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_disk_time,
name="system.disk.time",
description="System disk time",
unit="seconds",
)

self.meter.create_observable_counter(
callback=self._get_system_disk_merged,
name="system.disk.merged",
description="System disk merged",
unit="1",
)

# self.accumulator.register_valueobserver(
# callback=self._get_system_filesystem_usage,
# name="system.filesystem.usage",
Expand All @@ -247,50 +251,50 @@ def __init__(
# value_type=int,
# )

# self.meter.create_observable_gauge(
# self._meter.create_observable_gauge(
# callback=self._get_system_filesystem_utilization,
# name="system.filesystem.utilization",
# description="System filesystem utilization",
# unit="1",
# value_type=float,
# )

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_network_dropped_packets,
name="system.network.dropped_packets",
description="System network dropped_packets",
unit="packets",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_network_packets,
name="system.network.packets",
description="System network packets",
unit="packets",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_network_errors,
name="system.network.errors",
description="System network errors",
unit="errors",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_system_network_io,
name="system.network.io",
description="System network io",
unit="bytes",
)

self.meter.create_observable_up_down_counter(
self._meter.create_observable_up_down_counter(
callback=self._get_system_network_connections,
name="system.network.connections",
description="System network connections",
unit="connections",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_runtime_memory,
name="runtime.{}.memory".format(self._python_implementation),
description="Runtime {} memory".format(
Expand All @@ -299,7 +303,7 @@ def __init__(
unit="bytes",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_runtime_cpu_time,
name="runtime.{}.cpu_time".format(self._python_implementation),
description="Runtime {} CPU time".format(
Expand All @@ -308,14 +312,17 @@ def __init__(
unit="seconds",
)

self.meter.create_observable_counter(
self._meter.create_observable_counter(
callback=self._get_runtime_gc_count,
name="runtime.{}.gc_count".format(self._python_implementation),
description="Runtime {} GC count".format(
self._python_implementation
),
unit="bytes",
)

def _uninstrument(self, **__):
pass

def _get_system_cpu_time(self) -> Iterable[Measurement]:
"""Observer callback for system CPU time
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


_instruments = ("psutil >= 5.7.3",)
Loading

0 comments on commit 32687f8

Please sign in to comment.