service_name and level; the log line itself is JSON, ready for LogQL’s | json parser.
Setup
- Have a Loki instance (e.g.
http://localhost:3100) or a Grafana Cloud stack (find the push URL and credentials under Connections → Loki). - Set the environment variables:
- Wire the transport:
- Query in Grafana:
{service_name="my-api"} | json.
Environment Variables
Options
Plus the shared batching options:
maxBatchSize, flushIntervalMs, timeout, retries — see the overview.
Keep labels low-cardinality (environment, region) — per-request values like request IDs belong in the log line, where LogQL can still filter them, not in labels, where they explode Loki’s index.
Payload
Each batch groups entries by level into streams:{service_name="my-api", level="ERROR"} | json | durationMs > 1000.
Troubleshooting
401— Grafana Cloud needs bothLOKI_USERNAME(instance ID) andLOKI_PASSWORD(token withlogs:write).400“entry too far behind” — Loki rejects out-of-order or too-old timestamps; check the server clock.429— per-tenant ingestion limits hit; raiseflushIntervalMs/maxBatchSizeto send fewer, larger batches, or raise the tenant limits.