| 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 27 matching lines...) Expand all Loading... |
| 38 import time | 38 import time |
| 39 import traceback | 39 import traceback |
| 40 | 40 |
| 41 from infra_libs.ts_mon.common import errors | 41 from infra_libs.ts_mon.common import errors |
| 42 from infra_libs.ts_mon.common import metric_store | 42 from infra_libs.ts_mon.common import metric_store |
| 43 from infra_libs.ts_mon.protos.current import metrics_pb2 | 43 from infra_libs.ts_mon.protos.current import metrics_pb2 |
| 44 from infra_libs.ts_mon.protos.new import metrics_pb2 as new_metrics_pb2 | 44 from infra_libs.ts_mon.protos.new import metrics_pb2 as new_metrics_pb2 |
| 45 | 45 |
| 46 # The maximum number of MetricsData messages to include in each HTTP request. | 46 # The maximum number of MetricsData messages to include in each HTTP request. |
| 47 # MetricsCollections larger than this will be split into multiple requests. | 47 # MetricsCollections larger than this will be split into multiple requests. |
| 48 METRICS_DATA_LENGTH_LIMIT = 1000 | 48 METRICS_DATA_LENGTH_LIMIT = 500 |
| 49 | 49 |
| 50 | 50 |
| 51 class State(object): | 51 class State(object): |
| 52 """Package-level state is stored here so that it is easily accessible. | 52 """Package-level state is stored here so that it is easily accessible. |
| 53 | 53 |
| 54 Configuration is kept in this one object at the global level so that all | 54 Configuration is kept in this one object at the global level so that all |
| 55 libraries in use by the same tool or service can all take advantage of the | 55 libraries in use by the same tool or service can all take advantage of the |
| 56 same configuration. | 56 same configuration. |
| 57 """ | 57 """ |
| 58 | 58 |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 except Exception: | 249 except Exception: |
| 250 logging.exception('Automatic monitoring flush failed.') | 250 logging.exception('Automatic monitoring flush failed.') |
| 251 | 251 |
| 252 def run(self): | 252 def run(self): |
| 253 # Jitter the first interval so tasks started at the same time (say, by cron) | 253 # Jitter the first interval so tasks started at the same time (say, by cron) |
| 254 # on different machines don't all send metrics simultaneously. | 254 # on different machines don't all send metrics simultaneously. |
| 255 next_timeout = random.uniform(self.interval_secs / 2.0, self.interval_secs) | 255 next_timeout = random.uniform(self.interval_secs / 2.0, self.interval_secs) |
| 256 | 256 |
| 257 while True: | 257 while True: |
| 258 if self.stop_event.wait(next_timeout): | 258 if self.stop_event.wait(next_timeout): |
| 259 self._flush_and_log_exceptions() | |
| 260 return | 259 return |
| 261 | 260 |
| 262 # Try to flush every N seconds exactly so rate calculations are more | 261 # Try to flush every N seconds exactly so rate calculations are more |
| 263 # consistent. | 262 # consistent. |
| 264 start = time.time() | 263 start = time.time() |
| 265 self._flush_and_log_exceptions() | 264 self._flush_and_log_exceptions() |
| 266 flush_duration = time.time() - start | 265 flush_duration = time.time() - start |
| 267 next_timeout = self.interval_secs - flush_duration | 266 next_timeout = self.interval_secs - flush_duration |
| 268 | 267 |
| 269 if next_timeout < 0: | 268 if next_timeout < 0: |
| 270 logging.warning( | 269 logging.warning( |
| 271 'Last monitoring flush took %f seconds (longer than ' | 270 'Last monitoring flush took %f seconds (longer than ' |
| 272 '--ts-mon-flush-interval-secs = %f seconds)', | 271 '--ts-mon-flush-interval-secs = %f seconds)', |
| 273 flush_duration, self.interval_secs) | 272 flush_duration, self.interval_secs) |
| 274 next_timeout = 0 | 273 next_timeout = 0 |
| 275 | 274 |
| 276 def stop(self): | 275 def stop(self): |
| 277 """Stops the background thread and performs a final flush.""" | 276 """Stops the background thread and performs a final flush.""" |
| 278 | 277 |
| 279 self.stop_event.set() | 278 self.stop_event.set() |
| 280 self.join() | 279 self.join() |
| OLD | NEW |