Observability
LGTM stack (Grafana)
Logs, traces and metrics in Grafana, from one module.
LGTM is the Grafana open-source observability stack. Each letter is a tool, and each answers one question:
| Tool | Stores | Question | |
|---|---|---|---|
| L | Loki | logs | "what did the application say?" |
| G | Grafana | nothing: it draws | "show me everything in one place" |
| T | Tempo | traces | "where did this request spend its time?" |
| M | Mimir (or Prometheus) | metrics | "how is it doing over time?" |
The lgtm package connects your application to all four with one module, and generates the files to run the stack on your machine.
1. Add the module
Kest.run<AppModule> {
import(LgtmModule.forRoot {
serviceName = "shop-api"
environment = "production"
version = "1.4.2"
lokiUrl = "http://loki:3100" // logs are pushed here; blank = not shipped
tempoEndpoint = "http://tempo:4317" // spans are exported here (OTLP gRPC); blank = not exported
})
}
app.http(adapter) { useLgtm() } // /metrics for Prometheus, and a span for every requestThat is all. From now on:
- every request is a trace (continuing the caller's, if it sent a
traceparent); - every log line (
Logger("x").log(...)) goes to Loki with the trace id of the request that wrote it; GET /metricsserves the HTTP and JVM metrics for Prometheus to scrape.
| Option | |
|---|---|
serviceName, environment, version | how this service is labelled everywhere |
lokiUrl, lokiLabels | Loki's address; extra labels (keep them few: each distinct value is a new log stream) |
tempoEndpoint, sampleRatio | Tempo's OTLP address; the share of new traces kept |
otlpMetricsEndpoint, metricsPushInterval | push metrics over OTLP instead of being scraped (Mimir, Grafana Cloud, an OTel Collector) |
minLogLevel | the lowest level shipped (default LOG; use WARN to ship less) |
registry | which MetricsRegistry to expose (the shared default) |
2. Run the stack locally
LgtmStack.writeTo(File("observability"), service = "shop-api", appPort = 8080)docker compose -f observability/docker-compose.yml up -d
open http://localhost:3000 # Grafana: the "shop-api (Kest)" dashboard is already thereThis writes a docker-compose.yml (Prometheus, Loki, Tempo, Grafana), the Prometheus and Tempo configuration, the Grafana data sources (already linked so that a log line's trace_id opens the trace in Tempo, and a trace opens its logs), and a dashboard with request rate, 5xx ratio, p50/p95/p99 latency, requests by status, requests in flight, busiest routes, JVM memory, your logs and your recent traces.
Use the files as a starting point for development and testing. A production stack needs persistent storage, authentication and sizing: follow the Grafana documentation, or use Grafana Cloud (point the same options at its URLs, with its credentials in headers).
3. A walk-through: find out why a request was slow
- In the dashboard, the latency panel shows p99 jumped to 3 s at 14:05.
- Open the Recent traces panel and pick a slow trace: the waterfall shows the call to
paymentstook 2.9 s. - Click the span's Logs link: Grafana opens Loki filtered to this request's
trace_id, and you read "card gateway timeout, retrying".
Metrics told you that something was wrong, the trace where, and the logs why.
Just the pieces
You do not have to use the module. Each part works alone:
Logger.overrideLogger(LokiLogSink("http://loki:3100", labels = mapOf("service" to "shop-api", "env" to "prod"))) // logs to Loki
Logger.overrideLogger(OtlpLogSink("http://collector:4318", "shop-api")) // logs over OTLP
OtlpMetricsExporter(registry, "http://collector:4318", "shop-api").start() // metrics over OTLP
File("shop.json").writeText(GrafanaDashboard.json("shop-api")) // just the dashboardUse CompositeLogSink(console, loki) to send logs to several places.
Guarantees and limits
- Logging never slows a request. Lines are queued and sent in the background in batches. If Loki is down or slow, the oldest queued lines are dropped, never the request;
lokiSink.droppedandfailedcount what was lost. - Delivery is at most once. Observability data is worth having, not worth blocking for.
- Call
container.get(LgtmService::class).flush()before a short-lived job exits so the last lines are sent. Closing the container does it too. - Verified against real servers: logs through Loki's push API and its OTLP endpoint, metrics through Prometheus' OTLP receiver, a request's trace found in Tempo, and the generated dashboard accepted by a real Grafana (the tests run with
tools/test-infra.sh up obs). The generated docker-compose file itself was not started end to end.