Created
September 19, 2019 09:31
-
-
Save e-kostylov/4e6ad4e4ded39882dae2b9ce7cf053fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example of usage grafana/loki api when you need push any log/message from your python scipt | |
import requests | |
import json | |
import datetime | |
import pytz | |
host = 'myhost' | |
curr_datetime = datetime.datetime.now(pytz.timezone('Asia/Yekaterinburg')) | |
curr_datetime = curr_datetime.isoformat('T') | |
msg = 'On server {host} detected error'.format(host=host) | |
# push msg log into grafana-loki | |
url = 'http://host-where-loki-run:3100/api/prom/push' | |
headers = { | |
'Content-type': 'application/json' | |
} | |
payload = { | |
'streams': [ | |
{ | |
'labels': '{source=\"Name-of-your-source\",job=\"name-of-your-job\", host=\"' + host + '\"}', | |
'entries': [ | |
{ | |
'ts': curr_datetime, | |
'line': '[WARN] ' + msg | |
} | |
] | |
} | |
] | |
} | |
payload = json.dumps(payload) | |
answer = requests.post(url, data=payload, headers=headers) | |
print(answer) | |
response = answer | |
print(response) | |
# end pushing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment