Skip to content

Commit 48585cb

Browse files
authored
Merge pull request #3 from mweirauch/process-threads
Meter for process threads
2 parents cbfd1cc + f525968 commit 48585cb

File tree

5 files changed

+306
-0
lines changed

5 files changed

+306
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright © 2017 Michael Weirauch ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.mweirauch.micrometer.jvm.extras;
17+
18+
import java.util.Collections;
19+
import java.util.Objects;
20+
21+
import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsStatus;
22+
import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsStatus.KEY;
23+
import io.micrometer.core.instrument.Meter.Id;
24+
import io.micrometer.core.instrument.MeterRegistry;
25+
import io.micrometer.core.instrument.binder.MeterBinder;
26+
27+
public class ProcessThreadMetrics implements MeterBinder {
28+
29+
private final ProcfsStatus status;
30+
31+
public ProcessThreadMetrics() {
32+
this.status = new ProcfsStatus();
33+
}
34+
35+
/* default */ ProcessThreadMetrics(ProcfsStatus status) {
36+
this.status = Objects.requireNonNull(status);
37+
}
38+
39+
@Override
40+
public void bindTo(MeterRegistry registry) {
41+
final Id meterId = registry.createId("process.threads", Collections.emptyList(),
42+
"The number of process threads");
43+
registry.gauge(meterId, status, statusRef -> value(KEY.THREADS));
44+
}
45+
46+
private Double value(KEY key) {
47+
return status.get(key);
48+
}
49+
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright © 2017 Michael Weirauch ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.mweirauch.micrometer.jvm.extras.procfs;
17+
18+
import java.util.Collection;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Objects;
22+
23+
public class ProcfsStatus extends ProcfsEntry {
24+
25+
public enum KEY implements ValueKey {
26+
/**
27+
* Threads
28+
*/
29+
THREADS
30+
}
31+
32+
public ProcfsStatus() {
33+
super(ProcfsReader.getInstance("status"));
34+
}
35+
36+
/* default */ ProcfsStatus(ProcfsReader reader) {
37+
super(reader);
38+
}
39+
40+
@Override
41+
protected Map<ValueKey, Double> handle(Collection<String> lines) {
42+
Objects.requireNonNull(lines);
43+
44+
final Map<ValueKey, Double> values = new HashMap<>();
45+
46+
for (final String line : lines) {
47+
if (line.startsWith("Threads:")) {
48+
values.put(KEY.THREADS, parseValue(line));
49+
}
50+
}
51+
52+
return values;
53+
}
54+
55+
private static Double parseValue(String line) {
56+
Objects.requireNonNull(line);
57+
58+
return Double.parseDouble(line.split("\\s+")[1]);
59+
}
60+
61+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright © 2017 Michael Weirauch ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.mweirauch.micrometer.jvm.extras;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
22+
import org.junit.Test;
23+
24+
import com.google.common.testing.NullPointerTester;
25+
import com.google.common.testing.NullPointerTester.Visibility;
26+
27+
import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsStatus;
28+
import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsStatus.KEY;
29+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
30+
31+
public class ProcessThreadMetricsTest {
32+
33+
private final ProcfsStatus status = mock(ProcfsStatus.class);
34+
35+
@Test
36+
public void testNullContract() {
37+
final ProcessThreadMetrics uut = new ProcessThreadMetrics(status);
38+
39+
final NullPointerTester npt = new NullPointerTester();
40+
41+
npt.testConstructors(uut.getClass(), Visibility.PACKAGE);
42+
npt.testStaticMethods(uut.getClass(), Visibility.PACKAGE);
43+
npt.testInstanceMethods(uut, Visibility.PACKAGE);
44+
}
45+
46+
@SuppressWarnings("unused")
47+
@Test
48+
public void testInstantiation() {
49+
new ProcessThreadMetrics();
50+
}
51+
52+
@Test
53+
public void testGetMetrics() throws Exception {
54+
when(status.get(KEY.THREADS)).thenReturn(7D);
55+
56+
final SimpleMeterRegistry registry = new SimpleMeterRegistry();
57+
final ProcessThreadMetrics uut = new ProcessThreadMetrics(status);
58+
59+
uut.bindTo(registry);
60+
61+
assertEquals(7D, registry.find("process.threads").gauge().get().value(), 0.0);
62+
}
63+
64+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright © 2017 Michael Weirauch ([email protected])
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of 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,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.github.mweirauch.micrometer.jvm.extras.procfs;
17+
18+
import static org.junit.Assert.assertEquals;
19+
import static org.mockito.Mockito.mock;
20+
import static org.mockito.Mockito.when;
21+
22+
import java.io.IOException;
23+
import java.net.URISyntaxException;
24+
import java.nio.file.Path;
25+
import java.nio.file.Paths;
26+
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
import com.google.common.testing.NullPointerTester;
31+
import com.google.common.testing.NullPointerTester.Visibility;
32+
33+
import io.github.mweirauch.micrometer.jvm.extras.procfs.ProcfsStatus.KEY;
34+
35+
public class ProcfsStatusTest {
36+
37+
private static Path BASE;
38+
39+
@BeforeClass
40+
public static void beforeClass() throws URISyntaxException {
41+
BASE = Paths.get(Class.class.getResource("/procfs/").toURI());
42+
}
43+
44+
@Test
45+
public void testNullContract() {
46+
final ProcfsStatus uut = new ProcfsStatus(mock(ProcfsReader.class));
47+
48+
final NullPointerTester npt = new NullPointerTester();
49+
50+
npt.testConstructors(uut.getClass(), Visibility.PACKAGE);
51+
npt.testStaticMethods(uut.getClass(), Visibility.PACKAGE);
52+
npt.testInstanceMethods(uut, Visibility.PACKAGE);
53+
}
54+
55+
@SuppressWarnings("unused")
56+
@Test
57+
public void testInstantiation() {
58+
new ProcfsStatus();
59+
}
60+
61+
@Test
62+
public void testSimple() {
63+
final ProcfsStatus uut = new ProcfsStatus(new ProcfsReader(BASE, "status-001.txt"));
64+
65+
assertEquals(Double.valueOf(55), uut.get(KEY.THREADS));
66+
}
67+
68+
@Test
69+
public void testReturnDefaultValuesOnReaderFailure() throws IOException {
70+
final ProcfsReader reader = mock(ProcfsReader.class);
71+
when(reader.read()).thenThrow(new IOException("THROW"));
72+
73+
final ProcfsStatus uut = new ProcfsStatus(reader);
74+
75+
assertEquals(Double.valueOf(-1), uut.get(KEY.THREADS));
76+
}
77+
78+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Name: java
2+
Umask: 0002
3+
State: S (sleeping)
4+
Tgid: 4866
5+
Ngid: 0
6+
Pid: 4866
7+
PPid: 4827
8+
TracerPid: 0
9+
Uid: 1000 1000 1000 1000
10+
Gid: 1000 1000 1000 1000
11+
FDSize: 512
12+
Groups: 10 26 977 1000 1001
13+
NStgid: 4866
14+
NSpid: 4866
15+
NSpgid: 2996
16+
NSsid: 2996
17+
VmPeak: 8490668 kB
18+
VmSize: 8474900 kB
19+
VmLck: 0 kB
20+
VmPin: 0 kB
21+
VmHWM: 1024236 kB
22+
VmRSS: 1007304 kB
23+
RssAnon: 934092 kB
24+
RssFile: 71964 kB
25+
RssShmem: 1248 kB
26+
VmData: 1394448 kB
27+
VmStk: 132 kB
28+
VmExe: 4 kB
29+
VmLib: 123524 kB
30+
VmPTE: 3240 kB
31+
VmPMD: 44 kB
32+
VmSwap: 0 kB
33+
HugetlbPages: 0 kB
34+
Threads: 55
35+
SigQ: 0/61744
36+
SigPnd: 0000000000000000
37+
ShdPnd: 0000000000000000
38+
SigBlk: 0000000000000000
39+
SigIgn: 0000000000001000
40+
SigCgt: 2000000181004ccf
41+
CapInh: 0000000000000000
42+
CapPrm: 0000000000000000
43+
CapEff: 0000000000000000
44+
CapBnd: 0000003fffffffff
45+
CapAmb: 0000000000000000
46+
NoNewPrivs: 0
47+
Seccomp: 0
48+
Cpus_allowed: ff
49+
Cpus_allowed_list: 0-7
50+
Mems_allowed: 00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000001
51+
Mems_allowed_list: 0
52+
voluntary_ctxt_switches: 4
53+
nonvoluntary_ctxt_switches: 1

0 commit comments

Comments
 (0)