Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collect threads count in opentelemetry-instrumentation-system-metrics #1339

Merged
merged 19 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

### Added

- `opentelemetry-instrumentation-system-metrics` add supports to collect system thread count. ([#1339](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1339))

## [1.13.0-0.34b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.13.0-0.34b0) - 2022-09-26


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


### Fixed

- `opentelemetry-instrumentation-boto3sqs` Make propagation compatible with other SQS instrumentations, add 'messaging.url' span attribute, and fix missing package dependencies.
([#1234](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1234))
([#1234](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1234))
- `opentelemetry-instrumentation-pymongo` Change span names to not contain queries but only database name and command name
([#1247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1247))
([#1247](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1247))
- restoring metrics in django framework
([#1208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1208))
- `opentelemetry-instrumentation-aiohttp-client` Fix producing additional spans with each newly created ClientSession
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"system.network.errors": ["transmit", "receive"],
"system.network.io": ["transmit", "receive"],
"system.network.connections": ["family", "type"],
"system.thread_count": None
"runtime.memory": ["rss", "vms"],
"runtime.cpu.time": ["user", "system"],
}
Expand Down Expand Up @@ -71,6 +72,7 @@

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

Expand Down Expand Up @@ -99,6 +101,7 @@
"system.network.errors": ["transmit", "receive"],
"system.network.io": ["transmit", "receive"],
"system.network.connections": ["family", "type"],
"system.thread_count": None,
"runtime.memory": ["rss", "vms"],
"runtime.cpu.time": ["user", "system"],
"runtime.gc_count": None,
Expand Down Expand Up @@ -142,6 +145,8 @@ def __init__(
self._system_network_io_labels = self._labels.copy()
self._system_network_connections_labels = self._labels.copy()

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()
Expand Down Expand Up @@ -311,6 +316,13 @@ def _instrument(self, **kwargs):
unit="connections",
)

if "system.thread_count" in self._config:
self._meter.create_observable_gauge(
name="system.thread_count",
callbacks=[self._get_system_thread_count],
description="System active threads count",
)

if "runtime.memory" in self._config:
self._meter.create_observable_counter(
name=f"runtime.{self._python_implementation}.memory",
Expand Down Expand Up @@ -593,6 +605,14 @@ def _get_system_network_connections(
connection_counter["labels"],
)

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

def _get_runtime_memory(
self, options: CallbackOptions
) -> Iterable[Observation]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __init__(self, attributes, value) -> None:
self.value = value


# pylint:disable=too-many-public-methods
class TestSystemMetrics(TestBase):
def setUp(self):
super().setUp()
Expand All @@ -75,7 +76,7 @@ def test_system_metrics_instrument(self):
for scope_metrics in resource_metrics.scope_metrics:
for metric in scope_metrics.metrics:
metric_names.append(metric.name)
self.assertEqual(len(metric_names), 17)
self.assertEqual(len(metric_names), 18)

observer_names = [
"system.cpu.time",
Expand All @@ -92,6 +93,7 @@ def test_system_metrics_instrument(self):
"system.network.errors",
"system.network.io",
"system.network.connections",
"system.thread_count",
f"runtime.{self.implementation}.memory",
f"runtime.{self.implementation}.cpu_time",
f"runtime.{self.implementation}.gc_count",
Expand Down Expand Up @@ -680,6 +682,13 @@ def test_system_network_connections(self, mock_net_connections):
]
self._test_metrics("system.network.connections", expected)

@mock.patch("threading.active_count")
def test_system_thread_count(self, threading_active_count):
threading_active_count.return_value = 42

expected = [_SystemMetricsResult({}, 42)]
self._test_metrics("system.thread_count", expected)

@mock.patch("psutil.Process.memory_info")
def test_runtime_memory(self, mock_process_memory_info):

Expand Down