8000 Add target_info to registries · prometheus/client_python@41f7165 · GitHub
[go: up one dir, main page]

Skip to content

Commit 41f7165

Browse files
committed
Add target_info to registries
This allows target labels as needed by push-based systems to be provided, in a way that doesn't mess up Prometheus's own top-down pull based approach to SD. Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent d1d93b1 commit 41f7165

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

prometheus_client/registry.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ class CollectorRegistry(object):
1212
exposition formats.
1313
"""
1414

15-
def __init__(self, auto_describe=False):
15+
def __init__(self, auto_describe=False, target_info=None):
1616
self._collector_to_names = {}
1717
self._names_to_collectors = {}
1818
self._auto_describe = auto_describe
1919
self._lock = Lock()
20+
self._target_info = {}
21+
self.set_target_info(target_info)
2022

2123
def register(self, collector):
2224
"""Add a collector to the registry."""
@@ -69,8 +71,13 @@ def _get_names(self, collector):
6971
def collect(self):
7072
"""Yields metrics from the collectors in the registry."""
7173
collectors = None
74+
ti = None
7275
with self._lock:
7376
collectors = copy.copy(self._collector_to_names)
77+
if self._target_info:
78+
ti = self._target_info_metric()
79+
if ti:
80+
yield ti
7481
for collector in collectors:
7582
for metric in collector.collect():
7683
yield metric
@@ -87,11 +94,13 @@ def restricted_registry(self, names):
8794
Experimental."""
8895
names = set(names)
8996
collectors = set()
97+
metrics = []
9098
with self._lock:
9199
for name in names:
92100
if name in self._names_to_collectors:
93101
collectors.add(self._names_to_collectors[name])
94-
metrics = []
102+
if 'target_info' in names and self._target_info:
103+
metrics.append(self._target_info_metric())
95104
for collector in collectors:
96105
for metric in collector.collect():
97106
samples = [s for s in metric.samples if s[0] in names]
@@ -106,6 +115,25 @@ def collect(self):
106115

107116
return RestrictedRegistry()
108117

118+
def set_target_info(self, labels):
119+
with self._lock:
120+
if labels:
121+
if not self._target_info and 'target_info' in self._names_to_collectors:
122+
raise ValueError('CollectorRegistry already contains a target_info metric')
123+
self._names_to_collectors['target_info'] = None
124+
elif self._target_info:
125+
self._names_to_collectors.pop('target_info', None)
126+
self._target_info = labels
127+
128+
def get_target_info(self):
129+
with self._lock:
130+
return self._target_info
131+
132+
def _target_info_metric(self):
133+
m = Metric('target', 'Target metadata', 'info')
134+
m.add_sample('target_info', self._target_info, 1)
135+
return m
136+
109137
def get_sample_value(self, name, labels=None):
110138
"""Returns the sample value, or None if not found.
111139

tests/test_core.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,9 @@ def test_duplicate_metrics_raises(self):
664664
# The name of the histogram itself isn't taken.
665665
Gauge('h', 'help', registry=registry)
666666

667+
Info('i', 'help', registry=registry)
668+
self.assertRaises(ValueError, Gauge, 'i_info', 'help', registry=registry)
669+
667670
def test_unregister_works(self):
668671
registry = CollectorRegistry()
669672
s = Summary('s', 'help', registry=registry)
@@ -696,6 +699,22 @@ def test_restricted_registry(self):
696699
m.samples = [Sample('s_sum', {}, 7)]
697700
self.assertEquals([m], registry.restricted_registry(['s_sum']).collect())
698701

702+
def test_target_info_injected(self):
703+
registry = CollectorRegistry(target_info={'foo': 'bar'})
704+
self.assertEqual(1, registry.get_sample_value('target_info', {'foo': 'bar'}))
705+
706+
def test_target_info_duplicate_detected(self):
707+
registry = CollectorRegistry(target_info={'foo': 'bar'})
708+
self.assertRaises(ValueError, Info, 'target', 'help', registry=registry)
709+
710+
registry.set_target_info({})
711+
i = Info('target', 'help', registry=registry)
712+
registry.set_target_info({})
713+
self.assertRaises(ValueError, Info, 'target', 'help', registry=registry)
714+
self.assertRaises(ValueError, registry.set_target_info, {'foo': 'bar'})
715+
registry.unregister(i)
716+
registry.set_target_info({'foo': 'bar'})
717+
699718

700719
if __name__ == '__main__':
701720
unittest.main()

0 commit comments

Comments
 (0)
0