-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Log4j2Metrics
creates more MetricsFilter
instances than needed
#5818
Conversation
Log4j2Metrics
creates more MetricsFilter
instances than needed
Before, we were needlessly making multiple instances of `MetricsFilter` that were functionally equivalent. This reduces the instances to one per registry to which Log4j2Metrics is bound. This will reduce allocations and memory usage slightly.
Merging before review so we don't keep blocking #5810, but anyone feel free to leave review post merge and we can follow-up with anything that comes up. |
@shakuzen Here is a modified test from the test added in this PR that does not pass as the non-root logger configuration is not tracked for the second @Test
void multipleRegistriesCanBeBound() {
LoggerContext loggerContext = new LoggerContext("test");
LoggerConfig loggerConfig = new LoggerConfig("com.test", Level.INFO, false);
Configuration configuration = loggerContext.getConfiguration();
configuration.getRootLogger().setLevel(Level.INFO);
configuration.addLogger("com.test", loggerConfig);
loggerContext.updateLoggers(configuration);
MeterRegistry registry2 = new SimpleMeterRegistry();
Log4j2Metrics log4j2Metrics = new Log4j2Metrics(emptyList(), loggerContext);
log4j2Metrics.bindTo(registry);
// verify root logger
Logger logger = loggerContext.getLogger(Log4j2MetricsTest.class);
logger.info("Hello, world!");
assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(1);
// verify other logger
Logger logger2 = loggerContext.getLogger("com.test");
logger2.info("Using other logger than root logger");
assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(2);
log4j2Metrics.bindTo(registry2);
logger.info("Hello, world!");
assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(3);
assertThat(registry2.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(1);
logger2.info("Using other logger than root logger");
assertThat(registry.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(4);
// this final check does not pass as the log event is not properly counted
assertThat(registry2.get("log4j2.events").tags("level", "info").counter().count()).isEqualTo(2);
} The issue is the |
@pativa thank you for taking a look and for providing a failing unit test. I'll take a closer look tomorrow. |
Looking at the code, it seems this should have been a problem before as well. I don't know that binding to multiple registries was ever working properly so maybe it isn't a bug to fix, but the fix seems easy enough. I'll open a separate issue and use your test code. |
Before, we were needlessly making multiple instances of
MetricsFilter
that were functionally equivalent. This reduces theMetricsFilter
instances to one per registry to which aLog4j2Metrics
instance is bound. This will reduce allocations and memory usage slightly.