diff --git a/src/aioprometheus/__init__.py b/src/aioprometheus/__init__.py index 8fb56cf..3c19960 100644 --- a/src/aioprometheus/__init__.py +++ b/src/aioprometheus/__init__.py @@ -1,4 +1,5 @@ from .collectors import REGISTRY, Counter, Gauge, Histogram, Registry, Summary +from .collector import COLLECTOR_PLATFORM from .decorators import count_exceptions, inprogress, timer from .negotiator import negotiate from .renderer import render diff --git a/src/aioprometheus/collector/__init__.py b/src/aioprometheus/collector/__init__.py new file mode 100644 index 0000000..025dcf6 --- /dev/null +++ b/src/aioprometheus/collector/__init__.py @@ -0,0 +1 @@ +from .platform import COLLECTOR_PLATFORM diff --git a/src/aioprometheus/collector/platform.py b/src/aioprometheus/collector/platform.py new file mode 100644 index 0000000..c495a8d --- /dev/null +++ b/src/aioprometheus/collector/platform.py @@ -0,0 +1,32 @@ +import typing as t + +import sys + +import aioprometheus.collectors + + +class CollectorPlatform(aioprometheus.collectors.Gauge): + """Collector for python platform information""" + + def __init__( + self, + registry: aioprometheus.collectors.Registry = None, + ): + super().__init__( + "python_info", "Python platform information", registry=registry + ) + labels = self._labels() + self.set_value(labels, 1) + + def _labels(self) -> t.Dict[str, str]: + return { + "version": sys.version, + "implementation": sys.implementation.name, + "major": str(sys.version_info.major), + "minor": str(sys.version_info.minor), + "patchlevel": str(sys.version_info.micro), + "system": sys.platform, + } + + +COLLECTOR_PLATFORM = CollectorPlatform() diff --git a/tests/test_collector_platform.py b/tests/test_collector_platform.py new file mode 100644 index 0000000..b7e3f0b --- /dev/null +++ b/tests/test_collector_platform.py @@ -0,0 +1,24 @@ +import unittest + +from aioprometheus.collectors import REGISTRY +from aioprometheus.collector.platform import CollectorPlatform + + +class TestCollectorPlatfrom(unittest.TestCase): + @classmethod + def setUpClass(cls) -> None: + REGISTRY.clear() + + def setUp(self) -> None: + self.COLLECTOR_PLATFORM = CollectorPlatform() + + def tearDown(self): + REGISTRY.clear() + + def test_python_info_is_present(self): + REGISTRY.get(self.COLLECTOR_PLATFORM.name) + self.assertIn("python_info", REGISTRY.collectors) + + def test_get(self): + labels = self.COLLECTOR_PLATFORM._labels() + self.assertEqual(self.COLLECTOR_PLATFORM.get(labels), 1)