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

fun.fengwk.convention4j.tracer.finisher.Slf4jSpanFinisher Maven / Gradle / Ivy

The newest version!
package fun.fengwk.convention4j.tracer.finisher;

import fun.fengwk.convention4j.common.json.JsonUtils;
import fun.fengwk.convention4j.common.util.NullSafe;
import fun.fengwk.convention4j.tracer.Reference;
import fun.fengwk.convention4j.tracer.SpanImpl;
import fun.fengwk.convention4j.tracer.util.TracerUtils;
import io.opentracing.SpanContext;
import io.opentracing.tag.Tags;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author fengwk
 */
public class Slf4jSpanFinisher implements SpanFinisher {

    private final static Logger log = LoggerFactory.getLogger("TRACE");

    @Override
    public void finish(SpanImpl span, long finishMicros) {
        try {

            SpanContext context = span.context();

            List references = span.getReferences();
            LogBean logBean = new LogBean();
            logBean.setTraceId(context.toTraceId());
            logBean.setSpanId(context.toSpanId());
            logBean.setParentSpanId(
                NullSafe.map2(TracerUtils.getChildOfReference(references),
                    Reference::getSpanContext, SpanContext::toSpanId));
            logBean.setFollowSpanIds(
                NullSafe.map2List(TracerUtils.getFollowsFromReferences(references),
                    r -> r.getSpanContext().toSpanId()));
            logBean.setOperationName(span.getOperationName());
            logBean.setStartMicros(span.getStartTimestamp());
            logBean.setFinishMicros(finishMicros);
            logBean.setCostMs(((float) (finishMicros - span.getStartTimestamp())) / 1000f);
            logBean.setTags(getTags(span));
            logBean.setLogs(getLogs(span));
            logBean.setBaggage(TracerUtils.buildBaggage(span.context().baggageItems()));

            // 日志
            boolean error = (Boolean) NullSafe.of(span.getTags()).getOrDefault(Tags.ERROR.getKey(), false);
            if (error) {
                log.error(JsonUtils.toJson(logBean));
            } else {
                log.info(JsonUtils.toJson(logBean));
            }
        } catch (Throwable ex) {
            log.error("Finish span error", ex);
        }
    }

    private Map getTags(SpanImpl span) {
        LinkedHashMap result = new LinkedHashMap<>();

        Map tags = span.getTags();
        if (tags != null) {
            for (Map.Entry entry : tags.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    result.put(entry.getKey(), entry.getValue());
                }
            }
        }

        return result;
    }

    private TreeMap getLogs(SpanImpl span) {
        TreeMap result = new TreeMap<>();

        TreeMap eventLogs = span.getEventLogs();
        TreeMap> kvLogs = span.getKvLogs();

        if (eventLogs != null) {
            for (Map.Entry entry : eventLogs.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    result.put(entry.getKey(), entry.getValue());
                }
            }
        }
        if (kvLogs != null) {
            for (Map.Entry> entry : kvLogs.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    result.put(entry.getKey(), entry.getValue());
                }
            }
        }

        return result;
    }

    @Data
    static class LogBean {

        private String operationName;
        private float costMs;
        private String traceId;
        private String spanId;
        private String parentSpanId;
        private List followSpanIds;
        private long startMicros;
        private long finishMicros;
        private Map tags;
        private TreeMap logs;
        private Map baggage;

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy