31 lines
775 B
Python
31 lines
775 B
Python
import os
|
|
from time import time
|
|
|
|
import requests
|
|
|
|
from .console import logger
|
|
|
|
|
|
def send_to_loki(loki_url: str, line: str, backup: str):
|
|
ns = round(time() * 1_000_000_000)
|
|
|
|
payload = {
|
|
'streams': [
|
|
{
|
|
'stream': {'host': os.environ['HOSTNAME'], 'backup': backup},
|
|
'values': [[str(ns), line]],
|
|
}
|
|
]
|
|
}
|
|
try:
|
|
resp = requests.post(loki_url, json=payload, timeout=1)
|
|
except Exception as e:
|
|
logger.exception(e)
|
|
else:
|
|
if resp.status_code == 204:
|
|
logger.info(f'Sent line to loki at {loki_url}')
|
|
else:
|
|
logger.error(
|
|
f'Problem sending log line to Loki. Response status code {resp.status_code}: {resp.text}'
|
|
)
|