import com.sun.net.httpserver.HttpServer; import java.io.OutputStream; import java.net.InetSocketAddress; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.time.Instant; /** * A minimal, dependency-free stand-in for an OTLP collector. It accepts any POST to any path, * logs the path, content-length and arrival time to a file (one line per request), and returns * 200 OK with an empty OTLP-shaped protobuf response body. Used only to answer one factual * question: did this JVM actually attempt to POST metrics to the configured OTLP endpoint within * the observation window? Not a real collector -- doesn't parse the protobuf payload. * * Usage: java StubOtlpReceiver.java */ public class StubOtlpReceiver { public static void main(String[] args) throws Exception { int port = Integer.parseInt(args[0]); Path logFile = Path.of(args[1]); Files.deleteIfExists(logFile); Files.createFile(logFile); HttpServer server = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0); server.createContext("/", exchange -> { String line = Instant.now() + " " + exchange.getRequestMethod() + " " + exchange.getRequestURI() + " content-length=" + exchange.getRequestHeaders().getFirst("Content-Length") + System.lineSeparator(); Files.writeString(logFile, line, StandardOpenOption.APPEND); exchange.getRequestBody().readAllBytes(); // drain byte[] resp = new byte[0]; exchange.sendResponseHeaders(200, resp.length); try (OutputStream os = exchange.getResponseBody()) { os.write(resp); } }); server.setExecutor(null); server.start(); System.out.println("StubOtlpReceiver listening on :" + port + ", logging to " + logFile); // Run until killed externally. Thread.currentThread().join(); } }