Skip to content
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

Add ability to filter metrics in DropwizardExports #573

Closed
vgv opened this issue Oct 7, 2020 · 1 comment
Closed

Add ability to filter metrics in DropwizardExports #573

vgv opened this issue Oct 7, 2020 · 1 comment

Comments

@vgv
Copy link
Contributor

vgv commented Oct 7, 2020

We are migrating from dropwizard metrics to prometheus right now. We exported all old dropwizard metrics to prometheus and added all metrics to prometheus one by one. However, there is a problem – metric is added to prometheus is exported via DropwizardExports too, so, we see two similar metrics in output.

It will be great if we can filter metrics are exported to Dropwizard by standard MetricFilter mechanism.

vgv added a commit to vgv/client_java that referenced this issue Oct 7, 2020
vgv added a commit to vgv/client_java that referenced this issue Oct 7, 2020
vgv added a commit to vgv/client_java that referenced this issue Oct 8, 2020
Code review fixes

Fix prometheus#573

Signed-off-by: Vasily Vasilkov <[email protected]>
vgv added a commit to vgv/client_java that referenced this issue Oct 8, 2020
Code review fixes, split wildcard import into concrete classes

Fix prometheus#573

Signed-off-by: Vasily Vasilkov <[email protected]>
vgv added a commit to vgv/client_java that referenced this issue Oct 13, 2020
Code review fixes

Fix prometheus#573

Signed-off-by: Vasily Vasilkov <[email protected]>
vgv added a commit to vgv/client_java that referenced this issue Oct 14, 2020
Code review fixes

Fix prometheus#573

Signed-off-by: Vasily Vasilkov <[email protected]>
@vladimir-bukhtoyarov
Copy link

vladimir-bukhtoyarov commented Nov 13, 2020

If I understand correctly this fix has not been released. So, bellow is a powerful workaround, that can be used now:

public static MetricRegistry decorateMetricRegistryByFilter(MetricRegistry sourceRegistry, MetricFilter filter) {
        MetricRegistry decoratedRegistry = new MetricRegistry();

        MetricRegistryListener listener = new MetricRegistryListener() {
            @Override
            public void onGaugeAdded(String name, Gauge<?> gauge) {
                if (filter.matches(name, gauge)) {
                    decoratedRegistry.register(name, gauge);
                }
            }

            @Override
            public void onGaugeRemoved(String name) {
                decoratedRegistry.remove(name);
            }

            @Override
            public void onCounterAdded(String name, com.codahale.metrics.Counter counter) {
                if (filter.matches(name, counter)) {
                    decoratedRegistry.register(name, counter);
                }
            }

            @Override
            public void onCounterRemoved(String name) {
                decoratedRegistry.remove(name);
            }

            @Override
            public void onHistogramAdded(String name, Histogram histogram) {
                if (filter.matches(name, histogram)) {
                    decoratedRegistry.register(name, histogram);
                }
            }

            @Override
            public void onHistogramRemoved(String name) {
                decoratedRegistry.remove(name);
            }

            @Override
            public void onMeterAdded(String name, Meter meter) {
                if (filter.matches(name, meter)) {
                    decoratedRegistry.register(name, meter);
                }
            }

            @Override
            public void onMeterRemoved(String name) {
                decoratedRegistry.remove(name);
            }

            @Override
            public void onTimerAdded(String name, Timer timer) {
                if (filter.matches(name, timer)) {
                    decoratedRegistry.register(name, timer);
                }
            }

            @Override
            public void onTimerRemoved(String name) {
                decoratedRegistry.remove(name);
            }
        };
        sourceRegistry.addListener(listener);

        return decoratedRegistry;
    }

Then all we need is passing the decorated registry to the DropwizardExports instead of the original:

...
    MetricRegistry decoratedRegistry decorateMetricRegistryByFilter(originalRegistry, metricFilter);
    DropwizardExports exports = new DropwizardExports(decoratedRegistry, metricFilter);
    ...

Moreover, performance can be dramatically increased by the snippet above in case if the filter works slowly, for example because the filter computes regexp. Because each filter requires to be applied only once, we are free ещ use even inefficient filtering and forget about filtering costs.

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

No branches or pull requests

3 participants