skywalking上报请求参数及日志打印traceId

skywalking上报请求参数及日志打印traceId_第1张图片

前言

我们通过skywalking来查看我们应用的性能,有时看到哪个请求较慢时,我们想看下请求参数。有时候需要打印traceId到我们的日志中,通过elk根绝traceId查询日志详情,面对着这两场景,记录下解决方案。

上报请求参数给sky

引入pom

		<dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-trace</artifactId>
            <version>${skywalking.version}</version>
        </dependency>

做一层filter,拦截请求,上报一个tracespan,至于怎样拦截post请求,可以查看我之前的博客。

@Slf4j
@WebFilter(filterName = "requestWrapperFilter", urlPatterns = "/*")
public class RequestFilter implements Filter {

    public static final String TRACE_ID = "trace";
    public static final String SPAN_ID = "span";

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    /**
     * 初始化请求链路信息:唯一key,日志初始化,body包装防止获取日志打印时后续不能继续使用
     */
    @Override
    @Trace(operationName = "requestWrapperFilter")
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        String contextString = ((HttpServletRequest) request).getHeader(ProjectContext.CONTEXT_KEY);
        if (Objects.nonNull(contextString)) {
            ProjectContext.fromString(contextString);
        } else {
            // 无内容时,也自动初始化
            ProjectContext.initContext();
        }
        initLog();

        CustomHttpServletRequestWrapper requestWrapper = new CustomHttpServletRequestWrapper(
            (HttpServletRequest) request);
        String uri = requestWrapper.getRequestURI();
        String remoteAddr = requestWrapper.getRemoteAddr();
        String method = requestWrapper.getMethod();

        String requestStr;

        if (method.equals(RequestMethod.GET.name())) {
            requestStr = GsonUtil.toJson(requestWrapper.getParameterMap());
        } else {
            requestStr = requestWrapper.getBody();
        }
        ActiveSpan.tag("request", requestStr);
        log.info(
            "url:{} , method:{}, clientIp:{}, params:{}", uri, method, remoteAddr, requestStr);
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }

    public static void initLog() {
        MDC.put(TRACE_ID, ProjectContext.getContext().getTraceId());
        MDC.put(SPAN_ID, ProjectContext.getContext().getSpanId());
    }
}

配置sky的agent,启动项目访问。再次查看sky的面板。

-javaagent:C://apache-skywalking-apm-6.5.0//apache-skywalking-apm-bin//agent//skywalking-agent.jar -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.56.121:11800 -DSW_AGENT_NAME=spring-common-web

skywalking上报请求参数及日志打印traceId_第2张图片

打印sky的traceId

  • 引入pom
		<dependency>
            <groupId>org.apache.skywalkinggroupId>
            <artifactId>apm-toolkit-logback-1.xartifactId>
            <version>${skywalking.version}version>
        dependency>

修改logback文件


<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
            <layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level logger_name:%logger{36} - [%tid] - message:%msg%npattern>
            layout>
        encoder>

再次查看日志输入可以看到

你可能感兴趣的:(spring,cloud)