Skip to content

Commit

Permalink
add linux memory metrics (#52)
Browse files Browse the repository at this point in the history
* add linux memory metrics #50 
* lookoutAuotConfiguration get extensions from beanFactory;(企业版 lookoutAuotConfiguration 会很早从beanfactory中获取,当时可能还没注入这些扩展组件)
  • Loading branch information
luyiisme authored Mar 26, 2019
1 parent 0fb218d commit e8cba52
Show file tree
Hide file tree
Showing 27 changed files with 311 additions and 41 deletions.
2 changes: 1 addition & 1 deletion client/lookout-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-ext-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion client/lookout-ext-os/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.alipay.sofa.lookout</groupId>
<artifactId>lookout-client-parent</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public CpuUsageMetricsImporter() {
}

/**
* @param filePath
* @param timeout
* @param timeoutUnit
* @param filePath file path
* @param timeout timeout
* @param timeoutUnit timeout unit
*/
public CpuUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
super(timeout, timeoutUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public DiskUsageMetricsImporter() {

/**
*
* @param filePath
* @param timeout
* @param timeoutUnit
* @param filePath filePath
* @param timeout timeout
* @param timeoutUnit timeoutUnit
*/
public DiskUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
super(timeout, timeoutUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ public IOStatsMetricsImporter() {
}

/**
* @param filePath
* @param upTimeFilePath
* @param timeout
* @param timeoutUnit
* @param filePath filePath
* @param upTimeFilePath upTimeFilePath
* @param timeout timeout
* @param timeoutUnit timeoutUnit
*/
public IOStatsMetricsImporter(String filePath, String upTimeFilePath, long timeout,
TimeUnit timeoutUnit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alipay.lookout.os.linux;

import com.alipay.lookout.api.Gauge;
import com.alipay.lookout.api.Id;
import com.alipay.lookout.api.Registry;
import com.alipay.lookout.api.composite.MixinMetric;
import com.alipay.lookout.common.log.LookoutLoggerFactory;
import com.alipay.lookout.os.CachedMetricsImporter;
import com.alipay.lookout.os.utils.FileUtils;
import org.slf4j.Logger;

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* resolve memory metrics from "/proc/meminfo"
*
* @author: kevin.luy@antfin.com
* 2019-03-12 17:30
**/
public class MemoryStatsMetricsImporter extends CachedMetricsImporter {

private final Logger logger = LookoutLoggerFactory
.getLogger(DiskUsageMetricsImporter.class);

private static final String DEFAULT_FILE_PATH = "/proc/meminfo";
private static final String SPLIT = "\\s+";
private String filePath;
private Memstats memstats;

/**
* default constructor
*/
public MemoryStatsMetricsImporter() {
this(DEFAULT_FILE_PATH, DEFAULT_TIMEOUT_MS, TimeUnit.MILLISECONDS);
}

/**
* @param filePath filePath
* @param timeout timeout
* @param timeoutUnit timeoutUnit
*/
public MemoryStatsMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
super(timeout, timeoutUnit);
this.filePath = filePath;
memstats = new Memstats();
disable = !new File(filePath).exists();
if (!disable)
loadIfNessesary();
}

@Override
protected void doRegister(Registry registry) {
Id id = registry.createId("os.memory.stats");
MixinMetric mixin = registry.mixinMetric(id);

mixin.gauge("total.bytes", new Gauge<Long>() {
@Override
public Long value() {
loadIfNessesary();
return memstats.memTotal;
}
});

mixin.gauge("free.bytes", new Gauge<Long>() {
@Override
public Long value() {
loadIfNessesary();
return memstats.memFree;
}
});
mixin.gauge("buffers.bytes", new Gauge<Long>() {
@Override
public Long value() {
loadIfNessesary();
return memstats.buffers;
}
});
mixin.gauge("cached.bytes", new Gauge<Long>() {
@Override
public Long value() {
loadIfNessesary();
return memstats.cached;
}
});

}

@Override
protected void loadValues() {
try {
List<String> lines = FileUtils.readFileAsStringArray(filePath);
Memstats ms = new Memstats();
int i = 0;
for (String line : lines) {
if (i == 4)
break;
String[] infos = line.split(SPLIT);
if (infos == null) {
continue;
}
if ("MemTotal:".equals(infos[0])) {
ms.memTotal = Long.parseLong(infos[1]) * 1024;
i++;
continue;
}
if ("MemFree:".equals(infos[0])) {
ms.memFree = Long.parseLong(infos[1]) * 1024;
i++;
continue;
}
if ("Buffers:".equals(infos[0])) {
ms.buffers = Long.parseLong(infos[1]) * 1024;
i++;
continue;
}
if ("Cached:".equals(infos[0])) {
ms.cached = Long.parseLong(infos[1]) * 1024;
i++;
continue;
}
}
memstats = ms;
} catch (Exception e) {
logger.debug("warning,can't parse line at /proc/meminfo", e.getMessage());
}
}

private class Memstats {
long memTotal;
long memFree;
long cached;
long buffers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public SystemLoadMetricsImporter() {

/**
*
* @param filePath
* @param timeout
* @param timeoutUnit
* @param filePath filePath
* @param timeout timeout
* @param timeoutUnit timeoutUnit
*/
public SystemLoadMetricsImporter(String filePath, long timeout, TimeUnit timeoutUnit) {
super(timeout, timeoutUnit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class FileUtils {
*
* @param path the path to file
* @return the file content as string
* @throws IOException IOException
*/
public static String readFile(String path) throws IOException {
BufferedReader reader = null;
Expand Down Expand Up @@ -60,6 +61,7 @@ public static String readFile(String path) throws IOException {
*
* @param path the path to file
* @return the file content as string list
* @throws IOException IOException
*/
public static List<String> readFileAsStringArray(String path) throws IOException {
BufferedReader reader = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@
public class NumFormatUtils {
/**
* 保留两位小数
* @param data data
* @return float
*/
public static float formatFloat(float data) {
return (float) (Math.round(data * 100) / 100.0);
}

/**
* 保留两位小数
* @param usage usage
* @return float
*/
public static float formatFloat(String usage) {
return (float) (Math.round(Float.parseFloat(usage) * 100) / 100.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ com.alipay.lookout.os.linux.DiskUsageMetricsImporter
com.alipay.lookout.os.linux.IOStatsMetricsImporter
com.alipay.lookout.os.linux.NetTrafficMetricsImporter
com.alipay.lookout.os.linux.SystemLoadMetricsImporter
com.alipay.lookout.os.linux.MemoryStatsMetricsImporter

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 com.alipay.lookout.os.linux;

import com.alipay.lookout.api.Measurement;
import com.alipay.lookout.api.Metric;
import com.alipay.lookout.api.Registry;
import com.alipay.lookout.core.DefaultRegistry;
import org.junit.Assert;
import org.junit.Test;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/**
* @author: kevin.luy@antfin.com
* @create: 2019-03-12 21:40
**/
public class MemoryStatsMetricsImporterTest {

@Test
public void test() {
Registry registry = new DefaultRegistry();
MemoryStatsMetricsImporter memoryStatsMetricsImporter = new MemoryStatsMetricsImporter(
"src/test/resources/proc_meminfo", 1000, TimeUnit.MILLISECONDS);
memoryStatsMetricsImporter.register(registry);
Iterator<Metric> iterator = registry.iterator();
while (iterator.hasNext()) {
Metric metric = iterator.next();
Collection<Measurement> measurements = metric.measure().measurements();
Assert.assertEquals(measurements.size(), 4);
}
}
}
42 changes: 42 additions & 0 deletions client/lookout-ext-os/src/test/resources/proc_meminfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
MemTotal: 4106756 kB
MemFree: 51004 kB
Buffers: 495632 kB
Cached: 1691248 kB
SwapCached: 0 kB
Active: 2799008 kB
Inactive: 1047516 kB
Active(anon): 1393568 kB
Inactive(anon): 286376 kB
Active(file): 1405440 kB
Inactive(file): 761140 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 2097144 kB
SwapFree: 2097144 kB
Dirty: 660 kB
Writeback: 0 kB
AnonPages: 1658804 kB
Mapped: 73820 kB
Shmem: 21180 kB
Slab: 127452 kB
SReclaimable: 99996 kB
SUnreclaim: 27456 kB
KernelStack: 5928 kB
PageTables: 12424 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 4150520 kB
Committed_AS: 5889456 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 17652 kB
VmallocChunk: 34359717664 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 4194304 kB
DirectMap2M: 0 kB
Loading

0 comments on commit e8cba52

Please sign in to comment.