-
Notifications
You must be signed in to change notification settings - Fork 8
/
sample.py
53 lines (43 loc) · 1.43 KB
/
sample.py
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
46
47
48
49
50
51
52
53
import random
import typing as t
from aiohttp.web import Application
from prometheus_client import (
Counter,
Gauge,
)
from prometheus_client.metrics import MetricWrapperBase
from . import (
Arguments,
MetricConfig,
PrometheusExporterScript,
)
class SampleScript(PrometheusExporterScript):
"""A sample exporter."""
name = "prometheus-aioexporter-sample"
default_port = 9091
def configure(self, args: Arguments) -> None:
self.create_metrics(
[
MetricConfig(
"a_gauge", "a gauge", "gauge", labels=("foo", "bar")
),
MetricConfig(
"a_counter", "a counter", "counter", labels=("baz",)
),
]
)
async def on_application_startup(self, application: Application) -> None:
application["exporter"].set_metric_update_handler(self._update_handler)
async def _update_handler(
self, metrics: dict[str, MetricWrapperBase]
) -> None:
gauge = t.cast(Gauge, metrics["a_gauge"])
gauge.labels(
foo=random.choice(["this-foo", "other-foo"]),
bar=random.choice(["this-bar", "other-bar"]),
).set(random.uniform(0, 100))
counter = t.cast(Counter, metrics["a_counter"])
counter.labels(
baz=random.choice(["this-baz", "other-baz"]),
).inc(random.choice(range(10)))
script = SampleScript()