-
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.
Merge remote-tracking branch 'origin/feature/poc' into feature/poc
- Loading branch information
Showing
13 changed files
with
184 additions
and
15 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
20 changes: 20 additions & 0 deletions
20
src/main/java/it/pagopa/interop/signalhub/push/service/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.push.service.config; | ||
|
||
|
||
import it.pagopa.interop.signalhub.push.service.logging.ContextLifter; | ||
import it.pagopa.interop.signalhub.push.service.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))); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
src/main/java/it/pagopa/interop/signalhub/push/service/filter/TraceIdFilter.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,42 @@ | ||
package it.pagopa.interop.signalhub.push.service.filter; | ||
|
||
import it.pagopa.interop.signalhub.push.service.config.SignalHubPushConfig; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import org.springframework.web.server.WebFilter; | ||
import org.springframework.web.server.WebFilterChain; | ||
import reactor.core.publisher.Mono; | ||
import reactor.util.context.Context; | ||
|
||
import java.util.Collections; | ||
import java.util.UUID; | ||
|
||
import static it.pagopa.interop.signalhub.push.service.utils.Const.TRACE_ID_KEY; | ||
|
||
|
||
@Component | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@AllArgsConstructor | ||
public class TraceIdFilter implements WebFilter { | ||
private final SignalHubPushConfig cfg; | ||
|
||
|
||
@Override | ||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | ||
HttpHeaders headers = exchange.getRequest().getHeaders(); | ||
var traceId = UUID.randomUUID().toString(); | ||
if (headers.containsKey(cfg.getHeaderTraceIdKey())) { | ||
traceId = headers.getFirst(cfg.getHeaderTraceIdKey()); | ||
} | ||
exchange.getResponse().getHeaders() | ||
.putIfAbsent(cfg.getHeaderTraceIdKey(), Collections.singletonList(traceId)); | ||
|
||
|
||
String finalTraceId = traceId; | ||
return chain.filter(exchange).contextWrite(Context.of(TRACE_ID_KEY, finalTraceId)); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/it/pagopa/interop/signalhub/push/service/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.push.service.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/push/service/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.push.service.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.push.service.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
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