diff --git a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py index d0cd10739c..8d806e8b6f 100644 --- a/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py @@ -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 @@ -80,6 +81,7 @@ import gc import os +import threading from platform import python_implementation from typing import Collection, Dict, Iterable, List, Optional @@ -118,6 +120,7 @@ "runtime.memory": ["rss", "vms"], "runtime.cpu.time": ["user", "system"], "runtime.gc_count": None, + "runtime.threading.active_count": None, } @@ -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 @@ -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 @@ -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 + ) \ No newline at end of file