Skip to content

Commit

Permalink
collect threads count in opentelemetry-instrumentation-system-metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Yulin Li authored and Yulin Li committed Sep 18, 2022
1 parent 156203c commit 2ca7ab9
Showing 1 changed file with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"system.network.connections": ["family", "type"],
"runtime.memory": ["rss", "vms"],
"runtime.cpu.time": ["user", "system"],
"runtime.gc_count": None
"runtime.gc_count": None,
"runtime.threading.active_count": None
}
Usage
Expand Down Expand Up @@ -80,6 +81,7 @@

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

Expand Down Expand Up @@ -118,6 +120,7 @@
"runtime.memory": ["rss", "vms"],
"runtime.cpu.time": ["user", "system"],
"runtime.gc_count": None,
"runtime.threading.active_count": None,
}


Expand Down Expand Up @@ -168,6 +171,7 @@ def __init__(
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_threading_active_count_labels = self._labels.copy()

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments
Expand Down Expand Up @@ -415,6 +419,14 @@ def _instrument(self, **kwargs):
unit="bytes",
)

if "runtime.threading.active_count" in self._config:
self._meter.create_observable_gauge(
name=f"runtime.{self._python_implementation}.threading.active_count",
callbacks=[self._get_runtime_threading_active_count],
description=f"Runtime {self._python_implementation} active threads count",
unit="threads",
)

def _uninstrument(self, **__):
pass

Expand Down Expand Up @@ -747,3 +759,11 @@ def _get_runtime_gc_count(
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())

def _get_runtime_threading_active_count(
self, options: CallbackOptions
) -> Iterable[Observation]:
"""Observer callback for threading active count"""
yield Observation(
threading.active_count(), self._runtime_threading_active_count_labels
)

0 comments on commit 2ca7ab9

Please sign in to comment.