Chromium Code Reviews| Index: infra_libs/ts_mon/common/metrics.py |
| diff --git a/infra_libs/ts_mon/common/metrics.py b/infra_libs/ts_mon/common/metrics.py |
| index 28969e5f64c60a7a47a3fb69ae7f368621a29e39..6ec195a3afb26d0cbe6fcbaab1c5ceda565069e6 100644 |
| --- a/infra_libs/ts_mon/common/metrics.py |
| +++ b/infra_libs/ts_mon/common/metrics.py |
| @@ -44,7 +44,7 @@ class Metric(object): |
| See http://go/inframon-doc for help designing and using your metrics. |
| """ |
| - def __init__(self, name, fields=None, description=None): |
| + def __init__(self, name, fields=None, description=None, units=None): |
| """Create an instance of a Metric. |
| Args: |
| @@ -52,6 +52,9 @@ class Metric(object): |
| fields (dict): a set of key-value pairs to be set as default metric fields |
| description (string): help string for the metric. Should be enough to |
| know what the metric is about. |
| + units (int): the unit used to measure data for given |
| + metric. Please use the attributes of MetricDataUnit to find |
| + valid integer values for this argument. |
| """ |
| self._name = name.lstrip('/') |
| self._start_time = None |
| @@ -61,6 +64,7 @@ class Metric(object): |
| self._fields = fields |
| self._normalized_fields = self._normalize_fields(self._fields) |
| self._description = description |
| + self._units = units |
| interface.register(self) |
| @@ -98,6 +102,8 @@ class Metric(object): |
| metric_pb.name = self._name |
| if self._description is not None: |
| metric_pb.description = self._description |
| + if self._units is not None: |
| + metric_pb.units = self._units |
| self._populate_value(metric_pb, value, start_time) |
| self._populate_fields(metric_pb, fields) |
| @@ -247,9 +253,10 @@ class NumericMetric(Metric): # pylint: disable=abstract-method |
| class CounterMetric(NumericMetric): |
| """A metric whose value type is a monotonically increasing integer.""" |
| - def __init__(self, name, fields=None, start_time=None, description=None): |
| + def __init__(self, name, fields=None, start_time=None, description=None, |
| + units=None): |
| super(CounterMetric, self).__init__( |
| - name, fields=fields, description=description) |
| + name, fields=fields, description=description, units=units) |
| self._start_time = start_time |
| def _populate_value(self, metric, value, start_time): |
| @@ -288,9 +295,10 @@ class GaugeMetric(NumericMetric): |
| class CumulativeMetric(NumericMetric): |
| """A metric whose value type is a monotonically increasing float.""" |
| - def __init__(self, name, fields=None, start_time=None, description=None): |
| + def __init__(self, name, fields=None, start_time=None, description=None, |
| + units=None): |
| super(CumulativeMetric, self).__init__( |
| - name, fields=fields, description=description) |
| + name, fields=fields, description=description, units=units) |
| self._start_time = start_time |
| def _populate_value(self, metric, value, start_time): |
| @@ -336,9 +344,9 @@ class DistributionMetric(Metric): |
| } |
| def __init__(self, name, is_cumulative=True, bucketer=None, fields=None, |
| - start_time=None, description=None): |
| + start_time=None, description=None, units=None): |
| super(DistributionMetric, self).__init__( |
| - name, fields=fields, description=description) |
| + name, fields=fields, description=description, units=units) |
| self._start_time = start_time |
| if bucketer is None: |
| @@ -432,13 +440,14 @@ class CumulativeDistributionMetric(DistributionMetric): |
| """A DistributionMetric with is_cumulative set to True.""" |
| def __init__(self, name, bucketer=None, fields=None, |
| - description=None): |
| + description=None, units=None): |
| super(CumulativeDistributionMetric, self).__init__( |
| name, |
| is_cumulative=True, |
| bucketer=bucketer, |
| fields=fields, |
| - description=description) |
| + description=description, |
| + units=units) |
| def is_cumulative(self): |
| return True |
| @@ -448,13 +457,33 @@ class NonCumulativeDistributionMetric(DistributionMetric): |
| """A DistributionMetric with is_cumulative set to False.""" |
| def __init__(self, name, bucketer=None, fields=None, |
| - description=None): |
| + description=None, units=None): |
| super(NonCumulativeDistributionMetric, self).__init__( |
| name, |
| is_cumulative=False, |
| bucketer=bucketer, |
| fields=fields, |
| - description=description) |
| + description=description, |
| + units=units) |
| def is_cumulative(self): |
| return False |
| + |
| + |
| +class MetaMetricsDataUnits(type): |
| + """Metaclass to populate the enum values of metrics_pb2.MetricsData.Units |
|
agable
2016/06/29 18:25:23
nit: all our docstrings start with a single senten
ddoman
2016/07/06 03:35:37
Done.
|
| + as class-level attributes. |
| + """ |
| + def __new__(mcs, name, bases, attrs): |
| + attrs.update(metrics_pb2.MetricsData.Units.items()) |
| + return super(MetaMetricsDataUnits, mcs).__new__(mcs, name, bases, attrs) |
| + |
| + |
| +class MetricsDataUnits(object): |
| + """Wrapper class for MetricsData.Units. Providing this wrapper class, |
| + applications that use ts_mon.metrics don't need to import |
| + metrics_pb2.MetricsData. |
| + |
| + See infra_libs/ts_mon/protos/metrics.proto for a list of supported units. |
| + """ |
| + __metaclass__ = MetaMetricsDataUnits |