Manually pushing to Loki

There’s a very under-documented sharp edge in Loki: you need to give it timestamps as a string, and they need to be a nanosecond epoch.

Minimal example:

#!/usr/bin/env python3

from json     import dumps
from requests import post
from time     import time

loki_url = 'http://localhost:3100/loki/api/v1/push'

def loki_timestamp():
  return f"{(int(time() * 1_000_000_000))}"

payload = {
  'streams': [
    {
      'stream': { 'label1': 'test' },
      'values': [
          [ loki_timestamp(), 'hello world blah blah'],
          [ loki_timestamp(), 'hello world blah blah 2'] 
      ]
    }
  ]
}

headers = {'Content-Type': 'application/json'}
response = post(loki_url, json=payload, headers=headers)
print(response.status_code)     # 204 is success