Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
humanzz opened this issue Nov 26, 2022 · 1 comment · Fixed by #1000

Comments

@humanzz
Copy link
Contributor

humanzz commented Nov 26, 2022

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

@humanzz
Copy link
Contributor Author

humanzz commented Jan 1, 2023

Hello powertools team, any chance now that reinvent and holidays are almost over to check the PR for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants