-
Notifications
You must be signed in to change notification settings - Fork 327
make server_urls dynamically reloadable #734
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
make server_urls dynamically reloadable #734
Conversation
felixbarny
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, it's not as easy as that. The ApmServerClient#serverUrls variable has to be updated as well, in a thread-safe manner. Meaning that the variable should be declared volatile.
See also #725 (comment) for more details on how to use change listeners on how to register a config change callback. You can do that in the constructor of ApmServerClient. Also, reset the error count when the config changes.
@felixbarny |
|
success log example: |
|
|
||
| public ApmServerClient(ReporterConfiguration reporterConfiguration, List<URL> serverUrls) { | ||
| this.reporterConfiguration = reporterConfiguration; | ||
| for (ConfigurationOption configurationOption : reporterConfiguration.getConfigurationOptions()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better create a method public ConfigurationOption<List<URL>> getServerUrlsOption() in ReporterConfiguration and then just call reporterConfiguration.getServerUrlsOption().addChangeListener(...) here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
| T withConnection(HttpURLConnection connection) throws IOException; | ||
| } | ||
|
|
||
| public synchronized void overrideApmServerUrls(List<URL> serverUrls) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just inline this to the onChange method. Doesn't have to be synchronized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
|
||
| public synchronized void overrideApmServerUrls(List<URL> serverUrls) { | ||
| logger.debug("server_urls override with value = ({}).", serverUrls); | ||
| this.serverUrls = serverUrls; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shuffle the URLs by reusing the shuffleUrls method. To achieve that, change the parameter of shuffleUrls from ReporterConfiguration to List<URL>.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
|
@felixbarny I fixed according to comments |
felixbarny
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks ❤️
|
could you add a section to the changelog? |
added |
eyalkoren
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @kananindzya!!
We strive to adding a unit test to any new functionality, where applicable. You can add a package-private method ApmServerClient#getServerUrls to make the server url list available and add a unit test to ApmServerClientTest.
A good way to unit-test it is by changing it through system properties, then reload and see that the list had changed. For that, you will have to initialize an ElasticApmTracer and use the ReporterConfiguration from it when creating the test's client (as opposed to the current way the client is created with a new ReporterConfiguration, and after changing the setting reload through ElasticApmTracer#getConfigurationRegistry().reload("server_urls").
Just don't forget to restore at the end of the test, so that the other tests will not be affected.
|
Easier than using an actual properties file for testing is to use an in-memory PropertySource like here: Line 175 in 45857ce
|
…indzya/apm-agent-java into server_urls_dynamically_reloadable
hi @eyalkoren , I added protected ApmServerClient#getServerUrls, and added test with using |
eyalkoren
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent 👌 !
Few minor last comments about accessibility and unused imports.
I prefer the most restricted access, especially for something we expose for testing purposes. Package private is more "accurate" than protected in this case.
| import java.net.URLConnection; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.ServiceLoader; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove unused imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
apm-agent-core/src/test/java/co/elastic/apm/agent/report/ApmServerClientTest.java
Outdated
Show resolved
Hide resolved
apm-agent-core/src/main/java/co/elastic/apm/agent/report/ApmServerClient.java
Outdated
Show resolved
Hide resolved
…rverClientTest.java Co-Authored-By: eyalkoren <41850454+eyalkoren@users.noreply.github.com>
…rverClient.java Co-Authored-By: eyalkoren <41850454+eyalkoren@users.noreply.github.com>
eyalkoren
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again @kananindzya !
|
can you merge in the changes from master again? that should fix the test failures |
|
run the tests |
closes #723
Checklist