Skip to content

Commit

Permalink
Update OS process metrics name according to semconv open-telemetry#2662
Browse files Browse the repository at this point in the history
Signed-off-by: KarthikeyanB, Arun <[email protected]>
  • Loading branch information
arunk1988 committed Jul 13, 2024
1 parent 6293d6a commit a9caf33
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@
"system.network.io": ["transmit", "receive"],
"system.network.connections": ["family", "type"],
"system.thread_count": None,
"process.runtime.memory": ["rss", "vms"],
"process.runtime.cpu.time": ["user", "system"],
"process.runtime.gc_count": None,
"process.runtime.thread_count": None,
"process.runtime.cpu.utilization": None,
"process.runtime.context_switches": ["involuntary", "voluntary"],
"process.memory": ["rss", "vms"],
"process.cpu.time": ["user", "system"],
"process.gc_count": None,
"process.thread.count": None,
"process.cpu.utilization": None,
"process.context_switches": ["involuntary", "voluntary"],
"process.open_file_descriptor.count": None,
}

Expand Down Expand Up @@ -164,12 +164,12 @@ def __init__(

self._system_thread_count_labels = self._labels.copy()

self._runtime_memory_labels = self._labels.copy()
self._runtime_cpu_time_labels = self._labels.copy()
self._runtime_gc_count_labels = self._labels.copy()
self._runtime_thread_count_labels = self._labels.copy()
self._runtime_cpu_utilization_labels = self._labels.copy()
self._runtime_context_switches_labels = self._labels.copy()
self._process_memory_labels = self._labels.copy()
self._process_cpu_time_labels = self._labels.copy()
self._process_gc_count_labels = self._labels.copy()
self._process_thread_count_labels = self._labels.copy()
self._process_cpu_utilization_labels = self._labels.copy()
self._process_context_switches_labels = self._labels.copy()
self._open_file_descriptor_count_labels = self._labels.copy()

def instrumentation_dependencies(self) -> Collection[str]:
Expand Down Expand Up @@ -345,55 +345,55 @@ def _instrument(self, **kwargs):
description="System active threads count",
)

if "process.runtime.memory" in self._config:
if "process.memory" in self._config:
self._meter.create_observable_up_down_counter(
name=f"process.runtime.{self._python_implementation}.memory",
callbacks=[self._get_runtime_memory],
description=f"Runtime {self._python_implementation} memory",
name="process.memory",
callbacks=[self._get_process_memory],
description="The amount of memory in use",
unit="bytes",
)

if "process.runtime.cpu.time" in self._config:
if "process.cpu.time" in self._config:
self._meter.create_observable_counter(
name=f"process.runtime.{self._python_implementation}.cpu_time",
callbacks=[self._get_runtime_cpu_time],
description=f"Runtime {self._python_implementation} CPU time",
name="process.cpu_time",
callbacks=[self._get_process_cpu_time],
description="Total CPU seconds broken down by different states",
unit="seconds",
)

if "process.runtime.gc_count" in self._config:
if "process.gc_count" in self._config:
if self._python_implementation == "pypy":
_logger.warning(
"The process.runtime.gc_count metric won't be collected because the interpreter is PyPy"
"The process.gc_count metric won't be collected because the interpreter is PyPy"
)
else:
self._meter.create_observable_counter(
name=f"process.runtime.{self._python_implementation}.gc_count",
callbacks=[self._get_runtime_gc_count],
description=f"Runtime {self._python_implementation} GC count",
name="process.gc_count",
callbacks=[self._get_process_gc_count],
description="Process GC count",
unit="bytes",
)

if "process.runtime.thread_count" in self._config:
if "process.thread.count" in self._config:
self._meter.create_observable_up_down_counter(
name=f"process.runtime.{self._python_implementation}.thread_count",
callbacks=[self._get_runtime_thread_count],
description="Runtime active threads count",
name="process.thread.count",
callbacks=[self._get_process_thread_count],
description="The number of threads currently used by the process",
)

if "process.runtime.cpu.utilization" in self._config:
if "process.cpu.utilization" in self._config:
self._meter.create_observable_gauge(
name=f"process.runtime.{self._python_implementation}.cpu.utilization",
callbacks=[self._get_runtime_cpu_utilization],
description="Runtime CPU utilization",
name="process.cpu.utilization",
callbacks=[self._get_process_cpu_utilization],
description="Process CPU utilization",
unit="1",
)

if "process.runtime.context_switches" in self._config:
if "process.context_switches" in self._config:
self._meter.create_observable_counter(
name=f"process.runtime.{self._python_implementation}.context_switches",
callbacks=[self._get_runtime_context_switches],
description="Runtime context switches",
name="process.context_switches",
callbacks=[self._get_process_context_switches],
description="The number voluntary and involuntary context switches performed by the process ",
unit="switches",
)

Expand Down Expand Up @@ -681,67 +681,67 @@ def _get_system_thread_count(
threading.active_count(), self._system_thread_count_labels
)

def _get_runtime_memory(
def _get_process_memory(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime memory"""
"""Observer callback for process memory"""
proc_memory = self._proc.memory_info()
for metric in self._config["process.runtime.memory"]:
for metric in self._config["process.memory"]:
if hasattr(proc_memory, metric):
self._runtime_memory_labels["type"] = metric
self._process_memory_labels["type"] = metric
yield Observation(
getattr(proc_memory, metric),
self._runtime_memory_labels.copy(),
self._process_memory_labels.copy(),
)

def _get_runtime_cpu_time(
def _get_process_cpu_time(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime CPU time"""
proc_cpu = self._proc.cpu_times()
for metric in self._config["process.runtime.cpu.time"]:
for metric in self._config["process.cpu.time"]:
if hasattr(proc_cpu, metric):
self._runtime_cpu_time_labels["type"] = metric
self._process_cpu_time_labels["type"] = metric
yield Observation(
getattr(proc_cpu, metric),
self._runtime_cpu_time_labels.copy(),
self._process_cpu_time_labels.copy(),
)

def _get_runtime_gc_count(
def _get_process_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)
yield Observation(count, self._runtime_gc_count_labels.copy())
self._process_gc_count_labels["count"] = str(index)
yield Observation(count, self._process_gc_count_labels.copy())

def _get_runtime_thread_count(
def _get_process_thread_count(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime active thread count"""
"""Observer callback for process threads count"""
yield Observation(
self._proc.num_threads(), self._runtime_thread_count_labels.copy()
self._proc.num_threads(), self._process_thread_count_labels.copy()
)

def _get_runtime_cpu_utilization(
def _get_process_cpu_utilization(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime CPU utilization"""
"""Observer callback for CPU utilization"""
proc_cpu_percent = self._proc.cpu_percent()
yield Observation(
proc_cpu_percent,
self._runtime_cpu_utilization_labels.copy(),
self._process_cpu_utilization_labels.copy(),
)

def _get_runtime_context_switches(
def _get_process_context_switches(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for runtime context switches"""
"""Observer callback for context switches"""
ctx_switches = self._proc.num_ctx_switches()
for metric in self._config["process.runtime.context_switches"]:
for metric in self._config["process.context_switches"]:
if hasattr(ctx_switches, metric):
self._runtime_context_switches_labels["type"] = metric
self._process_context_switches_labels["type"] = metric
yield Observation(
getattr(ctx_switches, metric),
self._runtime_context_switches_labels.copy(),
self._process_context_switches_labels.copy(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ def test_system_metrics_instrument(self):
"system.network.io",
"system.network.connections",
"system.thread_count",
f"process.runtime.{self.implementation}.memory",
f"process.runtime.{self.implementation}.cpu_time",
f"process.runtime.{self.implementation}.thread_count",
f"process.runtime.{self.implementation}.context_switches",
f"process.runtime.{self.implementation}.cpu.utilization",
"process.memory",
"process.cpu_time",
"process.thread.count",
"process.context_switches",
"process.cpu.utilization",
"process.open_file_descriptor.count",
]

Expand All @@ -126,29 +126,29 @@ def test_system_metrics_instrument(self):
else:
self.assertEqual(len(metric_names), 22)
observer_names.append(
f"process.runtime.{self.implementation}.gc_count",
"process.gc_count",
)

for observer in metric_names:
self.assertIn(observer, observer_names)
observer_names.remove(observer)

def test_runtime_metrics_instrument(self):
runtime_config = {
"process.runtime.memory": ["rss", "vms"],
"process.runtime.cpu.time": ["user", "system"],
"process.runtime.thread_count": None,
"process.runtime.cpu.utilization": None,
"process.runtime.context_switches": ["involuntary", "voluntary"],
def test_process_metrics_instrument(self):
process_config = {
"process.memory": ["rss", "vms"],
"process.cpu.time": ["user", "system"],
"process.thread.count": None,
"process.cpu.utilization": None,
"process.context_switches": ["involuntary", "voluntary"],
}

if self.implementation != "pypy":
runtime_config["process.runtime.gc_count"] = None
process_config["process.gc_count"] = None

reader = InMemoryMetricReader()
meter_provider = MeterProvider(metric_readers=[reader])
runtime_metrics = SystemMetricsInstrumentor(config=runtime_config)
runtime_metrics.instrument(meter_provider=meter_provider)
process_metrics = SystemMetricsInstrumentor(config=process_config)
process_metrics.instrument(meter_provider=meter_provider)

metric_names = []
for resource_metrics in reader.get_metrics_data().resource_metrics:
Expand All @@ -157,19 +157,19 @@ def test_runtime_metrics_instrument(self):
metric_names.append(metric.name)

observer_names = [
f"process.runtime.{self.implementation}.memory",
f"process.runtime.{self.implementation}.cpu_time",
f"process.runtime.{self.implementation}.thread_count",
f"process.runtime.{self.implementation}.context_switches",
f"process.runtime.{self.implementation}.cpu.utilization",
"process.memory",
"process.cpu_time",
"process.thread.count",
"process.context_switches",
"process.cpu.utilization",
]

if self.implementation == "pypy":
self.assertEqual(len(metric_names), 5)
else:
self.assertEqual(len(metric_names), 6)
observer_names.append(
f"process.runtime.{self.implementation}.gc_count"
"process.gc_count",
)

for observer in metric_names:
Expand Down Expand Up @@ -763,7 +763,7 @@ def test_system_thread_count(self, threading_active_count):
self._test_metrics("system.thread_count", expected)

@mock.patch("psutil.Process.memory_info")
def test_runtime_memory(self, mock_process_memory_info):
def test_process_memory(self, mock_process_memory_info):
PMem = namedtuple("PMem", ["rss", "vms"])

mock_process_memory_info.configure_mock(
Expand All @@ -775,11 +775,11 @@ def test_runtime_memory(self, mock_process_memory_info):
_SystemMetricsResult({"type": "vms"}, 2),
]
self._test_metrics(
f"process.runtime.{self.implementation}.memory", expected
"process.memory", expected
)

@mock.patch("psutil.Process.cpu_times")
def test_runtime_cpu_time(self, mock_process_cpu_times):
def test_process_cpu_time(self, mock_process_cpu_times):
PCPUTimes = namedtuple("PCPUTimes", ["user", "system"])

mock_process_cpu_times.configure_mock(
Expand All @@ -791,14 +791,14 @@ def test_runtime_cpu_time(self, mock_process_cpu_times):
_SystemMetricsResult({"type": "system"}, 2.2),
]
self._test_metrics(
f"process.runtime.{self.implementation}.cpu_time", expected
"process.cpu_time", expected
)

@mock.patch("gc.get_count")
@skipIf(
python_implementation().lower() == "pypy", "not supported for pypy"
)
def test_runtime_get_count(self, mock_gc_get_count):
def test_process_get_count(self, mock_gc_get_count):
mock_gc_get_count.configure_mock(**{"return_value": (1, 2, 3)})

expected = [
Expand All @@ -807,11 +807,11 @@ def test_runtime_get_count(self, mock_gc_get_count):
_SystemMetricsResult({"count": "2"}, 3),
]
self._test_metrics(
f"process.runtime.{self.implementation}.gc_count", expected
"process.gc_count", expected
)

@mock.patch("psutil.Process.num_ctx_switches")
def test_runtime_context_switches(self, mock_process_num_ctx_switches):
def test_process_context_switches(self, mock_process_num_ctx_switches):
PCtxSwitches = namedtuple("PCtxSwitches", ["voluntary", "involuntary"])

mock_process_num_ctx_switches.configure_mock(
Expand All @@ -823,25 +823,25 @@ def test_runtime_context_switches(self, mock_process_num_ctx_switches):
_SystemMetricsResult({"type": "involuntary"}, 2),
]
self._test_metrics(
f"process.runtime.{self.implementation}.context_switches", expected
"process.context_switches", expected
)

@mock.patch("psutil.Process.num_threads")
def test_runtime_thread_num(self, mock_process_thread_num):
def test_process_thread_num(self, mock_process_thread_num):
mock_process_thread_num.configure_mock(**{"return_value": 42})

expected = [_SystemMetricsResult({}, 42)]
self._test_metrics(
f"process.runtime.{self.implementation}.thread_count", expected
"process.thread.count", expected
)

@mock.patch("psutil.Process.cpu_percent")
def test_runtime_cpu_percent(self, mock_process_cpu_percent):
def test_process_cpu_percent(self, mock_process_cpu_percent):
mock_process_cpu_percent.configure_mock(**{"return_value": 42})

expected = [_SystemMetricsResult({}, 42)]
self._test_metrics(
f"process.runtime.{self.implementation}.cpu.utilization", expected
"process.cpu.utilization", expected
)

@mock.patch("psutil.Process.num_fds")
Expand Down

0 comments on commit a9caf33

Please sign in to comment.