[BugFix] Fix metrics of Prometheus format bug (backport #62742) (#62838)

Signed-off-by: gengjun-git <gengjun@starrocks.com>
Co-authored-by: gengjun-git <gengjun@starrocks.com>
This commit is contained in:
mergify[bot] 2025-09-09 02:43:54 +00:00 committed by GitHub
parent 47386399b5
commit fb75a71742
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 16 deletions

View File

@ -981,8 +981,13 @@ public final class MetricRepo {
visitor.visitJvm(jvmStats);
// starrocks metrics
for (Metric metric : STARROCKS_METRIC_REGISTER.getMetrics()) {
visitor.visit(metric);
List<Metric> metrics = STARROCKS_METRIC_REGISTER.getMetrics();
// group metrics by name so that the same metric name will be visited together
Map<String, List<Metric>> groupedMetrics = metrics.stream().collect(Collectors.groupingBy(Metric::getName));
for (Map.Entry<String, List<Metric>> entry : groupedMetrics.entrySet()) {
for (Metric metric : entry.getValue()) {
visitor.visit(metric);
}
}
// database metrics

View File

@ -83,8 +83,8 @@ public class PrometheusMetricVisitor extends MetricVisitor {
@Override
public void visitJvm(JvmStats jvmStats) {
// heap
sb.append(Joiner.on(" ").join(HELP, JVM_HEAP_SIZE_BYTES, "jvm heap stat\n"));
sb.append(Joiner.on(" ").join(TYPE, JVM_HEAP_SIZE_BYTES, "gauge\n"));
sb.append(HELP).append(JVM_HEAP_SIZE_BYTES).append(" jvm heap stat\n");
sb.append(TYPE).append(JVM_HEAP_SIZE_BYTES).append(" gauge\n");
sb.append(JVM_HEAP_SIZE_BYTES).append("{type=\"max\"} ").append(jvmStats.getMem().getHeapMax())
.append("\n");
sb.append(JVM_HEAP_SIZE_BYTES).append("{type=\"committed\"} ")
@ -92,8 +92,8 @@ public class PrometheusMetricVisitor extends MetricVisitor {
sb.append(JVM_HEAP_SIZE_BYTES).append("{type=\"used\"} ").append(jvmStats.getMem().getHeapUsed())
.append("\n");
// non heap
sb.append(Joiner.on(" ").join(HELP, JVM_NON_HEAP_SIZE_BYTES, "jvm non heap stat\n"));
sb.append(Joiner.on(" ").join(TYPE, JVM_NON_HEAP_SIZE_BYTES, "gauge\n"));
sb.append(HELP).append(JVM_NON_HEAP_SIZE_BYTES).append(" jvm non heap stat\n");
sb.append(TYPE).append(JVM_NON_HEAP_SIZE_BYTES).append(" gauge\n");
sb.append(JVM_NON_HEAP_SIZE_BYTES).append("{type=\"committed\"} ")
.append(jvmStats.getMem().getNonHeapCommitted()).append("\n");
sb.append(JVM_NON_HEAP_SIZE_BYTES).append("{type=\"used\"} ")
@ -111,9 +111,8 @@ public class PrometheusMetricVisitor extends MetricVisitor {
// direct buffer pool
for (BufferPool pool : jvmStats.getBufferPools()) {
if (pool.getName().equalsIgnoreCase("direct")) {
sb.append(Joiner.on(" ").join(HELP, JVM_DIRECT_BUFFER_POOL_SIZE_BYTES,
"jvm direct buffer pool stat\n"));
sb.append(Joiner.on(" ").join(TYPE, JVM_DIRECT_BUFFER_POOL_SIZE_BYTES, "gauge\n"));
sb.append(HELP).append(JVM_DIRECT_BUFFER_POOL_SIZE_BYTES).append(" jvm direct buffer pool stat\n");
sb.append(TYPE).append(JVM_DIRECT_BUFFER_POOL_SIZE_BYTES).append(" gauge\n");
sb.append(JVM_DIRECT_BUFFER_POOL_SIZE_BYTES).append("{type=\"count\"} ").append(pool.getCount())
.append("\n");
sb.append(JVM_DIRECT_BUFFER_POOL_SIZE_BYTES).append("{type=\"used\"} ")
@ -134,15 +133,15 @@ public class PrometheusMetricVisitor extends MetricVisitor {
// threads
Threads threads = jvmStats.getThreads();
sb.append(Joiner.on(" ").join(HELP, JVM_THREAD, "jvm thread stat\n"));
sb.append(Joiner.on(" ").join(TYPE, JVM_THREAD, "gauge\n"));
sb.append(HELP).append(JVM_THREAD).append(" jvm thread stat\n");
sb.append(TYPE).append(JVM_THREAD).append(" gauge\n");
sb.append(JVM_THREAD).append("{type=\"count\"} ").append(threads.getCount()).append("\n");
sb.append(JVM_THREAD).append("{type=\"peak_count\"} ").append(threads.getPeakCount()).append("\n");
}
private void addGcMetrics(GarbageCollector gc, String metricName, String desc) {
sb.append(Joiner.on(" ").join(HELP, metricName, desc));
sb.append(Joiner.on(" ").join(TYPE, metricName, "gauge\n"));
sb.append(HELP).append(metricName).append(" ").append(desc);
sb.append(TYPE).append(metricName).append(" gauge\n");
sb.append(metricName).append("{type=\"count\"} ").append(gc.getCollectionCount()).append("\n");
sb.append(metricName).append("{type=\"time\"} ").append(gc.getCollectionTime().getMillis())
.append("\n");
@ -150,8 +149,8 @@ public class PrometheusMetricVisitor extends MetricVisitor {
}
private void addMemPoolMetrics(MemoryPool memPool, String metricName, String desc) {
sb.append(Joiner.on(" ").join(HELP, metricName, desc));
sb.append(Joiner.on(" ").join(TYPE, metricName, "gauge\n"));
sb.append(HELP).append(metricName).append(" ").append(desc);
sb.append(TYPE).append(metricName).append(" gauge\n");
sb.append(metricName).append("{type=\"committed\"} ").append(memPool.getCommitted())
.append("\n");
sb.append(metricName).append("{type=\"used\"} ").append(memPool.getUsed())
@ -254,7 +253,7 @@ public class PrometheusMetricVisitor extends MetricVisitor {
final String NODE_INFO = "node_info";
final NodeMgr nodeMgr = GlobalStateMgr.getCurrentState().getNodeMgr();
final SystemInfoService systemInfoService = nodeMgr.getClusterInfo();
sb.append(Joiner.on(" ").join(TYPE, NODE_INFO, "gauge\n"));
sb.append(TYPE).append(NODE_INFO).append(" gauge\n");
sb.append(NODE_INFO).append("{type=\"fe_node_num\", state=\"total\"} ")
.append(nodeMgr.getFrontends(null).size()).append("\n");
sb.append(NODE_INFO).append("{type=\"fe_node_num\", state=\"alive\"} ")