基于Skywalking开发分布式监控(一)

接手为微服务系统搞链路监控项目一年多,也和skywalking打了一年多的交道,也应该有个总结,主要谈一下搭建监控系统遇到的难点和解决方案。

说明: 本文的代码均由本地演示代码替代,非实际代码

为啥选skywalking,因为由于微服务分成多个团队在开发,有平台,有应用,日志分析已经力不从心,下决心引入分布式监控,skywalking对我们来说有两个基本好处,1) 代码入侵非常低(最后仅仅在多线程跟踪,加了几处装饰器),监控基本可以独立安排版本或者变更,这对管理友好 2) Agent+插件模式对应用性能影响不大,预估不太会出幺蛾子...

开发过程中,我们遇到一些问题,本篇先聚焦第一个问题:

Agent插件二次开发,分两类

1) 修改,例如对dubbo-plugin,把DubboInstrumentation.java中

bash 复制代码
public class DubboInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {

    // add logger
    private static final ILog LOGGER = LogManager.getLogger(DubboInstrumentation.class);

    private static final String ENHANCE_CLASS = "com.alibaba.dubbo.monitor.support.MonitorFilter";  //需修改
    private static final String INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.dubbo.DubboInterceptor";

    @Override
    protected ClassMatch enhanceClass() {
        return NameMatch.byName(ENHANCE_CLASS);
    }

    @Override
    public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {
        return null;
    }

    @Override
    public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {
            new InstanceMethodsInterceptPoint() {
                @Override
                public ElementMatcher<MethodDescription> getMethodsMatcher() {

                    //LOGGER.info("Dubbo Class is {}", ENHANCE_CLASS);
                    return named("invoke");
                }

                @Override
                public String getMethodsInterceptor() {
                    return INTERCEPT_CLASS;
                }

                @Override
                public boolean isOverrideArgs() {
                    return false;
                }
            }
        };
    }
}

新增一个DubboProviderInstrumentation,根据本地应用改成 ENHANCE_CLASS = "com.comstar.cnp.common.infrastructure.DubboProviderFilter";

以及 DubboComsumerInstrumentation,根据本地应用改成 ENHANCE_CLASS = "com.comstar.cnp.common.infrastructure.DubboConSumerFilter";

并修改skyworking-plugin.def文件

dubbo=org.apache.skywalking.apm.plugin.dubbo.DubboInstrumentation

dubbo=org.apache.skywalking.apm.plugin.dubbo.DubboProviderInstrumentation

dubbo=org.apache.skywalking.apm.plugin.dubbo.DubboComsumerInstrumentation

保证dubbo跟踪链正常

2 ) 另外如基于netty的非标应用,就需要开发自定义Agent

按SkyWalking Agent的开发规范,需要开发Instrumentation,定义拦截点的位置(类和方法),并开发拦截器Interceptor,在beforeMethod创建 Span 并把需要的参数,塞到上下文Tag(标签)中,

bash 复制代码
ublic class seckillwithPoolAndMQInterceptor implements InstanceMethodsAroundInterceptor {
    @Override
    public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] classes, MethodInterceptResult methodInterceptResult) throws Throwable {
        String methodName = method.getName();
        System.out.println("before..."+methodName);
        String operationName ="seckillwithPoolAndMQ";

        ContextCarrier contextCarrier = new ContextCarrier();
        CarrierItem carrierItem = contextCarrier.items();

        Object pid= allArguments[0];
        Object uid= allArguments[1];

        //carrierItem.setHeadValue(ctxName.name());

        //创建一个EntrySpan

        //AbstractSpan span= ContextManager.createEntrySpan(operationName, contextCarrier);
        AbstractSpan span= ContextManager.createLocalSpan(operationName);
        span.setComponent(ComponentsDefine.TOMCAT);
        span.tag(new StringTag(10, "method"), methodName);
        span.tag(new StringTag(20, "pid"), pid.toString());
        span.tag(new StringTag(30, "uid"), uid.toString());
        span.setLayer(SpanLayer.CACHE);

        //缓存上下文
        objInst.setSkyWalkingDynamicField(ContextManager.capture());
    }

最后修改skyworking-plugin.def文件,保证插件正常运行

testdemo2-0.1=org.apache.skywalking.apm.plugin.demo2.define1.Demo2Instrumentation

解决插件的问题后,我们进一步发现Skywalking原生提供100多种插件,监控数据项多达数万项,但是这些数据,并不是全部都需要重点关注,冗余的监控数据,一方面会影响正常监控,另一方面,我们也发现过多的监控数据甚至会影响 OAP的性能,最夸张的问题可能是云服务和大量监控导致 SW sgementId产生重复,者迫使我们要采取适当的措施,后续,我们将聚焦对SkyWalking 监控数据分类筛选和展示

相关推荐
小股虫5 小时前
分布式事务:在增长中台,我们如何做到“发出去的内容”和“记录的数据”不打架?
分布式·微服务·云原生·架构·团队建设·方法论
是三好6 小时前
分布式事务seata
java·分布式·seata
optimistic_chen6 小时前
【Redis 系列】常用数据结构---Hash类型
linux·数据结构·redis·分布式·哈希算法
yuankunliu6 小时前
【分布式事务】4、分布式事务Seata的高级应用详解
分布式
java1234_小锋6 小时前
ZooKeeper集群中服务器之间是怎样通信的?
分布式·zookeeper·云原生
昌sit!9 小时前
hadoop集群搭建
大数据·hadoop·分布式
左灯右行的爱情11 小时前
Kafka专辑- 消息队列是什么
分布式·kafka
小股虫11 小时前
让系统“杀不死”:同步与异步场景下的弹性设计模式手册
分布式·微服务·设计模式·架构·团队建设·方法论
前端世界12 小时前
鸿蒙分布式权限管理实战指南:架构原理 + 可运行 Demo
分布式·架构·harmonyos
西***634712 小时前
「技术筑基 医疗提质」—— 分布式视频通讯系统在医疗领域的应用解析
分布式·音视频