| OLD | NEW |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """Classes representing individual metrics that can be sent.""" | 5 """Classes representing individual metrics that can be sent.""" |
| 6 | 6 |
| 7 | 7 |
| 8 import copy | 8 import copy |
| 9 import threading | 9 import threading |
| 10 import time | 10 import time |
| 11 | 11 |
| 12 from monacq.proto import metrics_pb2 | 12 try: |
| 13 | 13 from infra_libs.ts_mon import interface |
| 14 from infra_libs.ts_mon import distribution | 14 from infra_libs.ts_mon.common import distribution |
| 15 from infra_libs.ts_mon import errors | 15 from infra_libs.ts_mon.common import errors |
| 16 from infra_libs.ts_mon import interface | 16 from monacq.proto import metrics_pb2 |
| 17 except ImportError: |
| 18 import interface |
| 19 from common import distribution |
| 20 from common import errors |
| 21 from proto import metrics_pb2 |
| 17 | 22 |
| 18 | 23 |
| 19 MICROSECONDS_PER_SECOND = 1000000 | 24 MICROSECONDS_PER_SECOND = 1000000 |
| 20 | 25 |
| 21 | 26 |
| 22 class Metric(object): | 27 class Metric(object): |
| 23 """Abstract base class for a metric. | 28 """Abstract base class for a metric. |
| 24 | 29 |
| 25 A Metric is an attribute that may be monitored across many targets. Examples | 30 A Metric is an attribute that may be monitored across many targets. Examples |
| 26 include disk usage or the number of requests a server has received. A single | 31 include disk usage or the number of requests a server has received. A single |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 self._target = target | 67 self._target = target |
| 63 fields = fields or {} | 68 fields = fields or {} |
| 64 if len(fields) > 7: | 69 if len(fields) > 7: |
| 65 raise errors.MonitoringTooManyFieldsError(self._name, fields) | 70 raise errors.MonitoringTooManyFieldsError(self._name, fields) |
| 66 self._fields = fields | 71 self._fields = fields |
| 67 self._normalized_fields = self._normalize_fields(self._fields) | 72 self._normalized_fields = self._normalize_fields(self._fields) |
| 68 self._thread_lock = threading.Lock() | 73 self._thread_lock = threading.Lock() |
| 69 | 74 |
| 70 interface.register(self) | 75 interface.register(self) |
| 71 | 76 |
| 77 def __eq__(self, other): |
| 78 name = self._name == other._name |
| 79 target = self._target == other._target |
| 80 field = self._fields == other._fields |
| 81 instance_type = type(self) == type(other) |
| 82 return name and target and field and instance_type |
| 83 |
| 72 def unregister(self): | 84 def unregister(self): |
| 73 interface.unregister(self) | 85 interface.unregister(self) |
| 74 | 86 |
| 75 def serialize_to(self, collection_pb, default_target=None, loop_action=None): | 87 def serialize_to(self, collection_pb, default_target=None, loop_action=None): |
| 76 """Generate metrics_pb2.MetricsData messages for this metric. | 88 """Generate metrics_pb2.MetricsData messages for this metric. |
| 77 | 89 |
| 78 Args: | 90 Args: |
| 79 collection_pb (metrics_pb2.MetricsCollection): protocol buffer into which | 91 collection_pb (metrics_pb2.MetricsCollection): protocol buffer into which |
| 80 to add the current metric values. | 92 to add the current metric values. |
| 81 default_target (Target): a Target to use if self._target is not set. | 93 default_target (Target): a Target to use if self._target is not set. |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 425 | 437 |
| 426 def __init__( | 438 def __init__( |
| 427 self, name, bucketer=None, target=None, fields=None, time_fn=time.time): | 439 self, name, bucketer=None, target=None, fields=None, time_fn=time.time): |
| 428 super(NonCumulativeDistributionMetric, self).__init__( | 440 super(NonCumulativeDistributionMetric, self).__init__( |
| 429 name, | 441 name, |
| 430 is_cumulative=False, | 442 is_cumulative=False, |
| 431 bucketer=bucketer, | 443 bucketer=bucketer, |
| 432 target=target, | 444 target=target, |
| 433 fields=fields, | 445 fields=fields, |
| 434 time_fn=time_fn) | 446 time_fn=time_fn) |
| OLD | NEW |