共计 7103 个字符,预计需要花费 18 分钟才能阅读完成。
某些业务需要须要追踪咱们的接口拜访状况,也就是把申请和响应记录下来。根本的记录维度蕴含了申请入参(门路 query 参数,申请体)、申请门路(uri)、申请办法(method)、申请头(headers)以及响应状态、响应头、甚至蕴含了敏感的响应体等等。明天总结了几种办法,你能够按需抉择。
申请追踪的实现形式
网关层
很多网关设施都具备 httptrace 的性能,能够帮忙咱们集中记录申请流量的状况。Orange、Kong、Apache Apisix 这些基于 Nginx 的网关都具备该能力,就连 Nginx 自身也提供了记录 httptrace 日志的能力。
长处 是能够集中的治理 httptrace 日志,免开发;毛病 是技术要求高,须要配套的散发、存储、查问的设施。
Spring Boot Actuator
在 Spring Boot 中,其实提供了简略的追踪性能。你只须要集成:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
开启/actuator/httptrace
:
management:
endpoints:
web:
exposure:
include: 'httptrace'
就能够通过 http://server:port/actuator/httptrace
获取最近的 Http 申请信息了。
不过在最新的版本中可能须要显式的申明这些追踪信息的存储形式,也就是实现
HttpTraceRepository
接口并注入Spring IoC。
例如放在内存中并限度为最近的 100 条(不举荐生产应用):
@Bean
public HttpTraceRepository httpTraceRepository(){return new InMemoryHttpTraceRepository();
}
追踪日志以 json
格局出现:
记录的维度不多,当然如果够用的话能够试试。
长处 在于集成起来简略,简直罢黜开发;毛病 在于记录的维度不多,而且须要搭建缓冲生产这些日志信息的设施。
CommonsRequestLoggingFilter
Spring Web模块还提供了一个过滤器CommonsRequestLoggingFilter
,它能够对申请的细节进行日志输入。配置起来也比较简单:
@Bean
CommonsRequestLoggingFilter loggingFilter(){CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
// 记录 客户端 IP 信息
loggingFilter.setIncludeClientInfo(true);
// 记录申请头
loggingFilter.setIncludeHeaders(true);
// 如果记录申请头的话,能够指定哪些记录,哪些不记录
// loggingFilter.setHeaderPredicate();
// 记录 申请体 特地是 POST 申请的 body 参数
loggingFilter.setIncludePayload(true);
// 申请体的大小限度 默认 50
loggingFilter.setMaxPayloadLength(10000);
// 记录申请门路中的 query 参数
loggingFilter.setIncludeQueryString(true);
return loggingFilter;
}
而且必须开启对 CommonsRequestLoggingFilter
的debug日志:
logging:
level:
org:
springframework:
web:
filter:
CommonsRequestLoggingFilter: debug
一次申请会输入两次日志,一次是在第一次通过过滤器前;一次是实现过滤器链后。
这里多说一句其实能够革新成输入 json 格局的。
长处 是灵便配置、而且对申请追踪的维度全面,毛病 是只记录申请而不记录响应。
ResponseBodyAdvice
Spring Boot 对立返回体其实也能记录,须要自行实现。这里借鉴了 CommonsRequestLoggingFilter
解析申请的办法。响应体也能够获取了,不过响应头和状态因为生命周期还不分明,这里获取还不分明是否适合,不过这是一个思路。
/**
* @author felord.cn
* @since 1.0.8.RELEASE
*/
@Slf4j
@RestControllerAdvice(basePackages = {"cn.felord.logging"})
public class RestBodyAdvice implements ResponseBodyAdvice<Object> {
private static final int DEFAULT_MAX_PAYLOAD_LENGTH = 10000;
public static final String REQUEST_MESSAGE_PREFIX = "Request [";
public static final String REQUEST_MESSAGE_SUFFIX = "]";
private ObjectMapper objectMapper = new ObjectMapper();
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {return true;}
@SneakyThrows
@Override
public Object beforeBodyWrite(Object body,
MethodParameter returnType,
MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request,
ServerHttpResponse response) {ServletServerHttpRequest servletServerHttpRequest = (ServletServerHttpRequest) request;
log.debug(createRequestMessage(servletServerHttpRequest.getServletRequest(), REQUEST_MESSAGE_PREFIX, REQUEST_MESSAGE_SUFFIX));
Rest<Object> objectRest;
if (body == null) {objectRest = RestBody.okData(Collections.emptyMap());
} else if (Rest.class.isAssignableFrom(body.getClass())) {objectRest = (Rest<Object>) body;
}
else if (checkPrimitive(body)) {return RestBody.okData(Collections.singletonMap("result", body));
}else {objectRest = RestBody.okData(body);
}
log.debug("Response Body ["+ objectMapper.writeValueAsString(objectRest) +"]");
return objectRest;
}
private boolean checkPrimitive(Object body) {Class<?> clazz = body.getClass();
return clazz.isPrimitive()
|| clazz.isArray()
|| Collection.class.isAssignableFrom(clazz)
|| body instanceof Number
|| body instanceof Boolean
|| body instanceof Character
|| body instanceof String;
}
protected String createRequestMessage(HttpServletRequest request, String prefix, String suffix) {StringBuilder msg = new StringBuilder();
msg.append(prefix);
msg.append(request.getMethod()).append(" ");
msg.append(request.getRequestURI());
String queryString = request.getQueryString();
if (queryString != null) {msg.append('?').append(queryString);
}
String client = request.getRemoteAddr();
if (StringUtils.hasLength(client)) {msg.append(", client=").append(client);
}
HttpSession session = request.getSession(false);
if (session != null) {msg.append(", session=").append(session.getId());
}
String user = request.getRemoteUser();
if (user != null) {msg.append(", user=").append(user);
}
HttpHeaders headers = new ServletServerHttpRequest(request).getHeaders();
msg.append(", headers=").append(headers);
String payload = getMessagePayload(request);
if (payload != null) {msg.append(", payload=").append(payload);
}
msg.append(suffix);
return msg.toString();}
protected String getMessagePayload(HttpServletRequest request) {
ContentCachingRequestWrapper wrapper =
WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {int length = Math.min(buf.length, DEFAULT_MAX_PAYLOAD_LENGTH);
try {return new String(buf, 0, length, wrapper.getCharacterEncoding());
} catch (UnsupportedEncodingException ex) {return "[unknown]";
}
}
}
return null;
}
}
别忘记配置
ResponseBodyAdvice
的 logging 级别为DEBUG
。
logstash-logback-encoder
这个是 logstash 的 logback 编码器,能够结构化输入 httptrace 为 json。引入:
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
在 logback 的配置中减少一个 ConsoleAppender
为LogstashEncoder
:
<configuration>
<appender name="jsonConsoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="jsonConsoleAppender"/>
</root>
</configuration>
而后同样实现一个解析的Filter
:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
/**
* @author felord.cn
* @since 1.0.8.RELEASE
*/
@Order(1)
@Component
public class MDCFilter implements Filter {private final Logger LOGGER = LoggerFactory.getLogger(MDCFilter.class);
private final String X_REQUEST_ID = "X-Request-ID";
@Override
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
try {addXRequestId(req);
LOGGER.info("path: {}, method: {}, query {}",
req.getRequestURI(), req.getMethod(), req.getQueryString());
res.setHeader(X_REQUEST_ID, MDC.get(X_REQUEST_ID));
chain.doFilter(request, response);
} finally {LOGGER.info("statusCode {}, path: {}, method: {}, query {}",
res.getStatus(), req.getRequestURI(), req.getMethod(), req.getQueryString());
MDC.clear();}
}
private void addXRequestId(HttpServletRequest request) {String xRequestId = request.getHeader(X_REQUEST_ID);
if (xRequestId == null) {MDC.put(X_REQUEST_ID, UUID.randomUUID().toString());
} else {MDC.put(X_REQUEST_ID, xRequestId);
}
}
}
这里解析形式其实还能够更加精密一些。
岂但能够记录接口申请日志,还能够结构化为 json:
{"@timestamp":"2021-08-10T23:48:51.322+08:00","@version":"1","message":"statusCode 200, path: /log/get, method: GET, query foo=xxx&bar=ooo","logger_name":"cn.felord.logging.MDCFilter","thread_name":"http-nio-8080-exec-1","level":"INFO","level_value":20000,"X-Request-ID":"7c0db56c-b1f2-4d85-ad9a-7ead67660f96"}
总结
明天介绍了不少记录追踪接口申请响应的办法,绝对都比较简单,如果你的我的项目做大了可能就要用到链路追踪,当前有机会了再补这个坑。当然或者你有更好的形式,欢送留言分享。
关注公众号:Felordcn 获取更多资讯
集体博客:https://felord.cn