'How to query logcli in python and store it in a python variable (preferably in json format)
I want to query a logcli query in python and store the output of the logcli query in a python variable. The logcli query is
logcli query '{app="events-collector"}'
How can I do this? Moreover, can I preferably store it in some kind of array or json file?
Solution 1:[1]
You could use Loki's HTTP API, or you could treat logcli just as any other system command and use one of python's utilities to execute commands. subprocess.Popen worked for me. Here is a script that works on my Mac:
import subprocess
import json
import pprint
logcli_command = "logcli --addr=https://your-loki-url.com --org-id=undefined -o raw query {app=\"events-collector\"} --limit=10"
logcli_command_as_list_of_args = logcli_command.split()
process = subprocess.Popen(logcli_command_as_list_of_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
list_of_json_records = [json.loads(line) for line in process.stdout.readlines()]
print(f"{len(list_of_json_records)} records returned.")
pprint.pprint(list_of_json_records[0])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Boyko |