Skip to content

Trace Id

Rain Ramm edited this page May 27, 2024 · 3 revisions

Trace Id is a flow/process identifier that can be used to identify which log entry belongs to which flow. Consider scenario where user makes a request to a service, that service makes a subsequent requests to several other services and each of those has separate subprocesses running in parallel. Now consider there are N users making M such requests in parallel. How do you know how to group all the logs for each user request and how to conveniently identify to which sub process each log belongs to. This can be done with Trace Id. Trace Id is injected to MDC of each process as early as possible and with MDC it is bound to each log entry.

Enabling feature

Core library contains auto-configuration mechanism that configures the addition and propagation of Trace Id to every log entry. In order to enable auto-configuration, simply add property to configuration.

ee.bitweb.core.trace.auto-configuration=true

Following dependencies have to be added as library does not provide transitive dependencies:

implementation 'org.springframework.boot:spring-boot-starter-web'

Enabling for @Async

When using @Async to create background jobs, custom ThreadPool should be used along with TaskDecorator. There are two decorator provided by spring-core library:

  • BasicMDCTaskDecorator for when security is not included or required
  • SecurityAwareMDCTaskDecorator that copies SecurityContext to the task

Sample AsyncConfiguration

@Slf4j
@EnableAsync
@Configuration
@RequiredArgsConstructor
public class AsyncConfig implements AsyncConfigurer {

    private static final Integer CORE_POOL_SIZE = 10;
    private static final Integer MAX_POOL_SIZE = 20;
    private static final String DEFAULT_PREFIX = "CustomAsync-";

    private final ThreadTraceIdResolver resolver;

    @Override
    public Executor getAsyncExecutor() {
        log.info("Creating Async thread pool executor");

        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix(DEFAULT_PREFIX);
        executor.setTaskDecorator(new BasicMDCTaskDecorator(resolver));
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.initialize();

        return executor;
    }
}

Available configuration properties

Property Default value Description
ee.bitweb.core.trace.auto-configuration false Enables all corresponding beans and a filter. Adds the filter to filterchain.
ee.bitweb.core.trace.invoker.prefix '' Adds a prefix which can be used to add service identifiers for easier reading
ee.bitweb.core.trace.invoker.delimiter - Delimiter used for the service part.
ee.bitweb.core.trace.invoker.length 20 Length of part of trace id for corresponding service. Must be between [10, 20].
ee.bitweb.core.trace.invoker.http.headerName X-Trace-ID Header name, used to propagate trace id via HTTP
ee.bitweb.core.trace.thread.delimiter : Delimiter used to separate thread part of trace id
ee.bitweb.core.trace.thread.length 10 Length of thread part in trace id. Must be between [5, 10].
ee.bitweb.core.trace.thread.prefix '' Thread part prefix, which can be used to improve readability
ee.bitweb.core.trace.scheduler.length 20 Length of the trace id for scheduled jobs. Must be between [10, 20].
ee.bitweb.core.trace.scheduler.prefix '' Prefix for the trace id for scheduled jobs

Clone this wiki locally