Browse Source

pinpoint接口获取数据并更新。

wukai 9 months ago
parent
commit
5f9e936aad

+ 14 - 0
jjt-admin/src/test/java/com/test/Test.java

@@ -0,0 +1,14 @@
+package com.test;
+
+/**
+ * Test$
+ *
+ * @author wukai
+ * @date 2024/8/22 22:13
+ */
+public class Test {
+    public static void main(String[] args) {
+        String xx = "pp.jvm.gc";
+        System.err.println(!xx.startsWith("pp.jvm."));
+    }
+}

+ 197 - 63
jjt-biz/src/main/java/com/jjt/biz/service/impl/BizObjMetricsServiceImpl.java

@@ -14,11 +14,13 @@ import org.apache.ibatis.session.SqlSessionFactory;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -177,8 +179,8 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
         List<MetricsTplDetail> list = detailService.selectMetricsTplDetailList(query);
         List<BizObjMetrics> objMetrics = new ArrayList<>();
         list.forEach(d -> {
-            if ("1".equals(d.getMetricsDef().getMetricsType())) {
-                //如果是pinpoint,需要
+            if ("1".equals(d.getMetricsDef().getMetricsType()) && !d.getMetricsDef().getMetricsCode().startsWith("pp.jvm.")) {
+                //如果是pinpoint,并且不是jvm参数
                 // 1.获取对象pinpoint组成
                 List<BizObjPp> pps = ppService.selectBizObjPpListByObjId(objId);
                 for (BizObjPp pp : pps) {
@@ -262,44 +264,64 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
     @Override
     public void getMetricsValue(Long objId) {
         BizObj bizObj = objService.selectBizObjByObjId(objId);
+
         if ("1".equals(bizObj.getObjType())) {
-            //如果是java应用,则需要获取应用程序的code传给pinpoint
+            // 获取应用程序的code
             BizObjApp app = appService.selectBizObjAppByAppId(bizObj.getAppId());
-            String name = app.getAppCode();
-            LocalDateTime ed = LocalDateTime.now();
-//            LocalDateTime st = ed.minusMinutes(5);
-            //TODO 暂时取2天的值,不然难得搞
-            LocalDateTime st = ed.minusDays(2);
+            String appName = app.getAppCode();
 
+            LocalDateTime endTime = LocalDateTime.now();
+            LocalDateTime startTime = endTime.minusDays(2);
+            //TODO 暂时取2天的值,不然难得搞
             Map<String, String> appMap = pinpointService.getApps();
-            String type = appMap.get(name);
-            //获取tps
-            Float tps = pinpointService.tps(name, st, ed);
-            //获取打开文件数量
-//            int opens = pinpointService.openFile(name, st, ed);
-            //获取jvm信息
-            Map<String, Object> jvm = pinpointService.jvmMemory(name, st, ed);
-            //获取链路数据
-            JSONObject linkObj = pinpointService.link(name, type, st, ed);
-            JSONArray array = linkObj.getJSONObject("applicationMapData").getJSONArray("nodeDataArray");
+            String appType = appMap.get(appName);
+
+            // 获取监控数据
+            Float tps = pinpointService.tps(appName, startTime, endTime);
+            int openFiles = pinpointService.openFile(appName, startTime, endTime);
+            Map<String, Object> jvmInfo = pinpointService.jvmMemory(appName, startTime, endTime);
+            float heapUsage = (float) jvmInfo.get("usage");
+            int gcCount = (int) jvmInfo.get("gc");
+
+            JSONObject linkData = pinpointService.link(appName, appType, startTime, endTime);
+            JSONArray nodeDataArray = linkData.getJSONObject("applicationMapData").getJSONArray("nodeDataArray");
+
             List<PinpointVO> pinpointVOList = new ArrayList<>();
-            for (int i = 0; i < array.size(); i++) {
-                JSONObject obj = array.getJSONObject(i);
+            for (int i = 0; i < nodeDataArray.size(); i++) {
+                JSONObject obj = nodeDataArray.getJSONObject(i);
                 PinpointVO vo = new PinpointVO(obj);
-                if (vo.getApplicationName().equals(name)) {
-                    //设置系统压力
+
+                if (vo.getApplicationName().equals(appName)) {
                     bizObj.setAccessTimes(vo.getTot());
                     objService.updateBizObj(bizObj);
                 } else {
                     pinpointVOList.add(vo);
                 }
             }
-            //pinpointVOList
-            //1.更新BIZ_OBJ_PP 表
-            List<BizObjPp> ppList = ppService.selectBizObjPpListByObjId(objId);
-            Set<String> nameSet = ppList.stream().map(BizObjPp::getPpName).collect(Collectors.toSet());
-            pinpointVOList.forEach(vo -> {
-                if (!nameSet.contains(vo.getApplicationName())) {
+
+            // 1.更新 BIZ_OBJ_PP 表
+            updateBizObjPpTable(objId, pinpointVOList);
+
+            // 2.更新业务对象指标表
+            updateBizObjMetrics(objId, tps, openFiles, heapUsage, gcCount, pinpointVOList);
+        }
+    }
+
+    /**
+     * 更新BIZ_OBJ_PP
+     *
+     * @param objId          对象ID
+     * @param pinpointVOList PP组成
+     */
+    private void updateBizObjPpTable(Long objId, List<PinpointVO> pinpointVOList) {
+        List<BizObjPp> existingPpList = ppService.selectBizObjPpListByObjId(objId);
+        Set<String> existingNames = existingPpList.stream()
+                .map(BizObjPp::getPpName)
+                .collect(Collectors.toSet());
+
+        pinpointVOList.stream()
+                .filter(vo -> !existingNames.contains(vo.getApplicationName()))
+                .forEach(vo -> {
                     BizObjPp pp = new BizObjPp();
                     pp.setObjId(objId);
                     pp.setPpKey(vo.getKey());
@@ -307,40 +329,21 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
                     pp.setPpType(vo.getCategory());
                     ppService.insertBizObjPp(pp);
 
-                    //2.更新业务对象指标表。
-                    //2.1查询模板列表
-                    List<BizObjTpl> tplList = objTplService.selectTplList4objId(objId);
-                    tplList.forEach(tpl -> {
-                        //2.2 根据模板添加指标
-                        insert(objId, tpl.getTplId(), pp);
-                    });
-
-                }
-            });
-
-            //3.更新数据,需要增加最近值和更新时间
-            //查询所有指标
-            BizObjMetrics om = new BizObjMetrics();
-            om.setObjId(objId);
-            List<BizObjMetrics> list = selectBizObjMetricsList(om);
-//            selectBizObjMetricsList()
-
-
-        }
-        BizObjMetrics om = new BizObjMetrics();
-        om.setObjId(objId);
-        List<BizObjMetrics> list = selectBizObjMetricsList(om);
-//        //根据指标类型进行分组
-//        Map<String, List<BizObjMetrics>> typeMap = list.stream().collect(Collectors.groupingBy(m -> m.getMetricsDef().getMetricsType()));
-//        for (String s : typeMap.keySet()) {
-//
-//        }
-//
-//        if (typeMap.containsKey("pinpoint")) {
-//            //如果有pinpoint,查询pinpoint所有指标
-//
-//        }
+                    // 根据模板更新业务对象指标表
+                    updateBizObjMetricsFromTemplate(objId, pp);
+                    existingPpList.add(pp);
+                });
+    }
 
+    /**
+     * 根据模板更新业务对象指标表
+     *
+     * @param objId 对象ID
+     * @param pp    pp对象
+     */
+    private void updateBizObjMetricsFromTemplate(Long objId, BizObjPp pp) {
+        List<BizObjTpl> templateList = objTplService.selectTplList4objId(objId);
+        templateList.forEach(template -> insertOrUpdateMetricsFromTemplate(objId, template.getTplId(), pp));
     }
 
     /**
@@ -350,13 +353,13 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
      * @param tplId 模板ID
      * @param pp    pinpoint组成
      */
-    private void insert(Long objId, Long tplId, BizObjPp pp) {
+    private void insertOrUpdateMetricsFromTemplate(Long objId, Long tplId, BizObjPp pp) {
         MetricsTplDetail query = new MetricsTplDetail();
         query.setTplId(tplId);
         List<MetricsTplDetail> list = detailService.selectMetricsTplDetailList(query);
         List<BizObjMetrics> objMetrics = new ArrayList<>();
         list.forEach(d -> {
-            if ("1".equals(d.getMetricsDef().getMetricsType())) {
+            if ("1".equals(d.getMetricsDef().getMetricsType()) && !d.getMetricsDef().getMetricsCode().startsWith("pp.jvm.")) {
                 //如果是pinpoint,需要
                 // 1.获取对象pinpoint组成
                 BizObjMetrics ms = new BizObjMetrics();
@@ -365,6 +368,21 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
                 ms.setMetricsId(d.getMetricsId());
                 ms.setMetricsName(pp.getPpName() + "/" + d.getMetricsDef().getMetricsName());
                 ms.setMetricsCode(pp.getPpName() + "/" + d.getMetricsCode());
+                if (pp.getPpType().equals("REDIS")) {
+                    //redis需要将1s换成100ms 3s换成300ms 5s换成500ms
+                    if (d.getMetricsCode().endsWith("1s")) {
+                        ms.setMetricsName(ms.getMetricsName().replace("1s", "100ms"));
+                        ms.setMetricsCode(ms.getMetricsCode().replace("1s", "100ms"));
+                    }
+                    if (d.getMetricsCode().endsWith("3s")) {
+                        ms.setMetricsName(ms.getMetricsName().replace("3s", "300ms"));
+                        ms.setMetricsCode(ms.getMetricsCode().replace("3s", "300ms"));
+                    }
+                    if (d.getMetricsCode().endsWith("5s")) {
+                        ms.setMetricsName(ms.getMetricsName().replace("5s", "500ms"));
+                        ms.setMetricsCode(ms.getMetricsCode().replace("5s", "500ms"));
+                    }
+                }
                 ms.setAlarmLow(d.getAlarmLow());
                 ms.setAlarmMid(d.getAlarmMid());
                 ms.setAlarmHigh(d.getAlarmHigh());
@@ -374,4 +392,120 @@ public class BizObjMetricsServiceImpl implements IBizObjMetricsService {
 
         bizObjMetricsMapper.batchInsert(objMetrics);
     }
+
+    /**
+     * 更新指标
+     *
+     * @param objId          对象ID
+     * @param tps            系统压力
+     * @param openFiles      打开文件数量
+     * @param heapUsage      堆内存使用率
+     * @param gcCount        gc次数
+     * @param pinpointVOList 链路列表数据
+     */
+    private void updateBizObjMetrics(Long objId, Float tps, int openFiles, float heapUsage, int gcCount, List<PinpointVO> pinpointVOList) {
+        updateJvmMetrics(objId, tps, heapUsage, openFiles, gcCount);
+        updateLinkMetrics(objId, pinpointVOList);
+    }
+
+    /**
+     * 更新jvm指标
+     *
+     * @param objId     对象ID
+     * @param tps       系统压力
+     * @param openFiles 打开文件数量
+     * @param heapUsage 堆内存使用率
+     * @param gcCount   gc次数
+     */
+    private void updateJvmMetrics(Long objId, Float tps, float heapUsage, int openFiles, int gcCount) {
+        BizObjMetrics jvmMetrics = new BizObjMetrics();
+        jvmMetrics.setObjId(objId);
+        jvmMetrics.setMetricsCode("pp.jvm.");
+        List<BizObjMetrics> omList = selectBizObjMetricsList(jvmMetrics);
+        updateMetricsValues(omList, "tps", BigDecimal.valueOf(tps));
+        updateMetricsValues(omList, "heap.usage", BigDecimal.valueOf(heapUsage));
+        updateMetricsValues(omList, "open", BigDecimal.valueOf(openFiles));
+        updateMetricsValues(omList, "gc", BigDecimal.valueOf(gcCount));
+    }
+
+    /**
+     * jvg指标更新
+     *
+     * @param omList       jvm指标列表
+     * @param metricSuffix 指标后缀
+     * @param value        值
+     */
+    private void updateMetricsValues(List<BizObjMetrics> omList, String metricSuffix, BigDecimal value) {
+        omList.stream()
+                .filter(om -> om.getMetricsCode().endsWith(metricSuffix))
+                .forEach(om -> {
+                    om.setDValue(value);
+                    updateBizObjMetrics(om);
+                });
+    }
+
+    /**
+     * 更新链路指标
+     *
+     * @param objId          对象ID
+     * @param pinpointVOList 链路列表
+     */
+    private void updateLinkMetrics(Long objId, List<PinpointVO> pinpointVOList) {
+        Map<String, BizObjMetrics> metricsMap = getAllMetricsForObjId(objId);
+        pinpointVOList.forEach(vo -> updateMetricsValues(metricsMap, vo));
+    }
+
+
+    /**
+     * 更新指标
+     *
+     * @param metricsMap 指标map
+     * @param vo         pp链路对象
+     */
+    private void updateMetricsValues(Map<String, BizObjMetrics> metricsMap, PinpointVO vo) {
+        metricsMap.values().stream()
+                .filter(om -> om.getMetricsCode().startsWith(vo.getApplicationName() + "/"))
+                .forEach(om -> {
+                    updateMetric(om, "pp.1s", vo.getTime1s());
+                    updateMetric(om, "pp.3s", vo.getTime3s());
+                    updateMetric(om, "pp.5s", vo.getTime5s());
+                    updateMetric(om, "pp.100ms", vo.getTime100ms());
+                    updateMetric(om, "pp.300ms", vo.getTime300ms());
+                    updateMetric(om, "pp.500ms", vo.getTime500ms());
+                    updateMetric(om, "pp.tot", vo.getTot());
+                    updateMetric(om, "pp.sum", vo.getSum());
+                    updateMetric(om, "pp.max", vo.getMax());
+                    updateMetric(om, "pp.avg", vo.getAvg());
+                    updateMetric(om, "pp.slow", vo.getSlow());
+                    updateMetric(om, "pp.error", vo.getError());
+                });
+    }
+
+    /**
+     * 调用更新方法
+     *
+     * @param om     对象
+     * @param suffix 前缘
+     * @param value  值
+     */
+    private void updateMetric(BizObjMetrics om, String suffix, Number value) {
+        if (om.getMetricsCode().endsWith(suffix)) {
+            om.setDValue(BigDecimal.valueOf(value.doubleValue()));
+            updateBizObjMetrics(om);
+        }
+    }
+
+    /**
+     * 获取所有指标对象,并以code转换成map
+     *
+     * @param objId 对象ID
+     * @return map
+     */
+    private Map<String, BizObjMetrics> getAllMetricsForObjId(Long objId) {
+        BizObjMetrics search = new BizObjMetrics();
+        search.setObjId(objId);
+        return selectBizObjMetricsList(search).stream()
+                .collect(Collectors.toMap(BizObjMetrics::getMetricsCode, Function.identity()));
+    }
+
 }

+ 6 - 3
jjt-biz/src/main/java/com/jjt/biz/service/impl/PinpointServiceImpl.java

@@ -128,7 +128,7 @@ public class PinpointServiceImpl implements IPinpointService {
 
         String result = HttpUtils.sendGet(uri);
         JSONObject jsonObject = JSONObject.parseObject(result);
-        JSONArray array = jsonObject.getJSONObject("charts").getJSONObject("y").getJSONArray("TPS_TOTAL");
+        JSONArray array = jsonObject.getJSONObject("charts").getJSONObject("y").getJSONArray("OPEN_FILE_DESCRIPTOR_COUNT");
         int num = 0;
         for (int i = 0; i < array.size(); i++) {
             JSONArray arr = array.getJSONArray(i);
@@ -168,14 +168,17 @@ public class PinpointServiceImpl implements IPinpointService {
         for (int i = 0; i < heapUsed.size(); i++) {
             float v = heapUsed.getJSONArray(i).getFloat(1);
             int gcV = gc.getJSONArray(i).getInteger(1);
-            gcTimes += gcV;
+            if (gcV > 0) {
+                //如果有gc则增加
+                gcTimes += gcV;
+            }
             if (v != 0f && v > used) {
                 used = v;
                 max = heapMax.getJSONArray(i).getFloat(1);
             }
         }
 
-        float usage = BigDecimal.valueOf(used).divide(BigDecimal.valueOf(max), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(1000)).floatValue();
+        float usage = BigDecimal.valueOf(used).divide(BigDecimal.valueOf(max), 2, RoundingMode.HALF_UP).multiply(BigDecimal.valueOf(100)).floatValue();
         Map<String, Object> map = new HashMap<>(16);
         map.put("usage", usage);
         map.put("gc", gcTimes);

+ 1 - 1
jjt-biz/src/main/resources/mapper/obj/BizObjMetricsMapper.xml

@@ -78,7 +78,7 @@
                 and METRICS_NAME like concat('%', #{metricsName}, '%')
             </if>
             <if test="metricsCode != null  and metricsCode != ''">
-                and METRICS_CODE = #{metricsCode}
+                and METRICS_CODE like concat(#{metricsCode}, '%')
             </if>
             <if test="alarmLow != null  and alarmLow != ''">
                 and ALARM_LOW = #{alarmLow}