sentinel读取监控文件分析
创始人
2024-03-13 08:32:46
0

主要分析的类

com.alibaba.csp.sentinel.dashboard.metric.MetricFetcher

在sentinel监控数据db持久化中,更换了MetricController的存储实现,可以发现com.alibaba.csp.sentinel.dashboard.repository.metric.MetricsRepository原来只有com.alibaba.csp.sentinel.dashboard.repository.metric.repository.InMemoryMetricsRepository实现
在这里插入图片描述
从全局可知,用到MetricsRepository的类原有的也就只有

com.alibaba.csp.sentinel.dashboard.repository.metric.repository.InMemoryMetricsRepository     监控数据的内存实现
com.alibaba.csp.sentinel.dashboard.metric.MetricFetcher    监控数据抓取器
com.alibaba.csp.sentinel.dashboard.controller.MetricController    dashboard获取监控数据入口

可以猜测监控数据获取实现的地方是在com.alibaba.csp.sentinel.dashboard.metric.MetricFetcher

还有一种方法不用猜的方式,在我们实现了dashboard的监控数据db存储后,发现

com.alibaba.csp.sentinel.dashboard.repository.metric.repository.SentinelMetricsRepository#saveAll

只要看哪里调用了存储的方法,就可以知道数据是怎么获取从而存储的。

com.alibaba.csp.sentinel.dashboard.metric.MetricFetcher#writeMetric
com.alibaba.csp.sentinel.dashboard.metric.MetricFetcher#fetchOnce

在这里插入图片描述

/*** fetch metric between [startTime, endTime], both side inclusive*/private void fetchOnce(String app, long startTime, long endTime, int maxWaitSeconds) {if (maxWaitSeconds <= 0) {throw new IllegalArgumentException("maxWaitSeconds must > 0, but " + maxWaitSeconds);}//获取应用的基本信息AppInfo appInfo = appManagement.getDetailApp(app);// auto remove for appif (appInfo.isDead()) {logger.info("Dead app removed: {}", app);appManagement.removeApp(app);return;}//查看应用的机器信息(比如ip)Set machines = appInfo.getMachines();logger.debug("enter fetchOnce(" + app + "), machines.size()=" + machines.size()+ ", time intervalMs [" + startTime + ", " + endTime + "]");if (machines.isEmpty()) {return;}final String msg = "fetch";AtomicLong unhealthy = new AtomicLong();final AtomicLong success = new AtomicLong();final AtomicLong fail = new AtomicLong();long start = System.currentTimeMillis();/** app_resource_timeSecond -> metric */final Map metricMap = new ConcurrentHashMap<>(16);final CountDownLatch latch = new CountDownLatch(machines.size());for (final MachineInfo machine : machines) {// 机器不正常 auto remove if (machine.isDead()) {latch.countDown();appManagement.getDetailApp(app).removeMachine(machine.getIp(), machine.getPort());logger.info("Dead machine removed: {}:{} of {}", machine.getIp(), machine.getPort(), app);continue;}if (!machine.isHealthy()) {latch.countDown();unhealthy.incrementAndGet();continue;}//机器正常的情况下final String url = "http://" + machine.getIp() + ":" + machine.getPort() + "/" + METRIC_URL_PATH+ "?startTime=" + startTime + "&endTime=" + endTime + "&refetch=" + false;final HttpGet httpGet = new HttpGet(url);httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);httpclient.execute(httpGet, new FutureCallback() {@Overridepublic void completed(final HttpResponse response) {try {handleResponse(response, machine, metricMap);success.incrementAndGet();} catch (Exception e) {logger.error(msg + " metric " + url + " error:", e);} finally {latch.countDown();}}@Overridepublic void failed(final Exception ex) {latch.countDown();fail.incrementAndGet();httpGet.abort();if (ex instanceof SocketTimeoutException) {logger.error("Failed to fetch metric from <{}>: socket timeout", url);} else if (ex instanceof ConnectException) {logger.error("Failed to fetch metric from <{}> (ConnectionException: {})", url, ex.getMessage());} else {logger.error(msg + " metric " + url + " error", ex);}}@Overridepublic void cancelled() {latch.countDown();fail.incrementAndGet();httpGet.abort();}});}try {latch.await(maxWaitSeconds, TimeUnit.SECONDS);} catch (Exception e) {logger.info(msg + " metric, wait http client error:", e);}//long cost = System.currentTimeMillis() - start;//logger.info("finished " + msg + " metric for " + app + ", time intervalMs [" + startTime + ", " + endTime//    + "], total machines=" + machines.size() + ", dead=" + dead + ", fetch success="//    + success + ", fetch fail=" + fail + ", time cost=" + cost + " ms");writeMetric(metricMap);}
final String url = "http://" + machine.getIp() + ":" + machine.getPort() + "/" + METRIC_URL_PATH+ "?startTime=" + startTime + "&endTime=" + endTime + "&refetch=" + false;final HttpGet httpGet = new HttpGet(url);

这里是调用机器的**“metric”**接口获取数据。

metric接口位于sentinel-transport【sentinel通信包】下

com.alibaba.csp.sentinel.command.handler.SendMetricCommandHandler
com.alibaba.csp.sentinel.node.metric.MetricSearcher

在这里插入图片描述
注释写的已经很清楚了,通过MetricSearcher查询机器的监控数据文件在这里插入图片描述

com.alibaba.csp.sentinel.node.metric.MetricWriter#listMetricFiles

此方法是获取应用机器下的监控文件的。

知道了dashboard怎么获取的监控数据,那监控数据是怎么写到应用机器的磁盘?

com.alibaba.csp.sentinel.n`在这里插入代码片`ode.metric.MetricWriter#listMetricFiles

所属的类com.alibaba.csp.sentinel.node.metric.MetricWriter从命名就可以看出是写监控数据的。

com.alibaba.csp.sentinel.node.metric.MetricWriter#write

查看write方法的调用
在这里插入图片描述
在这里插入图片描述

com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager

在这里插入图片描述
在这里插入图片描述

在应用启动的时候FlowRuleManager会注册监听配置项的变化,同时也会注册MetricTimerListener,通过MetricTimerListener将监控数据写到应用所在的磁盘上,dashborad通过ip+port+方法名+参数调用获取应用磁盘上文件的监控数据。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...