Debian Codegraph @

docs/content/collector/custom.md

raw symbols SCIP
1 ---
2 title: Custom Collectors
3 weight: 1
4 ---
5
6 Sometimes it is not possible to directly instrument code, as it is not
7 in your control. This requires you to proxy metrics from other systems.
8
9 To do so you need to create a custom collector, for example:
10
11 ```python
12 from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY
13 from prometheus_client.registry import Collector
14
15 class CustomCollector(Collector):
16 def collect(self):
17 yield GaugeMetricFamily('my_gauge', 'Help text', value=7)
18 c = CounterMetricFamily('my_counter_total', 'Help text', labels=['foo'])
19 c.add_metric(['bar'], 1.7)
20 c.add_metric(['baz'], 3.8)
21 yield c
22
23 REGISTRY.register(CustomCollector())
24 ```
25
26 `SummaryMetricFamily`, `HistogramMetricFamily` and `InfoMetricFamily` work similarly.
27
28 A collector may implement a `describe` method which returns metrics in the same
29 format as `collect` (though you don't have to include the samples). This is
30 used to predetermine the names of time series a `CollectorRegistry` exposes and
31 thus to detect collisions and duplicate registrations.
32
33 Usually custom collectors do not have to implement `describe`. If `describe` is
34 not implemented and the CollectorRegistry was created with `auto_describe=True`
35 (which is the case for the default registry) then `collect` will be called at
36 registration time instead of `describe`. If this could cause problems, either
37 implement a proper `describe`, or if that's not practical have `describe`
38 return an empty list.