本文参考原文-http://bjbsair.com/2020-03-22/tech-info/5100/

命令

本文主要研究skywalking的spring web plus插件

DispatcherHandlerInstrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/ v5/define/DispatcherHandlerInstrumentation.java

公共类 DispatcherHandlerInstrumentation 扩展 ClassInstanceMethodsEnhancePluginDefine {

@Override

public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {

 return new ConstructorInterceptPoint\[0\];

}

@Override

public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {

 return new Instance Methods Intercept Point\[\]{
   新的 InstanceMethodsInterceptPoint() {
     @Override
      public ElementMatcher<MethodDescription> getMethodsMatcher() {
        返回命名(“句柄”);
      }
     @Override
        public String get Method Interceptor() {
       返回“org.apache.skywalking.apm.plugin.spring.webflux.v5.DispatcherHandlerHandleMethodInterceptor”;
      }
     @Override
        public boolean isOverrideArgs() {
         返回 false;
      }
    }
 };

}

@Override

受保护的 ClassMatch 增强类() {

 return byName("org.springframework.web.reactive.DispatcherHandler");

}

}

  • DispatcherHandlerInstrumentation继承ClassInstanceMethodsEnhancePluginDefine,使用org.apache.skywalking.apm.plugin.spring.webplus.v5.dispatcherhandlerhandlermethodinterceptor增强org.springframework.web.reactive.DispatcherHandler的handle方法

Dispatcher Handler 句柄 MethodInterceptor

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/ v5/DispatcherHandlerHandleMethodInterceptor.java

公共类 DispatcherHandlerHandleMethodInterceptor 实现 InstanceMethodsAroundInterceptor {

private static final String DEFAULT_OPERATION_NAME u003d "WEBFLUX.handle";

@Override

public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,

                    MethodInterceptResult result) throws Throwable {

}

@Override

public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,

                    Object ret) throws Throwable {
增强实例实例 u003d getInstance(allArguments\[0\]);
ServerWebExchange 交换 u003d (ServerWebExchange) allArguments\[0\];
 ContextCarrier 运营商 u003d new ContextCarrier();
CarrierItem next u003d carrier.items();
 HttpHeaders 标头 u003d exchange.getRequest().getHeaders();
 while (next.hasNext()) {
   下一个 u003d next.next();
  List<String> header u003d headers.get(next.getHeadKey());
    if (header !u003d null && header.size() > 0) {
     next.setHeadValue(header.get(0));
    }
 }
 AbstractSpan span u003d ContextManager.createEntrySpan(DEFAULT\_OPERATION\_NAME, 运营商);
 span.setComponent(ComponentsDefine.SPRING\_WEBFLUX);
 SpanLayer.asHttp(span);
Tags.URL.set(span, exchange.getRequest().getURI().toString());
HTTP.METHOD.set(span, exchange.getRequest().getMethodValue());
 instance.setSkyWalkingDynamicField(ContextManager.capture());
 span.prepareForAsync();
 ContextManager.stopSpan(span);
 return ((Mono) ret).doFinally(s -> {
   试试{
       对象 pathPattern u003d exchange.getAttribute(HandlerMapping.BEST\_MATCHING\_PATTERN\_ATTRIBUTE);
      if (pathPattern !u003d null) {
        span.setOperationName(((PathPattern) pathPattern).getPatternString());
      }
     HttpStatus httpStatus u003d exchange.getResponse().getStatusCode();
        // 修复 webflux-2.0.0-2.1.0 版本有 bug。 httpStatus 为空。不支持
      if (httpStatus !u003d null) {
        Tags.STATUS\_CODE.set(span, Integer.toString(httpStatus.value()));
         if (httpStatus.isError()) {
            span.errorOccurred();
          }
      }
   } 最后{
      span.asyncFinish();
    }
 });

}

@Override

public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments,

                     类<?>\[\] argumentsTypes, Throwable t) {

}

public static EnhancedInstance getInstance(Object o) {

 增强实例实例 u003d null;
 if (o instanceof DefaultServerWebExchange) {
   实例 u003d (EnhancedInstance) o;
} else if (o instanceof ServerWebExchangeDecorator) {
  ServerWebExchange 委托 u003d ((ServerWebExchangeDecorator) o).getDelegate();
   return getInstance(delegate);
 }
返回实例;

}

}

  • dispatcherhandlerhandlemethodeinterceptor实现了interceptor接口周围的instancemethods。它的afterMethod METHOD创建AbstractSpan,设置URL和METHOD的标签,执行span.prepareForAsync(),注册Mono的doFinally Consumer,设置span的operationName,statusCode,是否有异常,最后执行span.asyncFinish()

ServerWebExchange Instrumentation

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/ v5/define/ServerWebExchangeInstrumentation.java

公共类 ServerWebExchangeInstrumentation 扩展 ClassInstanceMethodsEnhancePluginDefine {

@Override

public ConstructorInterceptPoint[] getConstructorsInterceptPoints() {

 return new ConstructorInterceptPoint\[\]{
   新的构造函数拦截点(){
     @Override
      public ElementMatcher<MethodDescription> getConstructorMatcher() {
         return any();
      }
     @Override
      public String getConstructorInterceptor() {
       返回“org.apache.skywalking.apm.plugin.spring.webflux.v5.ServerWebExchangeConstructorInterceptor”;
      }
    }
 };

}

@Override

public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {

 return new InstanceMethodsInterceptPoint\[0\];

}

@Override

受保护的 ClassMatch 增强类() {

 return byName("org.springframework.web.server.adapter.DefaultServerWebExchange");

}

}

  • Serverwebexchange指令继承classinstancemethods enhanceplugindefine,使用org.apache.skywalking.apm.plugin.spring.webplus.v5.serverwebexchangeconstructorinterceptor来增强org.springframework.web.server.adapter.DefaultServerWebExchange的所有方法

ServerWebExchangeConstructorInterceptor

skywalking-6.6.0/apm-sniffer/optional-plugins/optional-spring-plugins/spring-webflux-5.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/spring/webflux/ v5/ServerWebExchangeConstructorInterceptor.java

公共类 ServerWebExchangeConstructorInterceptor 实现 InstanceConstructorInterceptor {

@Override

public void onConstruct(EnhancedInstance objInst, Object[] allArguments) {

}

}

  • ServerWebExchangeConstructorInterceptor 当前为空

总结

DispatcherHandlerInstrumentation继承ClassInstanceMethodsEnhancePluginDefine,使用org.apache.skywalking.apm.plugin.spring.webplus.v5.dispatcherhandlerhandlermethodinterceptor增强org.springframework.web.reactive.DispatcherHandler的handle方法; serverwebexchangeinstruction 继承 ClassInstanceMethodsEnhancePluginDefine,它使用 org.apache.skywalking.apm.plugin.spring.webplus.v5.serverwebexchangeconstructorinterceptor 增强 org.springframework.web.server.adapter.DefaultServerWebExchange 的所有方法

文档

  • DispatcherHandlerInstrumentation

  • ServerWebExchange Instrumentation

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐