Skip to content

Commit 5ff986c

Browse files
lhotarimukesh-ctds
authored andcommitted
[improve][monitor] Add version=0.0.4 to /metrics content type for Prometheus 3.x compatibility (apache#24060)
(cherry picked from commit 3f45154) (cherry picked from commit 99dbc3b)
1 parent ad5b14c commit 5ff986c

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

pulsar-broker-common/src/main/java/org/apache/pulsar/broker/stats/prometheus/PrometheusMetricsServlet.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040

4141
public class PrometheusMetricsServlet extends HttpServlet {
4242
public static final String DEFAULT_METRICS_PATH = "/metrics";
43+
// Prometheus uses version 0.0.4 of the text format for metrics.
44+
// This content-type value ensures compatibility with Prometheus 3.x and later versions.
45+
// For details, refer to the Prometheus 3.x migration guide:
46+
// https://prometheus.io/docs/prometheus/latest/migration/#scrape-protocols
47+
public static final String PROMETHEUS_CONTENT_TYPE_004 = "text/plain; version=0.0.4; charset=utf-8";
4348
private static final long serialVersionUID = 1L;
4449
static final int HTTP_STATUS_OK_200 = 200;
4550
static final int HTTP_STATUS_INTERNAL_SERVER_ERROR_500 = 500;
@@ -164,7 +169,7 @@ private void handleAsyncMetricsRequest(AsyncContext context) {
164169

165170
private void generateMetricsSynchronously(HttpServletResponse res) throws IOException {
166171
res.setStatus(HTTP_STATUS_OK_200);
167-
res.setContentType("text/plain;charset=utf-8");
172+
res.setContentType(PROMETHEUS_CONTENT_TYPE_004);
168173
PrometheusMetricsGeneratorUtils.generate(cluster, res.getOutputStream(), metricsProviders);
169174
}
170175

pulsar-broker/src/main/java/org/apache/pulsar/broker/stats/prometheus/PulsarPrometheusMetricsServlet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public void onStartAsync(AsyncEvent event) throws IOException {
145145
response.setStatus(HTTP_STATUS_INTERNAL_SERVER_ERROR_500);
146146
} else {
147147
response.setStatus(HTTP_STATUS_OK_200);
148-
response.setContentType("text/plain;charset=utf-8");
148+
response.setContentType(PROMETHEUS_CONTENT_TYPE_004);
149149
if (compressOutput) {
150150
response.setHeader("Content-Encoding", "gzip");
151151
}

pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
import org.apache.pulsar.broker.service.BrokerServiceException.PersistenceException;
8989
import org.apache.pulsar.broker.service.persistent.PersistentSubscription;
9090
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
91+
import org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet;
9192
import org.apache.pulsar.broker.stats.prometheus.PrometheusRawMetricsProvider;
9293
import org.apache.pulsar.client.admin.BrokerStats;
9394
import org.apache.pulsar.client.admin.PulsarAdminException;
@@ -1472,6 +1473,8 @@ public void testMetricsProvider() throws IOException {
14721473
HttpClient httpClient = HttpClientBuilder.create().build();
14731474
final String metricsEndPoint = getPulsar().getWebServiceAddress() + "/metrics";
14741475
HttpResponse response = httpClient.execute(new HttpGet(metricsEndPoint));
1476+
assertEquals(response.getEntity().getContentType().getValue(),
1477+
PrometheusMetricsServlet.PROMETHEUS_CONTENT_TYPE_004);
14751478
InputStream inputStream = response.getEntity().getContent();
14761479
InputStreamReader isReader = new InputStreamReader(inputStream);
14771480
BufferedReader reader = new BufferedReader(isReader);

pulsar-functions/worker/src/main/java/org/apache/pulsar/functions/worker/rest/api/FunctionsMetricsResource.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.ws.rs.core.MediaType;
3333
import javax.ws.rs.core.Response;
3434
import javax.ws.rs.core.StreamingOutput;
35+
import org.apache.pulsar.broker.stats.prometheus.PrometheusMetricsServlet;
3536
import org.apache.pulsar.common.util.SimpleTextOutputStream;
3637
import org.apache.pulsar.functions.worker.WorkerService;
3738
import org.apache.pulsar.functions.worker.rest.FunctionApiResource;
@@ -63,7 +64,7 @@ public Response getMetrics() throws IOException {
6364
};
6465
return Response
6566
.ok(streamOut)
66-
.type(MediaType.TEXT_PLAIN_TYPE)
67+
.type(PrometheusMetricsServlet.PROMETHEUS_CONTENT_TYPE_004)
6768
.build();
6869
} finally {
6970
buf.release();

0 commit comments

Comments
 (0)