Skip to content

metrics: Expose a MetricsUtils.withSingleMetric overload that doesn't require metric name/value/unit #999

Closed
@humanzz

Description

@humanzz

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

  1. 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
  2. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions