Observability
OpenTelemetry
Traces, metrics and logs in the open standard every monitoring tool understands.
OpenTelemetry (OTel) is the industry standard for observability data. It defines three kinds of data and one wire format (OTLP) that Grafana, Datadog, New Relic, Honeycomb, Elastic and nearly every other tool accept, so you are not tied to a vendor.
| Signal | Answers | Kest |
|---|---|---|
| Traces | "where did the time go in this request, across services?" | Tracing, useTracing, @Traced (below) |
| Metrics | "how is the system doing?" | OtlpMetricsExporter, or Prometheus scraping (Metrics) |
| Logs | "what happened?" | OtlpLogSink |
Tracing
A trace is the story of one request. It is made of spans: one per step (the HTTP request, a database call, a call to another service), each with a start time, a duration and a parent. Drawn out, you see exactly which step was slow:
GET /orders/7 [=================== 212 ms ===================]
OrderService.load [=== 40 ms ===]
POST payments/charge (client span) [============ 150 ms ============]
POST /charge (server span in the payments service) [========= 138 ms =========]Kest.run<AppModule> {
import(OpenTelemetryModule.forRoot { serviceName = "orders"; otlpEndpoint = "http://collector:4317"; sampleRatio = 0.25 })
}
val tracing = app.get(Tracing::class)
app.http(adapter) { useTracing(tracing) }| Option | |
|---|---|
serviceName, serviceVersion | the resource attributes: how this service is named in the trace viewer |
otlpEndpoint | OTLP over gRPC, e.g. http://localhost:4317 (the collector, Tempo, Jaeger) |
exporter | any SpanExporter instead (console, an in-memory one for tests) |
immediate | export each span as it ends instead of in batches |
sampleRatio | the share of new traces kept; a request that arrives with a sampled context is always kept |
Sampling decides how many traces you keep. At 1.0 every request is traced (fine in development, costly under load). 0.05 keeps 5% of new traces. Because the decision is made once at the start and passed along, a trace is either complete across all services or absent, never half.
What you get
- A server span for each request, named like
GET /users/{id}, withhttp.request.method,url.path,http.route,http.response.status_code, and errors recorded. The/metricsand/healthpaths are not traced. - A
traceparentheader on the request continues the caller's trace; the response carriestraceparentback. - Outgoing calls become client spans and pass the context on:
HttpClientModule.register { interceptor(TracingClientInterceptor(tracing)) }@Traced("price-lookup") open fun lookup(...)wraps a method in a child span (anopenclass and method, see method decorators); an exception marks it failed.- By hand:
tracing.span("name") { ... },tracing.tracer, andtracing.currentTraceId()to put in log lines.
Sending metrics with OTLP
Normally Prometheus scrapes /metrics. Use OTLP when it cannot reach you (a short-lived job, a serverless function, a firewall) or when your backend takes OTLP (Grafana Cloud, Datadog, an OpenTelemetry Collector). OtlpMetricsExporter pushes the same registry:
// package: lgtm
val exporter = OtlpMetricsExporter(
registry = MetricsRegistry.default, endpoint = "http://collector:4318", // OTLP over HTTP, the exporter adds /v1/metrics
serviceName = "orders", environment = "production", version = "1.4.2",
interval = Duration.ofSeconds(15),
).start()Counters go out as cumulative sums, gauges as gauges and histograms as explicit-bucket histograms, so rates and percentiles computed by the backend match what a scrape would give. Prometheus itself accepts OTLP when started with --enable-feature=otlp-write-receiver: use the endpoint http://prometheus:9090/api/v1/otlp. exporter.close() pushes once more, which is what a short job wants before it exits. exporter.failures counts pushes that were refused.
Sending logs with OTLP
Logger.overrideLogger(OtlpLogSink("http://collector:4318", serviceName = "orders", environment = "production"))Every log line becomes an OTLP log record with its level, the logger name (Orders), exception details, and the trace and span id of the request that wrote it. In Grafana you click from a slow trace to the exact log lines of that request, and the other way round. Lines still go to the console. Sending happens in the background from a bounded queue, so logging never waits for the network; when the collector is down the oldest lines are dropped (sink.dropped).
Grafana Loki accepts OTLP logs at http://loki:3100/otlp.
Putting the trace id in your own logs
log.info("charging order ${order.id} (trace ${tracing.currentTraceId()})")Verified against real servers
The tracing was tested with an in-memory exporter, and against a real Grafana Tempo (a request's trace is found by searching for its service). The OTLP log sink was tested against a real Grafana Loki and the OTLP metrics exporter against a real Prometheus: the values written arrive with their labels. Not built: tracing of ORM queries and messaging transports, and baggage.