美文网首页K8s
skywalking展示http请求和响应

skywalking展示http请求和响应

作者: 十毛tenmao | 来源:发表于2021-10-21 23:47 被阅读0次

使用skywalking跟踪请求的时候,是看不到http请求的参数的,这样不方便定位问题。本文通过自定义的方式(ActiveSpan.tag),实现了http请求和响应的输出,方便快速定位问题

效果图

可以在请求中看到自定义请求信息input和返回值output,方便快速定位问题


实现请求和响应的输出

  • 添加依赖
<dependency>
    <groupId>org.apache.skywalking</groupId>
    <artifactId>apm-toolkit-trace</artifactId>
    <version>8.7.0</version>
    <scope>provided</scope>
</dependency>
  • 使用ActiveSpan.tag输出到skywalking
ActiveSpan.tag("input", sb.toString());
  • 实现http请求和返回值的的输出

因为HttpServletRequestHttpServletResponse中的body只能读取一次,如果在Filte中读取的话,应用本身就读取不到,所以需要使用ContentCachingRequestWrapperContentCachingResponseWrapper

@Slf4j
@Component
public class ApmHttpInfo extends HttpFilter {
    private static final ImmutableSet<String> IGNORED_HEADERS;
    static {
        Set<String> ignoredHeaders = ImmutableSet.of(
                        "Content-Type",
                        "User-Agent",
                        "Accept",
                        "Cache-Control",
                        "Postman-Token",
                        "Host",
                        "Accept-Encoding",
                        "Connection",
                        "Content-Length")
                .stream()
                .map(String::toUpperCase)
                .collect(Collectors.toSet());
        IGNORED_HEADERS = ImmutableSet.copyOf(ignoredHeaders);
    }

    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);

        try {
            filterChain.doFilter(requestWrapper, responseWrapper);
        } finally {
            try {
                //构造请求信息: 比如 curl -X GET http://localhost:18080/getPerson?id=1 -H 'token: me-token' -d '{ "name": "hello" }'
                //构造请求的方法&URL&参数
                StringBuilder sb = new StringBuilder("curl")
                        .append(" -X ").append(request.getMethod())
                        .append(" ").append(request.getRequestURL().toString());
                if (StringUtils.hasLength(request.getQueryString())) {
                    sb.append("?").append(request.getQueryString());
                }

                //构造header
                Enumeration<String> headerNames = request.getHeaderNames();
                while (headerNames.hasMoreElements()) {
                    String headerName = headerNames.nextElement();
                    if (!IGNORED_HEADERS.contains(headerName.toUpperCase())) {
                        sb.append(" -H '").append(headerName).append(": ").append(request.getHeader(headerName)).append("'");
                    }
                }

                //获取body
                String body = new String(requestWrapper.getContentAsByteArray(), StandardCharsets.UTF_8);
                if (StringUtils.hasLength(body)) {
                    sb.append(" -d '").append(body).append("'");
                }
                //输出到input
                ActiveSpan.tag("input", sb.toString());

                //获取返回值body
                String responseBody = new String(responseWrapper.getContentAsByteArray(), StandardCharsets.UTF_8);
                //输出到output
                ActiveSpan.tag("output", responseBody);
            } catch (Exception e) {
                log.warn("fail to build http log", e);
            } finally {
                //这一行必须添加,否则就一直不返回
                responseWrapper.copyBodyToResponse();
            }
        }
    }
}

参考

相关文章

  • skywalking展示http请求和响应

    使用skywalking跟踪请求的时候,是看不到http请求的参数的,这样不方便定位问题。本文通过自定义的方式(A...

  • HTTP请求和响应

    1.HTTP工作原理 HTTP协议工作于客户端-服务端架构上。浏览器作为HTTP客户端通过URL向HTTP服务端即...

  • http 请求和响应

    http请求: 1. 请求行 实例:GET /0606/01.php HTTP/1.1 请求行分为三部分:请求方法...

  • http请求和响应

    请求 curl -s -v -H "Frank: xxx" -- "https://www.baidu.com"对...

  • 一次完整的HTTP请求与响应涉及面有多广?

    本文以HTTP请求和响应的过程来讲解涉及到的相关知识点。 一、 HTTP请求和响应步骤 图片来自:理解Http请求...

  • 网络基础与 Node.js Server

    网络基础 网络与 IP 前面说了,请求和响应都是遵循 HTTP 协议的,HTTP 只是规定了请求和响应时那 4 个...

  • 一次完整的HTTP请求与响应涉及了哪些知识?

    本文以HTTP请求和响应的过程来讲解涉及到的相关知识点。 一、 HTTP请求和响应步骤 以上完整表示了HTTP请求...

  • HTTP简单认识

    1、HTTP的请求和响应 HTTP的请求和响应都包含4个部分,在命令行输入curl -v www.baidu.co...

  • 《图解HTTP》笔记(二)

    HTTP协议 Http是一种无状态协议。协议对于发送过的请求和响应之间的通信状态不进行保存。 客户端发送HTTP请...

  • 《图解HTTP》笔记概要2

    HTTP首部 HTTP协议的请求和响应报文中必定包含HTTP首部。首部内容为客户端和服务器分别处理请求和响应所提供...

网友评论

    本文标题:skywalking展示http请求和响应

    本文链接:https://www.haomeiwen.com/subject/nexcaltx.html