|
| 1 | +package datadog.trace.instrumentation.sparkjava; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | + |
| 6 | +import datadog.trace.agent.test.AbstractInstrumentationTest; |
| 7 | +import datadog.trace.agent.test.utils.PortUtils; |
| 8 | +import datadog.trace.core.DDSpan; |
| 9 | +import java.io.BufferedReader; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.io.InputStreamReader; |
| 12 | +import java.net.HttpURLConnection; |
| 13 | +import java.net.URL; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.concurrent.TimeoutException; |
| 17 | +import org.junit.jupiter.api.AfterAll; |
| 18 | +import org.junit.jupiter.api.BeforeAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.TestInstance; |
| 21 | +import spark.Request; |
| 22 | +import spark.Response; |
| 23 | +import spark.Route; |
| 24 | +import spark.Spark; |
| 25 | + |
| 26 | +/** |
| 27 | + * Forked test for the SparkJava 2.x instrumentation, running in an isolated JVM. This validates |
| 28 | + * that the {@link RoutesInstrumentation} loads and enriches Jetty server spans correctly when the |
| 29 | + * agent starts from scratch — no leftover state from other test classes. |
| 30 | + * |
| 31 | + * <p>This test focuses on the core enrichment contract: when a request matches a SparkJava route, |
| 32 | + * the server span gets operation name {@code spark.request}, component {@code spark-java}, and the |
| 33 | + * resource name / http.route reflect the parameterized route pattern. |
| 34 | + */ |
| 35 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 36 | +public class SparkJavaForkedTest extends AbstractInstrumentationTest { |
| 37 | + |
| 38 | + private int actualPort; |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + void setupServer() { |
| 42 | + actualPort = PortUtils.randomOpenPort(); |
| 43 | + Spark.port(actualPort); |
| 44 | + |
| 45 | + Spark.get( |
| 46 | + "/ping", |
| 47 | + new Route() { |
| 48 | + @Override |
| 49 | + public Object handle(Request request, Response response) { |
| 50 | + response.type("text/plain"); |
| 51 | + return "pong"; |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + Spark.get( |
| 56 | + "/items/:id", |
| 57 | + new Route() { |
| 58 | + @Override |
| 59 | + public Object handle(Request request, Response response) { |
| 60 | + response.type("application/json"); |
| 61 | + return "{\"id\": \"" + request.params(":id") + "\"}"; |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + Spark.get( |
| 66 | + "/fail", |
| 67 | + new Route() { |
| 68 | + @Override |
| 69 | + public Object handle(Request request, Response response) { |
| 70 | + throw new RuntimeException("Forked test error"); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + Spark.awaitInitialization(); |
| 75 | + } |
| 76 | + |
| 77 | + @AfterAll |
| 78 | + void tearDownServer() throws InterruptedException { |
| 79 | + Spark.stop(); |
| 80 | + Thread.sleep(500); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void simpleRouteEnrichesServerSpan() throws InterruptedException, TimeoutException { |
| 85 | + httpGet("/ping"); |
| 86 | + |
| 87 | + DDSpan serverSpan = waitForServerSpan(); |
| 88 | + assertServerSpan(serverSpan, "GET", "/ping", 200, false); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void parameterizedRoutePatternInResourceName() throws InterruptedException, TimeoutException { |
| 93 | + httpGet("/items/42"); |
| 94 | + |
| 95 | + DDSpan serverSpan = waitForServerSpan(); |
| 96 | + assertServerSpan(serverSpan, "GET", "/items/:id", 200, false); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + void errorRouteProducesErrorSpan() throws InterruptedException, TimeoutException { |
| 101 | + httpGet("/fail"); |
| 102 | + |
| 103 | + DDSpan serverSpan = waitForServerSpan(); |
| 104 | + assertServerSpan(serverSpan, "GET", "/fail", 500, true); |
| 105 | + } |
| 106 | + |
| 107 | + // --------------------------------------------------------------- |
| 108 | + // Helper methods |
| 109 | + // --------------------------------------------------------------- |
| 110 | + |
| 111 | + /** |
| 112 | + * Validates the complete structure of a server span, covering both SparkJava enrichment and the |
| 113 | + * underlying Jetty server span baseline. This single-point-of-assertion prevents regressions when |
| 114 | + * new required tags are added. |
| 115 | + * |
| 116 | + * <p>SparkJava enrichment (set by {@link RoutesInstrumentation}): |
| 117 | + * |
| 118 | + * <ul> |
| 119 | + * <li>operation name = {@code spark.request} |
| 120 | + * <li>component = {@code spark-java} |
| 121 | + * <li>resource name = {@code HTTP_METHOD route_pattern} |
| 122 | + * <li>http.route = parameterized route pattern |
| 123 | + * </ul> |
| 124 | + * |
| 125 | + * <p>Jetty baseline (set by the Jetty server instrumentation): |
| 126 | + * |
| 127 | + * <ul> |
| 128 | + * <li>span type = {@code web} |
| 129 | + * <li>span.kind = {@code server} |
| 130 | + * <li>http.method, http.status_code, http.url |
| 131 | + * <li>error flag (from HTTP status code) |
| 132 | + * </ul> |
| 133 | + * |
| 134 | + * @param span the server span to validate |
| 135 | + * @param httpMethod the expected HTTP method (e.g., "GET", "POST") |
| 136 | + * @param route the expected route pattern (e.g., "/items/:id") |
| 137 | + * @param statusCode the expected HTTP status code |
| 138 | + * @param isError whether the span should be marked as errored |
| 139 | + */ |
| 140 | + private void assertServerSpan( |
| 141 | + DDSpan span, String httpMethod, String route, int statusCode, boolean isError) { |
| 142 | + assertNotNull(span, "Expected a server span for " + httpMethod + " " + route); |
| 143 | + |
| 144 | + // SparkJava enrichment assertions |
| 145 | + assertEquals( |
| 146 | + "spark.request", |
| 147 | + span.getOperationName().toString(), |
| 148 | + "Operation name should be 'spark.request'"); |
| 149 | + assertEquals( |
| 150 | + "spark-java", |
| 151 | + String.valueOf(span.getTag("component")), |
| 152 | + "component tag should be 'spark-java'"); |
| 153 | + assertEquals( |
| 154 | + httpMethod + " " + route, |
| 155 | + span.getResourceName().toString(), |
| 156 | + "Resource name should be HTTP_METHOD + route_pattern"); |
| 157 | + assertEquals( |
| 158 | + route, |
| 159 | + String.valueOf(span.getTag("http.route")), |
| 160 | + "http.route should contain the route pattern, not the actual path"); |
| 161 | + |
| 162 | + // Jetty baseline assertions |
| 163 | + assertEquals("web", span.getSpanType(), "Span type should be 'web'"); |
| 164 | + assertEquals( |
| 165 | + "server", String.valueOf(span.getTag("span.kind")), "span.kind should be 'server'"); |
| 166 | + assertEquals(httpMethod, String.valueOf(span.getTag("http.method")), "http.method tag"); |
| 167 | + assertEquals(statusCode, span.getTag("http.status_code"), "http.status_code tag"); |
| 168 | + assertNotNull(span.getTag("http.url"), "http.url tag should be set"); |
| 169 | + assertEquals(isError, span.isError(), "error flag"); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Waits for at least one trace to be written and returns the server span. |
| 174 | + * |
| 175 | + * @return the server span (never null — fails assertion if not found) |
| 176 | + * @throws InterruptedException if the thread is interrupted while waiting |
| 177 | + * @throws TimeoutException if no trace is written within the timeout |
| 178 | + */ |
| 179 | + private DDSpan waitForServerSpan() throws InterruptedException, TimeoutException { |
| 180 | + writer.waitForTraces(1); |
| 181 | + List<DDSpan> spans = new ArrayList<>(); |
| 182 | + for (List<DDSpan> trace : writer) { |
| 183 | + spans.addAll(trace); |
| 184 | + } |
| 185 | + DDSpan serverSpan = null; |
| 186 | + for (DDSpan span : spans) { |
| 187 | + if ("server".equals(String.valueOf(span.getTag("span.kind"))) |
| 188 | + || "web".equals(span.getSpanType())) { |
| 189 | + serverSpan = span; |
| 190 | + break; |
| 191 | + } |
| 192 | + } |
| 193 | + assertNotNull(serverSpan, "Expected to find a server span in the collected traces"); |
| 194 | + return serverSpan; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Makes an HTTP GET request to the SparkJava server. |
| 199 | + * |
| 200 | + * @param path the request path |
| 201 | + * @return the HTTP status code |
| 202 | + */ |
| 203 | + private int httpGet(String path) { |
| 204 | + try { |
| 205 | + URL url = new URL("http://localhost:" + actualPort + path); |
| 206 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 207 | + conn.setRequestMethod("GET"); |
| 208 | + conn.setConnectTimeout(5000); |
| 209 | + conn.setReadTimeout(5000); |
| 210 | + int status = conn.getResponseCode(); |
| 211 | + InputStream is = |
| 212 | + conn.getResponseCode() >= 400 ? conn.getErrorStream() : conn.getInputStream(); |
| 213 | + if (is != null) { |
| 214 | + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); |
| 215 | + while (reader.readLine() != null) { |
| 216 | + // drain |
| 217 | + } |
| 218 | + reader.close(); |
| 219 | + } |
| 220 | + conn.disconnect(); |
| 221 | + return status; |
| 222 | + } catch (Exception e) { |
| 223 | + throw new RuntimeException("HTTP GET failed for path " + path, e); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments