Search in sources :

Example 1 with Span

use of org.apache.skywalking.apm.collector.storage.ui.trace.Span in project incubator-skywalking by apache.

the class TraceStackService method buildSpanList.

private List<Span> buildSpanList(String traceId, String segmentId, int applicationId, List<SpanObject> spanObjects) {
    List<Span> spans = new ArrayList<>();
    spanObjects.forEach(spanObject -> {
        Span span = new Span();
        span.setTraceId(traceId);
        span.setSegmentId(segmentId);
        span.setSpanId(spanObject.getSpanId());
        span.setParentSpanId(spanObject.getParentSpanId());
        span.setStartTime(spanObject.getStartTime());
        span.setEndTime(spanObject.getEndTime());
        span.setError(spanObject.getIsError());
        span.setLayer(spanObject.getSpanLayer().name());
        span.setType(spanObject.getSpanType().name());
        String segmentSpanId = segmentId + Const.SEGMENT_SPAN_SPLIT + String.valueOf(spanObject.getSpanId());
        span.setSegmentSpanId(segmentSpanId);
        String segmentParentSpanId = segmentId + Const.SEGMENT_SPAN_SPLIT + String.valueOf(spanObject.getParentSpanId());
        span.setSegmentParentSpanId(segmentParentSpanId);
        if (spanObject.getPeerId() == 0) {
            span.setPeer(spanObject.getPeer());
        } else {
            span.setPeer(networkAddressCacheService.getAddress(spanObject.getPeerId()));
        }
        String operationName = spanObject.getOperationName();
        if (spanObject.getOperationNameId() != 0) {
            ServiceName serviceName = serviceNameCacheService.get(spanObject.getOperationNameId());
            if (ObjectUtils.isNotEmpty(serviceName)) {
                operationName = serviceName.getServiceName();
            } else {
                operationName = Const.EMPTY_STRING;
            }
        }
        span.setOperationName(operationName);
        String applicationCode = applicationCacheService.getApplicationById(applicationId).getApplicationCode();
        span.setApplicationCode(applicationCode);
        if (spanObject.getComponentId() == 0) {
            span.setComponent(spanObject.getComponent());
        } else {
            span.setComponent(ComponentsDefine.getInstance().getComponentName(spanObject.getComponentId()));
        }
        spanObject.getRefsList().forEach(reference -> {
            Ref ref = new Ref();
            ref.setTraceId(traceId);
            switch(reference.getRefType()) {
                case CrossThread:
                    ref.setType(RefType.CROSS_THREAD);
                    break;
                case CrossProcess:
                    ref.setType(RefType.CROSS_PROCESS);
                    break;
            }
            ref.setParentSpanId(reference.getParentSpanId());
            UniqueId uniqueId = reference.getParentTraceSegmentId();
            StringBuilder segmentIdBuilder = new StringBuilder();
            for (int i = 0; i < uniqueId.getIdPartsList().size(); i++) {
                if (i == 0) {
                    segmentIdBuilder.append(String.valueOf(uniqueId.getIdPartsList().get(i)));
                } else {
                    segmentIdBuilder.append(".").append(String.valueOf(uniqueId.getIdPartsList().get(i)));
                }
            }
            ref.setParentSegmentId(segmentIdBuilder.toString());
            span.setSegmentParentSpanId(ref.getParentSegmentId() + Const.SEGMENT_SPAN_SPLIT + String.valueOf(ref.getParentSpanId()));
            span.getRefs().add(ref);
        });
        spanObject.getTagsList().forEach(tag -> {
            KeyValue keyValue = new KeyValue();
            keyValue.setKey(tag.getKey());
            keyValue.setValue(tag.getValue());
            span.getTags().add(keyValue);
        });
        spanObject.getLogsList().forEach(log -> {
            LogEntity logEntity = new LogEntity();
            logEntity.setTime(log.getTime());
            log.getDataList().forEach(data -> {
                KeyValue keyValue = new KeyValue();
                keyValue.setKey(data.getKey());
                keyValue.setValue(data.getValue());
                logEntity.getData().add(keyValue);
            });
            span.getLogs().add(logEntity);
        });
        spans.add(span);
    });
    return spans;
}
Also used : UniqueId(org.apache.skywalking.apm.network.proto.UniqueId) Ref(org.apache.skywalking.apm.collector.storage.ui.trace.Ref) KeyValue(org.apache.skywalking.apm.collector.storage.ui.trace.KeyValue) ServiceName(org.apache.skywalking.apm.collector.storage.table.register.ServiceName) ArrayList(java.util.ArrayList) Span(org.apache.skywalking.apm.collector.storage.ui.trace.Span) LogEntity(org.apache.skywalking.apm.collector.storage.ui.trace.LogEntity)

Example 2 with Span

use of org.apache.skywalking.apm.collector.storage.ui.trace.Span in project incubator-skywalking by apache.

the class TraceStackService method findRoot.

private List<Span> findRoot(List<Span> spans) {
    List<Span> rootSpans = new ArrayList<>();
    spans.forEach(span -> {
        String segmentParentSpanId = span.getSegmentParentSpanId();
        boolean hasParent = false;
        for (Span subSpan : spans) {
            if (segmentParentSpanId.equals(subSpan.getSegmentSpanId())) {
                hasParent = true;
            }
        }
        if (!hasParent) {
            span.setRoot(true);
            rootSpans.add(span);
        }
    });
    return rootSpans;
}
Also used : ArrayList(java.util.ArrayList) Span(org.apache.skywalking.apm.collector.storage.ui.trace.Span)

Example 3 with Span

use of org.apache.skywalking.apm.collector.storage.ui.trace.Span in project incubator-skywalking by apache.

the class TraceStackService method load.

public Trace load(String traceId) {
    Trace trace = new Trace();
    List<String> segmentIds = globalTraceDAO.getSegmentIds(traceId);
    if (CollectionUtils.isNotEmpty(segmentIds)) {
        for (String segmentId : segmentIds) {
            TraceSegmentObject segment = segmentDAO.load(segmentId);
            if (ObjectUtils.isNotEmpty(segment)) {
                trace.getSpans().addAll(buildSpanList(traceId, segmentId, segment.getApplicationId(), segment.getSpansList()));
            }
        }
    }
    List<Span> sortedSpans = new LinkedList<>();
    if (CollectionUtils.isNotEmpty(trace.getSpans())) {
        List<Span> rootSpans = findRoot(trace.getSpans());
        if (CollectionUtils.isNotEmpty(rootSpans)) {
            rootSpans.forEach(span -> {
                List<Span> childrenSpan = new ArrayList<>();
                childrenSpan.add(span);
                findChildren(trace.getSpans(), span, childrenSpan);
                sortedSpans.addAll(childrenSpan);
            });
        }
    }
    // minStartTime(sortedSpans);
    trace.setSpans(sortedSpans);
    return trace;
}
Also used : Trace(org.apache.skywalking.apm.collector.storage.ui.trace.Trace) ArrayList(java.util.ArrayList) TraceSegmentObject(org.apache.skywalking.apm.network.proto.TraceSegmentObject) Span(org.apache.skywalking.apm.collector.storage.ui.trace.Span) LinkedList(java.util.LinkedList)

Aggregations

ArrayList (java.util.ArrayList)3 Span (org.apache.skywalking.apm.collector.storage.ui.trace.Span)3 LinkedList (java.util.LinkedList)1 ServiceName (org.apache.skywalking.apm.collector.storage.table.register.ServiceName)1 KeyValue (org.apache.skywalking.apm.collector.storage.ui.trace.KeyValue)1 LogEntity (org.apache.skywalking.apm.collector.storage.ui.trace.LogEntity)1 Ref (org.apache.skywalking.apm.collector.storage.ui.trace.Ref)1 Trace (org.apache.skywalking.apm.collector.storage.ui.trace.Trace)1 TraceSegmentObject (org.apache.skywalking.apm.network.proto.TraceSegmentObject)1 UniqueId (org.apache.skywalking.apm.network.proto.UniqueId)1