@Override
    public void register(Metric metric, String metricName, AbstractMetricGroup group) {
        synchronized (lock) {
            if (isShutdown()) {
                LOG.warn(
                        "Cannot register metric, because the MetricRegistry has already been shut down.");
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(
                            "Registering metric {}.{}.",
                            group.getLogicalScope(CharacterFilter.NO_OP_FILTER),
                            metricName);
                }
                if (reporters != null) {
                    forAllReporters(MetricReporter::notifyOfAddedMetric, metric, metricName, group);
                }
                try {
                    if (queryService != null) {
                        queryService.addMetric(metricName, metric, group);
                    }
                } catch (Exception e) {
                    LOG.warn("Error while registering metric: {}.", metricName, e);
                }
                try {
                    if (metric instanceof View) {
                        if (viewUpdater == null) {
                            viewUpdater = new ViewUpdater(viewUpdaterScheduledExecutor);
                        }
                        viewUpdater.notifyOfAddedView((View) metric);
                    }
                } catch (Exception e) {
                    LOG.warn("Error while registering metric: {}.", metricName, e);
                }
            }
        }
    }
@Test
    void testMetricFiltering() {
        final String excludedMetricName = "excluded";
        final NotificationCapturingReporter reporter = new NotificationCapturingReporter();

        final Configuration reporterConfig = new Configuration();
        reporterConfig.set(MetricOptions.REPORTER_INCLUDES, Arrays.asList("*:*:counter"));
        reporterConfig.set(
                MetricOptions.REPORTER_EXCLUDES, Arrays.asList("*:" + excludedMetricName));

        MetricRegistryImpl registry =
                new MetricRegistryImpl(
                        MetricRegistryTestUtils.defaultMetricRegistryConfiguration(),
                        Arrays.asList(
                                ReporterSetup.forReporter(
                                        "test",
                                        DefaultMetricFilter.fromConfiguration(reporterConfig),
                                        reporter)));

        registry.register(
                new TestMeter(), "", new MetricGroupTest.DummyAbstractMetricGroup(registry));

        assertThat(reporter.getLastAddedMetric()).isEmpty();

        registry.register(
                new TestCounter(),
                excludedMetricName,
                new MetricGroupTest.DummyAbstractMetricGroup(registry));

        assertThat(reporter.getLastAddedMetric()).isEmpty();

        registry.register(
                new TestCounter(), "foo", new MetricGroupTest.DummyAbstractMetricGroup(registry));

        assertThat(reporter.getLastAddedMetric()).isNotEmpty();
    }