Skip to content
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

Add type_overrides member to PrometheusCheck class #3463

Merged
merged 1 commit into from
Aug 10, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions checks/prometheus_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def __init__(self, name, init_config, agentConfig, instances=None):
# will just not be added as tags when submitting the metric.
self.exclude_labels = []

# `type_overrides` is a dictionnary where the keys are prometheus metric names
# and the values are a metric type (name as string) to use instead of the one
# listed in the payload. It can be used to force a type on untyped metrics.
# Note: it is empty in the mother class but will need to be
# overloaded/hardcoded in the final check not to be counted as custom metric.
self.type_overrides = {}

def check(self, instance):
"""
check should take care of getting the url and other params
Expand Down Expand Up @@ -102,13 +109,30 @@ def parse_metric_family(self, buf, content_type):

message = metrics_pb2.MetricFamily()
message.ParseFromString(msg_buf)

# Lookup type overrides:
if self.type_overrides and message.name in self.type_overrides:
new_type = self.type_overrides[message.name]
if new_type in self.METRIC_TYPES:
message.type = self.METRIC_TYPES.index(new_type)
else:
self.log.debug("type override %s for %s is not a valid type name" % (new_type, message.name))
yield message
elif 'text/plain' in content_type:
messages = {} # map with the name of the element (before the labels) and the list of occurrences with labels and values
obj_map = {} # map of the types of each metrics
obj_help = {} # help for the metrics
for line in buf.splitlines():
self._extract_metrics_from_string(line, messages, obj_map, obj_help)

# Add type overrides:
for m_name, m_type in self.type_overrides.iteritems():
if m_type in self.METRIC_TYPES:
obj_map[m_name] = m_type
else:
self.log.debug("type override %s for %s is not a valid type name" % (m_type,m_name))


for _m in obj_map:
if _m in messages or (obj_map[_m] == 'histogram' and '{}_bucket'.format(_m) in messages):
yield self._extract_metric_from_map(_m, messages, obj_map, obj_help)
Expand Down