Skip to content

Commit 6ce3580

Browse files
jiangzhodongjoon-hyun
authored andcommitted
[SPARK-51770] Set Content-Type headers for Prometheus v3
### What changes were proposed in this pull request? This PR sets Content-Type header for Prometheus handler to make it compatible with Prometheus v3 ### Why are the changes needed? Prometheus v3 will fail the scrape if the Content-Type header is not present in response and the fallback scrape protocol is not configured. Therefore we'd configure the headers properly. Please also refer https://prometheus.io/docs/prometheus/latest/migration/#scrape-protocols ### Does this PR introduce any user-facing change? No ### How was this patch tested? Tested via CI and local set up for Prometheus v3, via steps: * Deploy Prometheus v3 in k8s cluster, with default configuration (no fallback_scrape_protocol configured) * Deploy Spark Operator in cluster * Prometheus v3 is able to successfully scrape operator metrics ### Was this patch authored or co-authored using generative AI tooling? No Closes apache#171 from jiangzho/monitoring. Authored-by: Zhou JIANG <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 25aa6cf commit 6ce3580

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

spark-operator/src/main/java/org/apache/spark/k8s/operator/metrics/PrometheusPullModelHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
import java.io.IOException;
2626
import java.util.ArrayList;
27+
import java.util.Collections;
2728
import java.util.List;
29+
import java.util.Map;
2830
import java.util.Properties;
2931

3032
import com.codahale.metrics.MetricRegistry;
@@ -58,7 +60,11 @@ public void stop() {
5860
public void handle(HttpExchange exchange) throws IOException {
5961
HttpServletRequest httpServletRequest = null;
6062
String value = getMetricsSnapshot(httpServletRequest);
61-
sendMessage(exchange, HTTP_OK, String.join("\n", filterNonEmptyRecords(value)));
63+
sendMessage(
64+
exchange,
65+
HTTP_OK,
66+
String.join("\n", filterNonEmptyRecords(value)),
67+
Map.of("Content-Type", Collections.singletonList("text/plain;version=0.0.4")));
6268
}
6369

6470
protected List<String> filterNonEmptyRecords(String metricsSnapshot) {

spark-operator/src/main/java/org/apache/spark/k8s/operator/utils/ProbeUtil.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.io.OutputStream;
2424
import java.nio.charset.StandardCharsets;
2525
import java.util.List;
26+
import java.util.Map;
2627
import java.util.Optional;
2728

29+
import com.sun.net.httpserver.Headers;
2830
import com.sun.net.httpserver.HttpExchange;
2931
import io.javaoperatorsdk.operator.Operator;
3032
import io.javaoperatorsdk.operator.RuntimeInfo;
@@ -46,8 +48,28 @@ private ProbeUtil() {}
4648
*/
4749
public static void sendMessage(HttpExchange httpExchange, int code, String message)
4850
throws IOException {
51+
sendMessage(httpExchange, code, message, null);
52+
}
53+
54+
/**
55+
* Send an HTTP response message with the given response header HTTP status code, message and
56+
* headers.
57+
*
58+
* @param httpExchange The handler for this HTTP response.
59+
* @param code A response header HTTP status code defined in java.net.HttpURLConnection.HTTP_*
60+
* @param message A message to send as a body
61+
* @param headers Headers to be sent with the response
62+
* @throws IOException Failed to send a response.
63+
*/
64+
public static void sendMessage(
65+
HttpExchange httpExchange, int code, String message, Map<String, List<String>> headers)
66+
throws IOException {
4967
try (OutputStream outputStream = httpExchange.getResponseBody()) {
5068
byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
69+
if (headers != null && !headers.isEmpty()) {
70+
Headers responseHeaders = httpExchange.getResponseHeaders();
71+
responseHeaders.putAll(headers);
72+
}
5173
httpExchange.sendResponseHeaders(code, bytes.length);
5274
outputStream.write(bytes);
5375
outputStream.flush();

0 commit comments

Comments
 (0)