-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkubeless.py
executable file
·98 lines (84 loc) · 2.87 KB
/
kubeless.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
import os
import imp
import datetime
from multiprocessing import Process, Queue
import bottle
import prometheus_client as prom
mod = imp.load_source('function',
'/kubeless/%s.py' % os.getenv('MOD_NAME'))
func = getattr(mod, os.getenv('FUNC_HANDLER'))
func_port = os.getenv('FUNC_PORT', 8080)
timeout = float(os.getenv('FUNC_TIMEOUT', 180))
app = application = bottle.app()
func_hist = prom.Histogram('function_duration_seconds',
'Duration of user function in seconds',
['method'])
func_calls = prom.Counter('function_calls_total',
'Number of calls to user function',
['method'])
func_errors = prom.Counter('function_failures_total',
'Number of exceptions in user function',
['method'])
function_context = {
'function-name': func,
'timeout': timeout,
'runtime': os.getenv('FUNC_RUNTIME'),
'memory-limit': os.getenv('FUNC_MEMORY_LIMIT'),
}
def funcWrap(q, event, c):
try:
q.put(func(event, c))
except Exception as inst:
q.put(inst)
@app.get('/healthz')
def healthz():
return 'OK'
@app.get('/metrics')
def metrics():
bottle.response.content_type = prom.CONTENT_TYPE_LATEST
return prom.generate_latest(prom.REGISTRY)
@app.route('/<:re:.*>', method=['GET', 'POST', 'PATCH', 'DELETE'])
def handler():
req = bottle.request
content_type = req.get_header('content-type')
data = req.body.read()
if content_type == 'application/json':
data = req.json
event = {
'data': data,
'event-id': req.get_header('event-id'),
'event-type': req.get_header('event-type'),
'event-time': req.get_header('event-time'),
'event-namespace': req.get_header('event-namespace'),
'extensions': {
'request': req
}
}
method = req.method
func_calls.labels(method).inc()
with func_errors.labels(method).count_exceptions():
with func_hist.labels(method).time():
q = Queue()
p = Process(target=funcWrap, args=(q, event, function_context))
p.start()
p.join(timeout)
# If thread is still active
if p.is_alive():
p.terminate()
p.join()
return bottle.HTTPError(408, "Timeout while processing the function")
else:
res = q.get()
if isinstance(res, Exception):
raise res
return res
if __name__ == '__main__':
import logging
import sys
import requestlogger
loggedapp = requestlogger.WSGILogger(
app,
[logging.StreamHandler(stream=sys.stdout)],
requestlogger.ApacheFormatter())
bottle.run(loggedapp, server='cherrypy', host='0.0.0.0', port=func_port)