spring cloud alibaba 学习(十七)Sentinel指标统计StatisticSlot

本文详细介绍了Spring Cloud Alibaba Sentinel的StatisticSlot工作流程,包括统计线程、请求、响应时间和异常数量等。通过DefaultNode和StatisticNode记录统计信息,利用ArrayMetric和LeapArray实现秒级与分钟级统计,并探讨了指标获取的步骤,如successQps等。文章还揭示了关键类间的层次关系,如StatisticNode、ArrayMetric、LeapArray和MetricBucket,展示了Sentinel如何进行高精度的流量监控。
摘要由CSDN通过智能技术生成

一、StatisticSlot 流程

1、StatisticSlot

StatisticSlot主要用于统计线程数量、请求数量、响应时间、阻塞数量、异常数量。

@SpiOrder(-7000)
public class StatisticSlot extends AbstractLinkedProcessorSlot<DefaultNode> {
   

    @Override
    public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
                      boolean prioritized, Object... args) throws Throwable {
   
        try {
   
            // Do some checking.
            //先校验其他规则,再进行数据统计
            fireEntry(context, resourceWrapper, node, count, prioritized, args);

            // Request passed, add thread count and pass count.
            //node 和 clusterNode 中访问线程数都加1
            node.increaseThreadNum();
            //node 和 clusterNode 中访问通过数都加1
            //包括 分钟级时间窗口:rollingCounterInMinute, 秒级时间窗口:rollingCounterInSecond
            node.addPassRequest(count);

			//前置资源节点不为空时,增加访问线程数和访问通过数,用于填充上层链路节点的数据
            if (context.getCurEntry().getOriginNode() != null) {
   
                // Add count for origin node.
                context.getCurEntry().getOriginNode().increaseThreadNum();
                context.getCurEntry().getOriginNode().addPassRequest(count);
            }

			//入栈流量,全局访问线程数、访问通过数都加1
            if (resourceWrapper.getEntryType() == EntryType.IN) {
   
                // Add count for global inbound entry node for global statistics.
                Constants.ENTRY_NODE.increaseThreadNum();
                Constants.ENTRY_NODE.addPassRequest(count);
            }

            // Handle pass event with registered entry callback handlers.
            // ProcessorSlotEntryCallback 回调
            for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
   
                handler.onPass(context, resourceWrapper, node, count, args);
            }
        } catch (PriorityWaitException ex) {
   
        	//优先级等待异常,记录统计数据,记录了通过线程数,然后直接放行请求
            node.increaseThreadNum();
            if (context.getCurEntry().getOriginNode() != null) {
   
                // Add count for origin node.
                context.getCurEntry().getOriginNode().increaseThreadNum();
            }

            if (resourceWrapper.getEntryType() == EntryType.IN) {
   
                // Add count for global inbound entry node for global statistics.
                Constants.ENTRY_NODE.increaseThreadNum();
            }
            // Handle pass event with registered entry callback handlers.
            for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
   
                handler.onPass(context, resourceWrapper, node, count, args);
            }
        } catch (BlockException e) {
   
        	//校验不通过异常
            // Blocked, set block exception to current entry.
            //设置异常信息
            context.getCurEntry().setBlockError(e);

            // Add block count.
            //阻塞请求数增加1
            node.increaseBlockQps(count);
            if (context.getCurEntry().getOriginNode() != null) {
   
                context.getCurEntry().getOriginNode().increaseBlockQps(count);
            }

            if (resourceWrapper.getEntryType() == EntryType.IN) {
   
                // Add count for global inbound entry node for global statistics.
                Constants.ENTRY_NODE.increaseBlockQps(count);
            }

            // Handle block event with registered entry callback handlers.
            for (ProcessorSlotEntryCallback<DefaultNode> handler : StatisticSlotCallbackRegistry.getEntryCallbacks()) {
   
                handler.onBlocked(e, context, resourceWrapper, node, count, args);
            }
			
			//继续抛出异常
            throw e;
        } catch (Throwable e) {
   
            // Unexpected internal error, set error to current entry.
            //其他异常,设置异常信息
            context.getCurEntry().setError(e);
			//抛出异常
            throw e
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_lrs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值