Skip to content

Instantly share code, notes, and snippets.

@e-kostylov
Created September 19, 2019 09:31
Show Gist options
  • Save e-kostylov/4e6ad4e4ded39882dae2b9ce7cf053fd to your computer and use it in GitHub Desktop.
Save e-kostylov/4e6ad4e4ded39882dae2b9ce7cf053fd to your computer and use it in GitHub Desktop.
# 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