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";

}

posted on 2020-11-27 11:27  月满清爵  阅读(540)  评论(0编辑  收藏  举报