Skip to content

Instantly share code, notes, and snippets.

@Abhi-Codes
Last active March 2, 2024 08:24
Show Gist options
  • Save Abhi-Codes/5c0f04e0a14e8e579db03d4e9a63d452 to your computer and use it in GitHub Desktop.
Save Abhi-Codes/5c0f04e0a14e8e579db03d4e9a63d452 to your computer and use it in GitHub Desktop.
RequestHeaderInterceptor
import io.micrometer.context.ContextRegistry;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.slf4j.MDC;
import org.springframework.graphql.server.WebGraphQlInterceptor;
import org.springframework.graphql.server.WebGraphQlRequest;
import org.springframework.graphql.server.WebGraphQlResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import reactor.util.context.Context;
import java.util.Collections;
import java.util.UUID;
@Component
@Slf4j
public class RequestHeaderInterceptor implements WebGraphQlInterceptor {
public static final String TID_HEADER_KEY = "tid";
private static final String TRANSACTION_ID_KEY = "txId";
@Override
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
//Passing txId from gateway in context and MDC
String tid = request.getHeaders().getFirst(TID_HEADER_KEY);
if (tid == null) {
log.warn("tid not found in request header, generating new tid");
tid = UUID.randomUUID().toString();
}
//Register a ThreadLocalAccessor that will help transferring
//the transaction id from Log4j's ThreadContext
//from and to various context objects
ContextRegistry.getInstance().registerThreadLocalAccessor(
TRANSACTION_ID_KEY,
() -> MDC.get(TRANSACTION_ID_KEY),
value1 -> MDC.put(TRANSACTION_ID_KEY, value1),
() -> MDC.remove(TRANSACTION_ID_KEY));
//Store the transaction id in MDC
MDC.put(TRANSACTION_ID_KEY,tid);
return chain.next(request).contextWrite(Context.of(TRANSACTION_ID_KEY,tid));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment