Skip to content

Commit e8cba52

Browse files
authored
add linux memory metrics (#52)
* add linux memory metrics #50 * lookoutAuotConfiguration get extensions from beanFactory;(企业版 lookoutAuotConfiguration 会很早从beanfactory中获取,当时可能还没注入这些扩展组件)
1 parent 0fb218d commit e8cba52

File tree

27 files changed

+311
-41
lines changed

27 files changed

+311
-41
lines changed

client/lookout-api/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.alipay.sofa.lookout</groupId>
77
<artifactId>lookout-client-parent</artifactId>
8-
<version>1.5.2</version>
8+
<version>1.5.3</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

client/lookout-client/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.2</version>
9+
<version>1.5.3</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-common/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.2</version>
9+
<version>1.5.3</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.2</version>
9+
<version>1.5.3</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-ext-jvm/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.alipay.sofa.lookout</groupId>
88
<artifactId>lookout-client-parent</artifactId>
9-
<version>1.5.2</version>
9+
<version>1.5.3</version>
1010
<relativePath>../pom.xml</relativePath>
1111
</parent>
1212

client/lookout-ext-os/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.alipay.sofa.lookout</groupId>
77
<artifactId>lookout-client-parent</artifactId>
8-
<version>1.5.2</version>
8+
<version>1.5.3</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
1111

client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/CpuUsageMetricsImporter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ public CpuUsageMetricsImporter() {
7070
}
7171

7272
/**
73-
* @param filePath
74-
* @param timeout
75-
* @param timeoutUnit
73+
* @param filePath file path
74+
* @param timeout timeout
75+
* @param timeoutUnit timeout unit
7676
*/
7777
public CpuUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
7878
super(timeout, timeoutUnit);

client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/DiskUsageMetricsImporter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public DiskUsageMetricsImporter() {
6565

6666
/**
6767
*
68-
* @param filePath
69-
* @param timeout
70-
* @param timeoutUnit
68+
* @param filePath filePath
69+
* @param timeout timeout
70+
* @param timeoutUnit timeoutUnit
7171
*/
7272
public DiskUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
7373
super(timeout, timeoutUnit);

client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/IOStatsMetricsImporter.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public IOStatsMetricsImporter() {
7575
}
7676

7777
/**
78-
* @param filePath
79-
* @param upTimeFilePath
80-
* @param timeout
81-
* @param timeoutUnit
78+
* @param filePath filePath
79+
* @param upTimeFilePath upTimeFilePath
80+
* @param timeout timeout
81+
* @param timeoutUnit timeoutUnit
8282
*/
8383
public IOStatsMetricsImporter(String filePath, String upTimeFilePath, long timeout,
8484
TimeUnit timeoutUnit) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.lookout.os.linux;
18+
19+
import com.alipay.lookout.api.Gauge;
20+
import com.alipay.lookout.api.Id;
21+
import com.alipay.lookout.api.Registry;
22+
import com.alipay.lookout.api.composite.MixinMetric;
23+
import com.alipay.lookout.common.log.LookoutLoggerFactory;
24+
import com.alipay.lookout.os.CachedMetricsImporter;
25+
import com.alipay.lookout.os.utils.FileUtils;
26+
import org.slf4j.Logger;
27+
28+
import java.io.File;
29+
import java.util.List;
30+
import java.util.concurrent.TimeUnit;
31+
32+
/**
33+
* resolve memory metrics from "/proc/meminfo"
34+
*
35+
* @author: [email protected]
36+
* 2019-03-12 17:30
37+
**/
38+
public class MemoryStatsMetricsImporter extends CachedMetricsImporter {
39+
40+
private final Logger logger = LookoutLoggerFactory
41+
.getLogger(DiskUsageMetricsImporter.class);
42+
43+
private static final String DEFAULT_FILE_PATH = "/proc/meminfo";
44+
private static final String SPLIT = "\\s+";
45+
private String filePath;
46+
private Memstats memstats;
47+
48+
/**
49+
* default constructor
50+
*/
51+
public MemoryStatsMetricsImporter() {
52+
this(DEFAULT_FILE_PATH, DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
53+
}
54+
55+
/**
56+
* @param filePath filePath
57+
* @param timeout timeout
58+
* @param timeoutUnit timeoutUnit
59+
*/
60+
public MemoryStatsMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
61+
super(timeout, timeoutUnit);
62+
this.filePath = filePath;
63+
memstats = new Memstats();
64+
disable = !new File(filePath).exists();
65+
if (!disable)
66+
loadIfNessesary();
67+
}
68+
69+
@Override
70+
protected void doRegister(Registry registry) {
71+
Id id = registry.createId("os.memory.stats");
72+
MixinMetric mixin = registry.mixinMetric(id);
73+
74+
mixin.gauge("total.bytes", new Gauge<Long>() {
75+
@Override
76+
public Long value() {
77+
loadIfNessesary();
78+
return memstats.memTotal;
79+
}
80+
});
81+
82+
mixin.gauge("free.bytes", new Gauge<Long>() {
83+
@Override
84+
public Long value() {
85+
loadIfNessesary();
86+
return memstats.memFree;
87+
}
88+
});
89+
mixin.gauge("buffers.bytes", new Gauge<Long>() {
90+
@Override
91+
public Long value() {
92+
loadIfNessesary();
93+
return memstats.buffers;
94+
}
95+
});
96+
mixin.gauge("cached.bytes", new Gauge<Long>() {
97+
@Override
98+
public Long value() {
99+
loadIfNessesary();
100+
return memstats.cached;
101+
}
102+
});
103+
104+
}
105+
106+
@Override
107+
protected void loadValues() {
108+
try {
109+
List<String> lines = FileUtils.readFileAsStringArray(filePath);
110+
Memstats ms = new Memstats();
111+
int i = 0;
112+
for (String line : lines) {
113+
if (i == 4)
114+
break;
115+
String[] infos = line.split(SPLIT);
116+
if (infos == null) {
117+
continue;
118+
}
119+
if ("MemTotal:".equals(infos[0])) {
120+
ms.memTotal = Long.parseLong(infos[1]) * 1024;
121+
i++;
122+
continue;
123+
}
124+
if ("MemFree:".equals(infos[0])) {
125+
ms.memFree = Long.parseLong(infos[1]) * 1024;
126+
i++;
127+
continue;
128+
}
129+
if ("Buffers:".equals(infos[0])) {
130+
ms.buffers = Long.parseLong(infos[1]) * 1024;
131+
i++;
132+
continue;
133+
}
134+
if ("Cached:".equals(infos[0])) {
135+
ms.cached = Long.parseLong(infos[1]) * 1024;
136+
i++;
137+
continue;
138+
}
139+
}
140+
memstats = ms;
141+
} catch (Exception e) {
142+
logger.debug("warning,can't parse line at /proc/meminfo", e.getMessage());
143+
}
144+
}
145+
146+
private class Memstats {
147+
long memTotal;
148+
long memFree;
149+
long cached;
150+
long buffers;
151+
}
152+
}

client/lookout-ext-os/src/main/java/com/alipay/lookout/os/linux/SystemLoadMetricsImporter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public SystemLoadMetricsImporter() {
6161

6262
/**
6363
*
64-
* @param filePath
65-
* @param timeout
66-
* @param timeoutUnit
64+
* @param filePath filePath
65+
* @param timeout timeout
66+
* @param timeoutUnit timeoutUnit
6767
*/
6868
public SystemLoadMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
6969
super(timeout, timeoutUnit);

client/lookout-ext-os/src/main/java/com/alipay/lookout/os/utils/FileUtils.java

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class FileUtils {
3333
*
3434
* @param path the path to file
3535
* @return the file content as string
36+
* @throws IOException IOException
3637
*/
3738
public static String readFile(String path) throws IOException {
3839
BufferedReader reader = null;
@@ -60,6 +61,7 @@ public static String readFile(String path) throws IOException {
6061
*
6162
* @param path the path to file
6263
* @return the file content as string list
64+
* @throws IOException IOException
6365
*/
6466
public static List<String> readFileAsStringArray(String path) throws IOException {
6567
BufferedReader reader = null;

client/lookout-ext-os/src/main/java/com/alipay/lookout/os/utils/NumFormatUtils.java

+4
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,17 @@
2424
public class NumFormatUtils {
2525
/**
2626
* 保留两位小数
27+
* @param data data
28+
* @return float
2729
*/
2830
public static float formatFloat(float data) {
2931
return (float) (Math.round(data * 100) / 100.0);
3032
}
3133

3234
/**
3335
* 保留两位小数
36+
* @param usage usage
37+
* @return float
3438
*/
3539
public static float formatFloat(String usage) {
3640
return (float) (Math.round(Float.parseFloat(usage) * 100) / 100.0);

client/lookout-ext-os/src/main/resources/META-INF/services/com.alipay.lookout.spi.MetricsImporter

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ com.alipay.lookout.os.linux.DiskUsageMetricsImporter
33
com.alipay.lookout.os.linux.IOStatsMetricsImporter
44
com.alipay.lookout.os.linux.NetTrafficMetricsImporter
55
com.alipay.lookout.os.linux.SystemLoadMetricsImporter
6+
com.alipay.lookout.os.linux.MemoryStatsMetricsImporter
67

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.lookout.os.linux;
18+
19+
import com.alipay.lookout.api.Measurement;
20+
import com.alipay.lookout.api.Metric;
21+
import com.alipay.lookout.api.Registry;
22+
import com.alipay.lookout.core.DefaultRegistry;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
import java.util.Collection;
27+
import java.util.Iterator;
28+
import java.util.concurrent.TimeUnit;
29+
30+
/**
31+
* @author: [email protected]
32+
* @create: 2019-03-12 21:40
33+
**/
34+
public class MemoryStatsMetricsImporterTest {
35+
36+
@Test
37+
public void test() {
38+
Registry registry = new DefaultRegistry();
39+
MemoryStatsMetricsImporter memoryStatsMetricsImporter = new MemoryStatsMetricsImporter(
40+
"src/test/resources/proc_meminfo", 1000, TimeUnit.MILLISECONDS);
41+
memoryStatsMetricsImporter.register(registry);
42+
Iterator<Metric> iterator = registry.iterator();
43+
while (iterator.hasNext()) {
44+
Metric metric = iterator.next();
45+
Collection<Measurement> measurements = metric.measure().measurements();
46+
Assert.assertEquals(measurements.size(), 4);
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
MemTotal: 4106756 kB
2+
MemFree: 51004 kB
3+
Buffers: 495632 kB
4+
Cached: 1691248 kB
5+
SwapCached: 0 kB
6+
Active: 2799008 kB
7+
Inactive: 1047516 kB
8+
Active(anon): 1393568 kB
9+
Inactive(anon): 286376 kB
10+
Active(file): 1405440 kB
11+
Inactive(file): 761140 kB
12+
Unevictable: 0 kB
13+
Mlocked: 0 kB
14+
SwapTotal: 2097144 kB
15+
SwapFree: 2097144 kB
16+
Dirty: 660 kB
17+
Writeback: 0 kB
18+
AnonPages: 1658804 kB
19+
Mapped: 73820 kB
20+
Shmem: 21180 kB
21+
Slab: 127452 kB
22+
SReclaimable: 99996 kB
23+
SUnreclaim: 27456 kB
24+
KernelStack: 5928 kB
25+
PageTables: 12424 kB
26+
NFS_Unstable: 0 kB
27+
Bounce: 0 kB
28+
WritebackTmp: 0 kB
29+
CommitLimit: 4150520 kB
30+
Committed_AS: 5889456 kB
31+
VmallocTotal: 34359738367 kB
32+
VmallocUsed: 17652 kB
33+
VmallocChunk: 34359717664 kB
34+
HardwareCorrupted: 0 kB
35+
AnonHugePages: 0 kB
36+
HugePages_Total: 0
37+
HugePages_Free: 0
38+
HugePages_Rsvd: 0
39+
HugePages_Surp: 0
40+
Hugepagesize: 2048 kB
41+
DirectMap4k: 4194304 kB
42+
DirectMap2M: 0 kB

0 commit comments

Comments
 (0)