基于Skywalking开发分布式监控(一)
接手为微服务系统搞链路监控项目一年多,也和skywalking打了一年多的交道,也应该有个总结,主要谈一下搭建监控系统遇到的难点和解决方案。
说明: 本文的代码均由本地演示代码替代,非实际代码
为啥选skywalking,因为由于微服务分成多个团队在开发,有平台,有应用,日志分析已经力不从心,下决心引入分布式监控,skywalking对我们来说有两个基本好处,1) 代码入侵非常低(最后仅仅在多线程跟踪,加了几处装饰器),监控基本可以独立安排版本或者变更,这对管理友好 2) Agent+插件模式对应用性能影响不大,预估不太会出幺蛾子…
开发过程中,我们遇到一些问题,本篇先聚焦第一个问题:
Agent插件二次开发,分两类
1) 修改,例如对dubbo-plugin,把DubboInstrumentation.java中
public class DubboInstrumentation extends ClassInstanceMethodsEnhancePluginDefine {// add loggerprivate 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";@Overrideprotected ClassMatch enhanceClass() {return NameMatch.byName(ENHANCE_CLASS);}@Overridepublic ConstructorInterceptPoint[] getConstructorsInterceptPoints() {return null;}@Overridepublic InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {return new InstanceMethodsInterceptPoint[] {new InstanceMethodsInterceptPoint() {@Overridepublic ElementMatcher<MethodDescription> getMethodsMatcher() {//LOGGER.info("Dubbo Class is {}", ENHANCE_CLASS);return named("invoke");}@Overridepublic String getMethodsInterceptor() {return INTERCEPT_CLASS;}@Overridepublic 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(标签)中,
ublic class seckillwithPoolAndMQInterceptor implements InstanceMethodsAroundInterceptor {@Overridepublic 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 监控数据分类筛选和展示