package com.yang.servo.demo;
import com.netflix.servo.annotations.DataSourceType;
import com.netflix.servo.annotations.Monitor;
import com.netflix.servo.monitor.Monitors;
import com.netflix.servo.publish.*;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
public class Server {
@Monitor(name = "Status", type = DataSourceType.INFORMATIONAL)
private static AtomicReference<String> status = new AtomicReference<String>("UP");
@Monitor(name = "Connections", type = DataSourceType.GAUGE)
private static AtomicInteger currentConnections = new AtomicInteger(0);
@Monitor(name = "TotalConnections", type = DataSourceType.COUNTER)
private static AtomicInteger totalConnections = new AtomicInteger(0);
@Monitor(name = "BytesIn", type = DataSourceType.COUNTER)
private static AtomicLong bytesIn = new AtomicLong(0L);
@Monitor(name = "BytesOut", type = DataSourceType.COUNTER)
private static AtomicLong bytesOut = new AtomicLong(0L);
private static PollScheduler scheduler = PollScheduler.getInstance();
private Server() {
}
public static void main(String[] args) {
Server s1 = new Server();
Monitors.registerObject("s1", s1);
startPoller();
// MonitorRegistryMetricPoller poller = new MonitorRegistryMetricPoller();
// List<Metric> metricList = poller.poll(BasicMetricFilter.MATCH_ALL);
// metricList.forEach(metric->{
// System.out.println(metric.getConfig().getName());
// System.out.println(metric.getValue());
// });
System.out.println(currentConnections.incrementAndGet());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void startPoller() {
scheduler.start();
final int heartbeatInterval = 1200;
final File metricsDir;
try {
metricsDir = File.createTempFile("zuul-servo-metrics-", "");
metricsDir.delete();
metricsDir.mkdir();
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("log dir: " + metricsDir);
MetricObserver transform = new CounterToRateMetricTransform(
new FileMetricObserver("ZuulMetrics", metricsDir),
heartbeatInterval, 10,TimeUnit.SECONDS);
PollRunnable task = new PollRunnable(
new MonitorRegistryMetricPoller(),
BasicMetricFilter.MATCH_ALL,
transform);
final int samplingInterval = 1;//采集间隔1s一次采集,所以会输出两个文件
scheduler.addPoller(task, samplingInterval, TimeUnit.SECONDS);
}
}
@MonitorTags 给类的方法和属性添加标签,主要用来添加兼职对。
http://www.infoq.com/cn/articles/chat-monitoring-part01
在很多情况下,系统的可视项越多,监控就越昂贵。监控的成本基本上与你收集的数据数量和细粒度成正比。所以,你收集的越多,付出的就越多,不过你收集到的东西都会对你有好处。我听说在Netflix,监控系统在整个运维系统里面的预算占比是两位数的百分比,他们在流媒体业内甚至有“监控之公司”之名。据说Netflix的人均营收在上市公司里是最高的,这样的成功也许和他们在监控上的投入也有一些关系吧!
https://blog.csdn.net/vking_wang/article/details/8668743
https://blog.csdn.net/lmy86263/article/details/71037316