Skip to content

Commit

Permalink
fix: fix apm trace tree
Browse files Browse the repository at this point in the history
  • Loading branch information
sanhuan.sw committed Aug 14, 2023
1 parent 427639d commit 02b27cb
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Const {
public static final String SEGMENT_SPAN_SPLIT = "S";
public static final String UNKNOWN = "Unknown";
public static final String NOT_APPLICABLE = "N/A";
public static final String MOSN_ATTR = "sofatracer.is_mosn";
public static final String EMPTY_STRING = "";
public static final String POINT = ".";
public static final String DOUBLE_COLONS_SPLIT = "::";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public class TraceTree implements Serializable {
private static final long serialVersionUID = 3436393920339302223L;

private Span span;
private Span mesh;
private List<TraceTree> children = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ public class Span implements Serializable {
private List<LogEntity> logs = new ArrayList<>();

private boolean isRoot;
private boolean isMesh;

}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public List<TraceTree> queryTraceTree(String tenant, long start, long end, Strin
root.setSpan(span);
List<TraceTree> children = new ArrayList<>();
root.setChildren(children);
findChildren1(spans, span, children);
findChildren1(spans, span, root, children);
result.add(root);
});
}
Expand Down Expand Up @@ -265,6 +265,11 @@ private Span buildSpan(SpanDO spanEsDO) {
io.holoinsight.server.apm.common.model.specification.sw.KeyValue keyValue =
new io.holoinsight.server.apm.common.model.specification.sw.KeyValue(tagk, tagv);
span.getTags().add(keyValue);
// mesh span
if (SpanDO.attributes(Const.MOSN_ATTR).equals(keyValue.getKey())
&& "true".equals(keyValue.getValue())) {
span.setMesh(true);
}
});
Collections.sort(span.getTags(), (o1, o2) -> StringUtils.compare(o1.getKey(), o2.getKey()));

Expand Down Expand Up @@ -331,22 +336,33 @@ private List<Span> findRoot1(List<Span> spans) {
ListIterator<Span> iterator = spans.listIterator(spans.size());
while (iterator.hasPrevious()) {
Span span = iterator.previous();
if (span.isMesh()) {
continue;
}
String spanId = span.getSpanId();
String parentSpanId = span.getParentSpanId();

boolean hasParent = false;
for (Span subSpan : spans) {
// sofatracer mq/rpc server span(parentSpanId == spanId)
// exclude subSpan = parentSpan
if (subSpan.getSpanId().equals(parentSpanId)
&& !subSpan.getSpanId().equals(span.getSpanId())
&& !subSpan.getParentSpanId().equals(span.getParentSpanId())) {
hasParent = true;
// if find parent, quick exit
break;
if (!StringUtils.isEmpty(parentSpanId)) {
for (Span subSpan : spans) {
if (!subSpan.isMesh() && subSpan.getSpanId().equals(parentSpanId)
&& !subSpan.equals(span)) {
hasParent = true;
// if find parent, quick exit
break;
}
}
}

if (!hasParent) {
// sofatracer mq/rpc server span(parentSpanId == spanId)
if (spanId.equals(parentSpanId) && parentSpanId.contains(".")) {
parentSpanId = parentSpanId.substring(0, parentSpanId.lastIndexOf("."));
span.setParentSpanId(parentSpanId);
iterator.remove();
iterator.add(span);
continue;
}
// rootSpan.parentSpanId == ""
if (StringUtils.isEmpty(parentSpanId)) {
span.setRoot(true);
Expand Down Expand Up @@ -379,22 +395,31 @@ private List<Span> findRoot1(List<Span> spans) {
* @param parentSpan
* @param children
*/
private void findChildren1(List<Span> spans, Span parentSpan, List<TraceTree> children) {
private void findChildren1(List<Span> spans, Span parentSpan, TraceTree parentTree,
List<TraceTree> children) {
long parentStartTime = Long.MAX_VALUE;
long parentEndTime = Long.MIN_VALUE;

for (Span span : spans) {
if (span.getParentSpanId().equals(parentSpan.getSpanId())) {
ListIterator<Span> iterator = spans.listIterator(spans.size());
while (iterator.hasPrevious()) {
Span span = iterator.previous();
// find mesh span
if (span.isMesh() && parentSpan.getSpanId().equals(span.getSpanId())
&& parentSpan.getParentSpanId().equals(span.getParentSpanId())
&& parentSpan.getType().equals(span.getType())) {
parentTree.setMesh(span);
}

if (!span.isMesh() && !span.equals(parentSpan)
&& span.getParentSpanId().equals(parentSpan.getSpanId())) {
TraceTree child = new TraceTree();
child.setSpan(span);
children.add(child);
List<TraceTree> newChildren = new ArrayList<>();
child.setChildren(newChildren);
// sofatracer mq/rpc server span(parentSpanId == spanId)
// prevent stack overflow
if (!span.getParentSpanId().equals(span.getSpanId())) {
findChildren1(spans, span, newChildren);
}

findChildren1(spans, span, child, newChildren);

parentStartTime = Math.min(parentStartTime, span.getStartTime());
parentEndTime = Math.max(parentEndTime, span.getEndTime());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public static QueryProto.TraceTreeList convertTraceTree(List<TraceTree> traceTre
if (childList != null) {
traceTreeBuilder.addAllChildren(childList.getTraceTreeList());
}
if (traceTree.getMesh() != null) {
traceTreeBuilder.setMesh(convertSpan(traceTree.getMesh()));
}
traceTreeListBuilder.addTraceTree(traceTreeBuilder.build());
});
return traceTreeListBuilder.build();
Expand All @@ -89,6 +92,9 @@ public static List<TraceTree> convertTraceTree(QueryProto.TraceTreeList traceTre
List<TraceTree> result = new ArrayList<>();
traceTreeList.getTraceTreeList().forEach(traceTree -> {
TraceTree root = new TraceTree();
if (traceTree.hasMesh()) {
root.setMesh(convertSpan(traceTree.getMesh()));
}
root.setSpan(convertSpan(traceTree.getSpan()));
List<QueryProto.TraceTree> childrenList = traceTree.getChildrenList();
if (CollectionUtils.isNotEmpty(childrenList)) {
Expand Down Expand Up @@ -219,7 +225,7 @@ public static Span convertSpan(QueryProto.Span spanProto) {
.collect(Collectors.toList()),
spanProto.getLogsList().stream().map(ApmConvertor::convertLogEntity)
.collect(Collectors.toList()),
spanProto.getIsRoot());
spanProto.getIsRoot(), spanProto.getIsMesh());
return span;
}

Expand Down
Loading

0 comments on commit 02b27cb

Please sign in to comment.