Skip to content

Commit 5e6f0a8

Browse files
committed
Add UTF-8 content negotiation changes
Signed-off-by: Federico Torres <[email protected]>
1 parent 5f587b8 commit 5e6f0a8

File tree

10 files changed

+502
-84
lines changed

10 files changed

+502
-84
lines changed

prometheus-metrics-exporter-common/src/main/java/io/prometheus/metrics/exporter/common/PrometheusScrapeHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.prometheus.metrics.expositionformats.ExpositionFormats;
77
import io.prometheus.metrics.model.registry.MetricNameFilter;
88
import io.prometheus.metrics.model.registry.PrometheusRegistry;
9+
import io.prometheus.metrics.model.snapshots.EscapingScheme;
910
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
1011

1112
import java.io.ByteArrayOutputStream;
@@ -51,15 +52,16 @@ public void handleRequest(PrometheusHttpExchange exchange) throws IOException {
5152
PrometheusHttpRequest request = exchange.getRequest();
5253
PrometheusHttpResponse response = exchange.getResponse();
5354
MetricSnapshots snapshots = scrape(request);
54-
if (writeDebugResponse(snapshots, exchange)) {
55+
String acceptHeader = request.getHeader("Accept");
56+
EscapingScheme escapingScheme = EscapingScheme.fromAcceptHeader(acceptHeader);
57+
if (writeDebugResponse(snapshots, exchange, escapingScheme)) {
5558
return;
5659
}
5760
ByteArrayOutputStream responseBuffer = new ByteArrayOutputStream(lastResponseSize.get() + 1024);
58-
String acceptHeader = request.getHeader("Accept");
5961
ExpositionFormatWriter writer = expositionFormats.findWriter(acceptHeader);
60-
writer.write(responseBuffer, snapshots);
62+
writer.write(responseBuffer, snapshots, escapingScheme);
6163
lastResponseSize.set(responseBuffer.size());
62-
response.setHeader("Content-Type", writer.getContentType());
64+
response.setHeader("Content-Type", writer.getContentType() + escapingScheme.toHeaderFormat());
6365

6466
if (shouldUseCompression(request)) {
6567
response.setHeader("Content-Encoding", "gzip");
@@ -126,7 +128,7 @@ private Predicate<String> makeNameFilter(String[] includedNames) {
126128
return result;
127129
}
128130

129-
private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange) throws IOException {
131+
private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExchange exchange, EscapingScheme escapingScheme) throws IOException {
130132
String debugParam = exchange.getRequest().getParameter("debug");
131133
PrometheusHttpResponse response = exchange.getResponse();
132134
if (debugParam == null) {
@@ -138,10 +140,10 @@ private boolean writeDebugResponse(MetricSnapshots snapshots, PrometheusHttpExch
138140
OutputStream body = response.sendHeadersAndGetBody(responseStatus, 0);
139141
switch (debugParam) {
140142
case "openmetrics":
141-
expositionFormats.getOpenMetricsTextFormatWriter().write(body, snapshots);
143+
expositionFormats.getOpenMetricsTextFormatWriter().write(body, snapshots, escapingScheme);
142144
break;
143145
case "text":
144-
expositionFormats.getPrometheusTextFormatWriter().write(body, snapshots);
146+
expositionFormats.getPrometheusTextFormatWriter().write(body, snapshots, escapingScheme);
145147
break;
146148
case "prometheus-protobuf":
147149
String debugString = expositionFormats.getPrometheusProtobufWriter().toDebugString(snapshots);

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormatWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.prometheus.metrics.expositionformats;
22

3+
import io.prometheus.metrics.model.snapshots.EscapingScheme;
34
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
45

56
import java.io.IOException;
@@ -11,6 +12,6 @@ public interface ExpositionFormatWriter {
1112
/**
1213
* Text formats use UTF-8 encoding.
1314
*/
14-
void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException;
15+
void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) throws IOException;
1516
String getContentType();
1617
}

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/OpenMetricsTextFormatWriter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ public String getContentType() {
4040
return CONTENT_TYPE;
4141
}
4242

43-
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
43+
public void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) throws IOException {
4444
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
45-
for (MetricSnapshot snapshot : metricSnapshots) {
45+
for (MetricSnapshot s : metricSnapshots) {
46+
MetricSnapshot snapshot = PrometheusNaming.escapeMetricSnapshot(s, escapingScheme);
47+
4648
if (snapshot.getDataPoints().size() > 0) {
4749
if (snapshot instanceof CounterSnapshot) {
4850
writeCounter(writer, (CounterSnapshot) snapshot);

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusProtobufWriter.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
package io.prometheus.metrics.expositionformats;
22

3+
import io.prometheus.metrics.model.snapshots.*;
34
import io.prometheus.metrics.shaded.com_google_protobuf_3_21_7.TextFormat;
45
import io.prometheus.metrics.expositionformats.generated.com_google_protobuf_3_21_7.Metrics;
5-
import io.prometheus.metrics.model.snapshots.ClassicHistogramBuckets;
6-
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
76
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot;
8-
import io.prometheus.metrics.model.snapshots.Exemplar;
9-
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
10-
import io.prometheus.metrics.model.snapshots.HistogramSnapshot;
11-
import io.prometheus.metrics.model.snapshots.InfoSnapshot;
12-
import io.prometheus.metrics.model.snapshots.Labels;
13-
import io.prometheus.metrics.model.snapshots.DataPointSnapshot;
14-
import io.prometheus.metrics.model.snapshots.MetricMetadata;
15-
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
16-
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
17-
import io.prometheus.metrics.model.snapshots.NativeHistogramBuckets;
18-
import io.prometheus.metrics.model.snapshots.Quantiles;
19-
import io.prometheus.metrics.model.snapshots.StateSetSnapshot;
20-
import io.prometheus.metrics.model.snapshots.SummarySnapshot;
21-
import io.prometheus.metrics.model.snapshots.UnknownSnapshot;
227

238
import java.io.IOException;
249
import java.io.OutputStream;
@@ -61,7 +46,7 @@ public String toDebugString(MetricSnapshots metricSnapshots) {
6146
}
6247

6348
@Override
64-
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
49+
public void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) throws IOException {
6550
for (MetricSnapshot snapshot : metricSnapshots) {
6651
if (snapshot.getDataPoints().size() > 0) {
6752
convert(snapshot).writeDelimitedTo(out);

prometheus-metrics-exposition-formats/src/main/java/io/prometheus/metrics/expositionformats/PrometheusTextFormatWriter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ public String getContentType() {
3737
return CONTENT_TYPE;
3838
}
3939

40-
public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOException {
40+
public void write(OutputStream out, MetricSnapshots metricSnapshots, EscapingScheme escapingScheme) throws IOException {
4141
// See https://prometheus.io/docs/instrumenting/exposition_formats/
4242
// "unknown", "gauge", "counter", "stateset", "info", "histogram", "gaugehistogram", and "summary".
4343
OutputStreamWriter writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
44-
for (MetricSnapshot snapshot : metricSnapshots) {
44+
for (MetricSnapshot s : metricSnapshots) {
45+
MetricSnapshot snapshot = PrometheusNaming.escapeMetricSnapshot(s, escapingScheme);
46+
4547
if (snapshot.getDataPoints().size() > 0) {
4648
if (snapshot instanceof CounterSnapshot) {
4749
writeCounter(writer, (CounterSnapshot) snapshot);
@@ -61,7 +63,9 @@ public void write(OutputStream out, MetricSnapshots metricSnapshots) throws IOEx
6163
}
6264
}
6365
if (writeCreatedTimestamps) {
64-
for (MetricSnapshot snapshot : metricSnapshots) {
66+
for (MetricSnapshot ms : metricSnapshots) {
67+
MetricSnapshot snapshot = PrometheusNaming.escapeMetricSnapshot(ms, escapingScheme);
68+
6569
if (snapshot.getDataPoints().size() > 0) {
6670
if (snapshot instanceof CounterSnapshot) {
6771
writeCreated(writer, snapshot);

0 commit comments

Comments
 (0)