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