-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
189 additions
and
78 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
20 changes: 20 additions & 0 deletions
20
src/main/java/it/pagopa/interop/signalhub/persister/config/ReactorConfiguration.java
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,20 @@ | ||
package it.pagopa.interop.signalhub.persister.config; | ||
|
||
|
||
import it.pagopa.interop.signalhub.persister.logging.ContextLifter; | ||
import it.pagopa.interop.signalhub.persister.logging.MdcContextLifter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import reactor.core.publisher.Hooks; | ||
import reactor.core.publisher.Operators; | ||
|
||
@Configuration | ||
public class ReactorConfiguration { | ||
|
||
|
||
@Bean | ||
public void contextLifterConfiguration() { | ||
Hooks.onEachOperator(MdcContextLifter.class.getSimpleName(), | ||
Operators.lift((sc, sub) -> new ContextLifter<>(sub))); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/it/pagopa/interop/signalhub/persister/logging/ContextLifter.java
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,55 @@ | ||
package it.pagopa.interop.signalhub.persister.logging; | ||
|
||
import org.reactivestreams.Subscription; | ||
import reactor.core.CoreSubscriber; | ||
import reactor.util.context.Context; | ||
|
||
public class ContextLifter<T> implements CoreSubscriber<T> { | ||
private final CoreSubscriber<T> actualSubscriber; | ||
private final Context context; | ||
|
||
public ContextLifter(CoreSubscriber<T> actualSubscriber) { | ||
this.actualSubscriber = actualSubscriber; | ||
this.context = actualSubscriber.currentContext(); | ||
} | ||
|
||
@Override | ||
public Context currentContext() { | ||
return context; | ||
} | ||
|
||
@Override | ||
public void onSubscribe(Subscription subscription) { | ||
actualSubscriber.onSubscribe(subscription); | ||
} | ||
|
||
@Override | ||
public void onNext(T t) { | ||
MdcContextLifter.setContextToMdc(context); | ||
try { | ||
actualSubscriber.onNext(t); | ||
} finally { | ||
MdcContextLifter.clearMdc(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Throwable throwable) { | ||
MdcContextLifter.setContextToMdc(context); | ||
try { | ||
actualSubscriber.onError(throwable); | ||
} finally { | ||
MdcContextLifter.clearMdc(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
MdcContextLifter.setContextToMdc(context); | ||
try { | ||
actualSubscriber.onComplete(); | ||
} finally { | ||
MdcContextLifter.clearMdc(); | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/it/pagopa/interop/signalhub/persister/logging/MdcContextLifter.java
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,40 @@ | ||
package it.pagopa.interop.signalhub.persister.logging; | ||
|
||
import org.slf4j.MDC; | ||
import reactor.core.publisher.Signal; | ||
import reactor.util.context.Context; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
import static it.pagopa.interop.signalhub.persister.utils.Const.TRACE_ID_KEY; | ||
|
||
public class MdcContextLifter implements Consumer<Signal<?>> { | ||
|
||
@Override | ||
public void accept(Signal<?> signal) { | ||
if (!signal.isOnComplete() && !signal.isOnError()) { | ||
Optional<Map.Entry<Object, Object>> context = signal.getContextView().stream() | ||
.filter(cxt -> cxt.getKey().equals(TRACE_ID_KEY)) | ||
.findFirst(); | ||
|
||
context.ifPresent(ctx -> MDC.put(TRACE_ID_KEY, (String)ctx.getValue())); | ||
} else { | ||
MDC.clear(); | ||
} | ||
} | ||
|
||
public static void setContextToMdc(Context context) { | ||
context.stream().forEach(entry -> { | ||
if (entry.getKey().equals(TRACE_ID_KEY)){ | ||
MDC.put(TRACE_ID_KEY, (String) entry.getValue()); | ||
} | ||
}); | ||
} | ||
|
||
public static void clearMdc(){ | ||
MDC.clear(); | ||
} | ||
|
||
} |
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
5 changes: 5 additions & 0 deletions
5
src/main/java/it/pagopa/interop/signalhub/persister/utils/Const.java
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,5 @@ | ||
package it.pagopa.interop.signalhub.persister.utils; | ||
|
||
public class Const { | ||
public static final String TRACE_ID_KEY = "traceId"; | ||
} |
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
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
Oops, something went wrong.