Skip to content

Commit

Permalink
Stop applying MeterFilters to auto-configured composite registry
Browse files Browse the repository at this point in the history
Previously, all MeterFilter beans were applied to all MeterRegistry
beans. As a result, when a composite registry was auto-configured, both
the composite and all of its delegates would have the same MeterFilters
applied. This made it impossible for one of the delegate registries to
have a locally-configured filter that would allow a meter that would be
denied by one of the MeterFilter beans applied to the composite.

This commit update MeterRegistryConfigurer to skips the auto-configured
composite meter registry when applying MeterFilter beans to
MeterRegistry beans. As a result, the composite's filters will no
longer deny a meter before it reaches a delegate that would have
accepted it due to one of its locally-configured filters.

Closes spring-projectsgh-23381
  • Loading branch information
wilkinsona committed Sep 21, 2020
1 parent 9ecc548 commit 9f21413
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
@@ -0,0 +1,37 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.actuate.autoconfigure.metrics;

import java.util.List;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;

/**
* Specialization of {@link CompositeMeterRegistry} used to identify the auto-configured
* composite.
*
* @author Andy Wilkinson
*/
class AutoConfiguredCompositeMeterRegistry extends CompositeMeterRegistry {

AutoConfiguredCompositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
super(clock, registries);
}

}
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,8 +42,8 @@ class CompositeMeterRegistryConfiguration {

@Bean
@Primary
CompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
return new CompositeMeterRegistry(clock, registries);
AutoConfiguredCompositeMeterRegistry compositeMeterRegistry(Clock clock, List<MeterRegistry> registries) {
return new AutoConfiguredCompositeMeterRegistry(clock, registries);
}

static class MultipleNonPrimaryMeterRegistriesCondition extends NoneNestedConditions {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +61,9 @@ void configure(MeterRegistry registry) {
// Customizers must be applied before binders, as they may add custom
// tags or alter timer or summary configuration.
customize(registry);
addFilters(registry);
if (!(registry instanceof AutoConfiguredCompositeMeterRegistry)) {
addFilters(registry);
}
if (!this.hasCompositeMeterRegistry || registry instanceof CompositeMeterRegistry) {
addBinders(registry);
}
Expand Down
Expand Up @@ -16,13 +16,17 @@

package org.springframework.boot.actuate.autoconfigure.metrics;

import java.util.Arrays;
import java.util.Set;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleConfig;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.micrometer.graphite.GraphiteMeterRegistry;
import io.micrometer.jmx.JmxMeterRegistry;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.autoconfigure.metrics.export.graphite.GraphiteMetricsExportAutoConfiguration;
Expand Down Expand Up @@ -109,6 +113,35 @@ void compositeCreatedWithMultipleRegistries() {
});
}

@Test
void autoConfiguredCompositeDoesNotHaveMeterFiltersApplied() {
new ApplicationContextRunner().with(MetricsRun.limitedTo(GraphiteMetricsExportAutoConfiguration.class,
JmxMetricsExportAutoConfiguration.class)).run((context) -> {
MeterRegistry composite = context.getBean(MeterRegistry.class);
assertThat(composite).extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(0);
assertThat(composite).isInstanceOf(CompositeMeterRegistry.class);
Set<MeterRegistry> registries = ((CompositeMeterRegistry) composite).getRegistries();
assertThat(registries).hasSize(2);
assertThat(registries).hasAtLeastOneElementOfType(GraphiteMeterRegistry.class)
.hasAtLeastOneElementOfType(JmxMeterRegistry.class);
assertThat(registries).allSatisfy((registry) -> assertThat(registry)
.extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(1));
});
}

@Test
void userConfiguredCompositeHasMeterFiltersApplied() {
new ApplicationContextRunner().with(MetricsRun.limitedTo())
.withUserConfiguration(CompositeMeterRegistryConfiguration.class).run((context) -> {
MeterRegistry composite = context.getBean(MeterRegistry.class);
assertThat(composite).extracting("filters", InstanceOfAssertFactories.ARRAY).hasSize(1);
assertThat(composite).isInstanceOf(CompositeMeterRegistry.class);
Set<MeterRegistry> registries = ((CompositeMeterRegistry) composite).getRegistries();
assertThat(registries).hasSize(2);
assertThat(registries).hasOnlyElementsOfTypes(SimpleMeterRegistry.class);
});
}

@Configuration(proxyBeanMethods = false)
static class PrimaryMeterRegistryConfiguration {

Expand All @@ -120,4 +153,17 @@ MeterRegistry simpleMeterRegistry() {

}

@Configuration(proxyBeanMethods = false)
static class CompositeMeterRegistryConfiguration {

@Bean
CompositeMeterRegistry compositeMeterRegistry() {
CompositeMeterRegistry compositeMeterRegistry = new CompositeMeterRegistry(new MockClock(),
Arrays.asList(new SimpleMeterRegistry(), new SimpleMeterRegistry()));
System.out.println(compositeMeterRegistry.getRegistries());
return compositeMeterRegistry;
}

}

}

0 comments on commit 9f21413

Please sign in to comment.