• skywalking-拦截器实现(2)



    /**
    * @author Ly
    * @create 2020/8/20 10:27
    * @desc
    **/
    @Slf4j
    public abstract class TagFilter{

    protected SkywalkingConfigurationProperties skywalkingConfigurationProperties;

    public TagFilter(SkywalkingConfigurationProperties skywalkingConfigurationProperties) {
    this.skywalkingConfigurationProperties = skywalkingConfigurationProperties;
    }

    public TagFilter() {
    }

    public void handle(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    if(skywalkingConfigurationProperties.getEnable()){
    CustomHttpServletResponseWrapper customHttpServletResponseWrapper = new CustomHttpServletResponseWrapper(response);
    CustomHttpServletRequestWrapper customHttpServletRequestWrapper = new CustomHttpServletRequestWrapper(request);
    filterChain.doFilter(customHttpServletRequestWrapper,customHttpServletResponseWrapper);
    byte[] handle = this.handle(customHttpServletResponseWrapper, customHttpServletRequestWrapper, response);
    if(handle != null){
    response.getOutputStream().write(handle);
    }
    }else{
    try {
    filterChain.doFilter(request,response);
    } catch (Exception e) {
    log.error("添加skywalking-tag失败:",e);
    }
    }
    }

    /**
    * 处理请求和响应
    * @param customHttpServletResponseWrapper
    * @param customHttpServletRequestWrapper
    * @return
    */
    protected abstract byte[] handle( CustomHttpServletResponseWrapper customHttpServletResponseWrapper, CustomHttpServletRequestWrapper customHttpServletRequestWrapper,HttpServletResponse response);

    }

    =======================================================================================================================================================



    /**
    * @author Ly
    * @create 2020/11/6 16:08
    * @desc
    **/
    @Slf4j
    @Component
    @ConditionalOnProperty(prefix = "skywalking.filter" , name = "default",havingValue = "true",matchIfMissing = true)
    public class DefaultTagFilter extends TagFilter implements Filter {

    @Autowired
    public DefaultTagFilter(SkywalkingConfigurationProperties skywalkingConfigurationProperties) {
    super(skywalkingConfigurationProperties);
    }

    public DefaultTagFilter() {
    super();
    }

    @Override
    @Trace
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    super.handle(servletRequest, servletResponse, filterChain);
    }

    @Override
    protected byte[] handle( CustomHttpServletResponseWrapper customHttpServletResponseWrapper, CustomHttpServletRequestWrapper customHttpServletRequestWrapper,HttpServletResponse response){
    try {
    String requestURI = customHttpServletRequestWrapper.getRequestURI();
    byte[] bytes = customHttpServletResponseWrapper.getBytes();
    try{
    ActiveSpan.tag(Constants.URI_TAG,requestURI);
    ActiveSpan.tag(Constants.HEAD_TAG,JSON.toJSONString(customHttpServletRequestWrapper.getAllHeaders()));
    if(HttpMethod.GET.name().equals(customHttpServletRequestWrapper.getMethod())){
    Map<String, String[]> parameterMap = customHttpServletRequestWrapper.getParameterMap();
    ActiveSpan.tag(Constants.GET_REQUEST_PARAMS_TAG, JSON.toJSONString(parameterMap));
    }else if(HttpMethod.POST.name().equals(customHttpServletRequestWrapper.getMethod())){
    String bodyParams = CustomHttpServletRequestWrapper.getBodyMap(customHttpServletRequestWrapper.getInputStream());
    ActiveSpan.tag(Constants.POST_REQUEST_PARAMS_TAG, bodyParams);
    }
    String content = null;
    if(bytes.length > Constants.RESP_DEFAULT_ALLOWED_SIZE){
    byte[] targetBytes = new byte[Constants.RESP_DEFAULT_ALLOWED_SIZE];
    System.arraycopy(bytes,0,targetBytes,0,Constants.RESP_DEFAULT_ALLOWED_SIZE);
    Map<String,String> maps = Maps.newHashMap();
    maps.put(Constants.RESPONSE_BODY_TAG,new String(targetBytes,"UTF-8"));
    content = JSON.toJSONString(maps) ;
    }else{
    content = new String(bytes,"UTF-8");
    }
    ActiveSpan.tag(Constants.RESPONSE_BODY_TAG,content);
    }catch (Exception e){
    log.error("添加skywalking信息失败:",e);
    }
    return bytes;
    } catch (Exception e) {
    log.error("处理请求响应失败!");
    return null;
    }
    }
    }


    =======================================================================================================================================================

    /**
    * @author Ly
    * @create 2020/7/1 14:19
    * @desc
    **/
    public class Constants {

    public static final String PREFIX_MATCHING = "puma.third.prefixMatching";

    public static final String FULL_MATCHING = "puma.third.fullMatching";

    public static final String GET_REQUEST_PARAMS_TAG = "G_requestParam";

    public static final String POST_REQUEST_PARAMS_TAG = "P_requestParam";

    public static final String RESPONSE_BODY_TAG = "responseBody";

    public static final String URI_TAG = "uri";

    public static final String HEAD_TAG = "head";

    public static final Integer RESP_DEFAULT_ALLOWED_SIZE = 1024 * 1024;

    public static final String DATA = "data";

    public static final String QUERY_TRACE = "queryTrace";

    }

  • 相关阅读:
    cssReset
    CSS的一些小技巧
    前端图标神器
    单例模式
    CSS 控制Html页面高度导致抖动问题的原因
    PHP中include()与require()的区别说明
    extends和implements区别
    静态,抽象类、接口、类库
    jQuery轮播图(手动点击轮播)
    jQuery实现大图轮播
  • 原文地址:https://www.cnblogs.com/ymqj520/p/14047024.html
Copyright © 2020-2023  润新知