Skip to content

Commit

Permalink
[Enhancement] Add histogram metric and node info metric in json metric (
Browse files Browse the repository at this point in the history
#53735)

Signed-off-by: xiangran0327 <[email protected]>
(cherry picked from commit 1e402d6)
  • Loading branch information
xiangran0327 authored and mergify[bot] committed Dec 13, 2024
1 parent ca834d0 commit 447def1
Showing 1 changed file with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,32 @@
package com.starrocks.metric;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.Snapshot;
import com.starrocks.monitor.jvm.GcNames;
import com.starrocks.monitor.jvm.JvmStats;
import com.starrocks.server.GlobalStateMgr;
import com.starrocks.server.NodeMgr;
import com.starrocks.system.SystemInfoService;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class JsonMetricVisitor extends MetricVisitor {
private boolean isFirstElement;
private final StringBuilder sb;

private static final String MILLISECONDS = "milliseconds";
private static final String QUANTILE = "quantile";
private static final String NOUNIT = "nounit";
private static final String TYPE = "type";
private static final String STATUS = "status";
private static final String TOTAL = "total";
private static final String FE_NODE_NUM = "fe_node_num";
private static final String BE_NODE_NUM = "be_node_num";
private static final String CN_NODE_NUM = "cn_node_num";
private static final String BROKER_NODE_NUM = "broker_node_num";

public JsonMetricVisitor(String prefix) {
super(prefix);
isFirstElement = true;
Expand Down Expand Up @@ -139,10 +156,57 @@ public void visitHistogram(HistogramMetric histogram) {

@Override
public void visitHistogram(String name, Histogram histogram) {
final String fullName = prefix + "_" + name.replace("\\.", "_");
Snapshot snapshot = histogram.getSnapshot();

buildMetric(fullName, MILLISECONDS, String.valueOf(snapshot.get75thPercentile()),
Collections.singletonList(new MetricLabel(QUANTILE, "0.75")));
buildMetric(fullName, MILLISECONDS, String.valueOf(snapshot.get95thPercentile()),
Collections.singletonList(new MetricLabel(QUANTILE, "0.95")));
buildMetric(fullName, MILLISECONDS, String.valueOf(snapshot.get98thPercentile()),
Collections.singletonList(new MetricLabel(QUANTILE, "0.98")));
buildMetric(fullName, MILLISECONDS, String.valueOf(snapshot.get99thPercentile()),
Collections.singletonList(new MetricLabel(QUANTILE, "0.99")));
buildMetric(fullName, MILLISECONDS, String.valueOf(snapshot.get999thPercentile()),
Collections.singletonList(new MetricLabel(QUANTILE, "0.999")));

buildMetric(fullName + "_sum", MILLISECONDS, String.valueOf(histogram.getCount() * snapshot.getMean()),
null);
buildMetric(fullName + "_count", NOUNIT, String.valueOf(histogram.getCount()), null);
}

@Override
public void getNodeInfo() {
final String NODE_INFO = "node_info";
final NodeMgr nodeMgr = GlobalStateMgr.getCurrentState().getNodeMgr();
final SystemInfoService systemInfoService = nodeMgr.getClusterInfo();

buildMetric(NODE_INFO, NOUNIT, String.valueOf(nodeMgr.getFrontends(null).size()),
Arrays.asList(new MetricLabel(TYPE, FE_NODE_NUM), new MetricLabel(STATUS, TOTAL)));
buildMetric(NODE_INFO, NOUNIT, String.valueOf(systemInfoService.getTotalBackendNumber()),
Arrays.asList(new MetricLabel(TYPE, BE_NODE_NUM), new MetricLabel(STATUS, TOTAL)));
buildMetric(NODE_INFO, NOUNIT, String.valueOf(systemInfoService.getAliveBackendNumber()),
Arrays.asList(new MetricLabel(TYPE, BE_NODE_NUM), new MetricLabel(STATUS, "alive")));
buildMetric(NODE_INFO, NOUNIT,
String.valueOf(systemInfoService.getDecommissionedBackendIds().size()),
Arrays.asList(new MetricLabel(TYPE, BE_NODE_NUM), new MetricLabel(STATUS, "decommissioned")));
buildMetric(NODE_INFO, NOUNIT, String.valueOf(
GlobalStateMgr.getCurrentState().getBrokerMgr().getAllBrokers().stream().filter(b -> !b.isAlive)
.count()),
Arrays.asList(new MetricLabel(TYPE, BROKER_NODE_NUM), new MetricLabel(STATUS, "dead")));

buildMetric(NODE_INFO, NOUNIT,
String.valueOf(systemInfoService.getTotalComputeNodeNumber()),
Arrays.asList(new MetricLabel(TYPE, CN_NODE_NUM), new MetricLabel(STATUS, TOTAL)));
buildMetric(NODE_INFO, NOUNIT,
String.valueOf(systemInfoService.getAliveComputeNodeNumber()),
Arrays.asList(new MetricLabel(TYPE, CN_NODE_NUM), new MetricLabel(STATUS, "alive")));

// only master FE has this metrics, to help the Grafana knows who is the leader
if (GlobalStateMgr.getCurrentState().isLeader()) {
buildMetric(NODE_INFO, NOUNIT, String.valueOf(1),
Collections.singletonList(new MetricLabel(TYPE, "is_master")));
}
}

@Override
Expand Down

0 comments on commit 447def1

Please sign in to comment.