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

Rebind log4j2 metrics if configuration is changed #5810

Merged
merged 5 commits into from
Feb 6, 2025

Conversation

pativa
Copy link
Contributor

@pativa pativa commented Jan 17, 2025

Describe the issue

This PR is intended to fix #5756

When log4j2 metrics are reconfigured, metrics will no longer be recorded. For example, if monitorInterval (docs) is configured to 30 is used and configuration is reloaded after application start, metrics would only be recorded for the first 30 seconds.


By using a PropertyChangeListener, it looks like we can register the MetricsFilter in much the same way, but it will be rebound after reloading the configuration. All the previous tests seem to pass too when using this method of configuration.

Copy link
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the quick turnaround time on submitting a pull request for this. It generally looks good to me. Will metrics filters still be configured on the old Configuration instance? Should that be the case? Does that not result in a memory leak of sorts?

@pativa
Copy link
Contributor Author

pativa commented Jan 20, 2025

Thank you for the quick turnaround time on submitting a pull request for this. It generally looks good to me. Will metrics filters still be configured on the old Configuration instance? Should that be the case? Does that not result in a memory leak of sorts?

Thanks for the review!

That is a good point! I don't think it would be any big issue, as the old configuration is already stopped here, but I do think it would be cleaner to clean up any old references to MetricsFilter when we rebind it.

I made a new commit here where I also do a cleanup of the old configuration instance. I also changed the changeListener to only do an update if the configuration instance is not the same object as the old instance (as no rebinding or cleanup should be needed then).

@pativa pativa force-pushed the log4j2-reconfigure-rebind branch 2 times, most recently from 4361d51 to 41d0e7e Compare January 20, 2025 08:53
@pativa
Copy link
Contributor Author

pativa commented Jan 23, 2025

Kind of unrelated to this PR, so I haven't changed it, but the call to

loggerContext.updateLoggers(configuration);

Seems to be completely unnecessary. I don't know how expensive this operation is, but it seems to be unnecessary to trigger a reload as this class modifies the configuration in-place?

@pativa pativa force-pushed the log4j2-reconfigure-rebind branch 2 times, most recently from 466e132 to ffe57d9 Compare January 23, 2025 20:29
@jonatan-ivanov
Copy link
Member

updateLoggers was removed in the past and was quickly restored it, see:

Basically it does two things:

  1. It updates the internal "copy" of the config, since references are used in some cases, this might not be an issue but things can change (in Log4J2 or in Micrometer) so it's safer to call updateLoggers too
  2. It emits a PropertyChangeEvent to notify components about the change

Copy link
Member

@shakuzen shakuzen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all the quick turnaround on reviews and sorry for all the back-and-forth. Things look good to me overall. I left a couple more minor review comments.

pativa and others added 5 commits February 5, 2025 14:50
* Use `PropertyChangeListener` to rebind the `MetricsFilter` to log4j2 if configuration is reloaded
* Keep track of the listener to deregister it when we close the binder

Signed-off-by: Patrik Ivarsson <[email protected]>
Signed-off-by: Patrik Ivarsson <[email protected]>
@shakuzen shakuzen force-pushed the log4j2-reconfigure-rebind branch from 5e9c78f to 66368f4 Compare February 5, 2025 06:08
@shakuzen shakuzen merged commit 04f6d8c into micrometer-metrics:main Feb 6, 2025
9 checks passed
@shakuzen
Copy link
Member

shakuzen commented Feb 6, 2025

@pativa Thank you for the great work on this PR and your patience in getting it reviewed and merged through all the side tasks that came out of it. This instrumentation is now better for everyone thanks to your contributions.

@pativa pativa deleted the log4j2-reconfigure-rebind branch February 6, 2025 08:11
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

Successfully merging this pull request may close these issues.

Rebind Log4j2Metrics when LoggerContext#reconfigure is called
3 participants