Video in progress

How much do tracing and metrics affect application performance, and how do you measure it?

Properly configured Micrometer and OpenTelemetry usually add a few percent of CPU and less than a few milliseconds of latency, but high cardinality, 100% sampling and synchronous export make it noticeable. You only know the exact number by measuring: compare the enabled and disabled states under the same load.

📖 Answer

Sources of overhead:

  • Span creation: every span is several objects, attributes, context switches. At 5000 requests a second with 30 spans each, that is noticeable allocation.
  • Export: BatchSpanProcessor exports asynchronously; SimpleSpanProcessor exports synchronously on every span and is not used in prod. If the batch queue fills up, spans are dropped (which is good, since the application doesn't wait).
  • Metrics: each Timer record is cheap, but timers with histograms and thousands of tag combinations increase memory and scrape time. Serializing 50 thousand series during a scrape causes a CPU spike.
  • Java agent: bytecode instrumentation makes startup a few seconds longer, sometimes noticeably.

How to measure:

  1. The same load test (k6, Gatling) with tracing off, head sampling at 10%, and 100%.
  2. Compare: p99 latency, CPU, allocation rate (jvm_gc_memory_allocated_bytes_total), startup time.
  3. In prod, enable it on a canary pod and compare with the other pods.
// you can also look at the cost of instrumentation separately in JMH
@Benchmark
public void withObservation(Blackhole bh) {
    Observation.createNotStarted("bench", registry).observe(() -> bh.consume(work()));
}

In practice, the numbers I got were usually around a few percent, but this depends heavily on the workload: for CPU-heavy, very short requests the share is larger. So I don't promise a number in an interview. I say it has to be measured.