Skip to content

Commit b15d16e

Browse files
authored
fix: handle multi-type Accept headers in parseOpenMetricsVersion (#1987)
1 parent 01a4080 commit b15d16e

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/ExpositionFormats.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,19 @@ private static String parseOpenMetricsVersion(@Nullable String acceptHeader) {
108108
if (acceptHeader == null) {
109109
return null;
110110
}
111-
for (String mediaType : acceptHeader.split(";")) {
112-
String[] tokens = mediaType.split("=");
113-
if (tokens.length == 2) {
114-
String key = tokens[0].trim();
115-
String value = tokens[1].trim();
116-
if (key.equals("version")) {
117-
return value;
111+
for (String mediaType : acceptHeader.split(",")) {
112+
if (mediaType.contains("application/openmetrics-text")) {
113+
for (String param : mediaType.split(";")) {
114+
String[] tokens = param.split("=");
115+
if (tokens.length == 2) {
116+
String key = tokens[0].trim();
117+
String value = tokens[1].trim();
118+
if (key.equals("version")) {
119+
return value;
120+
}
121+
}
118122
}
123+
return null;
119124
}
120125
}
121126
return null;

prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/ExpositionFormatsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,37 @@ void testOM2ContentNegotiationDisabled() {
231231
assertThat(writer3).isInstanceOf(OpenMetrics2TextFormatWriter.class);
232232
}
233233

234+
@Test
235+
void testOM2ContentNegotiationMultiTypeAcceptHeaderWithoutOMVersion() {
236+
// When the Accept header has multiple media types and only text/plain has a version,
237+
// the openmetrics type should be treated as having no version (use OM1).
238+
PrometheusProperties props =
239+
PrometheusProperties.builder()
240+
.openMetrics2Properties(
241+
OpenMetrics2Properties.builder().enabled(true).contentNegotiation(true).build())
242+
.build();
243+
ExpositionFormats formats = ExpositionFormats.init(props);
244+
ExpositionFormatWriter writer =
245+
formats.findWriter("application/openmetrics-text, text/plain; version=0.0.4");
246+
assertThat(writer).isInstanceOf(OpenMetricsTextFormatWriter.class);
247+
}
248+
249+
@Test
250+
void testOM2ContentNegotiationMultiTypeAcceptHeaderWithOMVersion() {
251+
// When the Accept header has multiple media types and openmetrics has version=2.0.0,
252+
// the OM2 writer should be selected.
253+
PrometheusProperties props =
254+
PrometheusProperties.builder()
255+
.openMetrics2Properties(
256+
OpenMetrics2Properties.builder().enabled(true).contentNegotiation(true).build())
257+
.build();
258+
ExpositionFormats formats = ExpositionFormats.init(props);
259+
ExpositionFormatWriter writer =
260+
formats.findWriter(
261+
"application/openmetrics-text; version=2.0.0, text/plain; version=0.0.4");
262+
assertThat(writer).isInstanceOf(OpenMetrics2TextFormatWriter.class);
263+
}
264+
234265
@Test
235266
void testCounterComplete() throws IOException {
236267
String openMetricsText =

0 commit comments

Comments
 (0)