Skip to content

Commit

Permalink
Group metrics by name
Browse files Browse the repository at this point in the history
Support NaN and +/- Inf
Escape label value chars
Remove unnecessary comments
  • Loading branch information
olaaun committed Mar 27, 2024
1 parent f596542 commit 3e226d9
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ protected Iterable<ByteBuffer> responseContent() {
}

private String resolveContentType(URI requestUri) {
if (resolvePath(requestUri).equals(HISTOGRAMS_PATH) || isPrometheusRequest(requestUri.getQuery())) {
if (resolvePath(requestUri).equals(HISTOGRAMS_PATH)) {
return "text/plain; charset=utf-8";
} else if (isPrometheusRequest(requestUri.getQuery())) {
return "text/plain; version=0.0.4";
} else {
return "application/json";
}
Expand Down Expand Up @@ -224,45 +226,38 @@ private byte[] buildPrometheusForConsumer(String consumer) {
var timestamp = snapshot.getToTime(TimeUnit.MILLISECONDS);
var builder = new StringBuilder();
builder.append("# NOTE: THIS API IS NOT INTENDED FOR PUBLIC USE\n");
var metrics = new ArrayList<PrometheusEntry>();

for (var tuple : collapseMetrics(snapshot, consumer)) {
var dims = toPrometheusDimensions(tuple.dim);
var metricName = prometheusSanitizedName(tuple.key) + "_";
if (tuple.val instanceof GaugeMetric gauge) {
appendPrometheusEntry(builder, metricName + "max", dims, gauge.getMax(), timestamp);
appendPrometheusEntry(builder, metricName + "sum", dims, gauge.getSum(), timestamp);
appendPrometheusEntry(builder, metricName + "count", dims, gauge.getCount(), timestamp);
metrics.add(new PrometheusEntry(metricName + "max", dims, gauge.getMax()));
metrics.add(new PrometheusEntry(metricName + "sum", dims, gauge.getSum()));
metrics.add(new PrometheusEntry(metricName + "count", dims, gauge.getCount()));
if (gauge.getPercentiles().isPresent()) {
for (Tuple2<String, Double> prefixAndValue : gauge.getPercentiles().get()) {
appendPrometheusEntry(builder, metricName + prefixAndValue.first + "percentile", dims, prefixAndValue.second, timestamp);
metrics.add(new PrometheusEntry(metricName + prefixAndValue.first + "percentile", dims, prefixAndValue.second));
}
}
} else if (tuple.val instanceof CountMetric count) {
appendPrometheusEntry(builder, metricName + "count", dims, count.getCount(), timestamp);
metrics.add(new PrometheusEntry(metricName + "count", dims, count.getCount()));
}
}
Collections.sort(metrics);
metrics.forEach(prometheusEntry -> prometheusEntry.appendPrometheusEntry(builder, timestamp));
return builder.toString().getBytes(UTF_8);
}

private void appendPrometheusEntry(StringBuilder builder, String metricName, String dimension, Number value, long timeStamp) {
builder.append("# HELP ")
.append(metricName)
.append("\n# TYPE ")
.append(metricName)
.append(" untyped\n");

builder.append(metricName)
.append("{").append(dimension).append("}")
.append(" ").append(sanitizeIfDouble(value)).append(" ")
.append(timeStamp).append("\n");
}

private String toPrometheusDimensions(MetricDimensions dimensions) {
if (dimensions == null) return "";
if (dimensions == null || !dimensions.iterator().hasNext()) return "";
StringBuilder builder = new StringBuilder();
builder.append("{");
dimensions.forEach(entry -> {
var sanitized = prometheusSanitizedName(entry.getKey()) + "=\"" + entry.getValue() + "\",";
var sanitized = prometheusSanitizedName(entry.getKey()) + "=\"" + escapedLabelValue(entry.getValue()) + "\",";
builder.append(sanitized);
});
builder.append("}");
return builder.toString();
}

Expand Down Expand Up @@ -385,8 +380,33 @@ private String prometheusSanitizedName(String name) {
return name.replaceAll("\\.", "_");
}

private Number sanitizeIfDouble(Number num) {
return num instanceof Double d ? sanitizeDouble(d) : num;
private String sanitizeIfDouble(Number num) {
return num instanceof Double d ? prettyDouble(d) : num.toString();
}

private String escapedLabelValue(String labelValue) {
var builder = new StringBuilder();
for (int i = 0; i < labelValue.length(); i++) {
var c = labelValue.charAt(i);
switch (c) {
case '\n':
builder.append("\\n");
break;
case '\\':
case '"':
builder.append("\\")
.append(c);
break;
default:
builder.append(c);
}
}
return builder.toString();
}

private String prettyDouble(Double d) {
if (Double.isFinite(d) || d.isNaN()) return d.toString();
return d.equals(Double.NEGATIVE_INFINITY) ? "-Inf" : "Inf";
}

private static byte[] toPrettyString(JsonNode resources) throws JsonProcessingException {
Expand Down Expand Up @@ -419,4 +439,29 @@ void add(MetricValue val) {
}
}

class PrometheusEntry implements Comparable<PrometheusEntry> {
final String metricName;
final String dimensions;
final Number value;

public PrometheusEntry(String metricName, String dimensions, Number value) {
this.metricName = metricName;
this.dimensions = dimensions;
this.value = value;
}

@Override
public int compareTo(PrometheusEntry o) {
int comparison = this.metricName.compareTo(o.metricName);
return comparison != 0 ? comparison : this.dimensions.compareTo(o.dimensions);
}

public void appendPrometheusEntry(StringBuilder builder, long timestamp) {
builder.append(metricName)
.append(dimensions)
.append(" ").append(sanitizeIfDouble(value)).append(" ")
.append(timestamp).append("\n");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,34 @@ void gaugeSnapshotsTracksCountMinMaxAvgPerPeriod() throws Exception {
@Test
public void testPrometheusFormat() {
var counterContext = StateMetricContext.newInstance(Map.of("label1", "val1", "label2", "val2"));
var otherContext = StateMetricContext.newInstance(Map.of(
"label1", "This label has \"quotes\"",
"label2", "This label, a\nnewline"));
var snapshot = new MetricSnapshot(0L, SNAPSHOT_INTERVAL, TimeUnit.MILLISECONDS);
snapshot.set(null, "bar", 20);
snapshot.set(null, "bar", 40);
snapshot.add(counterContext, "some.counter", 10);
snapshot.add(counterContext, "some.counter", 20);
snapshot.add(otherContext, "some.counter", 1);
snapshot.add(otherContext, "some.counter", 2);
snapshot.set(null, "bar", 20);
snapshot.set(null, "bar", 40);
snapshot.set(null, "testing.infinity", Double.NEGATIVE_INFINITY);
snapshot.set(null, "testing.nan", Double.NaN);
snapshotProvider.setSnapshot(snapshot);

var response = requestAsString(V1_URI + "metrics?format=prometheus");
var expectedResponse = """
# NOTE: THIS API IS NOT INTENDED FOR PUBLIC USE
# HELP bar_max
# TYPE bar_max untyped
bar_max{} 40.0 300000
# HELP bar_sum
# TYPE bar_sum untyped
bar_sum{} 60.0 300000
# HELP bar_count
# TYPE bar_count untyped
bar_count{} 2 300000
# HELP some_counter_count
# TYPE some_counter_count untyped
bar_count 2 300000
bar_max 40.0 300000
bar_sum 60.0 300000
some_counter_count{label1="This label has \\"quotes\\"",label2="This label, a\\nnewline",} 3 300000
some_counter_count{label1="val1",label2="val2",} 30 300000
testing_infinity_count 1 300000
testing_infinity_max -Inf 300000
testing_infinity_sum -Inf 300000
testing_nan_count 1 300000
testing_nan_max NaN 300000
testing_nan_sum NaN 300000
""";
assertEquals(expectedResponse, response);
}
Expand Down

0 comments on commit 3e226d9

Please sign in to comment.