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+方法名+参数调用获取应用磁盘上文件的监控数据。

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...