| 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 25 matching lines...) Expand all Loading... |
| 36 import json | 36 import json |
| 37 import logging | 37 import logging |
| 38 import os | 38 import os |
| 39 | 39 |
| 40 from monacq import acquisition_api | 40 from monacq import acquisition_api |
| 41 from monacq.proto import metrics_pb2 | 41 from monacq.proto import metrics_pb2 |
| 42 | 42 |
| 43 from infra_libs import logs | 43 from infra_libs import logs |
| 44 import infra_libs | 44 import infra_libs |
| 45 | 45 |
| 46 import httplib2 | |
| 47 from apiclient import discovery | 46 from apiclient import discovery |
| 48 from oauth2client.client import GoogleCredentials | 47 from oauth2client.client import GoogleCredentials |
| 49 from oauth2client.file import Storage | 48 from oauth2client.file import Storage |
| 49 import httplib2 |
| 50 | 50 |
| 51 | 51 |
| 52 def _logging_callback(resp, content): # pragma: no cover | 52 def _logging_callback(resp, content): # pragma: no cover |
| 53 logging.debug(repr(resp)) | 53 logging.debug(repr(resp)) |
| 54 logging.debug(content) | 54 logging.debug(content) |
| 55 | 55 |
| 56 | 56 |
| 57 class Monitor(object): | 57 class Monitor(object): |
| 58 """Abstract base class encapsulating the ability to collect and send metrics. | 58 """Abstract base class encapsulating the ability to collect and send metrics. |
| 59 | 59 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 self._api = api | 106 self._api = api |
| 107 | 107 |
| 108 def send(self, metric_pb): | 108 def send(self, metric_pb): |
| 109 """Send a metric proto to the monitoring api. | 109 """Send a metric proto to the monitoring api. |
| 110 | 110 |
| 111 Args: | 111 Args: |
| 112 metric_pb (MetricsData or MetricsCollection): the metric protobuf to send | 112 metric_pb (MetricsData or MetricsCollection): the metric protobuf to send |
| 113 """ | 113 """ |
| 114 try: | 114 try: |
| 115 self._api.Send(self._wrap_proto(metric_pb)) | 115 self._api.Send(self._wrap_proto(metric_pb)) |
| 116 except acquisition_api.AcquisitionApiRequestException as e: | 116 except acquisition_api.AcquisitionApiRequestException as e: # pylint: disabl
e=undefined-variable |
| 117 logging.error('Failed to send the metrics: %s', e) | 117 logging.error('Failed to send the metrics: %s', e) |
| 118 | 118 |
| 119 | 119 |
| 120 class PubSubMonitor(Monitor): | 120 class PubSubMonitor(Monitor): |
| 121 """Class which publishes metrics to a Cloud Pub/Sub topic.""" | 121 """Class which publishes metrics to a Cloud Pub/Sub topic.""" |
| 122 | 122 |
| 123 _SCOPES = [ | 123 _SCOPES = [ |
| 124 'https://www.googleapis.com/auth/pubsub', | 124 'https://www.googleapis.com/auth/pubsub', |
| 125 ] | 125 ] |
| 126 | 126 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 logs.add_handler(self._logger, handler=filehandler, level=logging.INFO) | 177 logs.add_handler(self._logger, handler=filehandler, level=logging.INFO) |
| 178 | 178 |
| 179 def send(self, metric_pb): | 179 def send(self, metric_pb): |
| 180 self._logger.info('\n' + str(self._wrap_proto(metric_pb))) | 180 self._logger.info('\n' + str(self._wrap_proto(metric_pb))) |
| 181 | 181 |
| 182 | 182 |
| 183 class NullMonitor(Monitor): | 183 class NullMonitor(Monitor): |
| 184 """Class that doesn't send metrics anywhere.""" | 184 """Class that doesn't send metrics anywhere.""" |
| 185 def send(self, metric_pb): | 185 def send(self, metric_pb): |
| 186 pass | 186 pass |
| OLD | NEW |