zoukankan      html  css  js  c++  java
  • 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";

    }

  • 相关阅读:
    Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String
    edu30F. Forbidden Indices
    QQ登录模拟GUI综合
    Thread与Runnable线程继承与实现
    windowsProgram系统强制隐藏解除代码
    最长公共上升子序列(LCIS问题)
    分层图学习笔记 & [JLOI2011]飞行路线题解
    Luogu P1441 砝码称重
    NOIP 2020 复习计划
    浅析BST二叉搜索树
  • 原文地址:https://www.cnblogs.com/ymqj520/p/14047024.html
Copyright © 2011-2022 走看看