Skip to content

Commit

Permalink
Adding the right MediaTypes for Prometheus
Browse files Browse the repository at this point in the history
This seems like a hack and Producible seems weird.
fixes spring-projectsgh-28130
  • Loading branch information
jonatan-ivanov committed Sep 25, 2021
1 parent 8c8f278 commit b16f10f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
Expand Up @@ -56,7 +56,7 @@ public WebEndpointResponse<String> scrape(TextOutputFormat format, @Nullable Set
? this.collectorRegistry.filteredMetricFamilySamples(includedNames)
: this.collectorRegistry.metricFamilySamples();
format.write(writer, samples);
return new WebEndpointResponse<>(writer.toString(), format);
return new WebEndpointResponse<>(writer.toString(), format.getContentType());
}
catch (IOException ex) {
// This actually never happens since StringWriter doesn't throw an IOException
Expand Down
Expand Up @@ -24,6 +24,7 @@
import io.prometheus.client.exporter.common.TextFormat;

import org.springframework.boot.actuate.endpoint.Producible;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;

Expand All @@ -38,7 +39,7 @@ public enum TextOutputFormat implements Producible<TextOutputFormat> {
/**
* OpenMetrics text version 1.0.0.
*/
CONTENT_TYPE_OPENMETRICS_100(TextFormat.CONTENT_TYPE_OPENMETRICS_100) {
CONTENT_TYPE_OPENMETRICS_100("application/openmetrics-text") {

@Override
void write(Writer writer, Enumeration<MetricFamilySamples> samples) throws IOException {
Expand All @@ -50,7 +51,7 @@ void write(Writer writer, Enumeration<MetricFamilySamples> samples) throws IOExc
/**
* Prometheus text version 0.0.4.
*/
CONTENT_TYPE_004(TextFormat.CONTENT_TYPE_004) {
CONTENT_TYPE_004(MediaType.TEXT_PLAIN_VALUE) {

@Override
void write(Writer writer, Enumeration<MetricFamilySamples> samples) throws IOException {
Expand All @@ -61,15 +62,22 @@ void write(Writer writer, Enumeration<MetricFamilySamples> samples) throws IOExc

private final MimeType mimeType;

private final MimeType contentType;

TextOutputFormat(String mimeType) {
this.mimeType = MimeTypeUtils.parseMimeType(mimeType);
this.contentType = MimeTypeUtils.parseMimeType(TextFormat.chooseContentType(mimeType));
}

@Override
public MimeType getProducedMimeType() {
return this.mimeType;
}

MimeType getContentType() {
return this.contentType;
}

abstract void write(Writer writer, Enumeration<MetricFamilySamples> samples) throws IOException;

}

0 comments on commit b16f10f

Please sign in to comment.