Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 3 # Use of this source code is governed by a BSD-style license that can be | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Send system monitoring data to the timeseries monitoring API.""" | |
| 7 | |
| 8 import argparse | |
| 9 import logging | |
| 10 import os | |
| 11 import random | |
| 12 import sys | |
| 13 import time | |
| 14 import urllib2 | |
| 15 import webapp2 | |
| 16 | |
| 17 import gae_ts_mon | |
| 18 import ts_alerts | |
| 19 | |
| 20 from google.appengine.api import background_thread | |
| 21 from google.appengine.api import modules | |
| 22 | |
| 23 | |
| 24 access_count = gae_ts_mon.CounterMetric('gae/access/count') | |
|
agable
2015/08/10 23:04:38
Rather than having metrics for these endpoints ("a
| |
| 25 alerts_count = gae_ts_mon.GaugeMetric('gae/alerts/count') | |
| 26 | |
| 27 | |
| 28 class InitializeMonitoringHandler(webapp2.RequestHandler): | |
| 29 | |
| 30 def get(self): | |
| 31 job_name = 'sheriff-o-matic' | |
| 32 instance_id = int(modules.get_current_instance_id()) | |
| 33 endpoint = 'pubsub://chrome-infra-mon-pubsub/monacq' | |
| 34 gae_ts_mon.initialize(job_name=job_name, number=instance_id, | |
| 35 service_name='usage', endpoint=endpoint) | |
| 36 self.response.set_status(200, 'Initialized instance of ts_mon.') | |
| 37 | |
| 38 | |
| 39 class MonitoringHandler(webapp2.RequestHandler): | |
| 40 | |
| 41 ACCESS_COUNT = {'ts-alerts': 0, | |
|
agable
2015/08/10 23:04:38
Rather than keeping a dictionary here, you should
| |
| 42 'ts-alerts-history': 0, | |
| 43 'alerts': 0, | |
| 44 'alerts-history': 0, | |
| 45 'api/v1/alerts': 0} | |
| 46 | |
| 47 | |
| 48 ''' Called my cron jobs every 5 minutes to update alerts_count. ''' | |
|
agable
2015/08/10 23:04:38
nit: """Called by cron... alerts_count."""
| |
| 49 def get(self, key=None): | |
| 50 if key and key in self.ACCESS_COUNT.keys(): | |
| 51 self.ACCESS_COUNT[key] += 1 | |
| 52 self.response.write('POST SUCCESS: %s' % key) | |
| 53 return | |
| 54 elif key: | |
| 55 self.response.write('Unknown key %s' % key) | |
| 56 self.abort(400) | |
| 57 return | |
| 58 for endpoint in self.ACCESS_COUNT.keys(): | |
| 59 access_count.set(self.ACCESS_COUNT[endpoint], {'service': endpoint}) | |
| 60 time_series_count = len(ts_alerts.TSAlertsJSON.query_active().fetch()) | |
| 61 alerts_count.set(time_series_count, {'service': 'time-series-alerts'}) | |
| 62 gae_ts_mon.flush() | |
| 63 self.response.write('Sheriff-o-matic metrics updated.') | |
| 64 return | |
| 65 | |
| 66 def post(self): | |
| 67 logging.info('in post request') | |
|
agable
2015/08/10 23:04:38
nit: remove some of the extraneous logging before
| |
| 68 logging.info(self.request.body) | |
| 69 key = self.request.body['endpoint'] | |
| 70 if key not in self.ACCESS_COUNT.keys(): | |
| 71 self.response.write('Unknown key %s', key) | |
| 72 self.abort(400) | |
| 73 return | |
| 74 self.ACCESS_COUNT[key] += 1 | |
| 75 self.response.write('POST SUCCESS: ' + key) | |
| 76 | |
| 77 | |
| 78 app = webapp2.WSGIApplication([ | |
| 79 ('/_ah/start', InitializeMonitoringHandler), | |
| 80 ('/monitoring', MonitoringHandler), | |
| 81 ('/monitoring/(.*)', MonitoringHandler) | |
| 82 ], debug=True) | |
| 83 | |
| OLD | NEW |