ApiBoot Logging will create a link number (TraceID) and a cell number (SpanID) for each request, which are used to classify each request log. The Parent SpanID of the log unit under a link can be used to sort out the relationship between the superior and the subordinate.
Previous review
Understand how link numbers are passed
In each request, the link number (traceId) and cell number (spanId) are passed through the HttpHeader. The traceId and spanId will be generated actively at the start of the log, while the Parent SpanId of the start location does not exist, and the value is null.
In this way, each time you access the interface of other services in the form of restTemplate and Openfeign, the traceId and spanId generated from the starting location will be carried to the next service unit.
default number
The default number format is provided in ApiBoot Logging. It is a general format by default. There is no distinction, so it is impossible to classify the logs from the number.
Default link number
In ApiBoot Logging, the basic functions such as log collection are completed by integrating the minbox logging log component. Each time a collected log is generated, the link number (TraceID) is generated through the LoggingTraceGenerator interface. The source code of the interface is as follows:
/** * ApiBoot Logging Tracer * Create new traceId * * @author: Hengyu youth - Yu Qiyu * <p> * DateTime: 2019-07-10 17:01 * Blog: http://blog.yuqiyu.com * WebSite: http://www.jianshu.com/u/092df3f77bca * Gitee: https://gitee.com/hengboy * GitHub: https://github.com/hengboy */ public interface LoggingTraceGenerator { /** * create new traceId * * @return traceId * @throws MinBoxLoggingException exception */ String createTraceId() throws MinBoxLoggingException; }
The default link number (TraceID) of ApiBoot Logging is generated by UUID random string. The internal implementation is generated by the default implementation class LoggingDefaultTraceGenerator of LoggingTraceGenerator interface. The generated class source code is as follows:
/** * ApiBoot Logging Tracer Default Support Instance * * @author: Hengyu youth - Yu Qiyu * <p> * DateTime: 2019-07-10 17:28 * Blog: http://blog.yuqiyu.com * WebSite: http://www.jianshu.com/u/092df3f77bca * Gitee: https://gitee.com/hengboy * GitHub: https://github.com/hengboy */ public class LoggingDefaultTraceGenerator implements LoggingTraceGenerator { /** * Use UUID as the default traceId * * @return traceId * @throws MinBoxLoggingException Exception */ @Override public String createTraceId() throws MinBoxLoggingException { return UUID.randomUUID().toString(); } }
Default cell number
Unit number is the unique identification of each business unit passing through a link. In the spring cloud microservice scenario, each request sent internally through Openfeign may pass through multiple services, so that each passing service is called a unit, while the current unit unique identification string under this link is called a unit number.
Minbox logging provides the LoggingSpanGenerator interface for generating unit numbers. The source code is as follows:
/** * ApiBoot Logging Span * Create new spanId * * @author: Hengyu youth - Yu Qiyu * <p> * DateTime: 2019-07-10 17:02 * Blog: http://blog.yuqiyu.com * WebSite: http://www.jianshu.com/u/092df3f77bca * Gitee: https://gitee.com/hengboy * GitHub: https://github.com/hengboy */ public interface LoggingSpanGenerator { /** * create new spanId * * @return span id * @throws MinBoxLoggingException exception */ String createSpanId() throws MinBoxLoggingException; }
spanId is generated in the same way as traceId by default. It is UUID random string. Minbox logging provides the LoggingSpanGenerator interface to implement LoggingDefaultSpanGenerator by default. The source code is as follows:
/** * ApiBoot Logging Default Span * Use By Create New SpanId * * @author: Hengyu youth - Yu Qiyu * <p> * DateTime: 2019-07-15 17:24 * Blog: http://blog.yuqiyu.com * WebSite: http://www.jianshu.com/u/092df3f77bca * Gitee: https://gitee.com/hengboy * GitHub: https://github.com/hengboy */ public class LoggingDefaultSpanGenerator implements LoggingSpanGenerator { /** * Create New SpanId * * @return SpanId * @throws MinBoxLoggingException Exception */ @Override public String createSpanId() throws MinBoxLoggingException { return UUID.randomUUID().toString(); } }
Custom number
We can customize traceId and spanId according to our own business. We can add some elements of our own business. We only need to provide the interface LoggingTraceGenerator provided by minbox logging to generate traceId, the implementation class corresponding to LoggingSpanGenerator to generate spanId, and submit the implementation class to LoggingFaceBean management.
Custom link number
/** * Custom traceId generation policy * * @author Heng Yu junior */ public class CustomTraceIdGenerator implements LoggingTraceGenerator { /** * Link number prefix */ private static final String TRACE_ID_PREFIX = "local"; @Override public String createTraceId() throws MinBoxLoggingException { return TRACE_ID_PREFIX + UUID.randomUUID().toString().hashCode(); } }
We create a class named CustomTraceIdGenerator and implement the LoggingTraceGenerator interface. The return value of the createTraceId() method is prefixed with local -, and the hashCode value of UUID random string is spliced as the suffix.
Custom cell number
/** * Custom cell number generation strategy * * @author Heng Yu junior */ public class CustomSpanIdGenerator implements LoggingSpanGenerator { /** * Cell number prefix */ private static final String SPAN_ID_PREFIX = "group"; @Override public String createSpanId() throws MinBoxLoggingException { return SPAN_ID_PREFIX + UUID.randomUUID().toString().hashCode(); } }
We create a class named CustomSpanIdGenerator and implement the LoggingSpanGenerator interface. The return value of the createSpanId() method is prefixed by group -, and the hashCode value of UUID random string is used as the suffix.
We have created the implementation classes of the custom traceId and spanId above. We need to give the instance of the implementation class to LoggingFactoryBean management so that we can implement the custom number.
LoggingFactoryBeanCustomizer
ApiBoot Logging provides a user-defined LoggingFactoryBean interface, LoggingFactoryBeanCustomizer, through which you can modify any value allowed to be modified in LoggingFactoryBean.
We create a class named CustomCreateTraceAndSpanId and implement the LoggingFactoryBeanCustomizer interface. The source code is as follows:
/** * Custom create link and cell number * * @author Heng Yu junior * @see LoggingFactoryBeanCustomizer * @see LoggingFactoryBean * @see org.minbox.framework.logging.client.tracer.LoggingTraceGenerator * @see org.minbox.framework.logging.client.span.LoggingSpanGenerator */ @Component public class CustomCreateTraceAndSpanId implements LoggingFactoryBeanCustomizer { /** * {@link CustomTraceIdGenerator} Custom link number generation policy * {@link CustomSpanIdGenerator} Custom cell number generation strategy * * @param factoryBean {@link LoggingFactoryBean} */ @Override public void customize(LoggingFactoryBean factoryBean) { CustomTraceIdGenerator traceIdGenerator = new CustomTraceIdGenerator(); factoryBean.setTraceGenerator(traceIdGenerator); CustomSpanIdGenerator spanIdGenerator = new CustomSpanIdGenerator(); factoryBean.setSpanGenerator(spanIdGenerator); } }
Customize is a common design method in SpringBoot. ApiBoot also uses this design method. The customize() method provides an instance of LoggingFactoryBean object as a parameter. We can directly modify the default configuration defined in the internal through the setXxx method.
The default traceId generation policy can be modified through the facetory.setTraceGenerator method.
You can modify the default spanId generation policy through the facetory.setSpanGenerator method.
test
After starting the project, we will check the log printed by the console to confirm whether the modification is successful.
{ "endTime":1571711067664, "httpStatus":200, "requestBody":"", "requestHeaders":{ "accept":"*/*", "host":"localhost:8080", "user-agent":"curl/7.64.1" }, "requestIp":"0:0:0:0:0:0:0:1", "requestMethod":"GET", "requestParam":"{}", "requestUri":"/index", "responseBody":"this is index.", "responseHeaders":{}, "serviceId":"apiboot-custom-logging-traceid", "serviceIp":"127.0.0.1", "servicePort":"8080", "spanId":"group-1780993769", "startTime":1571711067643, "timeConsuming":21, "traceId":"local1111437283" }
traceId and spanId have been modified to our custom number generation strategy.
Knock on the blackboard and make a point
This chapter mainly talks about how to customize traceId and spanId. We can use LoggingFactoryBeanCustomizer to customize the depth of LoggingFactoryBean object. There are many correct postures for ApiBoot Logging, please look forward to.