Created
January 28, 2020 15:25
-
-
Save Ananto30/6789842bb604019b701784a8ceaf9fb2 to your computer and use it in GitHub Desktop.
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
@Component | |
@Slf4j | |
public class TraceIdFilter implements WebFilter { | |
@Override | |
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) { | |
Map<String, String> headers = exchange.getRequest().getHeaders().toSingleValueMap(); | |
return chain.filter(exchange) | |
.subscriberContext(context -> { | |
var traceId = ""; | |
if (headers.containsKey("X-B3-TRACEID")) { | |
traceId = headers.get("X-B3-TRACEID"); | |
MDC.put("X-B3-TraceId", traceId); | |
} else if (!exchange.getRequest().getURI().getPath().contains("/actuator")) { | |
log.warn("TRACE_ID not present in header: {}", exchange.getRequest().getURI()); | |
} | |
// simple hack to provide the context with the exchange, so the whole chain can get the same trace id | |
Context contextTmp = context.put("X-B3-TraceId", traceId); | |
exchange.getAttributes().put("X-B3-TraceId", traceId); | |
return contextTmp; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment