11#!/usr/bin/python
22
33from google .cloud import logging as gcloud_logging
4- from google .cloud import monitoring
4+ from google .cloud import monitoring as gcloud_monitoring
55from google .cloud .monitoring import MetricKind , ValueType
6+ from google .cloud .exceptions import Forbidden as ForbiddenException
7+ from google .cloud .exceptions import NotFound as NotFoundException
68from oauth2client .client import GoogleCredentials
79import logging
810from flask import Flask , request , abort , jsonify
@@ -15,7 +17,7 @@ def hello_world():
1517
1618
1719@app .route ('/logging' , methods = ['POST' ])
18- def logging ():
20+ def _logging ():
1921 request_data = request .get_json ()
2022 if request_data is None :
2123 raise ErrorResponse ("Unable to parse request JSON: did you set the Content-type header?" )
@@ -32,24 +34,25 @@ def logging():
3234 return ('' , 204 )
3335
3436
37+ # TODO (nkubala): just as a note, currently the client logging API is broken
3538def _log (token , log_name = 'stdout' ):
3639 # TODO (nkubala): write token to 'log_name' log, instead of stdout
3740 # is this possible in non-standard (flex)???
3841
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))
42+ try :
43+ client = gcloud_logging .Client (credentials = GoogleCredentials .get_application_default ())
44+ gcloud_logger = client .logger (log_name )
45+ gcloud_logger .log_text (token )
46+ except Exception as e :
47+ logging .error ("error while writing logs" )
48+ raise ErrorResponse ("error while writing logs: {0}" .format (e ))
4649
4750 # logging.info(token)
4851 print token
4952
5053
5154@app .route ('/monitoring' , methods = ['POST' ])
52- def monitoring ():
55+ def _monitoring ():
5356 request_data = request .get_json ()
5457 if request_data is None :
5558 raise ErrorResponse ("Unable to parse request JSON: did you set the Content-type header?" )
@@ -60,33 +63,49 @@ def monitoring():
6063 if token == '' :
6164 raise ErrorResponse ("please provide metric token" )
6265
63- client = monitoring .Client (credentials = GoogleCredentials .get_application_default ())
66+ try :
67+ client = gcloud_monitoring .Client (credentials = GoogleCredentials .get_application_default ())
68+
69+ try :
70+ descriptor = client .fetch_metric_descriptor (name )
71+ if descriptor is None :
72+ _create_descriptor (name , client )
73+ except (ForbiddenException , NotFoundException ) as ignored :
74+ # print "forbidden"
75+ _create_descriptor (name , client )
76+
77+ metric = client .metric (name , {})
78+ resource = client .resource ('global' , labels = {})
79+ client .write_point (metric , resource , token )
80+ except Exception as e :
81+ logging .error (e )
82+ raise ErrorResponse ("error while writing custom metric: {0}" .format (e ))
83+ # finally:
84+ # if descriptor is not None:
85+ # descriptor.delete()
6486
87+ print 'OK'
88+
89+
90+ def _create_descriptor (name , client ):
91+ logging .info ("no descriptor found with name {0}: creating" .format (name ))
6592 descriptor = client .metric_descriptor (
66- 'custom.googleapis.com/{0}' . format ( name ) ,
93+ name ,
6794 metric_kind = MetricKind .GAUGE ,
6895 value_type = ValueType .DOUBLE ,
69- description = token
96+ description = "this is a test metric"
7097 )
7198 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'
99+ time .sleep (5 )
81100
82101
83102@app .route ('/exception' , methods = ['POST' ])
84- def exception ():
103+ def _exception ():
85104 print ''
86105
87106
88107@app .route ('/trace' , methods = ['POST' ])
89- def trace ():
108+ def _trace ():
90109 print ''
91110
92111
0 commit comments