Video in progress

Strong Middle 🧩 Microservices

Why is it dangerous to add `userId` or the full URL as a tag on Micrometer metrics?

Every unique tag value creates a separate time series. A userId tag with a million users means a million series, and both Prometheus memory and the service's own heap fill up. Tags must come from a small, bounded set: method, status, URI template.

📖 Answer

On one project this code went to prod:

meterRegistry.counter("orders.created", "customer", customerId.toString()).increment();

Within a week the series count in Prometheus went from 30 thousand to 4 million, queries got slow, and then Prometheus died with an OOM. In the service itself, every Counter object lives forever inside the MeterRegistry, so the heap kept growing too.

The rule: the number of tag values should be known in advance and small (dozens, hundreds at most). Good tags: method, status, outcome, uri (template), region. Bad tags: user ID, order ID, email, full URL, error message, traceId.

There is a trap with URLs. Spring MVC writes the template /orders/{id} into the uri tag, not /orders/812. But if you build the URI as a string in RestClient, the real path ends up in the tag:

// bad: every ID creates a new uri tag
restClient.get().uri("/customers/" + id).retrieve()...
// good: the template is preserved
restClient.get().uri("/customers/{id}", id).retrieve()...

As a safety net you can add a MeterFilter:

@Bean
MeterFilter limitUriTags() {
    return MeterFilter.maximumAllowableTags("http.client.requests", "uri", 100,
            MeterFilter.deny());
}

Boot also has ready-made settings for this: management.metrics.web.client.max-uri-tags and management.metrics.web.server.max-uri-tags (default 100).

If you need data about one specific user, that is a job for logs or traces, not metrics. High cardinality is not a problem there.