|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +from google.cloud import logging as gcloud_logging |
| 4 | +from google.cloud import monitoring |
| 5 | +from google.cloud.monitoring import MetricKind, ValueType |
| 6 | +from oauth2client.client import GoogleCredentials |
| 7 | +import logging |
| 8 | +from flask import Flask, request, abort, jsonify |
| 9 | +app = Flask(__name__) |
| 10 | + |
| 11 | + |
| 12 | +@app.route('/') |
| 13 | +def hello_world(): |
| 14 | + return 'Hello World!' |
| 15 | + |
| 16 | + |
| 17 | +@app.route('/logging', methods=['POST']) |
| 18 | +def logging(): |
| 19 | + request_data = request.get_json() |
| 20 | + if request_data is None: |
| 21 | + raise ErrorResponse("Unable to parse request JSON: did you set the Content-type header?") |
| 22 | + log_name = request_data.get('log_name', '') |
| 23 | + if log_name == '': |
| 24 | + raise ErrorResponse("please provide log name") |
| 25 | + token = request_data.get('token', '') |
| 26 | + if token == '': |
| 27 | + raise ErrorResponse("please provide token name") |
| 28 | + |
| 29 | + _log("log name is {0}, token is {1}".format(log_name, token)) |
| 30 | + _log(token, log_name) |
| 31 | + |
| 32 | + return ('', 204) |
| 33 | + |
| 34 | + |
| 35 | +def _log(token, log_name='stdout'): |
| 36 | + # TODO (nkubala): write token to 'log_name' log, instead of stdout |
| 37 | + # is this possible in non-standard (flex)??? |
| 38 | + |
| 39 | + # try: |
| 40 | + # client = gcloud_logging.Client(credentials=GoogleCredentials.get_application_default()) |
| 41 | + # gcloud_logger = client.logger(log_name) |
| 42 | + # gcloud_logger.log_text(token) |
| 43 | + # except Exception as e: |
| 44 | + # logging.error("error while writing logs") |
| 45 | + # raise ErrorResponse("error while writing logs: {0}".format(e)) |
| 46 | + |
| 47 | + # logging.info(token) |
| 48 | + print token |
| 49 | + |
| 50 | + |
| 51 | +@app.route('/monitoring', methods=['POST']) |
| 52 | +def monitoring(): |
| 53 | + request_data = request.get_json() |
| 54 | + if request_data is None: |
| 55 | + raise ErrorResponse("Unable to parse request JSON: did you set the Content-type header?") |
| 56 | + name = request_data.get('name', '') |
| 57 | + if name == '': |
| 58 | + raise ErrorResponse("please provide name") |
| 59 | + token = request_data.get('token', '') |
| 60 | + if token == '': |
| 61 | + raise ErrorResponse("please provide metric token") |
| 62 | + |
| 63 | + client = monitoring.Client(credentials=GoogleCredentials.get_application_default()) |
| 64 | + |
| 65 | + descriptor = client.metric_descriptor( |
| 66 | + 'custom.googleapis.com/{0}'.format(name), |
| 67 | + metric_kind=MetricKind.GAUGE, |
| 68 | + value_type=ValueType.DOUBLE, |
| 69 | + description=token |
| 70 | + ) |
| 71 | + descriptor.create() |
| 72 | + |
| 73 | + # resource = client.resource() |
| 74 | + |
| 75 | + metric = client.metric( |
| 76 | + type='custom.googleapis.com/{0}'.format(name)) |
| 77 | + |
| 78 | + client.write_point(metric=metric, value=token) |
| 79 | + |
| 80 | + print 'OK' |
| 81 | + |
| 82 | + |
| 83 | +@app.route('/exception', methods=['POST']) |
| 84 | +def exception(): |
| 85 | + print '' |
| 86 | + |
| 87 | + |
| 88 | +@app.route('/trace', methods=['POST']) |
| 89 | +def trace(): |
| 90 | + print'' |
| 91 | + |
| 92 | + |
| 93 | +class ErrorResponse(Exception): |
| 94 | + status_code = 400 |
| 95 | + |
| 96 | + def __init__(self, message, status_code=None, payload=None): |
| 97 | + Exception.__init__(self) |
| 98 | + self.message = message |
| 99 | + if status_code is not None: |
| 100 | + self.status_code = status_code |
| 101 | + self.payload = payload |
| 102 | + |
| 103 | + def to_dict(self): |
| 104 | + rv = dict(self.payload or ()) |
| 105 | + rv['message'] = self.message |
| 106 | + return rv |
| 107 | + |
| 108 | + |
| 109 | +@app.errorhandler(ErrorResponse) |
| 110 | +def handle_invalid_usage(error): |
| 111 | + response = jsonify(error.to_dict()) |
| 112 | + response.status_code = error.status_code |
| 113 | + return response |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == '__main__': |
| 117 | + app.run(debug=True, host='0.0.0.0', port=8080) |
0 commit comments