es2loki is a migration library that helps to transfer logs from Elasticsearch to Grafana Loki
Project description
es2loki
es2loki is a migration library that helps to transfer logs from
Elasticsearch to Grafana Loki.
To use es2loki currently you need to define your own mapping of elasticsearch documents to labels for Grafana Loki.
Demo
You may find helpful a demo folder which contains a fully-sufficient demo stand
that demonstrates transferring logs using es2loki.
Usage
In the simplest form you don't need to write any Python code at all, Loki will receive no meaningful labels, but nevertheless - let's see how it works.
$ pip install -U es2loki
$ ELASTIC_HOSTS=http://localhost:9200 \
ELASTIC_INDEX="filebeat-*" \
LOKI_URL=http://localhost:3100 \
python -m es2loki
In order to override default es2loki behaviour you need to subclass
a es2loki.BaseTransfer class.
To declare how documents map to Loki labels you have to override a
extract_doc_labels method (see demo/example.py):
from es2loki import BaseTransfer
class TransferLogs(BaseTransfer):
def extract_doc_labels(self, source: dict) -> Optional[MutableMapping[str, str]]:
return dict(
app=source.get("fields", {}).get("service_name"),
job="logs",
level=source.get("level"),
node_name=source.get("host", {}).get("name"),
logger_name=source.get("logger_name"),
)
You can run this using the following code:
import sys
from es2loki import run_transfer
if __name__ == "__main__":
sys.exit(run_transfer(TransferLogs()))
You can find more examples in the demo folder.
Sorting
By default es2loki assumes that in the documents returned from Elasticsearch
there are fields @timestamp (you can change the name - see below) and log.offset.
Using these 2 fields we can be sure that we will not reread the same lines multiple times.
But if you have your fields that could guarantee such a behaviour - please
override a make_es_sort and make_es_search_after methods.
make_es_sortdefines by which fields the sorting will happen.make_es_search_afterdefines an initial "offset". It is needed to resume es2loki after a shutdown. By default it extracts information from the internal state, which can be saved persistently.
Persistence
es2loki has a mechanism to store the Elasticsearch scrolling state
in the database (highly recommended). In this mode es2loki saves
the scrolling state inside an SQL database (PostgreSQL, MySQL, SQLite, ...).
You can opt out of enabling persistence completely using STATE_MODE=none env variable, which is the default.
But we highly recommend to enable persistence with some SQL storage.
Deployment
You can deploy es2loki via our helm chart.
Add kts repo:
helm repo add kts https://charts.kts.studio
helm repo update
Install the chart:
helm upgrade --install RELEASE_NAME kts/es2loki
More information about helm chart deployment can be found here.
Configuration
You can configure es2loki using the following environment variables:
| name | default | description |
|---|---|---|
| ELASTIC_HOSTS | http://localhost:9200 | Elasticsearch hosts. Separate multiple hosts using , |
| ELASTIC_USER | "" | Elasticsearch username |
| ELASTIC_PASSWORD | "" | Elasticsearch password |
| ELASTIC_INDEX | "" | Elasticsearch index pattern to search documents in |
| ELASTIC_BATCH_SIZE | 3000 | How much documents to extract from ES in one batch |
| ELASTIC_TIMEOUT | 120 | Elasticsearch search query timeout |
| ELASTIC_MAX_DATE | Upper date limit (format is the same as @timestamp field) | |
| ELASTIC_TIMESTAMP_FIELD | @timestamp | Name of timesteamp field in Elasticsearch |
| LOKI_URL | http://localhost:3100 | Loki instance URL |
| LOKI_USERNAME | "" | Loki username |
| LOKI_PASSWORD | "" | Loki password |
| LOKI_TENANT_ID | "" | Loki Tenant ID (Org ID) |
| LOKI_BATCH_SIZE | 1048576 | Maximum batch size (in bytes) |
| LOKI_POOL_LOAD_FACTOR | 10 | Maximum number of push non-waiting requests |
| LOKI_PUSH_MODE | pb | pb - protobuf + snappy, gzip - json + gzip, json - just json |
| LOKI_WAIT_TIMEOUT | 0 | How much time (in seconds) to wait after a Loki push request |
| STATE_MODE | none | Configures es2loki persistence (db is recommended). Use none to disable persistence completely |
| STATE_START_OVER | Clean up persisted data and start over | |
| STATE_DB_URL | postgres://127.0.0.1:5432/postgres | Database URL for db persistence |
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
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 es2loki-0.1.6.tar.gz.
File metadata
- Download URL: es2loki-0.1.6.tar.gz
- Upload date:
- Size: 25.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e61835f3bda08373b069fa8de41b2ed107ccb018f1639b5ff24bf3e9f30eeda5
|
|
| MD5 |
8b2300d53cfaaec5751ed250dc79fc06
|
|
| BLAKE2b-256 |
bc02d002d2e599cf0805fc5b916bd1f91ad72ad9c4f8818a852099bc4390598c
|
File details
Details for the file es2loki-0.1.6-py3-none-any.whl.
File metadata
- Download URL: es2loki-0.1.6-py3-none-any.whl
- Upload date:
- Size: 32.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.16
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ba8d392f7d8843cfbc9403085d61e9766594f80777ecd02f7fff077dd742d11
|
|
| MD5 |
b35dac2dd09e0505b34af7e3ee41b466
|
|
| BLAKE2b-256 |
ba61854b66eff958efde3536f122e9eaf8fa6de17275d5f48982901c07584944
|