Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Unified Diff: infra_libs/ts_mon/common/metrics.py

Issue 2111473003: Issue 623854: Support unit annotations in ts_mon metrics (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Split off go changes to a separate CL: https://codereview.chromium.org/2125943003 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « infra_libs/ts_mon/__init__.py ('k') | infra_libs/ts_mon/common/test/metrics_test.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 33a543761940d2f758dd5294ec722b361daeadbb..a9ee417384f1b4142449ab25ecf93beb040cf339 100644
--- a/infra_libs/ts_mon/common/metrics.py
+++ b/infra_libs/ts_mon/common/metrics.py
@@ -32,6 +32,12 @@ class Metric(object):
set() or increment() methods to modify a particular value, or passed to the
constructor in which case they will be used as the defaults for this Metric.
+ The unit of measurement for Metric data can be specified with MetricsDataUnits
+ when a Metric object is created:
+ e.g., MetricsDataUnits.SECONDS, MetricsDataUnits.BYTES, and etc..,
+ A full list of supported units can be found in the following protobuf file
+ : infra_libs/ts_mon/protos/metrics.proto
+
Do not directly instantiate an object of this class.
Use the concrete child classes instead:
* StringMetric for metrics with string value
@@ -44,7 +50,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 +58,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 +70,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 +108,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)
@@ -250,9 +262,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):
@@ -291,9 +304,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):
@@ -339,9 +353,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:
@@ -435,13 +449,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
@@ -451,13 +466,28 @@ 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."""
+ 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):
+ """An enumeration class for units of measurement for Metrics data.
+ See infra_libs/ts_mon/protos/metrics.proto for a full list of supported units.
+ """
+ __metaclass__ = MetaMetricsDataUnits
« no previous file with comments | « infra_libs/ts_mon/__init__.py ('k') | infra_libs/ts_mon/common/test/metrics_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698