-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for struts 2.3 with tests #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
plugins { | ||
`java-library` | ||
id("io.opentelemetry.instrumentation.auto-instrumentation") | ||
muzzle | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group = "org.apache.struts" | ||
module = "struts2-core" | ||
versions = "[2.3.1,)" | ||
} | ||
} | ||
|
||
val versions: Map<String, String> by extra | ||
shashank11p marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
dependencies { | ||
testImplementation(project(":instrumentation:servlet:servlet-rw")) | ||
testImplementation(project(":instrumentation:servlet:servlet-3.0-no-wrapping")) | ||
testImplementation(project(":testing-common")) { | ||
exclude(group ="org.eclipse.jetty", module= "jetty-server") | ||
} | ||
testImplementation("io.opentelemetry.javaagent.instrumentation:opentelemetry-javaagent-servlet-3.0:${versions["opentelemetry_java_agent"]}") | ||
testImplementation("org.apache.struts:struts2-core:2.3.1") | ||
testImplementation("org.apache.struts:struts2-json-plugin:2.3.1") | ||
testImplementation("org.apache.struts:struts2-convention-plugin:2.3.1") | ||
testImplementation("org.eclipse.jetty:jetty-server:8.0.0.v20110901") | ||
testImplementation("org.eclipse.jetty:jetty-servlet:8.0.0.v20110901") | ||
testImplementation("javax.servlet:javax.servlet-api:3.0.1") | ||
testImplementation("javax.servlet:jsp-api:2.0") | ||
} |
44 changes: 44 additions & 0 deletions
44
...test/java/io/opentelemetry/javaagent/instrumentation/hypertrace/struts/Struts2Action.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright The Hypertrace Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hypertrace.struts; | ||
|
||
import com.opensymphony.xwork2.ActionSupport; | ||
import org.apache.struts2.convention.annotation.ParentPackage; | ||
import org.apache.struts2.convention.annotation.Result; | ||
|
||
@Result(type = "json") | ||
@ParentPackage("json-default") | ||
public class Struts2Action extends ActionSupport { | ||
|
||
private String jsonString = "{'balance':1000.21,'is_vip':true,'num':100,'name':'foo'}"; | ||
|
||
public String body() { | ||
return "body"; | ||
} | ||
|
||
public String headers() { | ||
return "headers"; | ||
} | ||
|
||
public String getJsonString() { | ||
return jsonString; | ||
} | ||
|
||
public void setJsonString(String jsonString) { | ||
this.jsonString = jsonString; | ||
} | ||
} |
156 changes: 156 additions & 0 deletions
156
.../opentelemetry/javaagent/instrumentation/hypertrace/struts/StrutsInstrumentationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright The Hypertrace Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.hypertrace.struts; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
import javax.servlet.DispatcherType; | ||
import okhttp3.MediaType; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
import okhttp3.Response; | ||
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.servlet.DefaultServlet; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.util.resource.FileResource; | ||
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; | ||
import org.hypertrace.agent.testing.AbstractInstrumenterTest; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class StrutsInstrumentationTest extends AbstractInstrumenterTest { | ||
|
||
private static final String REQUEST_BODY = "hello"; | ||
private static final String REQUEST_HEADER = "requestheader"; | ||
private static final String REQUEST_HEADER_VALUE = "requestvalue"; | ||
private static final String RESPONSE_HEADER = "headerName"; | ||
private static final String RESPONSE_HEADER_VALUE = "headerValue"; | ||
|
||
private static Server server = new Server(0); | ||
private static int serverPort; | ||
|
||
@BeforeAll | ||
public static void startServer() throws Exception { | ||
ServletContextHandler handler = new ServletContextHandler(); | ||
FileResource resource = new FileResource(StrutsInstrumentationTest.class.getResource("/")); | ||
handler.setContextPath("/context"); | ||
handler.setBaseResource(resource); | ||
handler.addServlet(DefaultServlet.class, "/"); | ||
handler.addFilter( | ||
StrutsPrepareAndExecuteFilter.class, "/*", EnumSet.allOf(DispatcherType.class)); | ||
server.setHandler(handler); | ||
server.start(); | ||
serverPort = server.getConnectors()[0].getLocalPort(); | ||
} | ||
|
||
@AfterAll | ||
public static void stopServer() throws Exception { | ||
server.stop(); | ||
} | ||
|
||
@Test | ||
public void postUrlEncoded() throws IOException, TimeoutException, InterruptedException { | ||
Request request = | ||
new Request.Builder() | ||
.url(String.format("http://localhost:%d/context/body", serverPort)) | ||
.post( | ||
RequestBody.create( | ||
REQUEST_BODY, MediaType.get("application/x-www-form-urlencoded"))) | ||
.build(); | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
TEST_WRITER.waitForTraces(1); | ||
List<List<SpanData>> traces = TEST_WRITER.getTraces(); | ||
assertEquals(1, traces.size()); | ||
List<SpanData> spans = traces.get(0); | ||
assertEquals(1, spans.size()); | ||
SpanData spanData = spans.get(0); | ||
assertEquals( | ||
"\"" + new Struts2Action().getJsonString() + "\"", | ||
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); | ||
assertEquals( | ||
REQUEST_BODY, spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); | ||
} | ||
|
||
@Test | ||
public void getHeaders() throws IOException, TimeoutException, InterruptedException { | ||
Request request = | ||
new Request.Builder() | ||
.url(String.format("http://localhost:%d/context/headers", serverPort)) | ||
.get() | ||
.header(REQUEST_HEADER, REQUEST_HEADER_VALUE) | ||
.build(); | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
assertEquals(200, response.code()); | ||
} | ||
|
||
TEST_WRITER.waitForTraces(1); | ||
List<List<SpanData>> traces = TEST_WRITER.getTraces(); | ||
assertEquals(1, traces.size()); | ||
List<SpanData> spans = traces.get(0); | ||
assertEquals(1, spans.size()); | ||
SpanData spanData = spans.get(0); | ||
assertEquals( | ||
RESPONSE_HEADER_VALUE, | ||
spanData | ||
.getAttributes() | ||
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER))); | ||
assertEquals( | ||
REQUEST_HEADER_VALUE, | ||
spanData | ||
.getAttributes() | ||
.get(HypertraceSemanticAttributes.httpRequestHeader(REQUEST_HEADER))); | ||
} | ||
|
||
@Test | ||
public void block() throws IOException, TimeoutException, InterruptedException { | ||
Request request = | ||
new Request.Builder() | ||
.url(String.format("http://localhost:%d/context/body", serverPort)) | ||
.get() | ||
.header("mockblock", "true") | ||
.build(); | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
Assertions.assertEquals(403, response.code()); | ||
} | ||
|
||
TEST_WRITER.waitForTraces(1); | ||
List<List<SpanData>> traces = TEST_WRITER.getTraces(); | ||
Assertions.assertEquals(1, traces.size()); | ||
List<SpanData> spans = traces.get(0); | ||
Assertions.assertEquals(1, spans.size()); | ||
SpanData spanData = spans.get(0); | ||
Assertions.assertNull( | ||
spanData | ||
.getAttributes() | ||
.get(HypertraceSemanticAttributes.httpResponseHeader(RESPONSE_HEADER))); | ||
Assertions.assertNull( | ||
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_REQUEST_BODY)); | ||
Assertions.assertNull( | ||
spanData.getAttributes().get(HypertraceSemanticAttributes.HTTP_RESPONSE_BODY)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE struts PUBLIC | ||
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" | ||
"http://struts.apache.org/dtds/struts-2.3.dtd"> | ||
|
||
<struts> | ||
|
||
<constant name="struts.enable.SlashesInActionNames" value="true"/> | ||
<constant name="struts.patternMatcher" value="namedVariable"/> | ||
|
||
<package name="basic-struts2" extends="struts-default"> | ||
<global-results> | ||
<result name="headers" type="httpheader" > | ||
<param name="headers.headerName">headerValue</param> | ||
</result> | ||
</global-results> | ||
<action name="headers" class="io.opentelemetry.javaagent.instrumentation.hypertrace.struts.Struts2Action" method="headers"/> | ||
</package> | ||
|
||
<package name="json-struts2" extends="json-default"> | ||
<action name="body" class="io.opentelemetry.javaagent.instrumentation.hypertrace.struts.Struts2Action" method="body"> | ||
<result name="body" type="json"> | ||
<param name="noCache">true</param> | ||
<param name="excludeNullProperties">true</param> | ||
<param name="root">jsonString</param> | ||
</result> | ||
</action> | ||
</package> | ||
|
||
</struts> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.