Skip to content

Instantly share code, notes, and snippets.

@tuck1s
Created November 5, 2023 17:28
Show Gist options
  • Save tuck1s/6a17e6a5c86a2c58d518ee450a277878 to your computer and use it in GitHub Desktop.
Save tuck1s/6a17e6a5c86a2c58d518ee450a277878 to your computer and use it in GitHub Desktop.
example of grafana/loki api when you need push any log/message from your python script
#!/usr/bin/env python3
# example of grafana/loki api when you need push any log/message from your python script
import requests, time
import names # get some random data
nano_ts = int(time.time()*1e9)
name = names.get_full_name()
# push msg log into grafana-loki
url="http://localhost:3100/loki/api/v1/push"
payload = {
"streams": [
{
"stream": {
"foo": "bar2"
},
"values": [
[
str(nano_ts),
name,
]
]
}
]
}
answer = requests.post(url, json=payload) # automatically sets Content-Type
print(answer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment