When implementing metrics and telemetry in your application, avoid using dynamic values like path parameters or user IDs directly as metric tags or dimensions. This practice causes cardinality explosion, which leads to excessive memory usage, degraded performance, and can overwhelm your monitoring systems.
When implementing metrics and telemetry in your application, avoid using dynamic values like path parameters or user IDs directly as metric tags or dimensions. This practice causes cardinality explosion, which leads to excessive memory usage, degraded performance, and can overwhelm your monitoring systems.
Instead:
/client-endpoint-with-path-param/{name}
as a metric tag/client-endpoint-with-path-param/123
as a metric tagExample of proper implementation:
// WRONG: Will cause cardinality explosion
@Path("template/path/{value}")
public class PathTemplateResource {
@GET
public String get(@PathParam("value") String value) {
// Don't do this - adds a high-cardinality tag
ContextLocals.put("metric-tag", value);
// ...
}
}
// CORRECT: Uses route templates instead
@Path("template/path/{value}")
public class PathTemplateResource {
@GET
public String get(@PathParam("value") String value) {
// For tracing/debugging, put in span attributes instead
Span.current().setAttribute("path.value", value);
// ...
}
}
Remember that for each unique tag value, you multiply the number of time series across all other dimensions (methods, status codes, endpoints), which can quickly overwhelm your metrics system.
Enter the URL of a public GitHub repository