File: prometheus.py

package info (click to toggle)
dask.distributed 2022.12.1%2Bds.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 10,164 kB
  • sloc: python: 81,938; javascript: 1,549; makefile: 228; sh: 100
file content (45 lines) | stat: -rw-r--r-- 1,480 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations

from importlib import import_module

import dask.config

from distributed.http.utils import RequestHandler


class PrometheusCollector:
    def __init__(self, server):
        self.server = server
        self.namespace = dask.config.get("distributed.dashboard.prometheus.namespace")
        self.subsystem = None

    def build_name(self, name):
        full_name = []
        if self.namespace:
            full_name.append(self.namespace)
        if self.subsystem:
            full_name.append(self.subsystem)
        full_name.append(name)
        return "_".join(full_name)


class PrometheusNotAvailableHandler(RequestHandler):
    def get(self):
        self.write(
            "# Prometheus metrics are not available, see: "
            "https://docs.dask.org/en/stable/how-to/setup-prometheus.html#setup-prometheus-monitoring"
        )
        self.set_header("Content-Type", "text/plain; version=0.0.4")


def import_metrics_handler(module_name: str, handler_name: str) -> type[RequestHandler]:
    """Import ``handler_name`` from ``module_name`` if ``prometheus_client``
    is installed, import the ``PrometheusNotAvailableHandler`` otherwise."""
    try:
        import prometheus_client  # noqa: F401
    except ModuleNotFoundError:
        return PrometheusNotAvailableHandler
    module = import_module(module_name)
    handler = getattr(module, handler_name)
    assert issubclass(handler, RequestHandler)
    return handler