import com.codahale.metrics.MetricRegistry;
import com.google.common.eventbus.AllowConcurrentEvents;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.msxf.datasource.utils.AlarmEvent;
import com.msxf.datasource.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@Service
public class MonitorService {
private Logger logger = LoggerFactory.getLogger(MonitorService.class);
@Autowired
private MetricRegistry metrics;
private ExecutorService executor = Executors.newFixedThreadPool(3);
private EventBus eventBus = new AsyncEventBus(executor);
@PostConstruct
public void init() {
eventBus.register(this);
}
@PreDestroy
public void destroy() {
executor.shutdown();
}
public void post(AlarmEvent event) {
if (event == null) return;
if (event.type == null) return;
if (StringUtil.isNullAndEmpty(event.key)) return;
eventBus.post(event);
}
@Subscribe
@AllowConcurrentEvents
public void consume(AlarmEvent event) {
try {
switch (event.type) {
case COUNT:
metrics.counter(event.key).inc(event.value);
logger.info("{} {}", event.key, event.value);
break;
case METE:
metrics.meter(event.key).mark(event.value);
logger.info("{} {}", event.key, event.value);
break;
case HISTOGRAM:
metrics.histogram(event.key).update(event.value);
logger.info("{} {}", event.key, event.value);
break;
case TIME:
metrics.timer(event.key).update(event.value, TimeUnit.MILLISECONDS);
logger.info("{} {}", event.key, event.value);
break;
default:
logger.error("*alarm event type no match,type={},key={}", event.type, event.key);
break;
}
} catch (Throwable e) {
}
}
}
MonitorService-监控服务类
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- Metrics是一个给JAVA服务的各项指标提供度量工具的包,在JAVA代码中嵌入Metrics代码,可以方便的对...
- 一、Zabbix监控指定服务器 第一里程:在指定服务器上安装zabbix客户端,即zabbix-agent访问清华...