| 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 the monitoring interface for tasks or devices. | 5 """Classes representing the monitoring interface for tasks or devices. |
| 6 | 6 |
| 7 Usage: | 7 Usage: |
| 8 import argparse | 8 import argparse |
| 9 from infra_libs import ts_mon | 9 from infra_libs import ts_mon |
| 10 | 10 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 """ | 32 """ |
| 33 | 33 |
| 34 import logging | 34 import logging |
| 35 import os | 35 import os |
| 36 import random | 36 import random |
| 37 import threading | 37 import threading |
| 38 import time | 38 import time |
| 39 | 39 |
| 40 from monacq.proto import metrics_pb2 | 40 from monacq.proto import metrics_pb2 |
| 41 | 41 |
| 42 from infra_libs.ts_mon import errors | 42 from infra_libs.ts_mon.common import errors |
| 43 | 43 |
| 44 # The maximum number of MetricsData messages to include in each HTTP request. | 44 # The maximum number of MetricsData messages to include in each HTTP request. |
| 45 # MetricsCollections larger than this will be split into multiple requests. | 45 # MetricsCollections larger than this will be split into multiple requests. |
| 46 METRICS_DATA_LENGTH_LIMIT = 5000 | 46 METRICS_DATA_LENGTH_LIMIT = 5000 |
| 47 | 47 |
| 48 | 48 |
| 49 class State(object): | 49 class State(object): |
| 50 """Package-level state is stored here so that it is easily accessible. | 50 """Package-level state is stored here so that it is easily accessible. |
| 51 | 51 |
| 52 Configuration is kept in this one object at the global level so that all | 52 Configuration is kept in this one object at the global level so that all |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 for metric in state.metrics: | 104 for metric in state.metrics: |
| 105 metric.serialize_to(proto, default_target=state.default_target, | 105 metric.serialize_to(proto, default_target=state.default_target, |
| 106 loop_action=loop_action) | 106 loop_action=loop_action) |
| 107 | 107 |
| 108 state.global_monitor.send(proto) | 108 state.global_monitor.send(proto) |
| 109 | 109 |
| 110 | 110 |
| 111 def register(metric): | 111 def register(metric): |
| 112 """Adds the metric to the list of metrics sent by flush(). | 112 """Adds the metric to the list of metrics sent by flush(). |
| 113 | 113 |
| 114 This is called automatically by Metric's constructor - you don't need to call | 114 This is called automatically by Metric's constructor. |
| 115 it manually. | |
| 116 """ | 115 """ |
| 117 # If someone is registering the same metric object twice, that's okay, but | 116 # If someone is registering the same metric object twice, that's okay, but |
| 118 # registering two different metric objects with the same metric name is not. | 117 # registering two different metric objects with the same metric name is not. |
| 119 if metric in state.metrics: | 118 for m in state.metrics: |
| 120 return | 119 if metric == m: |
| 120 state.metrics.remove(m) |
| 121 state.metrics.add(metric) |
| 122 return |
| 121 if any([metric._name == m._name for m in state.metrics]): | 123 if any([metric._name == m._name for m in state.metrics]): |
| 122 raise errors.MonitoringDuplicateRegistrationError(metric._name) | 124 raise errors.MonitoringDuplicateRegistrationError(metric._name) |
| 123 | 125 |
| 124 state.metrics.add(metric) | 126 state.metrics.add(metric) |
| 125 | 127 |
| 126 | |
| 127 def unregister(metric): | 128 def unregister(metric): |
| 128 """Removes the metric from the list of metrics sent by flush().""" | 129 """Removes the metric from the list of metrics sent by flush().""" |
| 129 state.metrics.remove(metric) | 130 state.metrics.remove(metric) |
| 130 | 131 |
| 131 | 132 |
| 132 def close(): | 133 def close(): |
| 133 """Stops any background threads and waits for them to exit.""" | 134 """Stops any background threads and waits for them to exit.""" |
| 134 if state.flush_thread is not None: | 135 if state.flush_thread is not None: |
| 135 state.flush_thread.stop() | 136 state.flush_thread.stop() |
| 136 | 137 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 'Last monitoring flush took %f seconds (longer than ' | 177 'Last monitoring flush took %f seconds (longer than ' |
| 177 '--ts-mon-flush-interval-secs = %f seconds)', | 178 '--ts-mon-flush-interval-secs = %f seconds)', |
| 178 flush_duration, self.interval_secs) | 179 flush_duration, self.interval_secs) |
| 179 next_timeout = 0 | 180 next_timeout = 0 |
| 180 | 181 |
| 181 def stop(self): | 182 def stop(self): |
| 182 """Stops the background thread and performs a final flush.""" | 183 """Stops the background thread and performs a final flush.""" |
| 183 | 184 |
| 184 self.stop_event.set() | 185 self.stop_event.set() |
| 185 self.join() | 186 self.join() |
| OLD | NEW |