forked from vmware-archive/kubeless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubeless.py
executable file
·74 lines (63 loc) · 2.2 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
#!/usr/bin/env python
import os
import imp
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'])
def funcWrap(q, req):
if req is None:
q.put(func())
else:
q.put(func(req))
@app.route('/', method=['GET', 'POST'])
def handler():
req = bottle.request
method = req.method
func_calls.labels(method).inc()
with func_errors.labels(method).count_exceptions():
with func_hist.labels(method).time():
q = Queue()
if method == 'GET':
p = Process(target=funcWrap, args=(q,None,))
else:
p = Process(target=funcWrap, args=(q,bottle.request,))
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:
return q.get()
@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)
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)