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.CONSUMER)
public class TraceConsumerFilter implements Filter {

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