forked from micrometer-metrics/micrometer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warn when a MeterFilter is configured after a meter is registered
In normal usage we would expect this should not happen, but there is nothing enforcing or encouraging it to not happen. This warns users via logging that it is happening and advises to correct it, if possible. Otherwise, we want to learn about why it is not fixable. The problem with configuring MeterFilters after a Meter has been registered is that such filters will not be applied to previously registered meters. This can result in a mix of metrics with all filters applied and only some filters applied, which is potentially hard to notice.
- Loading branch information
Showing
6 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
micrometer-core/src/test/java/io/micrometer/core/instrument/MeterRegistryLoggingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* 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 io.micrometer.core.instrument; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import com.innoq.junit.jupiter.logging.Logging; | ||
import com.innoq.junit.jupiter.logging.LoggingEvents; | ||
import io.micrometer.core.instrument.config.MeterFilter; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import org.assertj.core.api.InstanceOfAssertFactories; | ||
import org.junit.jupiter.api.Test; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import static org.assertj.core.api.Assertions.as; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for expected logging from {@link MeterRegistry}. | ||
*/ | ||
@Logging | ||
class MeterRegistryLoggingTest { | ||
|
||
MeterRegistry registry = new SimpleMeterRegistry(); | ||
|
||
@Test | ||
void meterRegistrationBeforeMeterFilterConfig(LoggingEvents logEvents) { | ||
registry.counter("counter"); | ||
registry.config().meterFilter(MeterFilter.commonTags(Tags.of("common", "tag"))); | ||
|
||
assertThat(logEvents.withLevel(Level.DEBUG)).isEmpty(); | ||
assertThat(logEvents.withLevel(Level.WARN)).singleElement() | ||
.extracting(ILoggingEvent::getFormattedMessage, as(InstanceOfAssertFactories.STRING)) | ||
.contains("A MeterFilter is being configured after a Meter has been registered to this registry.") | ||
.doesNotContain("meterRegistrationBeforeMeterFilterConfig") | ||
.doesNotContain("\tat "); | ||
} | ||
|
||
@Test | ||
void meterRegistrationBeforeMeterFilterConfigWithDebugLogging(LoggingEvents logEvents) { | ||
Level priorLevel = ((Logger) LoggerFactory.getLogger(SimpleMeterRegistry.class)).getLevel(); | ||
((Logger) LoggerFactory.getLogger(SimpleMeterRegistry.class)).setLevel(Level.DEBUG); | ||
try { | ||
registry.counter("counter"); | ||
registry.config().meterFilter(MeterFilter.commonTags(Tags.of("common", "tag"))); | ||
|
||
assertThat(logEvents.withLevel(Level.WARN)).isEmpty(); | ||
assertThat(logEvents.withLevel(Level.DEBUG)).singleElement() | ||
.extracting(ILoggingEvent::getFormattedMessage, as(InstanceOfAssertFactories.STRING)) | ||
.contains("A MeterFilter is being configured after a Meter has been registered to this registry.") | ||
// this test method (filter config call site) is in stack trace | ||
.contains("meterRegistrationBeforeMeterFilterConfigWithDebugLogging") | ||
.contains("\tat ") // stack trace output | ||
; | ||
} | ||
finally { | ||
((Logger) LoggerFactory.getLogger(SimpleMeterRegistry.class)).setLevel(priorLevel); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters