Skip to content

Commit 8e0c8d9

Browse files
collect threads count in opentelemetry-instrumentation-system-metrics (#1339)
* collect threads count in opentelemetry-instrumentation-system-metrics * update * avoid devidedByZero exception when sawp memory is 0 * add ut * change log * lint * lint Co-authored-by: Srikanth Chekuri <srikanth.chekuri92@gmail.com>
1 parent 9d8228f commit 8e0c8d9

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

CHANGELOG.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.13.0-0.34b0...HEAD)
99

10+
### Added
11+
12+
- `opentelemetry-instrumentation-system-metrics` add supports to collect system thread count. ([#1339](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1339))
13+
1014
## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0-0.34b0) - 2022-09-26
1115

1216

@@ -26,14 +30,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2630
([#1253](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1253))
2731
- Add metric instrumentation in starlette
2832
([#1327](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1327))
29-
33+
3034

3135
### Fixed
3236

3337
- `opentelemetry-instrumentation-boto3sqs` Make propagation compatible with other SQS instrumentations, add 'messaging.url' span attribute, and fix missing package dependencies.
34-
([#1234](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1234))
38+
([#1234](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1234))
3539
- `opentelemetry-instrumentation-pymongo` Change span names to not contain queries but only database name and command name
36-
([#1247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1247))
40+
([#1247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1247))
3741
- restoring metrics in django framework
3842
([#1208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1208))
3943
- `opentelemetry-instrumentation-aiohttp-client` Fix producing additional spans with each newly created ClientSession

instrumentation/opentelemetry-instrumentation-system-metrics/src/opentelemetry/instrumentation/system_metrics/__init__.py

+20
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"system.network.errors": ["transmit", "receive"],
3434
"system.network.io": ["transmit", "receive"],
3535
"system.network.connections": ["family", "type"],
36+
"system.thread_count": None
3637
"runtime.memory": ["rss", "vms"],
3738
"runtime.cpu.time": ["user", "system"],
3839
}
@@ -71,6 +72,7 @@
7172

7273
import gc
7374
import os
75+
import threading
7476
from platform import python_implementation
7577
from typing import Collection, Dict, Iterable, List, Optional
7678

@@ -99,6 +101,7 @@
99101
"system.network.errors": ["transmit", "receive"],
100102
"system.network.io": ["transmit", "receive"],
101103
"system.network.connections": ["family", "type"],
104+
"system.thread_count": None,
102105
"runtime.memory": ["rss", "vms"],
103106
"runtime.cpu.time": ["user", "system"],
104107
"runtime.gc_count": None,
@@ -142,6 +145,8 @@ def __init__(
142145
self._system_network_io_labels = self._labels.copy()
143146
self._system_network_connections_labels = self._labels.copy()
144147

148+
self._system_thread_count_labels = self._labels.copy()
149+
145150
self._runtime_memory_labels = self._labels.copy()
146151
self._runtime_cpu_time_labels = self._labels.copy()
147152
self._runtime_gc_count_labels = self._labels.copy()
@@ -311,6 +316,13 @@ def _instrument(self, **kwargs):
311316
unit="connections",
312317
)
313318

319+
if "system.thread_count" in self._config:
320+
self._meter.create_observable_gauge(
321+
name="system.thread_count",
322+
callbacks=[self._get_system_thread_count],
323+
description="System active threads count",
324+
)
325+
314326
if "runtime.memory" in self._config:
315327
self._meter.create_observable_counter(
316328
name=f"runtime.{self._python_implementation}.memory",
@@ -593,6 +605,14 @@ def _get_system_network_connections(
593605
connection_counter["labels"],
594606
)
595607

608+
def _get_system_thread_count(
609+
self, options: CallbackOptions
610+
) -> Iterable[Observation]:
611+
"""Observer callback for active thread count"""
612+
yield Observation(
613+
threading.active_count(), self._system_thread_count_labels
614+
)
615+
596616
def _get_runtime_memory(
597617
self, options: CallbackOptions
598618
) -> Iterable[Observation]:

instrumentation/opentelemetry-instrumentation-system-metrics/tests/test_system_metrics.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(self, attributes, value) -> None:
5151
self.value = value
5252

5353

54+
# pylint:disable=too-many-public-methods
5455
class TestSystemMetrics(TestBase):
5556
def setUp(self):
5657
super().setUp()
@@ -75,7 +76,7 @@ def test_system_metrics_instrument(self):
7576
for scope_metrics in resource_metrics.scope_metrics:
7677
for metric in scope_metrics.metrics:
7778
metric_names.append(metric.name)
78-
self.assertEqual(len(metric_names), 17)
79+
self.assertEqual(len(metric_names), 18)
7980

8081
observer_names = [
8182
"system.cpu.time",
@@ -92,6 +93,7 @@ def test_system_metrics_instrument(self):
9293
"system.network.errors",
9394
"system.network.io",
9495
"system.network.connections",
96+
"system.thread_count",
9597
f"runtime.{self.implementation}.memory",
9698
f"runtime.{self.implementation}.cpu_time",
9799
f"runtime.{self.implementation}.gc_count",
@@ -680,6 +682,13 @@ def test_system_network_connections(self, mock_net_connections):
680682
]
681683
self._test_metrics("system.network.connections", expected)
682684

685+
@mock.patch("threading.active_count")
686+
def test_system_thread_count(self, threading_active_count):
687+
threading_active_count.return_value = 42
688+
689+
expected = [_SystemMetricsResult({}, 42)]
690+
self._test_metrics("system.thread_count", expected)
691+
683692
@mock.patch("psutil.Process.memory_info")
684693
def test_runtime_memory(self, mock_process_memory_info):
685694

0 commit comments

Comments
 (0)