Skip to content

Commit 4ddeac2

Browse files
morningmanclaude
andcommitted
[fix](metrics) Write a null label value as null in the Prometheus output of FE
### What problem does this PR solve? Issue Number: #69907 Related PR: #69837 Problem Summary: `escapeLabelValue` called `replace` on every label value, so a null one threw a NullPointerException, and nothing on the way to `/metrics` catches it: `DorisMetricRegistry.accept`, `MetricRepo.getMetric` and `MetricsAction` let it through, and the whole response fails. Label values can be null: a table warm-up job (`WARM UP COMPUTE GROUP <dst> WITH TABLE ...`) has no source cluster, so `CloudWarmUpJob` keeps its `srcClusterName` null, and the job registers `doris_fe_file_cache_warm_up_job_latest_start_time` each time it starts and `doris_fe_file_cache_warm_up_job_last_finish_time` when it is cancelled, both with `src_cluster_name` set to null. Neither series is ever removed, so in cloud mode the first table warm-up job to start made every scrape of the Master FE's `/metrics` fail until the FE restarted. The registry itself already accepts a null label value (`computeLabelId` maps it to an empty string). Before label values were escaped, the string concatenation wrote it as `null`. `escapeLabelValue` now writes a null value as `null`, so the output is again unchanged for every value without a backslash, a double quote or a line feed. ### Release note None ### Check List (For Author) - Test: Unit Test - `MetricsTest#testPrometheusVisitorWritesNullLabelValue`: a gauge and a labeled histogram with a null label value are written with `null`; without this fix the case fails with a NullPointerException. - Behavior changed: No - Does this need documentation: No Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8a9f618 commit 4ddeac2

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/metric/PrometheusMetricVisitor.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,9 @@ public void visitWorkloadGroup() {
367367
}
368368

369369
// The text format requires a backslash, a double quote and a line feed in a label value to be escaped,
370-
// and names such as a table name may contain any of them.
370+
// and names such as a table name may contain any of them. A label value may be null (a table warm-up job
371+
// has no source cluster), which is written as null, as the concatenation without escaping wrote it.
371372
private static String escapeLabelValue(String value) {
372-
return value.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n");
373+
return String.valueOf(value).replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n");
373374
}
374375
}

‎fe/fe-core/src/test/java/org/apache/doris/metric/MetricsTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,31 @@ public void testPrometheusVisitorEscapesLabelValues() {
378378
+ escapedName + "\"} 1\n"), result);
379379
}
380380

381+
@Test
382+
public void testPrometheusVisitorWritesNullLabelValue() {
383+
// A table warm-up job has no source cluster, so the series of its start and finish times carry a null
384+
// src_cluster_name; it is written as null, as before label values were escaped.
385+
GaugeMetricImpl<Long> gauge = new GaugeMetricImpl<>("null_label_test", MetricUnit.NOUNIT, "null label test",
386+
1L);
387+
gauge.addLabel(new MetricLabel("src_cluster_name", null)).addLabel(new MetricLabel("dst_cluster_name", "cg1"));
388+
MetricVisitor visitor = new PrometheusMetricVisitor();
389+
visitor.visit(MetricVisitor.FE_PREFIX, gauge);
390+
Assertions.assertEquals("# HELP doris_fe_null_label_test null label test\n"
391+
+ "# TYPE doris_fe_null_label_test gauge\n"
392+
+ "doris_fe_null_label_test{src_cluster_name=\"null\", dst_cluster_name=\"cg1\"} 1\n",
393+
visitor.finish());
394+
395+
HistogramMetric histogram = new HistogramMetric("null_label.latency.ms",
396+
Lists.newArrayList(new MetricLabel("src_cluster_name", null)));
397+
histogram.update(10L);
398+
visitor = new PrometheusMetricVisitor();
399+
visitor.visitHistogram(MetricVisitor.FE_PREFIX, histogram.getName(), histogram.getHistogram(),
400+
histogram.getLabels());
401+
String result = visitor.finish();
402+
Assertions.assertTrue(result.contains("doris_fe_null_label_latency_ms_count{src_cluster_name=\"null\"} 1\n"),
403+
result);
404+
}
405+
381406
@Test
382407
public void testCloudTableStatsEscapeTableName() {
383408
String originCloudUniqueId = Config.cloud_unique_id;

0 commit comments

Comments
 (0)