Step one:

Modified project cat-Client

Com. Dianping. The status. StatusInfoCollector. VisitStatus method

/ * ** Customize indicators   * @param status* /  private void addCustomItems(StatusInfo status) {ServerInfo serverInfo = ServerInfoService.instance().calcServerInfo(); // Generate CPU and memory data    CustomInfo cpuRate = new CustomInfo();cpuRate.setKey("cpuRate"); // Customize the KEY of an indicatorcpuRate.setValue(serverInfo.getCpuPercent() + ""); // Customize the indicator valuestatus.addCustomInfo(cpuRate); / / in statusInfo        CustomInfo memoryRate = new CustomInfo();      memoryRate.setKey("memoryRate");    memoryRate.setValue(serverInfo.getMemoryPercent() + "");    status.addCustomInfo(memoryRate);  }Copy the code

This allows the client to send cpuRate and memoryRate to the server during heartbeat.

Step 2:

Modified project cat-Consumer

com.dianping.cat.consumer.heartbeat.HeartbeatAnalyzer.

TranslateHeartbeat () method

/ * ** Customize indicators   * @param info* /  private void addCustomItems(StatusInfo info) {Extension customExtension = info.findOrCreateExtension("ServerInfo"); // This is the type of indicator        CustomInfo cpuRate = info.getCustomInfos().get("cpuRate");    customExtension.findOrCreateExtensionDetail(cpuRate.getKey())        .setValue(ServerInfoUtils.str2Double(cpuRate.getValue()));        CustomInfo memoryRate = info.getCustomInfos().get("memoryRate");    customExtension.findOrCreateExtensionDetail(memoryRate.getKey())    .setValue(ServerInfoUtils.str2Double(memoryRate.getValue()));      }Copy the code

Step 3:

Modified project cat-Consumer

com.dianping.cat.consumer.heartbeat.HeartbeatReportMerger

. MergePeriod () method

  @Override  protected void mergePeriod(Period old, Period period) {    old.setCatMessageOverflow(period.getCatMessageOverflow());    old.setCatMessageProduced(period.getCatMessageProduced());    old.setCatMessageSize(period.getCatMessageSize());    old.setCatThreadCount(period.getCatThreadCount());    old.setDaemonCount(period.getDaemonCount());    old.setHeapUsage(period.getHeapUsage());    old.setHttpThreadCount(period.getHttpThreadCount());    old.setMemoryFree(period.getMemoryFree());    old.setMinute(period.getMinute());    old.setNewGcCount(period.getNewGcCount());    old.setNoneHeapUsage(period.getNoneHeapUsage());    old.setOldGcCount(period.getOldGcCount());    old.setPigeonThreadCount(period.getPigeonThreadCount());    old.setSystemLoadAverage(period.getSystemLoadAverage());    old.setThreadCount(period.getThreadCount());    old.setTotalStartedCount(period.getTotalStartedCount());// Add two indicators    old.setDynamicAttribute("cpuRate", period.getDynamicAttribute("cpuRate"));    old.setDynamicAttribute("memoryRate", period.getDynamicAttribute("memoryRate"));  }Copy the code

Step 4: Project cat-Home

Modify the inner class com. Dianping. The report. The page. The heartbeat. Service

. HeartbeatReportService. HeartbeatConvertor visitPeriod method

/ * ** Customize indicators     * @param period* /    private void addCustomItems(Period period) {      Extension customExtension = period.findOrCreateExtension("ServerInfo");                  customExtension.findOrCreateDetail("cpuRate")          .setValue(ServerInfoUtils.str2Double(period.getDynamicAttribute("cpuRate")));                        customExtension.findOrCreateDetail("memoryRate")      .setValue(ServerInfoUtils.str2Double(period.getDynamicAttribute("memoryRate")));          }Copy the code

At this point, the modification part of the code is complete.

Step 5: Configure [Application Monitoring Configuration – Heartbeat Report Display] to add a group

   <group id="ServerInfo" order="1">      <metric id="cpuRate" unit="1" delta="false" order="1" lable="%" alert="true"/>      <metric id="memoryRate" unit="1" delta="false" order="2" lable="%" alert="true"/>   </group>Copy the code

Custom metrics are successful and can be seen in the heartbeat report.

You can customize indicators and configure alarms. In this way, alarms can be sent when the CPU is abnormal. For details on how to configure alarms, see this article

Yelp CAT alarm configuration manual teaching

Source moment, public number: source moment
Yelp CAT alarm configuration manual teaching

If this article helps you, please pay attention to and forward, grateful disrespect!