package com.czg.config;

import cn.hutool.core.util.StrUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.slf4j.MDC;

/**
 * @author ww
 * @description
 */
@Activate(group = CommonConstants.PROVIDER)
public class TraceProviderFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String traceId = invocation.getAttachment("traceId");
        if (StrUtil.isBlank(traceId)) {
            traceId = StrUtil.uuid();
        }

        // 设置到 MDC
        MDC.put("traceId", traceId);
        return invoker.invoke(invocation);
    }
}