Skip to content
Closed
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 @@ -24,7 +24,9 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.codahale.metrics.MetricRegistry;
Expand Down Expand Up @@ -58,7 +60,11 @@ public void stop() {
public void handle(HttpExchange exchange) throws IOException {
HttpServletRequest httpServletRequest = null;
String value = getMetricsSnapshot(httpServletRequest);
sendMessage(exchange, HTTP_OK, String.join("\n", filterNonEmptyRecords(value)));
sendMessage(
exchange,
HTTP_OK,
String.join("\n", filterNonEmptyRecords(value)),
Map.of("Content-Type", Collections.singletonList("text/plain;version=0.0.4")));
}

protected List<String> filterNonEmptyRecords(String metricsSnapshot) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import io.javaoperatorsdk.operator.Operator;
import io.javaoperatorsdk.operator.RuntimeInfo;
Expand All @@ -46,8 +48,28 @@ private ProbeUtil() {}
*/
public static void sendMessage(HttpExchange httpExchange, int code, String message)
throws IOException {
sendMessage(httpExchange, code, message, null);
}

/**
* Send an HTTP response message with the given response header HTTP status code, message and
* headers.
*
* @param httpExchange The handler for this HTTP response.
* @param code A response header HTTP status code defined in java.net.HttpURLConnection.HTTP_*
* @param message A message to send as a body
* @param headers Headers to be sent with the response
* @throws IOException Failed to send a response.
*/
public static void sendMessage(
HttpExchange httpExchange, int code, String message, Map<String, List<String>> headers)
throws IOException {
try (OutputStream outputStream = httpExchange.getResponseBody()) {
byte[] bytes = message.getBytes(StandardCharsets.UTF_8);
if (headers != null && !headers.isEmpty()) {
Headers responseHeaders = httpExchange.getResponseHeaders();
responseHeaders.putAll(headers);
}
httpExchange.sendResponseHeaders(code, bytes.length);
outputStream.write(bytes);
outputStream.flush();
Expand Down
Loading