Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1762)

Unified Diff: appengine_module/gae_ts_mon/monitors.py

Issue 1260293009: make version of ts_mon compatible with appengine (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: clean up code Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: appengine_module/gae_ts_mon/monitors.py
diff --git a/infra_libs/ts_mon/monitors.py b/appengine_module/gae_ts_mon/monitors.py
similarity index 59%
copy from infra_libs/ts_mon/monitors.py
copy to appengine_module/gae_ts_mon/monitors.py
index 4fd55664cb964bce13aa9f37f9611d11a06fc7d9..1be7b77d1cbfe2c3db4f78f96f697015eb0e533a 100644
--- a/infra_libs/ts_mon/monitors.py
+++ b/appengine_module/gae_ts_mon/monitors.py
@@ -34,24 +34,13 @@ Library usage:
import base64
import json
-import logging
import os
-from monacq import acquisition_api
-from monacq.proto import metrics_pb2
-
-from infra_libs import logs
-import infra_libs
+from proto import metrics_pb2
import httplib2
from apiclient import discovery
from oauth2client.client import GoogleCredentials
-from oauth2client.file import Storage
-
-
-def _logging_callback(resp, content): # pragma: no cover
- logging.debug(repr(resp))
- logging.debug(content)
class Monitor(object):
@@ -85,38 +74,6 @@ class Monitor(object):
raise NotImplementedError()
-class ApiMonitor(Monitor):
- """Class which sends metrics to the monitoring api, the default behavior."""
- def __init__(self, credsfile, endpoint, use_instrumented_http=True):
- """Process monitoring related command line flags and initialize api.
-
- Args:
- credsfile (str): path to the credentials json file
- endpoint (str): url of the monitoring endpoint to hit
- """
-
- creds = acquisition_api.AcquisitionCredential.Load(
- os.path.abspath(credsfile))
- api = acquisition_api.AcquisitionApi(creds, endpoint)
- api.SetResponseCallback(_logging_callback)
-
- if use_instrumented_http:
- api.SetHttp(infra_libs.InstrumentedHttp('acq-mon-api'))
-
- self._api = api
-
- def send(self, metric_pb):
- """Send a metric proto to the monitoring api.
-
- Args:
- metric_pb (MetricsData or MetricsCollection): the metric protobuf to send
- """
- try:
- self._api.Send(self._wrap_proto(metric_pb))
- except acquisition_api.AcquisitionApiRequestException as e:
- logging.error('Failed to send the metrics: %s', e)
-
-
class PubSubMonitor(Monitor):
"""Class which publishes metrics to a Cloud Pub/Sub topic."""
@@ -124,33 +81,23 @@ class PubSubMonitor(Monitor):
'https://www.googleapis.com/auth/pubsub',
]
- @classmethod
- def _load_credentials(cls, credentials_file_path):
- with open(credentials_file_path, 'r') as credentials_file:
- credentials_json = json.load(credentials_file)
- if credentials_json.get('type', None):
- credentials = GoogleCredentials.from_stream(credentials_file_path)
- credentials = credentials.create_scoped(cls._SCOPES)
- return credentials
- return Storage(credentials_file_path).get()
-
- def _initialize(self, credsfile, project, topic):
+ def _initialize(self, project, topic):
# Copied from acquisition_api.AcquisitionCredential.Load.
- creds = self._load_credentials(credsfile)
+ creds = GoogleCredentials.get_application_default()
agable 2015/08/10 23:04:38 And the pubsub pipeline is accepting these credent
jshu 2015/08/11 21:57:18 Sergey had to add it to the list of whitelisted ap
agable 2015/08/12 22:18:08 Whitelisting is fine for now.
+ creds = creds.create_scoped(self._SCOPES)
self._http = httplib2.Http()
creds.authorize(self._http)
self._api = discovery.build('pubsub', 'v1', http=self._http)
self._topic = 'projects/%s/topics/%s' % (project, topic)
- def __init__(self, credsfile, project, topic):
+ def __init__(self, project, topic):
"""Process monitoring related command line flags and initialize api.
Args:
- credsfile (str): path to the credentials json file
project (str): the name of the Pub/Sub project to publish to.
topic (str): the name of the Pub/Sub topic to publish to.
"""
- self._initialize(credsfile, project, topic)
+ self._initialize(project, topic)
def send(self, metric_pb):
"""Send a metric proto to the monitoring api.
@@ -169,17 +116,6 @@ class PubSubMonitor(Monitor):
body=body).execute(num_retries=5)
-class DiskMonitor(Monitor):
- """Class which writes metrics to a local file for debugging."""
- def __init__(self, filepath):
- self._logger = logging.getLogger('__name__')
- filehandler = logging.FileHandler(filepath, 'a')
- logs.add_handler(self._logger, handler=filehandler, level=logging.INFO)
-
- def send(self, metric_pb):
- self._logger.info('\n' + str(self._wrap_proto(metric_pb)))
-
-
class NullMonitor(Monitor):
"""Class that doesn't send metrics anywhere."""
def send(self, metric_pb):

Powered by Google App Engine
This is Rietveld 408576698