Skip to content

Commit

Permalink
enable linux os metrics (#38)
Browse files Browse the repository at this point in the history
- 增加 linux os 的metrics的采集和上报;
- 优化主动上报时逻辑,优化连接使用;
- 其他修复若干;
  • Loading branch information
luyiisme authored Sep 15, 2018
1 parent 87c4605 commit 191a005
Show file tree
Hide file tree
Showing 44 changed files with 841 additions and 433 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

[English Document](./README_EN.md)

访问 [WIKI](https://github.com/alipay/sofa-lookout/wiki) 查看完整的文档使用指南。
访问 [WIKI](http://www.sofastack.tech/sofa-lookout/docs/Home) 查看完整的文档使用指南。

SOFALookout 是一个利用多维度的 metrics 对目标系统进行度量和监控的项目。SOFALookout 的多维度 metrics 参考[Metrics2.0 标准](http://metrics20.org/)。SOFALookout 项目分为客户端部分与服务器端部分。

Expand Down
4 changes: 2 additions & 2 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ SOFALookout Client 项目支持 Maven 3.2.5+,JDK 6+ 进行编译。

## 2. API 埋点需知

lookout-api 支持被单独依赖和使用,方便植入您的项目代码,收集需要的 metrics,更多信息参考 [WIKI 文档](https://github.com/alipay/sofa-lookout/wiki)
lookout-api 支持被单独依赖和使用,方便植入您的项目代码,收集需要的 metrics,更多信息参考 [WIKI 文档](http://www.sofastack.tech/sofa-lookout/docs/Home)

## 3. 扩展能力

Expand Down Expand Up @@ -49,4 +49,4 @@ lookout 客户端提供了 SPI 机制(只需要实现 `com.alipay.lookout.spi.

## 6.如何使用

参考 [WIKI 文档](https://github.com/alipay/sofa-lookout/wiki)的快速开始和用户手册。
参考 [WIKI 文档](http://www.sofastack.tech/sofa-lookout/docs/Home)的快速开始和用户手册。
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected void addDefaultCommonTags(CommonTagsAccessor commonTagsAccessor) {
if (StringUtils.isNotEmpty(zone)) {
commonTagsAccessor.setCommonTag("zone", zone);
}
//Ant cloud middleware instanceId
//instanceId
String instanceId = System.getProperty(INSTANCE_ID_NAME);
if (StringUtils.isNotEmpty(instanceId)) {
commonTagsAccessor.setCommonTag("instance_id", instanceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import com.alipay.lookout.api.Lookout;
import com.alipay.lookout.api.MetricRegistry;
import com.alipay.lookout.core.config.MetricConfig;
import com.alipay.lookout.remote.step.LookoutRegistry;

import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_EXPORTER_ENABLE;

/**
* Created by kevin.luy@alipay.com on 2017/10/4.
*/
Expand All @@ -44,7 +47,12 @@ public synchronized void addRegistry(MetricRegistry registry) {
registry.registerExtendedMetrics();
}
super.addRegistry(registry);

if (registry instanceof LookoutRegistry) {
MetricConfig config = ((LookoutRegistry) registry).getConfig();
if (!config.getBoolean(LOOKOUT_EXPORTER_ENABLE, false)) {
return;
}
try {
setMetricsHttpExporter(PollerUtils.exportHttp((LookoutRegistry) registry));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import java.util.concurrent.atomic.AtomicInteger;

import static com.alipay.lookout.core.config.LookoutConfig.LOOKOUT_EXPORTER_ENABLE;

/**
* 与 @see DefaultLookoutClient 相比,SimpleLookoutClient限制更严格:
* 1.所有的Registry需要预先设置!
Expand Down Expand Up @@ -78,6 +80,9 @@ public SimpleLookoutClient(String appName, LookoutConfig config, MetricRegistry.
registry.registerExtendedMetrics();
super.addRegistry(registry);
if (registry instanceof LookoutRegistry) {
if (!lookoutConfig.getBoolean(LOOKOUT_EXPORTER_ENABLE, false)) {
return;
}
try {
setMetricsHttpExporter(PollerUtils.exportHttp((LookoutRegistry) registry));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static PRIORITY resolve(Iterable<Tag> tags) {
String value = Utils.getTagValue(tags, TAG_PRIORITY_KEY);
if (value != null) {
for (PRIORITY p : PRIORITY.values()) {
if (p.name().equals(value)) {
if (p.name().equalsIgnoreCase(value)) {
return p;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@
</Policies>
</RollingFile>

<RollingFile name="ANTEVENTCONTEXT-APPENDER" fileName="${LOOKOUT_LOG_PATH}/lookout/ant-biz-stat.log" append="true"
filePattern="${LOOKOUT_LOG_PATH}/lookout/ant-biz-stat-%d{yyyy-MM-dd}.log">
<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout charset="${LOOKOUT_FILE_ENCODING}">
<pattern>%d %-5p %-32t - %m%n</pattern>
</PatternLayout>
<Policies>
<!-- 按天分日志文件:重要的是 filePattern 配置到按照天 -->
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>

</appenders>

Expand All @@ -54,10 +43,5 @@
<appender-ref ref="ROOT-APPENDER"/>
<appender-ref ref="ERROR-APPENDER"/>
</root>

<logger name="ANTEVENTCONTEXT-LOG" value="INFO" additivity="false">
<appender-ref ref="ANTEVENTCONTEXT-APPENDER"/>
<appender-ref ref="ERROR-APPENDER" />
</logger>
</loggers>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alipay.lookout.api.BasicTag;
import com.alipay.lookout.api.PRIORITY;
import com.alipay.lookout.api.Tag;
import com.alipay.lookout.common.LookoutConstants;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -40,6 +41,14 @@ public void testResolveHigh() {
Assert.assertEquals(PRIORITY.HIGH, PriorityTagUtil.resolve(tags));
}

@Test
public void testResolveHigh2() {
List<Tag> tags = new ArrayList<Tag>();
tags.add(new BasicTag("k1", "v1"));
tags.add(LookoutConstants.HIGH_PRIORITY_TAG);
Assert.assertEquals(PRIORITY.HIGH, PriorityTagUtil.resolve(tags));
}

@Test
public void testResolveNothing() {
List<Tag> tags = new ArrayList<Tag>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void record(long amount) {
public Indicator measure() {
long now = clock.wallTime();
return new Indicator(now, id).addMeasurement(Statistic.count.name(), count.get())
.addMeasurement(Statistic.count.name(), totalAmount.get());
.addMeasurement(Statistic.totalAmount.name(), totalAmount.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,17 @@ public final class LookoutConfig extends MapConfiguration implements MetricConfi
public static final String LOOKOUT_AUTOPOLL_OS_METRIC_IGNORE = "lookout.autopoll.os.ignore";
public static final String LOOKOUT_AGENT_SERVER_PORT = "lookout.agent.server.port";
public static final String LOOKOUT_ANT_EVENT_LOG_ENABLE = "lookout.ant.event.log.enable";
/**
* 多久没有请求拉取数据就进入idle状态
*/
public static final String LOOKOUT_EXPORTER_IDLE_SECONDS = "lookout.exporter.idle.seconds";
public static final String LOOKOUT_EXPORTER_ENABLE = "lookout.exporter.enable";
public static final String LOOKOUT_EXPORTER_ACCESS_TOKEN = "lookout.exporter.access.token";

public static final String LOOKOUT_PROMETHEUS_EXPORTER_SERVER_PORT = "lookout.prometheus.exporter.server.port";
// default value
public static final int DEFAULT_WEB_SERVER_PORT = 8083;
public static final int DEFAULT_HTTP_EXPORTER_PORT = 19399;
public static final int DEFAULT_PROMETHEUS_EXPORTER_SERVER_PORT = 9494;
public static final String APP_NAME = "app.name";
public static int DEFAULT_REPORT_BATCH_SIZE = 1700;
Expand All @@ -55,11 +61,6 @@ public final class LookoutConfig extends MapConfiguration implements MetricConfi
*/
// public static final String POLLER_EXPORTER_ENABLED = "lookout.poller.enabled";

/**
* 多久没有请求拉取数据就进入idle状态
*/
public static final String POLLER_EXPORTER_IDLE_SECONDS = "lookout.poller.exporter.idle.seconds";

/**
* priority->Millsecond
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public DiskUsageMetricsImporter(String filePath, long timeout, TimeUnit timeoutU
protected void doRegister(Registry registry) {
for (Map.Entry<String, DiskUsage> entry : diskUsageByDevice.entrySet()) {
final String device = entry.getKey();
Id id = registry.createId("os.disk.usage." + device);
Id id = registry.createId("os.disk.usage");
id = id.withTag("device", device);
id = id.withTag("root", diskUsageByDevice.get(device).fsFile);
id = id.withTag("type", diskUsageByDevice.get(device).fsVfType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public IOStatsMetricsImporter(String filePath, String upTimeFilePath, long timeo
protected void doRegister(Registry registry) {
for (Map.Entry<String, Float[]> entry : statsByDevice.entrySet()) {
final String device = entry.getKey();
Id id = registry.createId("os.io.stats." + device);
Id id = registry.createId("os.io.stats");
id = id.withTag("device", device);
MixinMetric mixin = registry.mixinMetric(id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ public class NetTrafficMetricsImporter extends CachedMetricsImporter {

private static final Pattern NET_PATTERN = Pattern.compile(STR_PATTERN);

private static final String[] FIELDS = { "net.in.bytes", "net.in.packets",
"net.in.errs", "net.in.dropped", "net.in.fifo.errs", "net.in.frame.errs",
"net.in.compressed", "net.in.multicast", "net.out.bytes", "net.out.packets",
"net.out.errs", "net.out.dropped", "net.out.fifo.errs", "net.out.collisions",
"net.out.carrier.errs", "net.out.compressed" };
private static final String[] FIELDS = { "in.bytes", "in.packets", "in.errs",
"in.dropped", "in.fifo.errs", "in.frame.errs", "in.compressed", "in.multicast",
"out.bytes", "out.packets", "out.errs", "out.dropped", "out.fifo.errs",
"out.collisions", "out.carrier.errs", "out.compressed" };

private String filePath;
private Map<String, Long[]> statByFace;
Expand All @@ -104,7 +103,7 @@ public NetTrafficMetricsImporter(String filePath, long timeout, TimeUnit timeout
protected void doRegister(Registry registry) {
for (final Map.Entry<String, Long[]> entry : statByFace.entrySet()) {
final String face = entry.getKey();
Id id = registry.createId("os.net.stat." + face);
Id id = registry.createId("os.net.stats").withTag("intfc", face);
MixinMetric mixin = registry.mixinMetric(id);

for (int i = 0; i < entry.getValue().length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public class ExporterServerTest {

@Test
public void testExporterServer() throws IOException {
ExporterServer server = new ExporterServer(9494);
ExporterServer server = new ExporterServer(9194);
server.start();
String result = sendHttpRequest(new URL("http://localhost:9494/"));
String result = sendHttpRequest(new URL("http://localhost:9194/"));
Assert.assertTrue(result.contains("/metrics"));
server.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
*/
package com.alipay.lookout.remote.report;

import java.util.Random;

/**
* 动态地址服务
* Created by kevin.luy@alipay.com on 2017/3/8.
*/
public interface AddressService {
ThreadLocal<Random> randomThreadLocal = new ThreadLocal<Random>() {
@Override
protected Random initialValue() {
return new Random();
}
};

/**
* set a agentTestUrl,优先与动态发现的地址
Expand All @@ -38,8 +46,14 @@ public interface AddressService {

boolean isAgentServerExisted();

/**
* select one address from addresses
*
* @return agent address
*/
Address getAgentServerHost();

@Deprecated
void clearAddressCache();

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
*/
package com.alipay.lookout.remote.report;

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

/**
* 默认静态地址服务
* with vip and test model url
* Created by kevin.luy@alipay.com on 2017/5/31.
*/
public class DefaultAddressService implements AddressService {

private Address agentServerVip;
private Address agentTestUrl;
private Address agentTestUrl;
private List<Address> addressList;

public DefaultAddressService() {
}
Expand All @@ -35,7 +39,6 @@ public DefaultAddressService(String appName) {
}

// cache,for a connection keep alive & reuse;

public void clearAddressCache() {
}

Expand All @@ -46,21 +49,55 @@ public void setAgentTestUrl(String agentTestUrl) {
}

public void setAgentServerVip(String agentServerVip) {
if (!Strings.isNullOrEmpty(agentServerVip)) {
this.agentServerVip = new Address(agentServerVip);
if (Strings.isNullOrEmpty(agentServerVip)) {
return;
}
//multi addresses
if (agentServerVip.contains(",")) {
List<String> agentServers = Splitter.on(',').splitToList(agentServerVip);
setAddressList(agentServers);
return;
}
this.addressList = Lists.newArrayList(new Address(agentServerVip));
}

public void setAddressList(List<String> addresses) {
if (addresses == null || addresses.isEmpty()) {
return;
}
List<Address> addressList = new ArrayList<Address>();
for (String addressStr : addresses) {
addressList.add(new Address(addressStr.trim()));
}
this.addressList = addressList;
}

protected Address getAgentTestUrl() {
return agentTestUrl;
}

protected List<Address> getAddressList() {
return addressList;
}

@Override
public boolean isAgentServerExisted() {
return agentTestUrl != null || agentServerVip != null;
return agentTestUrl != null || (addressList != null && !addressList.isEmpty());
}

@Override
public Address getAgentServerHost() {
if (agentTestUrl != null) {
return agentTestUrl;
}
return agentServerVip;
List<Address> addrList = addressList;
if (addrList == null) {
return null;
}
if (addrList.size() == 1) {
return addrList.get(0);
}
int randomNodeIdx = randomThreadLocal.get().nextInt(addrList.size());
return addrList.get(randomNodeIdx);
}
}
Loading

0 comments on commit 191a005

Please sign in to comment.