|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.struts2; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.ERROR; |
| 9 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.EXCEPTION; |
| 10 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.NOT_FOUND; |
| 11 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.PATH_PARAM; |
| 12 | +import static io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint.REDIRECT; |
| 13 | +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; |
| 14 | +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION; |
| 15 | +import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | + |
| 18 | +import io.opentelemetry.api.trace.SpanKind; |
| 19 | +import io.opentelemetry.instrumentation.api.internal.HttpConstants; |
| 20 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 21 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerTest; |
| 22 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; |
| 23 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerTestOptions; |
| 24 | +import io.opentelemetry.instrumentation.testing.junit.http.ServerEndpoint; |
| 25 | +import io.opentelemetry.sdk.testing.assertj.SpanDataAssert; |
| 26 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 27 | +import io.opentelemetry.sdk.trace.data.StatusData; |
| 28 | +import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; |
| 29 | +import java.util.EnumSet; |
| 30 | +import java.util.Locale; |
| 31 | +import javax.servlet.DispatcherType; |
| 32 | +import javax.servlet.Filter; |
| 33 | +import org.eclipse.jetty.server.Server; |
| 34 | +import org.eclipse.jetty.server.session.HashSessionIdManager; |
| 35 | +import org.eclipse.jetty.server.session.HashSessionManager; |
| 36 | +import org.eclipse.jetty.server.session.SessionHandler; |
| 37 | +import org.eclipse.jetty.servlet.DefaultServlet; |
| 38 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 39 | +import org.eclipse.jetty.util.resource.FileResource; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 42 | + |
| 43 | +class Struts2ActionSpanTest extends AbstractHttpServerTest<Server> { |
| 44 | + |
| 45 | + @RegisterExtension |
| 46 | + public static final InstrumentationExtension testing = |
| 47 | + HttpServerInstrumentationExtension.forAgent(); |
| 48 | + |
| 49 | + @Override |
| 50 | + @SuppressWarnings("unchecked") |
| 51 | + protected Server setupServer() throws Exception { |
| 52 | + Server server = new Server(port); |
| 53 | + ServletContextHandler context = new ServletContextHandler(0); |
| 54 | + context.setContextPath(getContextPath()); |
| 55 | + FileResource resource = new FileResource(getClass().getResource("/")); |
| 56 | + context.setBaseResource(resource); |
| 57 | + server.setHandler(context); |
| 58 | + |
| 59 | + HashSessionIdManager sessionIdManager = new HashSessionIdManager(); |
| 60 | + server.setSessionIdManager(sessionIdManager); |
| 61 | + HashSessionManager sessionManager = new HashSessionManager(); |
| 62 | + SessionHandler sessionHandler = new SessionHandler(sessionManager); |
| 63 | + context.setHandler(sessionHandler); |
| 64 | + |
| 65 | + // disable adding jsessionid to url, affects redirect test |
| 66 | + context.setInitParameter("org.eclipse.jetty.servlet.SessionIdPathParameterName", "none"); |
| 67 | + |
| 68 | + context.addServlet(DefaultServlet.class, "/"); |
| 69 | + context.addServlet(GreetingServlet.class, "/greetingServlet"); |
| 70 | + Class<?> strutsFilterClass; |
| 71 | + try { |
| 72 | + // struts 2.3 |
| 73 | + strutsFilterClass = |
| 74 | + Class.forName("org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter"); |
| 75 | + } catch (ClassNotFoundException exception) { |
| 76 | + // struts 2.5 |
| 77 | + strutsFilterClass = |
| 78 | + Class.forName("org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter"); |
| 79 | + } |
| 80 | + context.addFilter( |
| 81 | + (Class<? extends Filter>) strutsFilterClass, "/*", EnumSet.of(DispatcherType.REQUEST)); |
| 82 | + |
| 83 | + server.start(); |
| 84 | + return server; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void stopServer(Server server) throws Exception { |
| 89 | + server.stop(); |
| 90 | + server.destroy(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void configure(HttpServerTestOptions options) { |
| 95 | + options.setContextPath("/context"); |
| 96 | + options.setTestPathParam(true); |
| 97 | + options.setTestErrorBody(false); |
| 98 | + options.setHasHandlerSpan(endpoint -> !endpoint.equals(NOT_FOUND)); |
| 99 | + options.setHasResponseSpan( |
| 100 | + endpoint -> |
| 101 | + endpoint == REDIRECT |
| 102 | + || endpoint == ERROR |
| 103 | + || endpoint == EXCEPTION |
| 104 | + || endpoint == NOT_FOUND); |
| 105 | + |
| 106 | + options.setExpectedHttpRoute( |
| 107 | + (ServerEndpoint endpoint, String method) -> { |
| 108 | + if (method.equals(HttpConstants._OTHER)) { |
| 109 | + return getContextPath() + endpoint.getPath(); |
| 110 | + } |
| 111 | + if (endpoint.equals(PATH_PARAM)) { |
| 112 | + return getContextPath() + "/path/{id}/param"; |
| 113 | + } else if (endpoint.equals(NOT_FOUND)) { |
| 114 | + return getContextPath() + "/*"; |
| 115 | + } else { |
| 116 | + return super.expectedHttpRoute(endpoint, method); |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + protected SpanDataAssert assertResponseSpan( |
| 123 | + SpanDataAssert span, SpanData parentSpan, String method, ServerEndpoint endpoint) { |
| 124 | + if (endpoint.equals(REDIRECT)) { |
| 125 | + span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendRedirect")); |
| 126 | + } else if (endpoint.equals(NOT_FOUND)) { |
| 127 | + span.satisfies(spanData -> assertThat(spanData.getName()).endsWith(".sendError")) |
| 128 | + .hasParent(parentSpan); |
| 129 | + } |
| 130 | + |
| 131 | + span.hasKind(SpanKind.INTERNAL); |
| 132 | + return span; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + protected SpanDataAssert assertHandlerSpan( |
| 137 | + SpanDataAssert span, String method, ServerEndpoint endpoint) { |
| 138 | + span.hasName("GreetingAction." + endpoint.name().toLowerCase(Locale.ROOT)) |
| 139 | + .hasKind(SpanKind.INTERNAL); |
| 140 | + |
| 141 | + if (endpoint.equals(EXCEPTION)) { |
| 142 | + span.hasStatus(StatusData.error()).hasException(new Exception(EXCEPTION.getBody())); |
| 143 | + } |
| 144 | + |
| 145 | + span.hasAttributesSatisfyingExactly( |
| 146 | + equalTo(CODE_NAMESPACE, GreetingAction.class.getName()), |
| 147 | + equalTo(CODE_FUNCTION, endpoint.name().toLowerCase(Locale.ROOT))); |
| 148 | + return span; |
| 149 | + } |
| 150 | + |
| 151 | + // Struts runs from a servlet filter. Test that dispatching from struts action to a servlet |
| 152 | + // does not overwrite server span name given by struts instrumentation. |
| 153 | + @Test |
| 154 | + void testDispatchToServlet() { |
| 155 | + AggregatedHttpResponse response = |
| 156 | + client.get(address.resolve("dispatch").toString()).aggregate().join(); |
| 157 | + |
| 158 | + assertThat(response.status().code()).isEqualTo(200); |
| 159 | + assertThat(response.contentUtf8()).isEqualTo("greeting"); |
| 160 | + |
| 161 | + testing.waitAndAssertTraces( |
| 162 | + trace -> |
| 163 | + trace.hasSpansSatisfyingExactly( |
| 164 | + span -> |
| 165 | + span.hasName("GET " + getContextPath() + "/dispatch") |
| 166 | + .hasKind(SpanKind.SERVER) |
| 167 | + .hasNoParent(), |
| 168 | + span -> |
| 169 | + span.hasName("GreetingAction.dispatch_servlet") |
| 170 | + .hasKind(SpanKind.INTERNAL) |
| 171 | + .hasParent(trace.getSpan(0)))); |
| 172 | + } |
| 173 | +} |
0 commit comments