diff --git a/TESTING.asciidoc b/TESTING.asciidoc index dbd11cce0256..0393cf92776f 100644 --- a/TESTING.asciidoc +++ b/TESTING.asciidoc @@ -108,6 +108,8 @@ password: `elastic-password`. - In order to set an Elasticsearch setting, provide a setting with the following prefix: `-Dtests.es.` - In order to pass a JVM setting, e.g. to disable assertions: `-Dtests.jvm.argline="-da"` - In order to use HTTPS: ./gradlew run --https +- In order to start a mock logging APM server on port 9999 and configure ES cluster to connect to it, +use `./gradlew run --with-apm-server` ==== Customizing the test cluster for ./gradlew run diff --git a/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/MockApmServer.java b/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/MockApmServer.java new file mode 100644 index 000000000000..7ec74ee19d1b --- /dev/null +++ b/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/MockApmServer.java @@ -0,0 +1,129 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +package org.elasticsearch.gradle.testclusters; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; + +import org.gradle.api.logging.Logger; +import org.gradle.api.logging.Logging; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +/** + * This is a server which just accepts lines of JSON code and if the JSON + * is valid and the root node is "transaction", then adds that JSON object + * to a transaction list which is accessible externally to the class. + * + * The Elastic agent sends lines of JSON code, and so this mock server + * can be used as a basic APM server for testing. + * + * The HTTP server used is the JDK embedded com.sun.net.httpserver + */ +public class MockApmServer { + private static final Logger logger = Logging.getLogger(MockApmServer.class); + private int port; + + public MockApmServer(int port) { + this.port = port; + } + + /** + * Simple main that starts a mock APM server and prints the port it is + * running on. This is not needed + * for testing, it is just a convenient template for trying things out + * if you want play around. + */ + public static void main(String[] args) throws IOException, InterruptedException { + MockApmServer server = new MockApmServer(9999); + server.start(); + } + + private static volatile HttpServer instance; + + /** + * Start the Mock APM server. Just returns empty JSON structures for every incoming message + * @return - the port the Mock APM server started on + * @throws IOException + */ + public synchronized int start() throws IOException { + if (instance != null) { + throw new IOException("MockApmServer: Ooops, you can't start this instance more than once"); + } + InetSocketAddress addr = new InetSocketAddress("0.0.0.0", port); + HttpServer server = HttpServer.create(addr, 10); + server.createContext("/exit", new ExitHandler()); + server.createContext("/", new RootHandler()); + + server.start(); + instance = server; + logger.lifecycle("MockApmServer started on port " + server.getAddress().getPort()); + return server.getAddress().getPort(); + } + + public int getPort() { + return port; + } + + /** + * Stop the server gracefully if possible + */ + public synchronized void stop() { + logger.lifecycle("stopping apm server"); + instance.stop(1); + instance = null; + } + + class RootHandler implements HttpHandler { + public void handle(HttpExchange t) { + try { + InputStream body = t.getRequestBody(); + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + byte[] buffer = new byte[8 * 1024]; + int lengthRead; + while ((lengthRead = body.read(buffer)) > 0) { + bytes.write(buffer, 0, lengthRead); + } + logger.lifecycle(("MockApmServer reading JSON objects: " + bytes.toString())); + + String response = "{}"; + t.sendResponseHeaders(200, response.length()); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + static class ExitHandler implements HttpHandler { + private static final int STOP_TIME = 3; + + public void handle(HttpExchange t) { + try { + InputStream body = t.getRequestBody(); + String response = "{}"; + t.sendResponseHeaders(200, response.length()); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + instance.stop(STOP_TIME); + instance = null; + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} diff --git a/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/RunTask.java b/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/RunTask.java index d0b2581895db..953c0447ec71 100644 --- a/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/RunTask.java +++ b/build-tools/src/main/java/org/elasticsearch/gradle/testclusters/RunTask.java @@ -31,7 +31,7 @@ import java.util.function.BooleanSupplier; import java.util.function.Function; import java.util.stream.Collectors; -public class RunTask extends DefaultTestClustersTask { +public abstract class RunTask extends DefaultTestClustersTask { public static final String CUSTOM_SETTINGS_PREFIX = "tests.es."; private static final Logger logger = Logging.getLogger(RunTask.class); @@ -40,6 +40,7 @@ public class RunTask extends DefaultTestClustersTask { private static final String transportCertificate = "private-cert2.p12"; private Boolean debug = false; + private Boolean apmServerEnabled = false; private Boolean preserveData = false; @@ -54,6 +55,7 @@ public class RunTask extends DefaultTestClustersTask { private final Path tlsBasePath = Path.of( new File(getProject().getRootDir(), "build-tools-internal/src/main/resources/run.ssl").toURI() ); + private MockApmServer mockServer; @Option(option = "debug-jvm", description = "Enable debugging configuration, to allow attaching a debugger to elasticsearch.") public void setDebug(boolean enabled) { @@ -65,6 +67,16 @@ public class RunTask extends DefaultTestClustersTask { return debug; } + @Input + public Boolean getApmServerEnabled() { + return apmServerEnabled; + } + + @Option(option = "with-apm-server", description = "Run simple logging http server to accept apm requests") + public void setApmServerEnabled(Boolean apmServerEnabled) { + this.apmServerEnabled = apmServerEnabled; + } + @Option(option = "data-dir", description = "Override the base data directory used by the testcluster") public void setDataDir(String dataDirStr) { dataDir = Paths.get(dataDirStr).toAbsolutePath(); @@ -172,6 +184,19 @@ public class RunTask extends DefaultTestClustersTask { node.setting("xpack.security.transport.ssl.keystore.path", "transport.keystore"); node.setting("xpack.security.transport.ssl.certificate_authorities", "transport.ca"); } + + if (apmServerEnabled) { + mockServer = new MockApmServer(9999); + try { + mockServer.start(); + node.setting("telemetry.metrics.enabled", "true"); + node.setting("tracing.apm.agent.server_url", "http://127.0.0.1:" + mockServer.getPort()); + } catch (IOException e) { + logger.warn("Unable to start APM server", e); + } + + } + } } if (debug) { @@ -242,6 +267,10 @@ public class RunTask extends DefaultTestClustersTask { if (thrown != null) { logger.debug("exception occurred during close of stdout file readers", thrown); } + + if (apmServerEnabled && mockServer != null) { + mockServer.stop(); + } } }