Provide the queryTrace query.
This commit is contained in:
parent
77f615a8ae
commit
1d625d2ad5
|
|
@ -24,7 +24,7 @@
|
|||
{
|
||||
"pts": [230150, 185809, 24040000], //上级的segment_id 一个应用中的一个实例在链路中产生的编号
|
||||
"pii": 2, //上级的实例编号
|
||||
"psp": 0, //上级的埋点编号span_id
|
||||
"psp": 1, //上级的埋点编号span_id
|
||||
"psi": 0, //上级的服务编号(org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()注册后的ID)
|
||||
"psn": "org.skywaking.apm.testcase.dubbo.services.GreetService.doBusiness()", //上级的服务名
|
||||
"ni": 0, //上级调用时使用的地址注册后的ID
|
||||
|
|
|
|||
|
|
@ -24,4 +24,20 @@ package org.apache.skywalking.apm.collector.storage.ui.trace;
|
|||
public class KeyValue {
|
||||
private String key;
|
||||
private String value;
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,12 +18,33 @@
|
|||
|
||||
package org.apache.skywalking.apm.collector.storage.ui.trace;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author peng-yongsheng
|
||||
*/
|
||||
public class LogEntity {
|
||||
private String time;
|
||||
private long time;
|
||||
private List<KeyValue> data;
|
||||
|
||||
public LogEntity() {
|
||||
this.data = new LinkedList<>();
|
||||
}
|
||||
|
||||
public long getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setTime(long time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public List<KeyValue> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(List<KeyValue> data) {
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,7 +23,39 @@ package org.apache.skywalking.apm.collector.storage.ui.trace;
|
|||
*/
|
||||
public class Ref {
|
||||
private String traceId;
|
||||
private Integer parentSegmentId;
|
||||
private String parentSegmentId;
|
||||
private Integer parentSpanId;
|
||||
private RefType type;
|
||||
|
||||
public String getTraceId() {
|
||||
return traceId;
|
||||
}
|
||||
|
||||
public void setTraceId(String traceId) {
|
||||
this.traceId = traceId;
|
||||
}
|
||||
|
||||
public String getParentSegmentId() {
|
||||
return parentSegmentId;
|
||||
}
|
||||
|
||||
public void setParentSegmentId(String parentSegmentId) {
|
||||
this.parentSegmentId = parentSegmentId;
|
||||
}
|
||||
|
||||
public Integer getParentSpanId() {
|
||||
return parentSpanId;
|
||||
}
|
||||
|
||||
public void setParentSpanId(Integer parentSpanId) {
|
||||
this.parentSpanId = parentSpanId;
|
||||
}
|
||||
|
||||
public RefType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(RefType type) {
|
||||
this.type = type;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@
|
|||
|
||||
package org.apache.skywalking.apm.collector.storage.ui.trace;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
|
@ -26,12 +27,12 @@ import java.util.List;
|
|||
public class Span {
|
||||
private String traceId;
|
||||
private String segmentId;
|
||||
private Integer spanId;
|
||||
private Integer parentSpanId;
|
||||
private int spanId;
|
||||
private int parentSpanId;
|
||||
private List<Ref> refs;
|
||||
private String applicationCode;
|
||||
private Long startTime;
|
||||
private Long endTime;
|
||||
private long startTime;
|
||||
private long endTime;
|
||||
private String operationName;
|
||||
private String type;
|
||||
private String peer;
|
||||
|
|
@ -40,4 +41,165 @@ public class Span {
|
|||
private String layer;
|
||||
private List<KeyValue> tags;
|
||||
private List<LogEntity> logs;
|
||||
private boolean isRoot;
|
||||
private String segmentSpanId;
|
||||
private String segmentParentSpanId;
|
||||
|
||||
public Span() {
|
||||
this.refs = new LinkedList<>();
|
||||
this.tags = new LinkedList<>();
|
||||
this.logs = new LinkedList<>();
|
||||
}
|
||||
|
||||
public String getTraceId() {
|
||||
return traceId;
|
||||
}
|
||||
|
||||
public void setTraceId(String traceId) {
|
||||
this.traceId = traceId;
|
||||
}
|
||||
|
||||
public String getSegmentId() {
|
||||
return segmentId;
|
||||
}
|
||||
|
||||
public void setSegmentId(String segmentId) {
|
||||
this.segmentId = segmentId;
|
||||
}
|
||||
|
||||
public int getSpanId() {
|
||||
return spanId;
|
||||
}
|
||||
|
||||
public void setSpanId(int spanId) {
|
||||
this.spanId = spanId;
|
||||
}
|
||||
|
||||
public int getParentSpanId() {
|
||||
return parentSpanId;
|
||||
}
|
||||
|
||||
public void setParentSpanId(int parentSpanId) {
|
||||
this.parentSpanId = parentSpanId;
|
||||
}
|
||||
|
||||
public List<Ref> getRefs() {
|
||||
return refs;
|
||||
}
|
||||
|
||||
public void setRefs(List<Ref> refs) {
|
||||
this.refs = refs;
|
||||
}
|
||||
|
||||
public String getApplicationCode() {
|
||||
return applicationCode;
|
||||
}
|
||||
|
||||
public void setApplicationCode(String applicationCode) {
|
||||
this.applicationCode = applicationCode;
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(long startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
public long getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(long endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public String getOperationName() {
|
||||
return operationName;
|
||||
}
|
||||
|
||||
public void setOperationName(String operationName) {
|
||||
this.operationName = operationName;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getPeer() {
|
||||
return peer;
|
||||
}
|
||||
|
||||
public void setPeer(String peer) {
|
||||
this.peer = peer;
|
||||
}
|
||||
|
||||
public String getComponent() {
|
||||
return component;
|
||||
}
|
||||
|
||||
public void setComponent(String component) {
|
||||
this.component = component;
|
||||
}
|
||||
|
||||
public Boolean getError() {
|
||||
return isError;
|
||||
}
|
||||
|
||||
public void setError(Boolean error) {
|
||||
isError = error;
|
||||
}
|
||||
|
||||
public String getLayer() {
|
||||
return layer;
|
||||
}
|
||||
|
||||
public void setLayer(String layer) {
|
||||
this.layer = layer;
|
||||
}
|
||||
|
||||
public List<KeyValue> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(List<KeyValue> tags) {
|
||||
this.tags = tags;
|
||||
}
|
||||
|
||||
public List<LogEntity> getLogs() {
|
||||
return logs;
|
||||
}
|
||||
|
||||
public void setLogs(List<LogEntity> logs) {
|
||||
this.logs = logs;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return isRoot;
|
||||
}
|
||||
|
||||
public void setRoot(boolean root) {
|
||||
isRoot = root;
|
||||
}
|
||||
|
||||
public String getSegmentParentSpanId() {
|
||||
return segmentParentSpanId;
|
||||
}
|
||||
|
||||
public void setSegmentParentSpanId(String segmentParentSpanId) {
|
||||
this.segmentParentSpanId = segmentParentSpanId;
|
||||
}
|
||||
|
||||
public String getSegmentSpanId() {
|
||||
return segmentSpanId;
|
||||
}
|
||||
|
||||
public void setSegmentSpanId(String segmentSpanId) {
|
||||
this.segmentSpanId = segmentSpanId;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,11 +18,25 @@
|
|||
|
||||
package org.apache.skywalking.apm.collector.storage.ui.trace;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author peng-yongsheng
|
||||
*/
|
||||
public class Trace {
|
||||
|
||||
private List<Span> spans;
|
||||
|
||||
public Trace() {
|
||||
this.spans = new LinkedList<>();
|
||||
}
|
||||
|
||||
public List<Span> getSpans() {
|
||||
return spans;
|
||||
}
|
||||
|
||||
public void setSpans(List<Span> spans) {
|
||||
this.spans = spans;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ import com.google.gson.Gson;
|
|||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import graphql.ExecutionInput;
|
||||
import graphql.ExecutionResult;
|
||||
import graphql.GraphQL;
|
||||
|
|
@ -31,6 +32,7 @@ import graphql.schema.GraphQLSchema;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
|
@ -64,6 +66,7 @@ public class GraphQLHandler extends JettyHandler {
|
|||
private final Gson gson = new Gson();
|
||||
private final GraphQL graphQL;
|
||||
private static final String QUERY = "query";
|
||||
private static final String VARIABLES = "variables";
|
||||
private static final String DATA = "data";
|
||||
private static final String ERRORS = "errors";
|
||||
private static final String MESSAGE = "message";
|
||||
|
|
@ -79,7 +82,7 @@ public class GraphQLHandler extends JettyHandler {
|
|||
.file("ui-graphql/service-layer.graphqls")
|
||||
.file("ui-graphql/trace.graphqls")
|
||||
.resolvers(new VersionQuery(), new VersionMutation(), new AlarmQuery(), new ApplicationQuery(moduleManager))
|
||||
.resolvers(new OverViewLayerQuery(moduleManager), new ServerQuery(moduleManager), new ServiceQuery(), new TraceQuery())
|
||||
.resolvers(new OverViewLayerQuery(moduleManager), new ServerQuery(moduleManager), new ServiceQuery(), new TraceQuery(moduleManager))
|
||||
.resolvers(new ConfigQuery(), new ConfigMutation())
|
||||
.dictionary(ConjecturalNode.class, VisualUserNode.class, ApplicationNode.class)
|
||||
.build()
|
||||
|
|
@ -105,7 +108,11 @@ public class GraphQLHandler extends JettyHandler {
|
|||
}
|
||||
|
||||
JsonObject requestJson = gson.fromJson(request, JsonObject.class);
|
||||
return execute(requestJson.get(QUERY).getAsString(), gson.fromJson(requestJson.get("variables"), Map.class));
|
||||
|
||||
Type mapType = new TypeToken<Map<String, Object>>() {
|
||||
}.getType();
|
||||
|
||||
return execute(requestJson.get(QUERY).getAsString(), gson.fromJson(requestJson.get(VARIABLES), mapType));
|
||||
}
|
||||
|
||||
private JsonObject execute(String request, Map<String, Object> variables) {
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import org.apache.skywalking.apm.collector.storage.ui.trace.TraceBrief;
|
|||
import org.apache.skywalking.apm.collector.storage.ui.trace.TraceQueryCondition;
|
||||
import org.apache.skywalking.apm.collector.ui.graphql.Query;
|
||||
import org.apache.skywalking.apm.collector.ui.service.SegmentTopService;
|
||||
import org.apache.skywalking.apm.collector.ui.service.TraceStackService;
|
||||
import org.apache.skywalking.apm.collector.ui.utils.DurationUtils;
|
||||
|
||||
/**
|
||||
|
|
@ -35,6 +36,7 @@ public class TraceQuery implements Query {
|
|||
|
||||
private final ModuleManager moduleManager;
|
||||
private SegmentTopService segmentTopService;
|
||||
private TraceStackService traceStackService;
|
||||
|
||||
public TraceQuery(ModuleManager moduleManager) {
|
||||
this.moduleManager = moduleManager;
|
||||
|
|
@ -47,6 +49,13 @@ public class TraceQuery implements Query {
|
|||
return segmentTopService;
|
||||
}
|
||||
|
||||
private TraceStackService getTraceStackService() {
|
||||
if (ObjectUtils.isEmpty(traceStackService)) {
|
||||
this.traceStackService = new TraceStackService(moduleManager);
|
||||
}
|
||||
return traceStackService;
|
||||
}
|
||||
|
||||
public TraceBrief queryBasicTraces(TraceQueryCondition condition) throws ParseException {
|
||||
long start = DurationUtils.INSTANCE.durationToSecondTimeBucket(condition.getQueryDuration().getStep(), condition.getQueryDuration().getStart());
|
||||
long end = DurationUtils.INSTANCE.durationToSecondTimeBucket(condition.getQueryDuration().getStep(), condition.getQueryDuration().getEnd());
|
||||
|
|
@ -59,10 +68,10 @@ public class TraceQuery implements Query {
|
|||
int limit = condition.getPaging().getPageSize();
|
||||
int from = condition.getPaging().getPageSize() * condition.getPaging().getPageNum();
|
||||
|
||||
return segmentTopService.loadTop(start, end, minDuration, maxDuration, operationName, traceId, applicationId, limit, from);
|
||||
return getSegmentTopService().loadTop(start, end, minDuration, maxDuration, operationName, traceId, applicationId, limit, from);
|
||||
}
|
||||
|
||||
public Trace queryTrace(String id) {
|
||||
return null;
|
||||
public Trace queryTrace(String traceId) {
|
||||
return getTraceStackService().load(traceId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,26 +16,33 @@
|
|||
*
|
||||
*/
|
||||
|
||||
|
||||
package org.apache.skywalking.apm.collector.ui.service;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import org.apache.skywalking.apm.collector.cache.CacheModule;
|
||||
import org.apache.skywalking.apm.collector.cache.service.ApplicationCacheService;
|
||||
import org.apache.skywalking.apm.collector.cache.service.NetworkAddressCacheService;
|
||||
import org.apache.skywalking.apm.collector.cache.service.ServiceNameCacheService;
|
||||
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
|
||||
import org.apache.skywalking.apm.collector.core.util.CollectionUtils;
|
||||
import org.apache.skywalking.apm.collector.core.util.Const;
|
||||
import org.apache.skywalking.apm.collector.core.util.ObjectUtils;
|
||||
import org.apache.skywalking.apm.collector.storage.dao.ui.IGlobalTraceUIDAO;
|
||||
import org.apache.skywalking.apm.collector.storage.dao.ui.ISegmentUIDAO;
|
||||
import org.apache.skywalking.apm.collector.cache.service.ApplicationCacheService;
|
||||
import org.apache.skywalking.apm.collector.cache.service.ServiceNameCacheService;
|
||||
import org.apache.skywalking.apm.collector.core.util.StringUtils;
|
||||
import org.apache.skywalking.apm.collector.storage.StorageModule;
|
||||
import org.apache.skywalking.apm.collector.storage.dao.ui.IGlobalTraceUIDAO;
|
||||
import org.apache.skywalking.apm.collector.storage.dao.ui.ISegmentUIDAO;
|
||||
import org.apache.skywalking.apm.collector.storage.ui.trace.KeyValue;
|
||||
import org.apache.skywalking.apm.collector.storage.ui.trace.LogEntity;
|
||||
import org.apache.skywalking.apm.collector.storage.ui.trace.Ref;
|
||||
import org.apache.skywalking.apm.collector.storage.ui.trace.RefType;
|
||||
import org.apache.skywalking.apm.collector.storage.ui.trace.Span;
|
||||
import org.apache.skywalking.apm.collector.storage.ui.trace.Trace;
|
||||
import org.apache.skywalking.apm.network.proto.SpanObject;
|
||||
import org.apache.skywalking.apm.network.proto.TraceSegmentObject;
|
||||
import org.apache.skywalking.apm.network.proto.UniqueId;
|
||||
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
|
||||
|
||||
/**
|
||||
* @author peng-yongsheng
|
||||
|
|
@ -46,60 +53,45 @@ public class TraceStackService {
|
|||
private final ISegmentUIDAO segmentDAO;
|
||||
private final ApplicationCacheService applicationCacheService;
|
||||
private final ServiceNameCacheService serviceNameCacheService;
|
||||
private final NetworkAddressCacheService networkAddressCacheService;
|
||||
|
||||
public TraceStackService(ModuleManager moduleManager) {
|
||||
this.globalTraceDAO = moduleManager.find(StorageModule.NAME).getService(IGlobalTraceUIDAO.class);
|
||||
this.segmentDAO = moduleManager.find(StorageModule.NAME).getService(ISegmentUIDAO.class);
|
||||
this.applicationCacheService = moduleManager.find(CacheModule.NAME).getService(ApplicationCacheService.class);
|
||||
this.serviceNameCacheService = moduleManager.find(CacheModule.NAME).getService(ServiceNameCacheService.class);
|
||||
this.networkAddressCacheService = moduleManager.find(CacheModule.NAME).getService(NetworkAddressCacheService.class);
|
||||
}
|
||||
|
||||
public JsonArray load(String globalTraceId) {
|
||||
List<Span> spans = new ArrayList<>();
|
||||
List<String> segmentIds = globalTraceDAO.getSegmentIds(globalTraceId);
|
||||
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)) {
|
||||
spans.addAll(buildSpanList(segmentId, segment));
|
||||
trace.getSpans().addAll(buildSpanList(traceId, segmentId, segment.getApplicationId(), segment.getSpansList()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<Span> sortedSpans = new ArrayList<>();
|
||||
if (CollectionUtils.isNotEmpty(spans)) {
|
||||
List<Span> rootSpans = findRoot(spans);
|
||||
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(spans, span, childrenSpan);
|
||||
findChildren(trace.getSpans(), span, childrenSpan);
|
||||
sortedSpans.addAll(childrenSpan);
|
||||
});
|
||||
}
|
||||
}
|
||||
minStartTime(sortedSpans);
|
||||
// minStartTime(sortedSpans);
|
||||
|
||||
return toJsonArray(sortedSpans);
|
||||
}
|
||||
|
||||
private JsonArray toJsonArray(List<Span> sortedSpans) {
|
||||
JsonArray traceStackArray = new JsonArray();
|
||||
sortedSpans.forEach(span -> {
|
||||
JsonObject spanJson = new JsonObject();
|
||||
spanJson.addProperty("spanId", span.getSpanId());
|
||||
spanJson.addProperty("parentSpanId", span.getParentSpanId());
|
||||
spanJson.addProperty("segmentSpanId", span.getSegmentSpanId());
|
||||
spanJson.addProperty("segmentParentSpanId", span.getSegmentParentSpanId());
|
||||
spanJson.addProperty("startTime", span.getStartTime());
|
||||
spanJson.addProperty("operationName", span.getOperationName());
|
||||
spanJson.addProperty("applicationCode", span.getApplicationCode());
|
||||
spanJson.addProperty("cost", span.getCost());
|
||||
spanJson.addProperty("isRoot", span.isRoot());
|
||||
traceStackArray.add(spanJson);
|
||||
});
|
||||
return traceStackArray;
|
||||
trace.setSpans(sortedSpans);
|
||||
return trace;
|
||||
}
|
||||
|
||||
private void minStartTime(List<Span> spans) {
|
||||
|
|
@ -115,56 +107,108 @@ public class TraceStackService {
|
|||
}
|
||||
}
|
||||
|
||||
private List<Span> buildSpanList(String segmentId, TraceSegmentObject segment) {
|
||||
private List<Span> buildSpanList(String traceId, String segmentId, int applicationId,
|
||||
List<SpanObject> spanObjects) {
|
||||
List<Span> spans = new ArrayList<>();
|
||||
if (segment.getSpansCount() > 0) {
|
||||
for (SpanObject spanObject : segment.getSpansList()) {
|
||||
int spanId = spanObject.getSpanId();
|
||||
int parentSpanId = spanObject.getParentSpanId();
|
||||
String segmentSpanId = segmentId + Const.SEGMENT_SPAN_SPLIT + String.valueOf(spanId);
|
||||
String segmentParentSpanId = segmentId + Const.SEGMENT_SPAN_SPLIT + String.valueOf(parentSpanId);
|
||||
long startTime = spanObject.getStartTime();
|
||||
|
||||
String operationName = spanObject.getOperationName();
|
||||
if (spanObject.getOperationNameId() != 0) {
|
||||
String serviceName = serviceNameCacheService.get(spanObject.getOperationNameId());
|
||||
if (StringUtils.isNotEmpty(serviceName)) {
|
||||
operationName = serviceName.split(Const.ID_SPLIT)[1];
|
||||
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) {
|
||||
String serviceName = serviceNameCacheService.get(spanObject.getOperationNameId());
|
||||
if (StringUtils.isNotEmpty(serviceName)) {
|
||||
operationName = serviceName.split(Const.ID_SPLIT)[1];
|
||||
} 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 {
|
||||
operationName = Const.EMPTY_STRING;
|
||||
segmentIdBuilder.append(".").append(String.valueOf(uniqueId.getIdPartsList().get(i)));
|
||||
}
|
||||
}
|
||||
String applicationCode = applicationCacheService.getApplicationById(segment.getApplicationId()).getApplicationCode();
|
||||
ref.setParentSegmentId(segmentIdBuilder.toString());
|
||||
|
||||
long cost = spanObject.getEndTime() - spanObject.getStartTime();
|
||||
if (cost == 0) {
|
||||
cost = 1;
|
||||
}
|
||||
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);
|
||||
});
|
||||
|
||||
// if (parentSpanId == -1 && segment.getRefsCount() > 0) {
|
||||
// for (TraceSegmentReference reference : segment.getRefsList()) {
|
||||
// parentSpanId = 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().getApplicationIdByCode(i)));
|
||||
// } else {
|
||||
// segmentIdBuilder.append(".").append(String.valueOf(uniqueId.getIdPartsList().getApplicationIdByCode(i)));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// String parentSegmentId = segmentIdBuilder.toString();
|
||||
// segmentParentSpanId = parentSegmentId + Const.SEGMENT_SPAN_SPLIT + String.valueOf(parentSpanId);
|
||||
//
|
||||
// spans.add(new Span(spanId, parentSpanId, segmentSpanId, segmentParentSpanId, startTime, operationName, applicationCode, cost));
|
||||
// }
|
||||
// } else {
|
||||
// spans.add(new Span(spanId, parentSpanId, segmentSpanId, segmentParentSpanId, startTime, operationName, applicationCode, cost));
|
||||
// }
|
||||
}
|
||||
}
|
||||
return spans;
|
||||
}
|
||||
|
||||
|
|
@ -174,8 +218,8 @@ public class TraceStackService {
|
|||
String segmentParentSpanId = span.getSegmentParentSpanId();
|
||||
|
||||
boolean hasParent = false;
|
||||
for (Span span1 : spans) {
|
||||
if (segmentParentSpanId.equals(span1.getSegmentSpanId())) {
|
||||
for (Span subSpan : spans) {
|
||||
if (segmentParentSpanId.equals(subSpan.getSegmentSpanId())) {
|
||||
hasParent = true;
|
||||
}
|
||||
}
|
||||
|
|
@ -196,72 +240,4 @@ public class TraceStackService {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
class Span {
|
||||
private int spanId;
|
||||
private int parentSpanId;
|
||||
private String segmentSpanId;
|
||||
private String segmentParentSpanId;
|
||||
private long startTime;
|
||||
private String operationName;
|
||||
private String applicationCode;
|
||||
private long cost;
|
||||
private boolean isRoot = false;
|
||||
|
||||
Span(int spanId, int parentSpanId, String segmentSpanId, String segmentParentSpanId, long startTime,
|
||||
String operationName, String applicationCode, long cost) {
|
||||
this.spanId = spanId;
|
||||
this.parentSpanId = parentSpanId;
|
||||
this.segmentSpanId = segmentSpanId;
|
||||
this.segmentParentSpanId = segmentParentSpanId;
|
||||
this.startTime = startTime;
|
||||
this.operationName = operationName;
|
||||
this.applicationCode = applicationCode;
|
||||
this.cost = cost;
|
||||
}
|
||||
|
||||
int getSpanId() {
|
||||
return spanId;
|
||||
}
|
||||
|
||||
int getParentSpanId() {
|
||||
return parentSpanId;
|
||||
}
|
||||
|
||||
String getSegmentSpanId() {
|
||||
return segmentSpanId;
|
||||
}
|
||||
|
||||
String getSegmentParentSpanId() {
|
||||
return segmentParentSpanId;
|
||||
}
|
||||
|
||||
long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
String getOperationName() {
|
||||
return operationName;
|
||||
}
|
||||
|
||||
String getApplicationCode() {
|
||||
return applicationCode;
|
||||
}
|
||||
|
||||
long getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public boolean isRoot() {
|
||||
return isRoot;
|
||||
}
|
||||
|
||||
public void setRoot(boolean root) {
|
||||
isRoot = root;
|
||||
}
|
||||
|
||||
public void setStartTime(long startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue