Skip to content

Commit

Permalink
[fix][monitor] topic with double quote breaks the prometheus format (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoloboschi authored May 10, 2023
1 parent 152b4a2 commit ea56197
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ void writeSample(String metricName, Number value, String... labelsAndValuesArray
SimpleTextOutputStream stream = initGaugeType(metricName);
stream.write(metricName).write('{');
for (int i = 0; i < labelsAndValuesArray.length; i += 2) {
stream.write(labelsAndValuesArray[i]).write("=\"").write(labelsAndValuesArray[i + 1]).write('\"');
String labelValue = labelsAndValuesArray[i + 1];
if (labelValue != null) {
labelValue = labelValue.replace("\"", "\\\"");
}
stream.write(labelsAndValuesArray[i]).write("=\"").write(labelValue).write('\"');
if (i + 2 != labelsAndValuesArray.length) {
stream.write(',');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1953,4 +1953,30 @@ public String toString() {
}
}

@Test
public void testEscapeLabelValue() throws Exception {
String ns1 = "prop/ns-abc1";
admin.namespaces().createNamespace(ns1);
String topic = "persistent://" + ns1 + "/\"mytopic";
admin.topics().createNonPartitionedTopic(topic);

@Cleanup
final Consumer<?> consumer = pulsarClient.newConsumer()
.subscriptionName("sub")
.topic(topic)
.subscribe();
@Cleanup
ByteArrayOutputStream statsOut = new ByteArrayOutputStream();
PrometheusMetricsGenerator.generate(pulsar, true, false,
false, statsOut);
String metricsStr = statsOut.toString();
final List<String> subCountLines = metricsStr.lines()
.filter(line -> line.startsWith("pulsar_subscriptions_count"))
.collect(Collectors.toList());
System.out.println(subCountLines);
assertEquals(subCountLines.size(), 1);
assertEquals(subCountLines.get(0),
"pulsar_subscriptions_count{cluster=\"test\",namespace=\"prop/ns-abc1\",topic=\"persistent://prop/ns-abc1/\\\"mytopic\"} 1");
}

}

0 comments on commit ea56197

Please sign in to comment.