33 lines
928 B
Python
33 lines
928 B
Python
|
|
import httpx
|
||
|
|
from config import LokiConfig
|
||
|
|
|
||
|
|
|
||
|
|
class LokiClient:
|
||
|
|
"""Talks to Loki's HTTP API to fetch logs."""
|
||
|
|
|
||
|
|
def __init__(self, config: LokiConfig):
|
||
|
|
# Store the config so we can use it later
|
||
|
|
self.config = config
|
||
|
|
|
||
|
|
# Create an HTTP client
|
||
|
|
# already knows Loki address and wait time
|
||
|
|
self.client = httpx.AsyncClient(
|
||
|
|
base_url=config.url,
|
||
|
|
timeout=config.timeout
|
||
|
|
)
|
||
|
|
|
||
|
|
async def query_range(self, query: str, start: str, end: str, limit: int = 100):
|
||
|
|
# Makes GET request to Loki's query endpoint with search parameters
|
||
|
|
response = await self.client.get(
|
||
|
|
"/loki/api/v1/query_range",
|
||
|
|
params={
|
||
|
|
"query": query,
|
||
|
|
"start": start,
|
||
|
|
"end": end,
|
||
|
|
"limit": limit
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
# Returns response into Python Dict
|
||
|
|
return response.json()
|