Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,19 @@ private static String parseOpenMetricsVersion(@Nullable String acceptHeader) {
if (acceptHeader == null) {
return null;
}
for (String mediaType : acceptHeader.split(";")) {
String[] tokens = mediaType.split("=");
if (tokens.length == 2) {
String key = tokens[0].trim();
String value = tokens[1].trim();
if (key.equals("version")) {
return value;
for (String mediaType : acceptHeader.split(",")) {
if (mediaType.contains("application/openmetrics-text")) {
for (String param : mediaType.split(";")) {
String[] tokens = param.split("=");
if (tokens.length == 2) {
String key = tokens[0].trim();
String value = tokens[1].trim();
if (key.equals("version")) {
return value;
}
}
}
return null;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,37 @@ void testOM2ContentNegotiationDisabled() {
assertThat(writer3).isInstanceOf(OpenMetrics2TextFormatWriter.class);
}

@Test
void testOM2ContentNegotiationMultiTypeAcceptHeaderWithoutOMVersion() {
// When the Accept header has multiple media types and only text/plain has a version,
// the openmetrics type should be treated as having no version (use OM1).
PrometheusProperties props =
PrometheusProperties.builder()
.openMetrics2Properties(
OpenMetrics2Properties.builder().enabled(true).contentNegotiation(true).build())
.build();
ExpositionFormats formats = ExpositionFormats.init(props);
ExpositionFormatWriter writer =
formats.findWriter("application/openmetrics-text, text/plain; version=0.0.4");
assertThat(writer).isInstanceOf(OpenMetricsTextFormatWriter.class);
}

@Test
void testOM2ContentNegotiationMultiTypeAcceptHeaderWithOMVersion() {
// When the Accept header has multiple media types and openmetrics has version=2.0.0,
// the OM2 writer should be selected.
PrometheusProperties props =
PrometheusProperties.builder()
.openMetrics2Properties(
OpenMetrics2Properties.builder().enabled(true).contentNegotiation(true).build())
.build();
ExpositionFormats formats = ExpositionFormats.init(props);
ExpositionFormatWriter writer =
formats.findWriter(
"application/openmetrics-text; version=2.0.0, text/plain; version=0.0.4");
assertThat(writer).isInstanceOf(OpenMetrics2TextFormatWriter.class);
}

@Test
void testCounterComplete() throws IOException {
String openMetricsText =
Expand Down
Loading