From 7aac7365ac9a297f527686cac9ebc14caa006ee1 Mon Sep 17 00:00:00 2001 From: pengys5 <8082209@qq.com> Date: Sat, 9 Sep 2017 10:29:38 +0800 Subject: [PATCH] Fixed the GlobalTraceId contidtion not effect in trece stack web ui. --- .../global/GlobalTraceSpanListener.java | 12 +++++-- .../worker/segment/SegmentParse.java | 10 +++++- .../src/main/resources/logback.xml | 2 +- .../collector/core/util/CollectionUtils.java | 4 +++ .../apm/collector/ui/dao/ISegmentCostDAO.java | 3 +- .../apm/collector/ui/dao/InstanceEsDAO.java | 4 +++ .../collector/ui/dao/SegmentCostEsDAO.java | 7 ++-- .../collector/ui/dao/SegmentCostH2DAO.java | 3 +- .../ui/service/SegmentTopService.java | 12 ++++++- .../ui/service/TraceDagDataBuilder.java | 34 ++++++++++++++++--- .../ui/service/TraceStackService.java | 8 ++++- .../dictionary/OperationNameDictionary.java | 2 +- 12 files changed, 85 insertions(+), 16 deletions(-) diff --git a/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/global/GlobalTraceSpanListener.java b/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/global/GlobalTraceSpanListener.java index 095a21ae1..62230883f 100644 --- a/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/global/GlobalTraceSpanListener.java +++ b/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/global/GlobalTraceSpanListener.java @@ -2,11 +2,11 @@ package org.skywalking.apm.collector.agentstream.worker.global; import java.util.ArrayList; import java.util.List; -import org.skywalking.apm.collector.storage.define.global.GlobalTraceDataDefine; import org.skywalking.apm.collector.agentstream.worker.segment.FirstSpanListener; import org.skywalking.apm.collector.agentstream.worker.segment.GlobalTraceIdsListener; -import org.skywalking.apm.collector.core.util.TimeBucketUtils; import org.skywalking.apm.collector.core.framework.CollectorContextHelper; +import org.skywalking.apm.collector.core.util.TimeBucketUtils; +import org.skywalking.apm.collector.storage.define.global.GlobalTraceDataDefine; import org.skywalking.apm.collector.stream.StreamModuleContext; import org.skywalking.apm.collector.stream.StreamModuleGroupDefine; import org.skywalking.apm.collector.stream.worker.WorkerInvokeException; @@ -35,7 +35,13 @@ public class GlobalTraceSpanListener implements FirstSpanListener, GlobalTraceId @Override public void parseGlobalTraceId(UniqueId uniqueId) { StringBuilder globalTraceIdBuilder = new StringBuilder(); - uniqueId.getIdPartsList().forEach(globalTraceIdBuilder::append); + for (int i = 0; i < uniqueId.getIdPartsList().size(); i++) { + if (i == 0) { + globalTraceIdBuilder.append(uniqueId.getIdPartsList().get(i)); + } else { + globalTraceIdBuilder.append(".").append(uniqueId.getIdPartsList().get(i)); + } + } globalTraceIds.add(globalTraceIdBuilder.toString()); } diff --git a/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/segment/SegmentParse.java b/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/segment/SegmentParse.java index 38815207d..ed1e3d874 100644 --- a/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/segment/SegmentParse.java +++ b/apm-collector/apm-collector-agentstream/src/main/java/org/skywalking/apm/collector/agentstream/worker/segment/SegmentParse.java @@ -49,7 +49,15 @@ public class SegmentParse { public void parse(List traceIds, TraceSegmentObject segmentObject) { StringBuilder segmentIdBuilder = new StringBuilder(); - segmentObject.getTraceSegmentId().getIdPartsList().forEach(segmentIdBuilder::append); + + for (int i = 0; i < segmentObject.getTraceSegmentId().getIdPartsList().size(); i++) { + if (i == 0) { + segmentIdBuilder.append(segmentObject.getTraceSegmentId().getIdPartsList().get(i)); + } else { + segmentIdBuilder.append(".").append(segmentObject.getTraceSegmentId().getIdPartsList().get(i)); + } + } + String segmentId = segmentIdBuilder.toString(); for (UniqueId uniqueId : traceIds) { diff --git a/apm-collector/apm-collector-boot/src/main/resources/logback.xml b/apm-collector/apm-collector-boot/src/main/resources/logback.xml index a0ca18fb1..46eba2b93 100644 --- a/apm-collector/apm-collector-boot/src/main/resources/logback.xml +++ b/apm-collector/apm-collector-boot/src/main/resources/logback.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/util/CollectionUtils.java b/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/util/CollectionUtils.java index 22141d6a2..3566341b3 100644 --- a/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/util/CollectionUtils.java +++ b/apm-collector/apm-collector-core/src/main/java/org/skywalking/apm/collector/core/util/CollectionUtils.java @@ -19,4 +19,8 @@ public class CollectionUtils { public static boolean isNotEmpty(List list) { return !isEmpty(list); } + + public static boolean isNotEmpty(T[] array) { + return array != null && array.length > 0; + } } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ISegmentCostDAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ISegmentCostDAO.java index 99c09d34e..02771b221 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ISegmentCostDAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/ISegmentCostDAO.java @@ -1,13 +1,14 @@ package org.skywalking.apm.collector.ui.dao; import com.google.gson.JsonObject; +import java.util.List; /** * @author pengys5 */ public interface ISegmentCostDAO { JsonObject loadTop(long startTime, long endTime, long minCost, long maxCost, String operationName, - String globalTraceId, int limit, int from, Sort sort); + List segmentIds, int limit, int from, Sort sort); public enum Sort { Cost, Time diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceEsDAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceEsDAO.java index e79a8874a..429e55c2a 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceEsDAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/InstanceEsDAO.java @@ -18,6 +18,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortMode; +import org.skywalking.apm.collector.core.util.TimeBucketUtils; import org.skywalking.apm.collector.storage.define.register.InstanceDataDefine; import org.skywalking.apm.collector.storage.define.register.InstanceTable; import org.skywalking.apm.collector.storage.elasticsearch.dao.EsDAO; @@ -34,12 +35,15 @@ public class InstanceEsDAO extends EsDAO implements IInstanceDAO { @Override public Long lastHeartBeatTime() { long fiveMinuteBefore = System.currentTimeMillis() - 5 * 60 * 1000; + fiveMinuteBefore = TimeBucketUtils.INSTANCE.getSecondTimeBucket(fiveMinuteBefore); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(InstanceTable.COLUMN_HEARTBEAT_TIME).gt(fiveMinuteBefore); return heartBeatTime(rangeQueryBuilder); } @Override public Long instanceLastHeartBeatTime(long applicationInstanceId) { long fiveMinuteBefore = System.currentTimeMillis() - 5 * 60 * 1000; + fiveMinuteBefore = TimeBucketUtils.INSTANCE.getSecondTimeBucket(fiveMinuteBefore); + BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder(); RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(InstanceTable.COLUMN_HEARTBEAT_TIME).gt(fiveMinuteBefore); MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(InstanceTable.COLUMN_INSTANCE_ID, applicationInstanceId); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostEsDAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostEsDAO.java index f724a7451..8e8942856 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostEsDAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostEsDAO.java @@ -25,7 +25,7 @@ import org.skywalking.apm.collector.storage.elasticsearch.dao.EsDAO; public class SegmentCostEsDAO extends EsDAO implements ISegmentCostDAO { @Override public JsonObject loadTop(long startTime, long endTime, long minCost, long maxCost, String operationName, - String globalTraceId, int limit, int from, Sort sort) { + List segmentIds, int limit, int from, Sort sort) { SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(SegmentCostTable.TABLE); searchRequestBuilder.setTypes(SegmentCostTable.TABLE_TYPE); searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -44,9 +44,12 @@ public class SegmentCostEsDAO extends EsDAO implements ISegmentCostDAO { } boolQueryBuilder.must().add(rangeQueryBuilder); } - if (!StringUtils.isEmpty(operationName)) { + if (StringUtils.isNotEmpty(operationName)) { mustQueryList.add(QueryBuilders.matchQuery(SegmentCostTable.COLUMN_SERVICE_NAME, operationName)); } + if (CollectionUtils.isNotEmpty(segmentIds)) { + boolQueryBuilder.must().add(QueryBuilders.termsQuery(SegmentCostTable.COLUMN_SEGMENT_ID, segmentIds.toArray(new String[0]))); + } if (Sort.Cost.equals(sort)) { searchRequestBuilder.addSort(SegmentCostTable.COLUMN_COST, SortOrder.DESC); diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostH2DAO.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostH2DAO.java index 644eab487..4da022890 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostH2DAO.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/dao/SegmentCostH2DAO.java @@ -1,6 +1,7 @@ package org.skywalking.apm.collector.ui.dao; import com.google.gson.JsonObject; +import java.util.List; import org.skywalking.apm.collector.storage.h2.dao.H2DAO; /** @@ -8,7 +9,7 @@ import org.skywalking.apm.collector.storage.h2.dao.H2DAO; */ public class SegmentCostH2DAO extends H2DAO implements ISegmentCostDAO { @Override public JsonObject loadTop(long startTime, long endTime, long minCost, long maxCost, String operationName, - String globalTraceId, int limit, int from, Sort sort) { + List segmentIds, int limit, int from, Sort sort) { return null; } } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/SegmentTopService.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/SegmentTopService.java index 48ed414af..c976cc3bf 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/SegmentTopService.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/SegmentTopService.java @@ -1,7 +1,11 @@ package org.skywalking.apm.collector.ui.service; import com.google.gson.JsonObject; +import java.util.LinkedList; +import java.util.List; +import org.skywalking.apm.collector.core.util.StringUtils; import org.skywalking.apm.collector.storage.dao.DAOContainer; +import org.skywalking.apm.collector.ui.dao.IGlobalTraceDAO; import org.skywalking.apm.collector.ui.dao.ISegmentCostDAO; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,7 +20,13 @@ public class SegmentTopService { public JsonObject loadTop(long startTime, long endTime, long minCost, long maxCost, String operationName, String globalTraceId, int limit, int from, ISegmentCostDAO.Sort sort) { logger.debug("startTime: {}, endTime: {}, minCost: {}, maxCost: {}, operationName: {}, globalTraceId: {}, limit: {}, from: {}", startTime, endTime, minCost, maxCost, operationName, globalTraceId, limit, from); + + List segmentIds = new LinkedList<>(); + if (StringUtils.isNotEmpty(globalTraceId)) { + IGlobalTraceDAO globalTraceDAO = (IGlobalTraceDAO)DAOContainer.INSTANCE.get(IGlobalTraceDAO.class.getName()); + segmentIds = globalTraceDAO.getSegmentIds(globalTraceId); + } ISegmentCostDAO segmentCostDAO = (ISegmentCostDAO)DAOContainer.INSTANCE.get(ISegmentCostDAO.class.getName()); - return segmentCostDAO.loadTop(startTime, endTime, minCost, maxCost, operationName, globalTraceId, limit, from, sort); + return segmentCostDAO.loadTop(startTime, endTime, minCost, maxCost, operationName, segmentIds, limit, from, sort); } } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceDagDataBuilder.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceDagDataBuilder.java index 092991334..d7109bdc8 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceDagDataBuilder.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceDagDataBuilder.java @@ -3,6 +3,7 @@ package org.skywalking.apm.collector.ui.service; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import org.skywalking.apm.collector.core.util.Const; import org.skywalking.apm.collector.storage.define.noderef.NodeReferenceTable; @@ -26,13 +27,14 @@ public class TraceDagDataBuilder { changeNodeComp2Map(nodeCompArray); changeMapping2Map(nodesMappingArray); - for (int i = 0; i < resSumArray.size(); i++) { - JsonObject nodeRefJsonObj = resSumArray.get(i).getAsJsonObject(); + Map mergedResSumMap = merge(resSumArray); + + mergedResSumMap.values().forEach(nodeRefJsonObj -> { String front = nodeRefJsonObj.get("front").getAsString(); String behind = nodeRefJsonObj.get("behind").getAsString(); if (hasMapping(behind)) { - continue; + return; } JsonObject lineJsonObj = new JsonObject(); @@ -42,7 +44,7 @@ public class TraceDagDataBuilder { lineArray.add(lineJsonObj); logger.debug("line: {}", lineJsonObj); - } + }); JsonObject dagJsonObj = new JsonObject(); dagJsonObj.add("nodes", pointArray); @@ -93,4 +95,28 @@ public class TraceDagDataBuilder { private boolean hasMapping(String peers) { return mappingMap.containsKey(peers); } + + private Map merge(JsonArray nodeReference) { + Map mergedRef = new LinkedHashMap<>(); + for (int i = 0; i < nodeReference.size(); i++) { + JsonObject nodeRefJsonObj = nodeReference.get(i).getAsJsonObject(); + String front = nodeRefJsonObj.get("front").getAsString(); + String behind = nodeRefJsonObj.get("behind").getAsString(); + + String id = front + Const.ID_SPLIT + behind; + if (mergedRef.containsKey(id)) { + JsonObject oldValue = mergedRef.get(id); + oldValue.addProperty(NodeReferenceTable.COLUMN_S1_LTE, oldValue.get(NodeReferenceTable.COLUMN_S1_LTE).getAsLong() + nodeRefJsonObj.get(NodeReferenceTable.COLUMN_S1_LTE).getAsLong()); + oldValue.addProperty(NodeReferenceTable.COLUMN_S3_LTE, oldValue.get(NodeReferenceTable.COLUMN_S3_LTE).getAsLong() + nodeRefJsonObj.get(NodeReferenceTable.COLUMN_S3_LTE).getAsLong()); + oldValue.addProperty(NodeReferenceTable.COLUMN_S5_LTE, oldValue.get(NodeReferenceTable.COLUMN_S5_LTE).getAsLong() + nodeRefJsonObj.get(NodeReferenceTable.COLUMN_S5_LTE).getAsLong()); + oldValue.addProperty(NodeReferenceTable.COLUMN_S5_GT, oldValue.get(NodeReferenceTable.COLUMN_S5_GT).getAsLong() + nodeRefJsonObj.get(NodeReferenceTable.COLUMN_S5_GT).getAsLong()); + oldValue.addProperty(NodeReferenceTable.COLUMN_ERROR, oldValue.get(NodeReferenceTable.COLUMN_ERROR).getAsLong() + nodeRefJsonObj.get(NodeReferenceTable.COLUMN_ERROR).getAsLong()); + oldValue.addProperty(NodeReferenceTable.COLUMN_SUMMARY, oldValue.get(NodeReferenceTable.COLUMN_SUMMARY).getAsLong() + nodeRefJsonObj.get(NodeReferenceTable.COLUMN_SUMMARY).getAsLong()); + } else { + mergedRef.put(id, nodeReference.get(i).getAsJsonObject()); + } + } + + return mergedRef; + } } diff --git a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceStackService.java b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceStackService.java index 19a90826b..96703b821 100644 --- a/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceStackService.java +++ b/apm-collector/apm-collector-ui/src/main/java/org/skywalking/apm/collector/ui/service/TraceStackService.java @@ -119,7 +119,13 @@ public class TraceStackService { UniqueId uniqueId = reference.getParentTraceSegmentId(); StringBuilder segmentIdBuilder = new StringBuilder(); - uniqueId.getIdPartsList().forEach(part -> segmentIdBuilder.append(String.valueOf(part))); + 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))); + } + } String parentSegmentId = segmentIdBuilder.toString(); segmentParentSpanId = parentSegmentId + Const.SEGMENT_SPAN_SPLIT + String.valueOf(parentSpanId); diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java index 1e06333f6..f5b0c6a79 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/dictionary/OperationNameDictionary.java @@ -35,7 +35,7 @@ public enum OperationNameDictionary { OperationNameKey key = new OperationNameKey(applicationId, operationName); Integer operationId = operationNameDictionary.get(key); if (operationId != null) { - return new Found(applicationId); + return new Found(operationId); } else { if (registerWhenNotFound && operationNameDictionary.size() + unRegisterOperationNames.size() < OPERATION_NAME_BUFFER_SIZE) { -- GitLab