Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,29 @@ Besides that you will also have transparent integration with fallowing:

The extension itself simply gives you means to propagate the information. How you going to use it is up to you.

For instance you can apply this information to your logging MDC map. You can achieve that by registering
`RequestCorrelationInterceptor` bean. The `RequestCorrelationInterceptor` gives you only an entry point so that
For instance you can apply this information to your logging MDC map by using `@EnableRequestCorrelationAndMDC` instead of `@EnableRequestCorrelation`

```java
@EnableRequestCorrelationAndMDC
@SpringBootApplication
public class Application {

}
```

This annotation will register the `correlationId` into MDC map. You can use it for example with logback by overridden the `logging.pattern.level` property

```
logging.pattern.level: "%5p [correlationId:%X{correlationId:-}]"
```

you will get the following log

```
2017-10-29 07:57:48.009 INFO [correlationId:674f1042-a767-4d64-a209-9f0ab1753a78] 31162 --- [nio-8888-exec-1] ...
```

Or You can achieve that by registering `RequestCorrelationInterceptor` bean. The `RequestCorrelationInterceptor` gives you only an entry point so that
any fallowing operation would be able to access the correlation identifier. You may also use Spring's
[HandlerInterceptor](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/HandlerInterceptor.html)
and set the value there.
Expand All @@ -79,6 +100,12 @@ public RequestCorrelationInterceptor correlationLoggingInterceptor() {
public void afterCorrelationIdSet(String correlationId) {
MDC.put("correlationId", correlationId);
}

@Override
public void cleanUp(String correlationId) {
MDC.remove("correlationId");
}

};
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.jmnarloch.spring.request.correlation.log;

import io.jmnarloch.spring.request.correlation.api.EnableRequestCorrelation;
import org.springframework.context.annotation.Import;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(MDCAutoConfig.class)
@EnableRequestCorrelation
public @interface EnableRequestCorrelationAndMDC {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.jmnarloch.spring.request.correlation.log;

import io.jmnarloch.spring.request.correlation.api.RequestCorrelationInterceptor;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MDCAutoConfig {
@Bean
public RequestCorrelationInterceptor correlationLoggingInterceptor() {
return new RequestCorrelationInterceptor() {
@Override
public void afterCorrelationIdSet(String correlationId) {
MDC.put("correlationId", correlationId);
}

@Override
public void cleanUp(String correlationId) {
MDC.remove("correlationId");
}
};
}
}