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

Expose service port as a system property, mainly to support use-case where configured port is zero and picked by OS #686

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public static void premain(String agentArgument, Instrumentation instrumentation
new JmxCollector(new File(config.file), JmxCollector.Mode.AGENT).register();
DefaultExports.initialize();
server = new HTTPServer(config.socket, CollectorRegistry.defaultRegistry, true);

// in case port was configured with port 0, then actual port (chosen by operating system) is known only after the socket is open
// in this case, we need a way to expose the actual port number used
// setting it as a system properties allows any other code running on the JVM to discover this
// this information could then be pushed into a discovery service
int actualServerPort = server.getPort();
System.setProperty("jmx_exporter.server.port.configured", String.valueOf(config.port));
System.setProperty("jmx_exporter.server.port.actual", String.valueOf(actualServerPort));
}
catch (IllegalArgumentException e) {
System.err.println("Usage: -javaagent:/path/to/JavaAgent.jar=[host:]<port>:<yaml configuration file> " + e.getMessage());
Expand Down