Skip to content

Commit

Permalink
Remove the use of fixed ports
Browse files Browse the repository at this point in the history
  • Loading branch information
psx95 committed Dec 18, 2024
1 parent c6049e3 commit eef7cb7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,21 @@ public void smokeTest() throws IOException, InterruptedException {
throw new RuntimeException(e);
}
backendServer.start();
int backendServerPort = backendServer.getAddress().getPort();
System.out.println("Started OTLP HTTP Endpoint on localhost:" + backendServerPort);
Process p =
new ProcessBuilder(
"java",
"-javaagent:" + javaAgentJarPath,
"-Dotel.javaagent.extensions=" + authExtensionJarPath,
"-Dotel.java.global-autoconfigure.enabled=true",
"-Dgoogle.cloud.project=dummy-test-project",
"-Dotel.exporter.otlp.endpoint=http://localhost:4318",
"-Dotel.java.global-autoconfigure.enabled=true",
"-Dotel.javaagent.logging=none",
"-Dotel.exporter.otlp.endpoint=http://localhost:" + backendServerPort,
"-Dotel.exporter.otlp.insecure=true",
"-Dotel.traces.exporter=otlp,logging",
"-Dotel.traces.exporter=otlp",
"-Dotel.metrics.exporter=none",
"-Dotel.logs.exporter=none",
"-Dotel.javaagent.debug=false",
"-Dotel.exporter.otlp.protocol=http/protobuf",
"-jar",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@

public class InstrumentedTestApp {
private static final String serverUrl = "http://localhost:%d/%s";
private static final int defaultPort = 8080;

public static void main(String[] args) throws IOException, InterruptedException {
int port = parsePort(args);

HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
int port;
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
port = server.getAddress().getPort();
server.createContext("/doWork", new TestHandler());
server.createContext(
"/stop",
Expand All @@ -48,7 +47,7 @@ public static void main(String[] args) throws IOException, InterruptedException
});
server.setExecutor(null); // creates a default executor
server.start();
System.out.println("Server ready");
System.out.println("Starting Server at port " + port);
System.out.println("Sending request to do work ...");
makeCall(String.format(serverUrl, port, "doWork"));
Thread.sleep(1000);
Expand All @@ -72,21 +71,4 @@ private static void makeCall(String url) {
System.err.println("Error making request: " + e.getMessage());
}
}

private static int parsePort(String[] args) {
int port;
if (args.length > 0) {
try {
port = Integer.parseInt(args[0]);
if (port < 0 || port > 65535) {
throw new NumberFormatException("Port number must be between 0 and 65535");
}
return port;
} catch (NumberFormatException e) {
System.err.println("Invalid port number provided: " + args[0]);
System.err.println("Using default port: " + defaultPort);
}
}
return defaultPort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public class DummyOTelHttpEndpoint {
public static Map<String, Boolean> receivedRequests = new HashMap<>();

public static HttpServer createTestServer() throws IOException {
int port = 4318; // Use a different port than gRPC (e.g., 4318)

HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
server.createContext("/v1/traces", new TraceHandler()); // Handle traces
server.setExecutor(null); // Use default thread pool
return server;
Expand Down

0 comments on commit eef7cb7

Please sign in to comment.