jellyseer-exporter/src/jellyseer_exporter/collector.py

41 lines
1.1 KiB
Python

# pylint: disable=too-few-public-methods
import collections
from prometheus_client import CollectorRegistry, generate_latest
from prometheus_client.core import GaugeMetricFamily
from prometheus_client.registry import Collector
from .jellyseer import JellyseerAPI
CollectorsOptions = collections.namedtuple('CollectorsOptions', [
'sessions',
])
class SessionsCollector(Collector):
"""
Collects Jellyseer requests API
"""
def __init__(self, js):
self._js = js
def collect(self): # pylint: disable=missing-docstring
for req_type, count in self._js.api("GET", "api/v1/request/count").items():
gauge = GaugeMetricFamily(
f"jellyseer_requests_{req_type}",
f"Count of active {req_type} requests", value=count
)
yield gauge
def collect_jellyseer(config, host, options: CollectorsOptions):
"""Scrape a host and return prometheus text format for it"""
js = JellyseerAPI(host, **config)
registry = CollectorRegistry()
if options.sessions:
registry.register(SessionsCollector(js))
return generate_latest(registry)