@@ -12,10 +12,11 @@ class CollectorRegistry(object):
12
12
exposition formats.
13
13
"""
14
14
15
- def __init__ (self , auto_describe = False ):
15
+ def __init__ (self , auto_describe = False , target_info = None ):
16
16
self ._collector_to_names = {}
17
17
self ._names_to_collectors = {}
18
18
self ._auto_describe = auto_describe
19
+ self ._target_info = target_info
19
20
self ._lock = Lock ()
20
21
21
22
def register (self , collector ):
@@ -69,8 +70,13 @@ def _get_names(self, collector):
69
70
def collect (self ):
70
71
"""Yields metrics from the collectors in the registry."""
71
72
collectors = None
73
+ ti = None
72
74
with self ._lock :
73
75
collectors = copy .copy (self ._collector_to_names )
76
+ if self ._target_info :
77
+ ti = self ._target_info_metric ()
78
+ if ti :
79
+ yield ti
74
80
for collector in collectors :
75
81
for metric in collector .collect ():
76
82
yield metric
@@ -87,11 +93,13 @@ def restricted_registry(self, names):
87
93
Experimental."""
88
94
names = set (names )
89
95
collectors = set ()
96
+ metrics = []
90
97
with self ._lock :
91
98
for name in names :
92
99
if name in self ._names_to_collectors :
93
100
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 ())
95
103
for collector in collectors :
96
104
for metric in collector .collect ():
97
105
samples = [s for s in metric .samples if s [0 ] in names ]
@@ -106,6 +114,19 @@ def collect(self):
106
114
107
115
return RestrictedRegistry ()
108
116
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
+
109
130
def get_sample_value (self , name , labels = None ):
110
131
"""Returns the sample value, or None if not found.
111
132
0 commit comments