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

Skip to content

Commit c5eceed

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 c5eceed

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

prometheus_client/registry.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ 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
19+
self._target_info = target_info
1920
self._lock = Lock()
2021

2122
def register(self, collector):
@@ -69,8 +70,13 @@ def _get_names(self, collector):
6970
def collect(self):
7071
"""Yields metrics from the collectors in the registry."""
7172
collectors = None
73+
ti = None
7274
with self._lock:
7375
collectors = copy.copy(self._collector_to_names)
76+
if self._target_info:
77+
ti = self._target_info_metric()
78+
if ti:
79+
yield ti
7480
for collector in collectors:
7581
for metric in collector.collect():
7682
yield metric
@@ -87,11 +93,13 @@ def restricted_registry(self, names):
8793
Experimental."""
8894
names = set(names)
8995
collectors = set()
96+
metrics = []
9097
with self._lock:
9198
for name in names:
9299
if name in self._names_to_collectors:
93100
collectors.add(self._names_to_collectors[name])
94-
metrics = []
101+
if 'target_info' in names and self._target_info:
102+
metrics.append(self._target_info_metric())
95103
for collector in collectors:
96104
for metric in collector.collect():
97105
samples = [s for s in metric.samples if s[0] in names]
@@ -106,6 +114,19 @@ def collect(self):
106114

107115
return RestrictedRegistry()
108116

117+
def set_target_info(self, labels):
118+
with self._lock:
119+
self._target_info = labels
120+
121+
def get_target_info(self):
122+
with self._lock:
123+
return self._target_info
124+
125+
def _target_info_metric():
126+
m = Metric('target', 'Target metadata', 'info')
127+
m.add_sample('target_info', self._target_info, 1)
128+
return m
129+
109130
def get_sample_value(self, name, labels=None):
110131
"""Returns the sample value, or None if not found.
111132

0 commit comments

Comments
 (0)
0