Skip to content

Commit f082dcb

Browse files
authored
Merge pull request #1236 from dropwizard/jdk9
Support metrics on JDK9
2 parents b04bf1e + 4637aef commit f082dcb

File tree

6 files changed

+131
-27
lines changed

6 files changed

+131
-27
lines changed

.travis.yml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,20 @@ install: echo "I trust Maven."
55
# don't just run the tests, also run error-prone
66
script: mvn verify
77

8-
jdk:
9-
- oraclejdk8
8+
matrix:
9+
include:
10+
- os: linux
11+
jdk: oraclejdk8
12+
addons:
13+
apt:
14+
packages:
15+
- oracle-java8-installer
16+
- os: linux
17+
jdk: oraclejdk9
18+
addons:
19+
apt:
20+
packages:
21+
- oracle-java9-installer
1022

1123
notifications:
1224
email:

metrics-core/src/test/java/com/codahale/metrics/ConsoleReporterTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ public class ConsoleReporterTest {
3535
.convertDurationsTo(TimeUnit.MILLISECONDS)
3636
.filter(MetricFilter.ALL)
3737
.build();
38+
private String dateHeader;
3839

3940
@Before
4041
public void setUp() throws Exception {
4142
when(clock.getTime()).thenReturn(1363568676000L);
43+
// JDK9 has changed the java.text.DateFormat API implementation according to Unicode.
44+
// See http://mail.openjdk.java.net/pipermail/jdk9-dev/2017-April/005732.html
45+
dateHeader = System.getProperty("java.version").startsWith("1.8") ?
46+
"3/17/13 6:04:36 PM =============================================================" :
47+
"3/17/13, 6:04:36 PM ============================================================";
4248
}
4349

4450
@Test
@@ -53,7 +59,7 @@ public void reportsGaugeValues() throws Exception {
5359

5460
assertThat(consoleOutput())
5561
.isEqualTo(lines(
56-
"3/17/13 6:04:36 PM =============================================================",
62+
dateHeader,
5763
"",
5864
"-- Gauges ----------------------------------------------------------------------",
5965
"gauge",
@@ -76,7 +82,7 @@ public void reportsCounterValues() throws Exception {
7682

7783
assertThat(consoleOutput())
7884
.isEqualTo(lines(
79-
"3/17/13 6:04:36 PM =============================================================",
85+
dateHeader,
8086
"",
8187
"-- Counters --------------------------------------------------------------------",
8288
"test.counter",
@@ -113,7 +119,7 @@ public void reportsHistogramValues() throws Exception {
113119

114120
assertThat(consoleOutput())
115121
.isEqualTo(lines(
116-
"3/17/13 6:04:36 PM =============================================================",
122+
dateHeader,
117123
"",
118124
"-- Histograms ------------------------------------------------------------------",
119125
"test.histogram",
@@ -150,7 +156,7 @@ public void reportsMeterValues() throws Exception {
150156

151157
assertThat(consoleOutput())
152158
.isEqualTo(lines(
153-
"3/17/13 6:04:36 PM =============================================================",
159+
dateHeader,
154160
"",
155161
"-- Meters ----------------------------------------------------------------------",
156162
"test.meter",
@@ -196,7 +202,7 @@ public void reportsTimerValues() throws Exception {
196202

197203
assertThat(consoleOutput())
198204
.isEqualTo(lines(
199-
"3/17/13 6:04:36 PM =============================================================",
205+
dateHeader,
200206
"",
201207
"-- Timers ----------------------------------------------------------------------",
202208
"test.another.timer",
@@ -250,7 +256,7 @@ public void reportMeterWithDisabledAttributes() throws Exception {
250256

251257
assertThat(consoleOutput())
252258
.isEqualTo(lines(
253-
"3/17/13 6:04:36 PM =============================================================",
259+
dateHeader,
254260
"",
255261
"-- Meters ----------------------------------------------------------------------",
256262
"test.meter",
@@ -306,7 +312,7 @@ public void reportTimerWithDisabledAttributes() throws Exception {
306312

307313
assertThat(consoleOutput())
308314
.isEqualTo(lines(
309-
"3/17/13 6:04:36 PM =============================================================",
315+
dateHeader,
310316
"",
311317
"-- Timers ----------------------------------------------------------------------",
312318
"test.another.timer",
@@ -366,7 +372,7 @@ public void reportHistogramWithDisabledAttributes() throws Exception {
366372

367373
assertThat(consoleOutput())
368374
.isEqualTo(lines(
369-
"3/17/13 6:04:36 PM =============================================================",
375+
dateHeader,
370376
"",
371377
"-- Histograms ------------------------------------------------------------------",
372378
"test.histogram",

metrics-jcache/pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@
1616
Uses the CacheStatisticsMXBean provided statistics.
1717
</description>
1818

19+
<profiles>
20+
<profile>
21+
<id>jdk9</id>
22+
<activation>
23+
<jdk>9</jdk>
24+
</activation>
25+
<build>
26+
<plugins>
27+
<plugin>
28+
<groupId>org.apache.maven.plugins</groupId>
29+
<artifactId>maven-compiler-plugin</artifactId>
30+
<version>3.7.0</version>
31+
<configuration>
32+
<source>9</source>
33+
<target>9</target>
34+
<forceJavacCompilerUse>true</forceJavacCompilerUse>
35+
<showWarnings>true</showWarnings>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-surefire-plugin</artifactId>
41+
<configuration>
42+
<argLine>--add-modules java.xml.bind</argLine>
43+
</configuration>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
</profile>
48+
</profiles>
49+
1950
<dependencies>
2051
<dependency>
2152
<groupId>io.dropwizard.metrics</groupId>

metrics-jvm/src/main/java/com/codahale/metrics/jvm/FileDescriptorRatioGauge.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package com.codahale.metrics.jvm;
22

33
import com.codahale.metrics.RatioGauge;
4+
import com.sun.management.UnixOperatingSystemMXBean;
45

56
import java.lang.management.ManagementFactory;
67
import java.lang.management.OperatingSystemMXBean;
7-
import java.lang.reflect.InvocationTargetException;
8-
import java.lang.reflect.Method;
98

109
/**
1110
* A gauge for the ratio of used to total file descriptors.
@@ -31,17 +30,11 @@ public FileDescriptorRatioGauge(OperatingSystemMXBean os) {
3130

3231
@Override
3332
protected Ratio getRatio() {
34-
try {
35-
return Ratio.of(invoke("getOpenFileDescriptorCount"),
36-
invoke("getMaxFileDescriptorCount"));
37-
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
33+
if (os instanceof UnixOperatingSystemMXBean) {
34+
final UnixOperatingSystemMXBean unixOs = (UnixOperatingSystemMXBean) os;
35+
return Ratio.of(unixOs.getOpenFileDescriptorCount(), unixOs.getMaxFileDescriptorCount());
36+
} else {
3837
return Ratio.of(Double.NaN, Double.NaN);
3938
}
4039
}
41-
42-
private long invoke(String name) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
43-
final Method method = os.getClass().getDeclaredMethod(name);
44-
method.setAccessible(true);
45-
return (Long) method.invoke(os);
46-
}
4740
}

metrics-jvm/src/test/java/com/codahale/metrics/jvm/FileDescriptorRatioGaugeTest.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codahale.metrics.jvm;
22

3+
import com.sun.management.UnixOperatingSystemMXBean;
34
import org.junit.Test;
45

56
import javax.management.ObjectName;
@@ -12,7 +13,7 @@
1213

1314
@SuppressWarnings("UnusedDeclaration")
1415
public class FileDescriptorRatioGaugeTest {
15-
private final OperatingSystemMXBean os = new OperatingSystemMXBean() {
16+
private final OperatingSystemMXBean os = new UnixOperatingSystemMXBean() {
1617
@Override
1718
public String getName() {
1819
return null;
@@ -38,16 +39,56 @@ public double getSystemLoadAverage() {
3839
return 0;
3940
}
4041

41-
// these duplicate methods from UnixOperatingSystem
42-
43-
private long getOpenFileDescriptorCount() {
42+
@Override
43+
public long getOpenFileDescriptorCount() {
4444
return 10;
4545
}
4646

47-
private long getMaxFileDescriptorCount() {
47+
@Override
48+
public long getMaxFileDescriptorCount() {
4849
return 100;
4950
}
5051

52+
@Override
53+
public long getCommittedVirtualMemorySize() {
54+
return 0;
55+
}
56+
57+
@Override
58+
public long getTotalSwapSpaceSize() {
59+
return 0;
60+
}
61+
62+
@Override
63+
public long getFreeSwapSpaceSize() {
64+
return 0;
65+
}
66+
67+
@Override
68+
public long getProcessCpuTime() {
69+
return 0;
70+
}
71+
72+
@Override
73+
public long getFreePhysicalMemorySize() {
74+
return 0;
75+
}
76+
77+
@Override
78+
public long getTotalPhysicalMemorySize() {
79+
return 0;
80+
}
81+
82+
@Override
83+
public double getSystemCpuLoad() {
84+
return 0;
85+
}
86+
87+
@Override
88+
public double getProcessCpuLoad() {
89+
return 0;
90+
}
91+
5192
// overridden on Java 1.7; random crap on Java 1.6
5293
@Override
5394
public ObjectName getObjectName() {

pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,27 @@
163163
</dependencies>
164164

165165
<profiles>
166+
<profile>
167+
<id>jdk9</id>
168+
<activation>
169+
<jdk>9</jdk>
170+
</activation>
171+
<build>
172+
<plugins>
173+
<plugin>
174+
<groupId>org.apache.maven.plugins</groupId>
175+
<artifactId>maven-compiler-plugin</artifactId>
176+
<version>3.7.0</version>
177+
<configuration>
178+
<source>9</source>
179+
<target>9</target>
180+
<forceJavacCompilerUse>true</forceJavacCompilerUse>
181+
<showWarnings>true</showWarnings>
182+
</configuration>
183+
</plugin>
184+
</plugins>
185+
</build>
186+
</profile>
166187
<profile>
167188
<id>release-sign-artifacts</id>
168189
<activation>

0 commit comments

Comments
 (0)