Kest

Search

Search the documentation

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:

ToolStoresQuestion
LLokilogs"what did the application say?"
GGrafananothing: it draws"show me everything in one place"
TTempotraces"where did this request spend its time?"
MMimir (or Prometheus)metrics"how is it doing over time?"
Loading diagram…

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 request

That 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 /metrics serves the HTTP and JVM metrics for Prometheus to scrape.
Option
serviceName, environment, versionhow this service is labelled everywhere
lokiUrl, lokiLabelsLoki's address; extra labels (keep them few: each distinct value is a new log stream)
tempoEndpoint, sampleRatioTempo's OTLP address; the share of new traces kept
otlpMetricsEndpoint, metricsPushIntervalpush metrics over OTLP instead of being scraped (Mimir, Grafana Cloud, an OTel Collector)
minLogLevelthe lowest level shipped (default LOG; use WARN to ship less)
registrywhich 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 there

This 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

  1. In the dashboard, the latency panel shows p99 jumped to 3 s at 14:05.
  2. Open the Recent traces panel and pick a slow trace: the waterfall shows the call to payments took 2.9 s.
  3. 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 dashboard

Use 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.dropped and failed count 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.

Read more