-
Notifications
You must be signed in to change notification settings - Fork 816
Add target_info to registries #453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,13 @@ class CollectorRegistry(object): | |
exposition formats. | ||
""" | ||
|
||
def __init__(self, auto_describe=False): | ||
def __init__(self, auto_describe=False, target_info=None): | ||
self._collector_to_names = {} | ||
self._names_to_collectors = {} | ||
self._auto_describe = auto_describe | ||
self._lock = Lock() | ||
self._target_info = {} | ||
self.set_target_info(target_info) | ||
|
||
def register(self, collector): | ||
"""Add a collector to the registry.""" | ||
|
@@ -69,8 +71,13 @@ def _get_names(self, collector): | |
def collect(self): | ||
"""Yields metrics from the collectors in the registry.""" | ||
collectors = None | ||
ti = None | ||
with self._lock: | ||
collectors = copy.copy(self._collector_to_names) | ||
if self._target_info: | ||
8000 ti = self._target_info_metric() | ||
if ti: | ||
yield ti | ||
for collector in collectors: | ||
for metric in collector.collect(): | ||
yield metric | ||
|
@@ -87,11 +94,14 @@ def restricted_registry(self, names): | |
Experimental.""" | ||
names = set(names) | ||
collectors = set() | ||
metrics = [] | ||
with self._lock: | ||
if 'target_info' in names and self._target_info: | ||
metrics.append(self._target_info_metric()) | ||
names.remove('target_info') | ||
for name in names: | ||
if name in self._names_to_collectors: | ||
collectors.add(self._names_to_collectors[name]) | ||
metrics = [] | ||
for collector in collectors: | ||
for metric in collector.collect(): | ||
samples = [s for s in metric.samples if s[0] in names] | ||
|
@@ -106,6 +116,25 @@ def collect(self): | |
|
||
return RestrictedRegistry() | ||
|
||
def set_target_info(self, labels): | ||
with self._lock: | ||
if labels: | ||
if not self._target_info and 'target_info' in self._names_to_collectors: | ||
raise ValueError('CollectorRegistry already contains a target_info metric') | ||
self._names_to_collectors['target_info'] = None | ||
elif self._target_info: | ||
self._names_to_collectors.pop('target_info', None) | ||
self._target_info = labels | ||
|
||
def get_target_info(self): | ||
with self._lock: | ||
return self._target_info | ||
|
||
def _target_info_metric(self): | ||
m = Metric('target', 'Target metadata', 'info') | ||
m.add_sample('target_info', self._target_info, 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, would it be required to also emit an actual value (i.e. in this case 1) or could we avoid that perhaps and special case it in the parser that metrics without a value are info metrics? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a standard info metric, so the value of 1 is required. |
||
return m | ||
|
||
def get_sample_value(self, name, labels=None): | ||
"""Returns the sample value, or None if not found. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little confused about this one, from my rudimentary understanding (or extreme rudimentary understanding should I say) - every value in
_names_to_collectors
should be a collector yeah?If None is a value in
_names_to_collectors
and then it gets added tocollectors
var inrestricted_registry
:Wouldn't the
collector.collect()
be a call toNone
from the linefor metric in collector.collect()
ifnames
happens to containtarget_info
?