2017-08-03 01:47:50 +03:00
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
from flask import Flask, request
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
logging.getLogger('werkzeug').setLevel(logging.ERROR)
|
|
|
|
|
|
|
|
|
|
|
|
def log(fields):
|
2017-08-05 00:25:19 +03:00
|
|
|
line = json.dumps({'Timestamp': int(time.time() * 1e9), 'Fields': fields})
|
|
|
|
if len(line) > 1024e3:
|
|
|
|
raise ValueError('body too long')
|
|
|
|
print(line)
|
2017-08-03 01:47:50 +03:00
|
|
|
|
|
|
|
|
|
|
|
def flatten(fields):
|
|
|
|
result = {}
|
|
|
|
if type(fields) is dict:
|
|
|
|
for key, value in fields.items():
|
|
|
|
if type(value) in [list, dict]:
|
|
|
|
result.update({(key + '.' + k): v for k, v in flatten(value).items()})
|
|
|
|
else:
|
|
|
|
result[key] = value
|
|
|
|
elif type(fields) is list:
|
|
|
|
for index, value in enumerate(fields):
|
|
|
|
if type(value) in [list, dict]:
|
|
|
|
result.update({(str(index) + '.' + k): v for k, v in flatten(value).items()})
|
|
|
|
else:
|
|
|
|
result[str(index)] = value
|
|
|
|
else:
|
2017-08-05 00:25:19 +03:00
|
|
|
raise TypeError('could not flatten fields')
|
2017-08-03 01:47:50 +03:00
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def normalize(fields):
|
|
|
|
if type(fields) is not dict:
|
2017-08-05 00:25:19 +03:00
|
|
|
raise TypeError('fields is not a dict')
|
2017-08-03 01:47:50 +03:00
|
|
|
return flatten(fields)
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/', defaults={'path': ''}, methods=['GET', 'POST'])
|
|
|
|
@app.route('/<path:path>', methods=['GET', 'POST'])
|
|
|
|
def catchall(path):
|
2017-08-03 02:19:33 +03:00
|
|
|
meta = {
|
|
|
|
'path': path,
|
2017-08-05 00:25:19 +03:00
|
|
|
'agent': request.headers.get('User-Agent'),
|
|
|
|
'remoteAddressChain': request.headers.get('X-Forwarded-For'),
|
|
|
|
'method': request.method,
|
2017-08-03 02:19:33 +03:00
|
|
|
}
|
2017-08-03 01:47:50 +03:00
|
|
|
data = request.get_json(force=True)
|
2017-08-03 02:19:33 +03:00
|
|
|
try:
|
|
|
|
if type(data) is list:
|
2017-08-03 01:47:50 +03:00
|
|
|
logs = [normalize(item) for item in data]
|
|
|
|
for item in logs:
|
2017-08-03 02:19:33 +03:00
|
|
|
log({**item, **meta})
|
|
|
|
# elif dict type bulk api call:
|
|
|
|
# logs = [normalize(item) for item in data['bulk']]
|
|
|
|
# for item in logs:
|
|
|
|
# log({**item, **meta})
|
|
|
|
elif type(data) is dict:
|
|
|
|
log(normalize({**data, **meta}))
|
|
|
|
else:
|
2017-08-03 01:47:50 +03:00
|
|
|
return '', 400
|
2017-08-03 02:19:33 +03:00
|
|
|
return '', 204
|
2017-08-05 00:25:19 +03:00
|
|
|
except (TypeError, ValueError):
|
2017-08-03 01:47:50 +03:00
|
|
|
return '', 400
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/__lbheartbeat__')
|
|
|
|
def lbheartbeat():
|
|
|
|
return '', 200
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/__heartbeat__')
|
|
|
|
def heartbeat():
|
|
|
|
return 'OK\n', 200
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open('version.json') as o:
|
|
|
|
version_json = o.read()
|
|
|
|
except FileNotFoundError:
|
|
|
|
version_json = '{"version":"notfound"}'
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/__version__')
|
|
|
|
def version():
|
|
|
|
return version_json, 200
|