package com.games.gateway.utils;

import org.slf4j.MDC;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.util.UUID;

public class TraceUtils {

    private static final String TRACE_ID = "TRACE_ID";
    private static final String HTTP_HEADER_TRACE_ID = "X-TRACE-ID";

    public TraceUtils() {
    }
    public static void setTraceId(  ServerHttpRequest request ) {
        //之在 第一个过滤器加 TRACE_ID 就 省去了  返回结果之前的清理逻辑。
        String traceId = UUID.randomUUID().toString();
        MDC.put(TRACE_ID, traceId);
        request.mutate().header(HTTP_HEADER_TRACE_ID, traceId);
    }
    public static String getTraceId() {
        return MDC.get(TRACE_ID);
    }

    public static void removeTraceId() {
        MDC.remove(TRACE_ID);
    }

    public static void setTraceId(String internalTraceId) {
        MDC.put(TRACE_ID, internalTraceId);
    }
}
