| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 # --ts-mon-flush-interval-secs seconds. May be None if | 71 # --ts-mon-flush-interval-secs seconds. May be None if |
| 72 # --ts-mon-flush != 'auto' or --ts-mon-flush-interval-secs == 0. | 72 # --ts-mon-flush != 'auto' or --ts-mon-flush-interval-secs == 0. |
| 73 self.flush_thread = None | 73 self.flush_thread = None |
| 74 # All metrics created by this application. | 74 # All metrics created by this application. |
| 75 self.metrics = {} | 75 self.metrics = {} |
| 76 # The MetricStore object that holds the actual metric values. | 76 # The MetricStore object that holds the actual metric values. |
| 77 self.store = store_ctor(self) | 77 self.store = store_ctor(self) |
| 78 # Cached time of the last flush. Useful mostly in AppEngine apps. | 78 # Cached time of the last flush. Useful mostly in AppEngine apps. |
| 79 self.last_flushed = datetime.datetime.utcfromtimestamp(0) | 79 self.last_flushed = datetime.datetime.utcfromtimestamp(0) |
| 80 | 80 |
| 81 def reset_for_unittest(self): | |
| 82 self.metrics = {} | |
| 83 self.last_flushed = datetime.datetime.utcfromtimestamp(0) | |
| 84 self.store.reset_for_unittest() | |
| 85 | |
| 86 state = State() | 81 state = State() |
| 87 | 82 |
| 88 | 83 |
| 89 def flush(): | 84 def flush(): |
| 90 """Send all metrics that are registered in the application.""" | 85 """Send all metrics that are registered in the application.""" |
| 91 if not state.global_monitor or not state.target: | 86 if not state.global_monitor or not state.target: |
| 92 raise errors.MonitoringNoConfiguredMonitorError(None) | 87 raise errors.MonitoringNoConfiguredMonitorError(None) |
| 93 | 88 |
| 94 if not state.flush_enabled_fn(): | 89 if not state.flush_enabled_fn(): |
| 95 logging.debug('ts_mon: sending metrics is disabled.') | 90 logging.debug('ts_mon: sending metrics is disabled.') |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 """Removes the metric from the list of metrics sent by flush().""" | 125 """Removes the metric from the list of metrics sent by flush().""" |
| 131 del state.metrics[metric.name] | 126 del state.metrics[metric.name] |
| 132 | 127 |
| 133 | 128 |
| 134 def close(): | 129 def close(): |
| 135 """Stops any background threads and waits for them to exit.""" | 130 """Stops any background threads and waits for them to exit.""" |
| 136 if state.flush_thread is not None: | 131 if state.flush_thread is not None: |
| 137 state.flush_thread.stop() | 132 state.flush_thread.stop() |
| 138 | 133 |
| 139 | 134 |
| 140 def reset_for_unittest(disable=False): | 135 def reset_for_unittest(): |
| 141 state.reset_for_unittest() | 136 state.store.reset_for_unittest() |
| 142 if disable: | |
| 143 state.flush_enabled_fn = lambda: False | |
| 144 | 137 |
| 145 | 138 |
| 146 class _FlushThread(threading.Thread): | 139 class _FlushThread(threading.Thread): |
| 147 """Background thread that flushes metrics on an interval.""" | 140 """Background thread that flushes metrics on an interval.""" |
| 148 | 141 |
| 149 def __init__(self, interval_secs, stop_event=None): | 142 def __init__(self, interval_secs, stop_event=None): |
| 150 super(_FlushThread, self).__init__(name='ts_mon') | 143 super(_FlushThread, self).__init__(name='ts_mon') |
| 151 | 144 |
| 152 if stop_event is None: | 145 if stop_event is None: |
| 153 stop_event = threading.Event() | 146 stop_event = threading.Event() |
| (...skipping 30 matching lines...) Expand all Loading... |
| 184 'Last monitoring flush took %f seconds (longer than ' | 177 'Last monitoring flush took %f seconds (longer than ' |
| 185 '--ts-mon-flush-interval-secs = %f seconds)', | 178 '--ts-mon-flush-interval-secs = %f seconds)', |
| 186 flush_duration, self.interval_secs) | 179 flush_duration, self.interval_secs) |
| 187 next_timeout = 0 | 180 next_timeout = 0 |
| 188 | 181 |
| 189 def stop(self): | 182 def stop(self): |
| 190 """Stops the background thread and performs a final flush.""" | 183 """Stops the background thread and performs a final flush.""" |
| 191 | 184 |
| 192 self.stop_event.set() | 185 self.stop_event.set() |
| 193 self.join() | 186 self.join() |
| OLD | NEW |