| OLD | NEW |
| (Empty) |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Monitor object which sends metrics to the monitoring api.""" | |
| 6 | |
| 7 | |
| 8 import logging | |
| 9 import os | |
| 10 | |
| 11 from monacq import acquisition_api | |
| 12 | |
| 13 from infra_libs import httplib2_utils | |
| 14 from infra_libs.ts_mon.common import monitors | |
| 15 | |
| 16 | |
| 17 def _logging_callback(resp, content): | |
| 18 logging.debug(repr(resp)) | |
| 19 logging.debug(content) | |
| 20 | |
| 21 | |
| 22 class ApiMonitor(monitors.Monitor): | |
| 23 """Class which sends metrics to the monitoring api.""" | |
| 24 def __init__(self, credsfile, endpoint, use_instrumented_http=True): | |
| 25 """Process monitoring related command line flags and initialize api. | |
| 26 | |
| 27 Args: | |
| 28 credsfile (str): path to the credentials json file | |
| 29 endpoint (str): url of the monitoring endpoint to hit | |
| 30 """ | |
| 31 | |
| 32 if credsfile in (monitors.APPENGINE_CREDENTIALS, | |
| 33 monitors.GCE_CREDENTIALS): | |
| 34 raise NotImplementedError( | |
| 35 'Appengine or GCE service accounts are not supported for ApiMonitor') | |
| 36 | |
| 37 creds = acquisition_api.AcquisitionCredential.Load( | |
| 38 os.path.abspath(credsfile)) | |
| 39 api = acquisition_api.AcquisitionApi(creds, endpoint) | |
| 40 api.SetResponseCallback(_logging_callback) | |
| 41 | |
| 42 if use_instrumented_http: | |
| 43 api.SetHttp(httplib2_utils.InstrumentedHttp('acq-mon-api')) | |
| 44 | |
| 45 self._api = api | |
| 46 | |
| 47 def send(self, metric_pb): | |
| 48 """Send a metric proto to the monitoring api. | |
| 49 | |
| 50 Args: | |
| 51 metric_pb (MetricsData or MetricsCollection): the metric protobuf to send | |
| 52 """ | |
| 53 try: | |
| 54 self._api.Send(self._wrap_proto(metric_pb)) | |
| 55 except acquisition_api.AcquisitionApiRequestException as e: | |
| 56 logging.error('Failed to send the metrics: %s', e) | |
| 57 | |
| OLD | NEW |