1. 自定义dubbo filter
import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.*;
import com.alibaba.dubbo.rpc.support.RpcUtils;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Timer;
@Activate(group = Constants.PROVIDER, order = 83000)
public class MetricsFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
String serviceName = invoker.getInterface().getName();
String simpleServiceName = invoker.getInterface().getSimpleName();
String methodName = RpcUtils.getMethodName(invocation);
String[] tags = new String[]{"dubbo_service", serviceName, "dubbo_svc", simpleServiceName, "dubbo_method", methodName};
Metrics.counter("qps", tags).increment();
Timer timer = Timer.builder("rt")
.publishPercentileHistogram(false)
.publishPercentiles(0.95, 0.99, 0.999)
.tags(tags)
.register(Metrics.globalRegistry);
Timer.Sample sample = Timer.start();
Result result = invoker.invoke(invocation);
sample.stop(timer);
return result;
}
}
2. 配置filter生效
在resources
文件夹下,创建META-INF/dubbo/com.alibaba.dubbo.rpc.Filter
,dubbo服务会自动扫描注册
这里有个小tips,只要引入的java sdk里面包含,也会被扫描
metricFilter=com.bosszp.space.metrics.MetricsFilter
3. 配置grafana监控图
qps监控图
# 总qps
sum(rate(qps_total{}[1m]))
# 按服务+接口分别统计qps
sum(rate(qps_total{}[1m])) by (dubbo_svc, dubbo_method)
rt监控图
# 毫秒
max(rt_seconds{} * 1000) by (dubbo_svc, dubbo_method)