Video in progress
What is `DistributionSummary` in Micrometer, how does it differ from `Timer`, and when do you need it?
DistributionSummary measures the distribution of values that are not durations: response size, number of items in a batch, payment amount. It also gives you count, sum, max and, if you want, a histogram. The only difference is that the unit is the baseUnit you give it, not seconds.
📖 Answer
Timer is only for durations: it takes nanoseconds and exports seconds to Prometheus. For questions like "how many messages came in each Kafka poll" or "how many bytes is the export file", you need DistributionSummary.
DistributionSummary batchSize = DistributionSummary.builder("outbox.batch.size")
.description("Bitta poll'da yuborilgan xabarlar soni")
.baseUnit("messages")
.serviceLevelObjectives(10, 100, 500) // explicit bucket boundaries
.register(registry);
batchSize.record(events.size());
In Prometheus you get outbox_batch_size_messages_count, _sum, _max and the buckets. Average batch = rate(_sum) / rate(_count).
When it helps:
- if the batch size is always close to 1, batching is not working and you are making a lot of pointless network calls;
- if response size suddenly grows, someone has started calling an endpoint without pagination;
- a distribution by amount shows anomalies such as fraud or a wrong currency conversion.
scale(...) multiplies the value before it is recorded (for example, from tiyin to som). For bytes, if you use baseUnit(BaseUnits.BYTES), the naming convention adds the right unit.
Interviewers often catch people on this: using Counter.increment(amount) for a value. A counter gives you the total, but not the distribution, so it can't answer "what is typical" or "what is the biggest".
Sign up to mark studied questions and save your progress.