Skip to content

Commit 0918a3c

Browse files
Flossyclaude
andcommitted
fix: Add null handling to PrometheusFormatter.escapeLabelValue()
Fixes #205 Added null check to escapeLabelValue() method to prevent NullPointerException when label maps contain null values. Returns empty string for null values as Prometheus doesn't support null label values. Changes: - escapeLabelValue(): Check if value is null before calling replace() methods - Return empty string for null values (Prometheus compatible) - Updated javadoc to document null handling behavior - Added test testLabelValueNull() to verify null labels are handled correctly - Test confirms null values are converted to empty strings without errors This prevents metrics export failures when applications have null metadata and ensures all metrics are successfully formatted. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 62de314 commit 0918a3c

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

jplatform-metrics-prometheus/src/main/java/org/flossware/jplatform/metrics/prometheus/PrometheusFormatter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,13 @@ private static String formatLabels(Map<String, String> labels) {
121121
* Escapes special characters in label values according to Prometheus format.
122122
* Escapes backslashes, double quotes, and newlines.
123123
*
124-
* @param value the label value to escape
125-
* @return escaped label value
124+
* @param value the label value to escape (may be null)
125+
* @return escaped label value, or empty string if value is null
126126
*/
127127
private static String escapeLabelValue(String value) {
128+
if (value == null) {
129+
return ""; // Prometheus doesn't allow null values, use empty string
130+
}
128131
return value
129132
.replace("\\", "\\\\") // Escape backslashes first
130133
.replace("\"", "\\\"") // Escape double quotes

jplatform-metrics-prometheus/src/test/java/org/flossware/jplatform/metrics/prometheus/PrometheusFormatterTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,20 @@ void testCompleteMetricFormatting() {
143143
assertTrue(result.contains("state=\"running\""));
144144
assertTrue(result.contains(" 1.0\n"));
145145
}
146+
147+
@Test
148+
void testLabelValueNull() {
149+
Map<String, String> labels = new HashMap<>();
150+
labels.put("app_id", "test-app");
151+
labels.put("version", null); // null label value
152+
153+
// Should not throw NullPointerException, null should be converted to empty string
154+
String result = PrometheusFormatter.formatGauge("my_metric", labels, 1.0);
155+
156+
assertNotNull(result);
157+
assertTrue(result.startsWith("my_metric{"));
158+
assertTrue(result.contains("app_id=\"test-app\""));
159+
assertTrue(result.contains("version=\"\"")); // null becomes empty string
160+
assertTrue(result.endsWith(" 1.0\n"));
161+
}
146162
}

0 commit comments

Comments
 (0)