Add some comments to metrics classes

pull/2785/head
Richard van der Hoff 2018-01-15 16:52:52 +00:00
parent 5e16c1dc8c
commit 80fa610f9c
1 changed files with 27 additions and 1 deletions

View File

@ -24,8 +24,16 @@ def map_concat(func, items):
class BaseMetric(object): class BaseMetric(object):
"""Base class for metrics which report a single value per label set
"""
def __init__(self, name, labels=[]): def __init__(self, name, labels=[]):
"""
Args:
name (str): principal name for this metric
labels (list(str)): names of the labels which will be reported
for this metric
"""
self.name = name self.name = name
self.labels = labels # OK not to clone as we never write it self.labels = labels # OK not to clone as we never write it
@ -36,7 +44,7 @@ class BaseMetric(object):
return not len(self.labels) return not len(self.labels)
def _render_labelvalue(self, value): def _render_labelvalue(self, value):
# TODO: some kind of value escape # TODO: escape backslashes, quotes and newlines
return '"%s"' % (value) return '"%s"' % (value)
def _render_key(self, values): def _render_key(self, values):
@ -47,6 +55,20 @@ class BaseMetric(object):
for k, v in zip(self.labels, values)]) for k, v in zip(self.labels, values)])
) )
def render(self):
"""Render this metric
Each metric is rendered as:
name{label1="val1",label2="val2"} value
https://prometheus.io/docs/instrumenting/exposition_formats/#text-format-details
Returns:
iterable[str]: rendered metrics
"""
raise NotImplementedError()
class CounterMetric(BaseMetric): class CounterMetric(BaseMetric):
"""The simplest kind of metric; one that stores a monotonically-increasing """The simplest kind of metric; one that stores a monotonically-increasing
@ -62,6 +84,10 @@ class CounterMetric(BaseMetric):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(CounterMetric, self).__init__(*args, **kwargs) super(CounterMetric, self).__init__(*args, **kwargs)
# dict[list[str]]: value for each set of label values. the keys are the
# label values, in the same order as the labels in self.labels.
#
# (if the metric is a scalar, the (single) key is the empty list).
self.counts = {} self.counts = {}
# Scalar metrics are never empty # Scalar metrics are never empty