All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.com.antcloud.api.common.trace.TracerContextUtil Maven / Gradle / Ivy

Go to download

Ant Fin Tech API SDK For Java Copyright (c) 2015-present Alipay.com, https://www.alipay.com

The newest version!
/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package cn.com.antcloud.api.common.trace;

public class TracerContextUtil {


    /**
     * 获取traceId
     * @return
     */
    public static String getTraceId() {
        AbstractTraceContext abstractTraceContext = AbstractTraceContext.get();
        if (abstractTraceContext == null) {
            return "";
        }
        String traceId = abstractTraceContext.getTraceId();

        return traceId == null ? "" : traceId;
    }

    /**
     * 设置traceId
     * @param traceId
     */
    public static void setTraceId(String traceId){
        AbstractTraceContext abstractTraceContext = AbstractTraceContext.get();
        if(abstractTraceContext == null){
            abstractTraceContext.set(new AbstractTraceContext());
        }
        if (!isBlank(traceId)) {
            abstractTraceContext.get().setTraceId(traceId);
        }
    }

    /**
     * 获取loadTestMark
     * @return
     */
    public static String getLoadTestMark() {
        AbstractTraceContext abstractTraceContext = AbstractTraceContext.get();
        if (abstractTraceContext == null) {
            return "";
        }
        String loadTestMark = abstractTraceContext.getLoadTestMark();

        return loadTestMark == null ? "" : loadTestMark;
    }

    /**
     * 设置loadTestMark
     * @param loadTestMark
     */
    public static void setLoadTestMark(String loadTestMark){
        AbstractTraceContext abstractTraceContext = AbstractTraceContext.get();
        if(abstractTraceContext == null){
            abstractTraceContext.set(new AbstractTraceContext());
        }
        if (!isBlank(loadTestMark)) {
            abstractTraceContext.get().setLoadTestMark(loadTestMark);
        }
    }


    /**
     * 获取loadTestUid
     * @return
     */
    public static String getLoadTestUid() {
        AbstractTraceContext abstractTraceContext = AbstractTraceContext.get();
        if (abstractTraceContext == null) {
            return "";
        }
        String loadTestUid = abstractTraceContext.getLoadTestUid();

        return loadTestUid == null ? "" : loadTestUid;
    }

    /**
     * 设置loadTestUid
     * @param loadTestUid
     */
    public static void setLoadTestUid(String loadTestUid){
        AbstractTraceContext abstractTraceContext = AbstractTraceContext.get();
        if(abstractTraceContext == null){
            abstractTraceContext.set(new AbstractTraceContext());
        }
        if (!isBlank(loadTestUid)) {
            abstractTraceContext.get().setLoadTestUid(loadTestUid);
        }
    }

    /**
     * 清除log上下文
     */
    public static void resetLogContext(){
        AbstractTraceContext.clear();
    }

    public static boolean isBlank(String str) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy