Observability
SDK
Instrument your own code: logs, metrics, traces and errors, side by side.
Kest does not ship a vendor SDK of its own. It gives you four small APIs that map onto open standards, so there is nothing to lock you in. Here they are together, in one realistic service:
@Injectable
class CheckoutService(
private val orders: OrderRepository,
private val gateway: PaymentGateway,
private val metrics: MetricsRegistry,
private val tracing: Tracing,
private val sentry: SentryReporter,
) {
private val log = Logger("Checkout")
private val paid = metrics.counter("checkout_paid_total", "Orders paid", "method")
private val failed = metrics.counter("checkout_failed_total", "Payments that failed", "reason")
private val latency = metrics.histogram("checkout_payment_seconds", "Time to charge a card")
fun pay(order: Order, user: User): Receipt = tracing.span("checkout.pay") { // a TRACE span
log.log("charging order ${order.id} (trace ${tracing.currentTraceId()})") // a LOG line, with the trace id
try {
val receipt = latency.time { gateway.charge(order) } // a METRIC: how long it took
paid.inc(order.method) // a METRIC: one more paid order
receipt
} catch (e: CardDeclined) {
failed.inc("declined") // expected: count it, do not alert on it
throw e
} catch (e: Exception) {
failed.inc("error")
sentry.capture(e, tags = mapOf("method" to order.method), extra = mapOf("orderId" to order.id), user = mapOf("id" to user.id)) // an ERROR report
throw e
}
}
}| You want to | Use | Details |
|---|---|---|
| write a line about what happened | Logger("Name").log / warn / error | Logging; ship them with Loki or OTLP |
| count things, time things, measure levels | MetricsRegistry.counter / gauge / histogram, @Timed, @Counted | Metrics |
| see where time goes in a request | tracing.span("name") { }, @Traced | Manual instrumentation |
| be told about a failure | sentry.capture(e, tags, extra, user); unhandled errors are automatic | Error monitoring |
| send errors somewhere else | ErrorReporter (HTTP) and ErrorHooks.add (everything else) | Error monitoring |
Guidelines that keep instrumentation useful
- Instrument decisions and boundaries, not every line: calls to other systems, payments, retries, fallbacks.
- One request id, everywhere. The trace id in the log line is what links a log to a trace. Put it in your log messages, or ship logs with the
lgtmsinks which add it for you. - Do not log secrets or personal data (passwords, card numbers, tokens). Logs live longer than you think and are read by many people.
- Expected failures are metrics, unexpected ones are errors. A declined card is a normal outcome: count it. A timeout from the gateway is a bug or an outage: report it.
- Keep it cheap. A counter costs nanoseconds, a span a little more. All of it is queued and sent in the background, so it never slows a response.
- Test it. Metrics are plain objects: assert
paid.value("card") == 1.0. Traces can use an in-memory exporter (OpenTelemetry).