/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/skywalking/apm/collector/ui/service/SpanService.java

https://github.com/YunaiV/skywalking · Java · 143 lines · 95 code · 19 blank · 29 comment · 15 complexity · d9b5cd270d63a5914e76487162b13afe MD5 · raw file

  1. /*
  2. * Copyright 2017, OpenSkywalking Organization All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Project repository: https://github.com/OpenSkywalking/skywalking
  17. */
  18. package org.skywalking.apm.collector.ui.service;
  19. import com.google.gson.JsonArray;
  20. import com.google.gson.JsonObject;
  21. import org.skywalking.apm.collector.cache.CacheModule;
  22. import org.skywalking.apm.collector.cache.service.ApplicationCacheService;
  23. import org.skywalking.apm.collector.cache.service.ServiceNameCacheService;
  24. import org.skywalking.apm.collector.core.module.ModuleManager;
  25. import org.skywalking.apm.collector.core.util.Const;
  26. import org.skywalking.apm.collector.core.util.StringUtils;
  27. import org.skywalking.apm.collector.storage.StorageModule;
  28. import org.skywalking.apm.collector.storage.dao.ISegmentUIDAO;
  29. import org.skywalking.apm.network.proto.KeyWithStringValue;
  30. import org.skywalking.apm.network.proto.LogMessage;
  31. import org.skywalking.apm.network.proto.SpanObject;
  32. import org.skywalking.apm.network.proto.TraceSegmentObject;
  33. import org.skywalking.apm.network.trace.component.ComponentsDefine;
  34. import java.util.List;
  35. /**
  36. * @author peng-yongsheng
  37. */
  38. public class SpanService {
  39. private final ISegmentUIDAO segmentDAO;
  40. private final ServiceNameCacheService serviceNameCacheService;
  41. private final ApplicationCacheService applicationCacheService;
  42. public SpanService(ModuleManager moduleManager) {
  43. this.segmentDAO = moduleManager.find(StorageModule.NAME).getService(ISegmentUIDAO.class);
  44. this.serviceNameCacheService = moduleManager.find(CacheModule.NAME).getService(ServiceNameCacheService.class);
  45. this.applicationCacheService = moduleManager.find(CacheModule.NAME).getService(ApplicationCacheService.class);
  46. }
  47. public JsonObject load(String segmentId, int spanId) {
  48. // 加载 TraceSegment
  49. TraceSegmentObject segmentObject = segmentDAO.load(segmentId);
  50. JsonObject spanJson = new JsonObject();
  51. List<SpanObject> spans = segmentObject.getSpansList();
  52. for (SpanObject spanObject : spans) {
  53. if (spanId == spanObject.getSpanId()) {
  54. // 操作名
  55. String operationName = spanObject.getOperationName();
  56. if (spanObject.getOperationNameId() != 0) {
  57. String serviceName = serviceNameCacheService.get(spanObject.getOperationNameId());
  58. if (StringUtils.isNotEmpty(serviceName)) {
  59. operationName = serviceName.split(Const.ID_SPLIT)[1];
  60. }
  61. }
  62. spanJson.addProperty("operationName", operationName);
  63. // 开始时间与结束时间
  64. spanJson.addProperty("startTime", spanObject.getStartTime());
  65. spanJson.addProperty("endTime", spanObject.getEndTime());
  66. // loggers
  67. JsonArray logsArray = new JsonArray();
  68. List<LogMessage> logs = spanObject.getLogsList();
  69. for (LogMessage logMessage : logs) {
  70. JsonObject logJson = new JsonObject();
  71. logJson.addProperty("time", logMessage.getTime());
  72. JsonArray logInfoArray = new JsonArray();
  73. for (KeyWithStringValue value : logMessage.getDataList()) {
  74. JsonObject valueJson = new JsonObject();
  75. valueJson.addProperty("key", value.getKey());
  76. valueJson.addProperty("value", value.getValue());
  77. logInfoArray.add(valueJson);
  78. }
  79. logJson.add("logInfo", logInfoArray);
  80. logsArray.add(logJson);
  81. }
  82. spanJson.add("logMessage", logsArray);
  83. JsonArray tagsArray = new JsonArray();
  84. // 【tags】span type ,Entry / Local / Exit 三种
  85. JsonObject spanTypeJson = new JsonObject();
  86. spanTypeJson.addProperty("key", "span type");
  87. spanTypeJson.addProperty("value", spanObject.getSpanType().name());
  88. tagsArray.add(spanTypeJson);
  89. // 【tags】component
  90. JsonObject componentJson = new JsonObject();
  91. componentJson.addProperty("key", "component");
  92. if (spanObject.getComponentId() == 0) {
  93. componentJson.addProperty("value", spanObject.getComponent());
  94. } else {
  95. componentJson.addProperty("value", ComponentsDefine.getInstance().getComponentName(spanObject.getComponentId()));
  96. }
  97. tagsArray.add(componentJson);
  98. // 【tags】peer 服务地址,例如:mongodb 的服务地址
  99. JsonObject peerJson = new JsonObject();
  100. peerJson.addProperty("key", "peer");
  101. if (spanObject.getPeerId() == 0) {
  102. peerJson.addProperty("value", spanObject.getPeer());
  103. } else {
  104. peerJson.addProperty("value", applicationCacheService.get(spanObject.getPeerId()));
  105. }
  106. tagsArray.add(peerJson);
  107. // 【tags】Span 的标签键值对
  108. for (KeyWithStringValue tagValue : spanObject.getTagsList()) {
  109. JsonObject tagJson = new JsonObject();
  110. tagJson.addProperty("key", tagValue.getKey());
  111. tagJson.addProperty("value", tagValue.getValue());
  112. tagsArray.add(tagJson);
  113. }
  114. // 【tags】isError ,是否发生错误
  115. JsonObject isErrorJson = new JsonObject();
  116. isErrorJson.addProperty("key", "is error");
  117. isErrorJson.addProperty("value", spanObject.getIsError());
  118. tagsArray.add(isErrorJson);
  119. spanJson.add("tags", tagsArray);
  120. }
  121. }
  122. return spanJson;
  123. }
  124. }