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 |
50 from oauth2client.gce import AppAssertionCredentials | 49 from oauth2client.gce import AppAssertionCredentials |
| 50 import httplib2 |
51 | 51 |
52 # Special string that can be passed through as the credentials path to use the | 52 # Special string that can be passed through as the credentials path to use the |
53 # default GCE service account. | 53 # default GCE service account. |
54 GCE_CREDENTIALS = ':gce' | 54 GCE_CREDENTIALS = ':gce' |
55 | 55 |
56 | 56 |
57 def _logging_callback(resp, content): # pragma: no cover | 57 def _logging_callback(resp, content): # pragma: no cover |
58 logging.debug(repr(resp)) | 58 logging.debug(repr(resp)) |
59 logging.debug(content) | 59 logging.debug(content) |
60 | 60 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 140 |
141 with open(credentials_file_path, 'r') as credentials_file: | 141 with open(credentials_file_path, 'r') as credentials_file: |
142 credentials_json = json.load(credentials_file) | 142 credentials_json = json.load(credentials_file) |
143 if credentials_json.get('type', None): | 143 if credentials_json.get('type', None): |
144 credentials = GoogleCredentials.from_stream(credentials_file_path) | 144 credentials = GoogleCredentials.from_stream(credentials_file_path) |
145 credentials = credentials.create_scoped(cls._SCOPES) | 145 credentials = credentials.create_scoped(cls._SCOPES) |
146 return credentials | 146 return credentials |
147 return Storage(credentials_file_path).get() | 147 return Storage(credentials_file_path).get() |
148 | 148 |
149 def _initialize(self, credsfile, project, topic, use_instrumented_http): | 149 def _initialize(self, credsfile, project, topic, use_instrumented_http): |
150 # Copied from acquisition_api.AcquisitionCredential.Load. | |
151 creds = self._load_credentials(credsfile) | 150 creds = self._load_credentials(credsfile) |
152 if use_instrumented_http: | 151 if use_instrumented_http: |
153 self._http = infra_libs.InstrumentedHttp('acq-mon-api-pubsub') | 152 self._http = infra_libs.InstrumentedHttp('acq-mon-api-pubsub') |
154 else: # pragma: no cover | 153 else: # pragma: no cover |
155 self._http = httplib2.Http() | 154 self._http = httplib2.Http() |
156 creds.authorize(self._http) | 155 creds.authorize(self._http) |
157 self._api = discovery.build('pubsub', 'v1', http=self._http) | 156 self._api = discovery.build('pubsub', 'v1', http=self._http) |
158 self._topic = 'projects/%s/topics/%s' % (project, topic) | 157 self._topic = 'projects/%s/topics/%s' % (project, topic) |
159 | 158 |
160 def __init__(self, credsfile, project, topic, use_instrumented_http=True): | 159 def __init__(self, credsfile, project, topic, use_instrumented_http=True): |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 logs.add_handler(self._logger, handler=filehandler, level=logging.INFO) | 193 logs.add_handler(self._logger, handler=filehandler, level=logging.INFO) |
195 | 194 |
196 def send(self, metric_pb): | 195 def send(self, metric_pb): |
197 self._logger.info('\n' + str(self._wrap_proto(metric_pb))) | 196 self._logger.info('\n' + str(self._wrap_proto(metric_pb))) |
198 | 197 |
199 | 198 |
200 class NullMonitor(Monitor): | 199 class NullMonitor(Monitor): |
201 """Class that doesn't send metrics anywhere.""" | 200 """Class that doesn't send metrics anywhere.""" |
202 def send(self, metric_pb): | 201 def send(self, metric_pb): |
203 pass | 202 pass |
OLD | NEW |