Skip to content

Commit

Permalink
platform collector - report details on python
Browse files Browse the repository at this point in the history
  • Loading branch information
ritzk committed Jul 2, 2022
1 parent 4220a99 commit 815aed3
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/aioprometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions src/aioprometheus/collector/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .platform import COLLECTOR_PLATFORM
32 changes: 32 additions & 0 deletions src/aioprometheus/collector/platform.py
Original file line number Diff line number Diff line change
@@ -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()
24 changes: 24 additions & 0 deletions tests/test_collector_platform.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 815aed3

Please sign in to comment.