com.dahua.eco.base.spring.http.log.TraceLogParser Maven / Gradle / Ivy
package com.dahua.eco.base.spring.http.log;
import com.alibaba.fastjson.JSON;
import com.dahua.eco.base.spring.http.log.domain.SpanInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
public class TraceLogParser {
private static Logger logger = LoggerFactory.getLogger(TraceLogParser.class);
/**
* 从 request 中解析日志跟踪信息
*
* @param request
* @return
*/
public static TraceInfo getTraceInfo(HttpServletRequest request) {
TraceInfo traceInfo = getTraceInfo(request.getHeader(TraceLogHelper.HEADER_NAME_TRACE_ID));
return traceInfo;
}
/**
* 从 response 中解析日志跟踪信息
*
* @param response
* @return
*/
public static TraceInfo getTraceInfo(HttpServletResponse response) {
TraceInfo traceInfo = getTraceInfo(response.getHeader(TraceLogHelper.HEADER_NAME_TRACE_ID));
return traceInfo;
}
/**
* 根据从请求头中拿到的 traceId 字符串,解析追踪信息
*
* @param traceIdStr
* @return
*/
public static TraceInfo getTraceInfo(String traceIdStr) {
if (StringUtils.isEmpty(traceIdStr)) {
return null;
}
TraceInfo traceInfo = new TraceInfo();
String[] elements = traceIdStr.split(":");
if (elements == null || elements.length < 2) {
logger.error("parse trace-info failed, empty sequence: {}", traceIdStr);
return null;
}
if (StringUtils.isEmpty(elements[0]) || StringUtils.isEmpty(elements[1])) {
logger.error("parse trace-info failed, invalid traceId or sequence: {}", traceIdStr);
return null;
}
// traceId
traceInfo.setTraceId(elements[0]);
// sequence
traceInfo.setSequence(elements[1]);
try {
Integer sequenceInt = Integer.parseInt(traceInfo.getSequence());
traceInfo.setSequenceInt(sequenceInt);
} catch (Exception e) {
logger.error("parse sequence from traceId string failed: " + traceInfo.getSequence(), e);
}
return traceInfo;
}
/**
* 从 request 中解析日志跟踪信息
*
* @param request
* @return
*/
public static SpanInfo getSpanInfo(HttpServletRequest request) {
SpanInfo spanInfo = getSpanInfo(request.getHeader(TraceLogHelper.HEADER_NAME_SPAN_ID));
return spanInfo;
}
/**
* 从 response 中解析日志跟踪信息
*
* @param response
* @return
*/
public static SpanInfo getSpanInfo(HttpServletResponse response) {
SpanInfo spanInfo = getSpanInfo(response.getHeader(TraceLogHelper.HEADER_NAME_SPAN_ID));
return spanInfo;
}
/**
* 根据从请求头中拿到的 traceId 字符串,解析追踪信息
*
* @param spanIdStr
* @return
*/
public static SpanInfo getSpanInfo(String spanIdStr) {
if (StringUtils.isEmpty(spanIdStr)) {
return null;
}
SpanInfo spanInfo = JSON.parseObject(spanIdStr, SpanInfo.class);
return spanInfo;
}
/**
* @Title: inheritHeaderSource
* @Description: 从response方法里继承日志链路节点数据
* @param @param HttpServletResponse response
* @param @return 设定文件
* @return HttpHeaders 返回类型
* @throws
*/
public static HttpHeaders inheritHeaderSource(HttpServletResponse response) {
HttpHeaders headers = new HttpHeaders();
Collection headerNames = response.getHeaderNames();
if(null!=headerNames&&headerNames.size()>0) {
for (String key : headerNames) {
if(TraceLogHelper.HEADER_NAME_TRACE_ID.equals(key)||TraceLogHelper.HEADER_NAME_SPAN_ID.equals(key)) {
String value = response.getHeader(key);
headers.add(key, value);
}
}
}
return headers;
}
}