-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Action and a Filter for MDC. By default logs a UUID for each re…
…quest.
- Loading branch information
Showing
6 changed files
with
112 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package zyx.cba; | ||
|
||
import play.mvc.With; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* @author rishabh | ||
*/ | ||
@With(MappedDiagnosticContextAction.class) | ||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface EnableMDC { | ||
boolean value() default true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package zyx.cba; | ||
|
||
|
||
import org.slf4j.MDC; | ||
import play.mvc.Action; | ||
import play.mvc.Http; | ||
import play.mvc.Result; | ||
|
||
import java.util.UUID; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* @author rishabh | ||
*/ | ||
public class MappedDiagnosticContextAction extends Action<EnableMDC> { | ||
public CompletionStage<Result> call(Http.Context ctx) { | ||
if (configuration.value()) { | ||
MDC.put("X-UUID", UUID.randomUUID().toString()); | ||
} | ||
return delegate.call(ctx).whenComplete((result, throwable) -> { | ||
if (configuration.value()) { | ||
MDC.remove("X-UUID"); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package zyx.cba; | ||
|
||
import akka.stream.Materializer; | ||
import org.slf4j.MDC; | ||
import play.mvc.Filter; | ||
import play.mvc.Http; | ||
import play.mvc.Result; | ||
|
||
import javax.inject.Inject; | ||
import java.util.UUID; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.Executor; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* @author rishabh | ||
*/ | ||
public class MappedDiagnosticContextFilter extends Filter { | ||
|
||
private final Executor exec; | ||
|
||
/** | ||
* @param mat This object is needed to handle streaming of requests | ||
* and responses. | ||
* @param exec This class is needed to execute code asynchronously. | ||
* It is used below by the <code>thenAsyncApply</code> method. | ||
*/ | ||
@Inject | ||
public MappedDiagnosticContextFilter(Materializer mat, Executor exec) { | ||
super(mat); | ||
this.exec = exec; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Result> apply(Function<Http.RequestHeader, CompletionStage<Result>> next, | ||
Http.RequestHeader requestHeader) { | ||
|
||
MDC.put("X-UUID", UUID.randomUUID().toString()); | ||
return next.apply(requestHeader).thenApplyAsync( | ||
result -> { | ||
MDC.remove("X-UUID"); | ||
return result; | ||
}, | ||
exec | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters