Skip to content

Commit

Permalink
Log an error if an exception occurs during logging of diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvisser authored and GuusdeWit committed Oct 2, 2023
1 parent cbde588 commit a4e3dba
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/main/java/io/github/rabobank/shadow_tool/ShadowFlow.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import org.javers.core.diff.changetype.PropertyChange;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

import javax.crypto.Cipher;
import java.security.PublicKey;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -32,11 +34,10 @@ public class ShadowFlow<T> {
private static final Logger logger = LoggerFactory.getLogger(ShadowFlow.class);
private static final String INSTANCE_PREFIX_FORMAT = "[instance=%s]";
private static final String DEFAULT_INSTANCE_NAME = "default";
private static final String MESSAGE_FORMAT = "{} Calling new flow: {}";
private static final String CALLING_NEW_FLOW = "{} Calling new flow: {}";
private static final String DEFAULT_ALGORITHM = "RSA";
private static final String DEFAULT_ALGORITHM_MODE_PADDING =
DEFAULT_ALGORITHM + "/ECB/OAEPWITHSHA-256ANDMGF1PADDING";

private final int percentage;
private final ExecutorService executorService;
private final EncryptionService encryptionService;
Expand Down Expand Up @@ -131,7 +132,7 @@ public Mono<T> compare(final Mono<T> currentFlow, final Mono<T> newFlow) {

return Mono.deferContextual(contextView ->
currentFlow.doOnNext(currentResponse -> {
logger.info(MESSAGE_FORMAT, instanceNameLogPrefix, callNewFlow);
logger.info(CALLING_NEW_FLOW, instanceNameLogPrefix, callNewFlow);
if (callNewFlow) {
newFlow.doOnNext(newResponse -> logDifferences(javers.compare(currentResponse, newResponse)))
.contextWrite(contextView)
Expand Down Expand Up @@ -161,7 +162,7 @@ public Mono<Collection<T>> compareCollections(final Mono<? extends Collection<T>

return Mono.deferContextual(contextView ->
currentFlow.doOnNext(currentResponse -> {
logger.info(MESSAGE_FORMAT, instanceNameLogPrefix, callNewFlow);
logger.info(CALLING_NEW_FLOW, instanceNameLogPrefix, callNewFlow);
if (callNewFlow) {
newFlow.doOnNext(newResponse -> logDifferences(javers.compareCollections(currentResponse, newResponse, clazz)))
.contextWrite(contextView)
Expand All @@ -173,17 +174,29 @@ public Mono<Collection<T>> compareCollections(final Mono<? extends Collection<T>

private void doShadowFlow(final Supplier<Diff> diffSupplier) {
final var callNewFlow = shouldCallNewFlow();
logger.info(MESSAGE_FORMAT, instanceNameLogPrefix, callNewFlow);
final var contextMap = MDC.getCopyOfContextMap();
logger.info(CALLING_NEW_FLOW, instanceNameLogPrefix, callNewFlow);

if (callNewFlow) {
try {
executorService.submit(() -> logDifferences(diffSupplier.get()));
executorService.submit(() -> logDifferenceWithMdc(diffSupplier, contextMap));
} catch (final Exception e) {
logger.error("{} Failed to run the shadow flow", instanceNameLogPrefix, e);
}
}
}

private void logDifferenceWithMdc(final Supplier<Diff> diffSupplier, final Map<String, String> contextMap) {
if (contextMap != null) MDC.setContextMap(contextMap);
try {
logDifferences(diffSupplier.get());
} catch (final Exception e) {
logger.error("{} Failed to run the shadow flow", instanceNameLogPrefix, e);
} finally {
MDC.clear();
}
}

private void logDifferences(final Diff differences) {
if (differences.hasChanges()) {
final var propertyNames = differences.getChanges().stream()
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>

<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>

0 comments on commit a4e3dba

Please sign in to comment.