Python library for querying Grafana Loki logs via REST API
Project description
loki-reader-core
A lightweight Python library for querying Grafana Loki logs via REST API.
Features
- Simple, intuitive client for Loki's HTTP API
- Supports
query,query_range, label discovery, and series matching - SSL/TLS support including custom CA certificates for self-signed certs
- Multi-tenant support via
X-Scope-OrgIDheader - Basic authentication
- Clean dataclass models with
to_dict()/from_dict()serialization - All timestamps as Unix nanoseconds (Loki's native format)
Installation
From PyPI:
pip install loki-reader-core
From GitHub:
pip install loki-reader-core @ git+ssh://git@github.com/jmazzahacks/loki-reader-core.git@main
Usage
Basic Query
from loki_reader_core import LokiClient
from loki_reader_core.utils import hours_ago_ns, now_ns
client = LokiClient(base_url="https://loki.example.com")
result = client.query_range(
logql='{job="api-server"} |= "error"',
start=hours_ago_ns(1),
end=now_ns(),
limit=500
)
for stream in result.streams:
print(f"Labels: {stream.labels}")
for entry in stream.entries:
print(f" [{entry.timestamp}] {entry.message}")
With Authentication and Self-Signed Certificates
client = LokiClient(
base_url="https://loki.internal.company.com:8443",
auth=("username", "password"),
ca_cert="/path/to/ca.pem"
)
Multi-Tenant Setup
client = LokiClient(
base_url="https://loki.example.com",
org_id="tenant-1"
)
Exploring Labels
labels = client.get_labels()
values = client.get_label_values("application")
series = client.get_series(match=['{application="my-app"}'])
Context Manager
with LokiClient(base_url="https://loki.example.com") as client:
result = client.query(logql='{job="api"}')
API Reference
LokiClient
| Parameter | Type | Default | Description |
|---|---|---|---|
base_url |
str |
required | Loki server URL |
auth |
tuple[str, str] |
None |
Basic auth (username, password) |
org_id |
str |
None |
X-Scope-OrgID for multi-tenant |
ca_cert |
str |
None |
Path to CA certificate PEM file |
verify_ssl |
bool |
True |
Set False to disable SSL verification |
timeout |
int |
30 |
Request timeout in seconds |
Methods
| Method | Description |
|---|---|
query(logql, time, limit) |
Instant query at a single point in time |
query_range(logql, start, end, limit, direction) |
Query across a time range |
get_labels(start, end) |
List available label names |
get_label_values(label, start, end) |
List values for a specific label |
get_series(match, start, end) |
List streams matching selectors |
Timestamp Utilities
from loki_reader_core.utils import (
now_ns, # Current time as nanoseconds
seconds_to_ns, # Convert Unix seconds to nanoseconds
ns_to_seconds, # Convert nanoseconds to Unix seconds
minutes_ago_ns, # Timestamp N minutes ago
hours_ago_ns, # Timestamp N hours ago
days_ago_ns, # Timestamp N days ago
)
Development
Setup
# Create virtual environment
python -m venv .
# Activate virtual environment
source bin/activate
# Install dependencies
pip install -r dev-requirements.txt
pip install -e .
Running Tests
source bin/activate
pytest tests/ -v
License
MIT
Author
Jason Byteforge (@jmazzahacks)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
loki_reader_core-0.2.1.tar.gz
(17.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loki_reader_core-0.2.1.tar.gz.
File metadata
- Download URL: loki_reader_core-0.2.1.tar.gz
- Upload date:
- Size: 17.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d323f9f7a3a422ad33418c2e9ee1d2c3a435d5b2984f5c6d371f3ab5d96a1b54
|
|
| MD5 |
2171740244c772cd5ed0f612831510f8
|
|
| BLAKE2b-256 |
8f2be94c205816cabc948ceb732e0bd3994b9ae60c56986dc0e1ce8799e90c3f
|
File details
Details for the file loki_reader_core-0.2.1-py3-none-any.whl.
File metadata
- Download URL: loki_reader_core-0.2.1-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7073ee4ba84db2b928ba3a3c1cd4df779cf423cbea52a5bb9b0a30527b2ffcc2
|
|
| MD5 |
38f53004a73c74d29771eac883cc9700
|
|
| BLAKE2b-256 |
beec0cbce8afb2277459d06e2d25c34e27b7dda24e2bb8797008447897f3a5a9
|