Skip to content

Commit 7ee56fd

Browse files
committed
adding python integration sample app
1 parent 771eff2 commit 7ee56fd

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

build.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
export IMAGE_NAME=$1
6+
7+
if [ -z "$1" ]; then
8+
echo "Usage: ./build.sh [image_path]"
9+
echo "Please provide fully qualified path to target image."
10+
exit 1
11+
fi
12+
13+
envsubst < cloudbuild.yaml.in > cloudbuild.yaml
14+
gcloud alpha container builds create . --config=cloudbuild.yaml

tests/integration/Dockerfile.in

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM ${STAGING_IMAGE}
2+
3+
COPY . /app
4+
WORKDIR /app
5+
6+
RUN pip install -r requirements.txt
7+
8+
ENV GOOGLE_APPLICATION_CREDENTIALS=/app/auth.json
9+
10+
ENTRYPOINT ["python"]
11+
CMD ["server.py"]

tests/integration/requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Flask
2+
google-cloud
3+
google-cloud-logging
4+
google-cloud-monitoring
5+
requests

tests/integration/server.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)