-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (44 loc) · 1.3 KB
/
main.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
from flask import Flask, request
import requests
import datetime
import os
host = os.getenv("LOKI_HOST","loki")
port = os.getenv("LOKI_PORT","3100")
url = f"http://{host}:{port}/api/prom/push"
app = Flask(__name__)
def is_label(key,value):
# TODO: choose labels carefully
if key in ["message","@message","extra_data"]:
return False
if isinstance(value,str) and len(value) > 50:
return False
return True
def make_labels(msgs):
labels=[]
for msg in msgs:
for key in msg.keys():
value = msg.get(key)
if is_label(key,value):
label=f"{key}=\"{value}\""
labels.append(label)
return "{"+",".join(labels)+"}"
@app.route("/push",methods=["POST"])
def push():
logs = request.get_json()
headers = { 'Content-type': 'application/json' }
# TODO: add timezone
curr_datetime = datetime.datetime.now().isoformat('T',timespec='microseconds')
entries=[]
for log in logs:
entry= { 'ts': curr_datetime+'+00:00', 'line': log.get("message")}
entries.append(entry)
payload = {
'streams': [
{
'labels': make_labels(logs),
'entries': entries
}
]
}
requests.post(url, json=payload, headers=headers)
return "ok",200