Skip to content

Commit c5ff1a7

Browse files
author
robbins
committed
Merge branch 'master' of https://github.com/RuntimeTools/javametrics.git into github/master
2 parents 7e0651f + 458fd3d commit c5ff1a7

File tree

9 files changed

+960
-26
lines changed

9 files changed

+960
-26
lines changed

java/.classpath

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
</classpathentry>
1010
<classpathentry kind="lib" path="lib/asm/asm-5.0.4.jar"/>
1111
<classpathentry kind="lib" path="lib/asm/asm-commons-5.0.4.jar"/>
12-
<classpathentry kind="src" path="test"/>
12+
<classpathentry kind="src" output="bintest" path="test"/>
1313
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
14+
<classpathentry kind="con" path="org.eclipse.jst.server.core.container/com.ibm.ws.st.core.runtimeClasspathProvider/WebSphere Application Server Liberty"/>
1415
<classpathentry kind="output" path="bin"/>
1516
</classpath>

java/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@
22
/.apt_generated/
33
/lib/javametrics.jar
44
/build/
5+
/bintest/
6+
/TEST-com.ibm.javametrics.JavametricsTest.xml
7+
/TEST-com.ibm.javametrics.TopicTest.xml
8+
/TEST-com.ibm.javametrics.dataproviders.CPUDataProviderTest.xml
9+
/TEST-com.ibm.javametrics.dataproviders.GCDataProviderTest.xml
10+
/TEST-com.ibm.javametrics.dataproviders.MBeanDataProviderTest.xml
11+
/TEST-com.ibm.javametrics.dataproviders.MemoryPoolDataProviderTest.xml
12+
/TEST-com.ibm.javametrics.instrument.InstrumentTest.xml

java/build.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ limitations under the License.
2020
<property name="lib.dir" value="${basedir}/build/lib" />
2121
<property name="java.dir" value="${basedir}/src" />
2222
<property name="classes.dir" value="${basedir}/build/classes" />
23+
<property name="test.dir" value="${basedir}/test" />
24+
<property name="test.classes.dir" value="${basedir}/build/testclasses" />
25+
<property name="report.dir" value="${basedir}/build/testreport" />
26+
<property name="liberty.dir" value="../../wlp/dev/api" />
27+
<property name="junit.dir" value="${basedir}/lib/junit" />
2328

2429

2530
<target name="compile.java">
@@ -57,6 +62,44 @@ limitations under the License.
5762
</zip>
5863
</target>
5964

65+
<target name="compile.java.tests" depends="compile.java" description="compile jUnit Test cases ">
66+
<mkdir dir="${test.classes.dir}" />
67+
<javac srcdir="${test.dir}" destdir="${test.classes.dir}" includeantruntime="false" debug="on">
68+
<classpath>
69+
<path location="${classes.dir}" />
70+
<fileset dir="${liberty.dir}">
71+
<include name="**/*.jar" />
72+
</fileset>
73+
<fileset dir="${basedir}/lib/asm/">
74+
<include name="**/*.jar" />
75+
</fileset>
76+
<fileset dir="${junit.dir}">
77+
<include name="*.jar" />
78+
</fileset>
79+
</classpath>
80+
</javac>
81+
</target>
82+
83+
<target name="test" depends="build.lib, compile.java.tests">
84+
<junit printsummary="yes" haltonfailure="no" showoutput="yes" fork="yes" forkmode="once">
85+
<jvmarg value="-javaagent:${lib.dir}/javametrics.jar" />
86+
<classpath>
87+
<fileset dir="${junit.dir}">
88+
<include name="*.jar" />
89+
</fileset>
90+
<fileset dir="${liberty.dir}">
91+
<include name="**/*.jar" />
92+
</fileset>
93+
<path location="${test.classes.dir}" />
94+
</classpath>
95+
<formatter type="plain" usefile="false" />
96+
<formatter type="xml" />
97+
<batchtest>
98+
<fileset dir="${test.classes.dir}" includes="**/*Test.class" />
99+
</batchtest>
100+
</junit>
101+
</target>
102+
60103
<target name="clean">
61104
<delete dir="${output.dir}"/>
62105
</target>

java/src/com/ibm/javametrics/instrument/ServletCallback.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public class ServletCallback {
3737
private static final String GET_ATTRIBUTE = "getAttribute";
3838
private static final String SET_ATTRIBUTE = "setAttribute";
3939
private static final Object TRACKER_ATTRIBUTE = "com.ibm.javametrics.tracker";
40+
41+
/*
42+
* Set to true to collect detailed Request/Response information
43+
*/
44+
private static boolean detailed = false;
4045

4146
/**
4247
* Called on method entry for HTTP/JSP requests public static void
@@ -129,37 +134,39 @@ public static void after(Object request, Object response) {
129134
Method getRequestURL = reqClass.getMethod(GET_REQUEST_URL);
130135
data.setUrl(((StringBuffer) getRequestURL.invoke(request)).toString());
131136

132-
Method getMethod = reqClass.getMethod(GET_METHOD);
133-
data.setMethod((String) getMethod.invoke(request));
134-
135-
Method getContentType = respClass.getMethod(GET_CONTENT_TYPE);
136-
data.setContentType((String) getContentType.invoke(response));
137-
138-
Method getHeaders = respClass.getMethod(GET_HEADER_NAMES);
139-
Method getHeader = respClass.getMethod(GET_HEADER, String.class);
140-
Collection<String> headers = (Collection<String>) getHeaders.invoke(response);
141-
if (headers != null) {
142-
for (String headerName : headers) {
143-
String header = (String) getHeader.invoke(response, headerName);
144-
if (header != null) {
145-
data.addHeader(headerName, header);
137+
if (detailed ) {
138+
Method getMethod = reqClass.getMethod(GET_METHOD);
139+
data.setMethod((String) getMethod.invoke(request));
140+
141+
Method getContentType = respClass.getMethod(GET_CONTENT_TYPE);
142+
data.setContentType((String) getContentType.invoke(response));
143+
144+
Method getHeaders = respClass.getMethod(GET_HEADER_NAMES);
145+
Method getHeader = respClass.getMethod(GET_HEADER, String.class);
146+
Collection<String> headers = (Collection<String>) getHeaders.invoke(response);
147+
if (headers != null) {
148+
for (String headerName : headers) {
149+
String header = (String) getHeader.invoke(response, headerName);
150+
if (header != null) {
151+
data.addHeader(headerName, header);
152+
}
146153
}
147154
}
148-
}
149155

150-
Method getReqHeaders = reqClass.getMethod(GET_HEADER_NAMES);
151-
Method getReqHeader = reqClass.getMethod(GET_HEADER, String.class);
152-
Enumeration<String> reqHeaders = (Enumeration<String>) getReqHeaders.invoke(request);
153-
if (reqHeaders != null) {
154-
while (reqHeaders.hasMoreElements()) {
155-
String headerName = reqHeaders.nextElement();
156-
String header = (String) getReqHeader.invoke(request, headerName);
157-
if (header != null) {
158-
data.addRequestHeader(headerName, header);
156+
Method getReqHeaders = reqClass.getMethod(GET_HEADER_NAMES);
157+
Method getReqHeader = reqClass.getMethod(GET_HEADER, String.class);
158+
Enumeration<String> reqHeaders = (Enumeration<String>) getReqHeaders.invoke(request);
159+
if (reqHeaders != null) {
160+
while (reqHeaders.hasMoreElements()) {
161+
String headerName = reqHeaders.nextElement();
162+
String header = (String) getReqHeader.invoke(request, headerName);
163+
if (header != null) {
164+
data.addRequestHeader(headerName, header);
165+
}
159166
}
160167
}
161168
}
162-
169+
163170
data.setDuration(System.currentTimeMillis() - tracker.getRequestTime());
164171

165172
if (Agent.debug) {
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*******************************************************************************
2+
* Copyright 2017 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
******************************************************************************/
16+
17+
package com.ibm.javametrics.instrument;
18+
19+
import static org.junit.Assert.assertTrue;
20+
21+
import java.io.IOException;
22+
import java.util.ArrayList;
23+
import java.util.Iterator;
24+
import java.util.List;
25+
import java.util.concurrent.TimeUnit;
26+
27+
import javax.servlet.ServletException;
28+
29+
import org.junit.Before;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
33+
import com.ibm.javametrics.Javametrics;
34+
import com.ibm.javametrics.JavametricsListener;
35+
import com.ibm.javametrics.TestUtils;
36+
37+
/**
38+
* Test for com.ibm.javametrics.instrument classes
39+
*/
40+
public class InstrumentTest {
41+
42+
private static final List<String> received = new ArrayList<String>();
43+
private static Javametrics javametrics = Javametrics.getInstance();
44+
45+
@BeforeClass
46+
public static void setUpBeforeClass() throws Exception {
47+
48+
javametrics.addListener(new JavametricsListener() {
49+
50+
@Override
51+
public void receive(String pluginName, String data) {
52+
List<String> events = TestUtils.splitIntoJSONObjects(data);
53+
received.addAll(events);
54+
}
55+
});
56+
57+
// Initial setup - turn off other types of data
58+
javametrics.getTopic("gc").disable();
59+
javametrics.getTopic("memoryPools").disable();
60+
javametrics.getTopic("cpu").disable();
61+
// Wait 3 seconds to make sure all events already sent have come
62+
// through.
63+
try {
64+
TimeUnit.SECONDS.sleep(3);
65+
} catch (InterruptedException e) {
66+
e.printStackTrace();
67+
}
68+
received.clear();
69+
}
70+
71+
/**
72+
* @throws java.lang.Exception
73+
*/
74+
@Before
75+
public void setUp() throws Exception {
76+
}
77+
78+
@Test
79+
public void testInstrumentHttpJspPage() {
80+
81+
try {
82+
new MockHttpJspPage()._jspService(new MockHttpServletRequest("http://testURL"),
83+
new MockHttpServletResponse());
84+
} catch (ServletException | IOException e) {
85+
e.printStackTrace();
86+
}
87+
// Wait 3 seconds for data to come through
88+
try {
89+
TimeUnit.SECONDS.sleep(3);
90+
} catch (InterruptedException e) {
91+
e.printStackTrace();
92+
}
93+
boolean foundHttp = false;
94+
for (Iterator<String> iterator = received.iterator(); iterator.hasNext();) {
95+
String message = iterator.next();
96+
if (message.startsWith("{\"topic\": \"http")) {
97+
foundHttp = true;
98+
assertTrue("Test URL should be in message " + message,
99+
message.indexOf("\"url\":\"http://testURL\"") != -1);
100+
}
101+
}
102+
assertTrue("Should be emitting Http data", foundHttp);
103+
}
104+
105+
@Test
106+
public void testInstrumentServlet() {
107+
try {
108+
MockServlet servlet = new MockServlet();
109+
servlet.service(new MockHttpServletRequest("http://testURL/service"),
110+
new MockHttpServletResponse());
111+
servlet.doGet(new MockHttpServletRequest("http://testURL/doGet"),
112+
new MockHttpServletResponse());
113+
servlet.doPost(new MockHttpServletRequest("http://testURL/doPost"),
114+
new MockHttpServletResponse());
115+
} catch (ServletException e) {
116+
e.printStackTrace();
117+
} catch (IOException e) {
118+
e.printStackTrace();
119+
}
120+
// Wait 3 seconds for data to come through
121+
try {
122+
TimeUnit.SECONDS.sleep(3);
123+
} catch (InterruptedException e) {
124+
e.printStackTrace();
125+
}
126+
boolean instrumentedService = false;
127+
boolean instrumentedDoGet = false;
128+
boolean instrumentedDoPost = false;
129+
for (Iterator<String> iterator = received.iterator(); iterator.hasNext();) {
130+
String message = iterator.next();
131+
if (message.startsWith("{\"topic\": \"http")) {
132+
if(message.indexOf("\"url\":\"http://testURL/service\"") != -1) {
133+
instrumentedService = true;
134+
} else if(message.indexOf("\"url\":\"http://testURL/doGet\"") != -1) {
135+
instrumentedDoGet = true;
136+
} else if(message.indexOf("\"url\":\"http://testURL/doPost\"") != -1) {
137+
instrumentedDoPost = true;
138+
}
139+
}
140+
}
141+
assertTrue("Should be instrumenting HttpServlet.service", instrumentedService);
142+
assertTrue("Should be instrumenting HttpServlet.doGet", instrumentedDoGet);
143+
assertTrue("Should be instrumenting HttpServlet.doPost", instrumentedDoPost);
144+
}
145+
146+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*******************************************************************************
2+
* Copyright 2017 IBM Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
******************************************************************************/
16+
17+
package com.ibm.javametrics.instrument;
18+
19+
import java.io.IOException;
20+
21+
import javax.servlet.ServletConfig;
22+
import javax.servlet.ServletException;
23+
import javax.servlet.ServletRequest;
24+
import javax.servlet.ServletResponse;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
import javax.servlet.jsp.HttpJspPage;
28+
29+
/**
30+
* Mock implementation of HttpJspPage for test purposes
31+
*
32+
*/
33+
public class MockHttpJspPage implements HttpJspPage {
34+
35+
public MockHttpJspPage() {
36+
}
37+
38+
@Override
39+
public void jspDestroy() {
40+
}
41+
42+
@Override
43+
public void jspInit() {
44+
}
45+
46+
@Override
47+
public void destroy() {
48+
49+
}
50+
51+
@Override
52+
public ServletConfig getServletConfig() {
53+
return null;
54+
}
55+
56+
@Override
57+
public String getServletInfo() {
58+
return null;
59+
}
60+
61+
@Override
62+
public void init(ServletConfig arg0) throws ServletException {
63+
}
64+
65+
@Override
66+
public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
67+
68+
}
69+
70+
@Override
71+
public void _jspService(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
72+
73+
}
74+
75+
}

0 commit comments

Comments
 (0)