Description
Is your feature request related to a problem? Please describe.
MetricsUtils.withSingleMetric
- MetricsUtils.java - provides a nice utility to hide the creation, configuration and publishing of metrics. The existing overloads however, assume that the user needs to know the name/value/unit of at least the first metric to add. This is not always true. I'd like to provide a Consumer<MetricLogger>
that will add metrics based on some conditional logic.
The main use case my team has
- We're emitting a number of metrics - ~10 metrics - that all have the same config (e.g. dimensions) that's meant to be different from MetricsUtils.metricsLogger() i.e. we need a new metrics logger instance
- The names of those metrics are generated dynamically - think loops, appending different constants - to generate those names
So, ideally, we create a new instance of a metrics logger, and then rely on to emit those 10 different metrics.
Describe the solution you'd like
Introduce a new method, an overload of MetricsUtils.withSingleMetric
or even a new name MetricsUtils.withMetric
as follows
public static void withMetric(Consumer<MetricsLogger> logger) {
MetricsLogger metricsLogger = logger();
try {
metricsLogger.setNamespace(defaultNameSpace());
captureRequestAndTraceId(metricsLogger);
logger.accept(metricsLogger);
} finally {
metricsLogger.flush();
}
}
which would allow the user to freely configure and add metrics via their provided Consumer<MetricsLogger>
Describe alternatives you've considered
Additional context