Skip to content

Commit

Permalink
fix(server): fix Kafka Streams metric values should be returned with …
Browse files Browse the repository at this point in the history
…content-type text/plain (#125)

Resolves: #125
  • Loading branch information
fhussonnois committed Mar 2, 2021
1 parent 3bdaadb commit 36f0bb0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Expand Up @@ -28,6 +28,7 @@
import io.undertow.util.Headers;
import io.undertow.util.StatusCodes;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Deque;
import java.util.Map;
Expand Down Expand Up @@ -126,6 +127,16 @@ public static void sendJsonResponseWithCode(final HttpServerExchange exchange,
exchange.getResponseSender().send(JSON.serialize(response), StandardCharsets.UTF_8);
}

public static void sendTextValue(final HttpServerExchange exchange, final Object value) {
final String s = value instanceof Double ?
new BigDecimal(value.toString()).stripTrailingZeros().toPlainString()
: value.toString();
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange
.setStatusCode(200)
.getResponseSender().send(s, StandardCharsets.UTF_8);
}

private static Optional<String> getFirst(final String name,
final Map<String, Deque<String>> parameters) {
Deque<String> parameter = parameters.get(name);
Expand Down
Expand Up @@ -45,6 +45,7 @@
import static io.streamthoughts.azkarra.http.ExchangeHelper.getOptionalQueryParam;
import static io.streamthoughts.azkarra.http.ExchangeHelper.getQueryParam;
import static io.streamthoughts.azkarra.http.ExchangeHelper.sendJsonResponse;
import static io.streamthoughts.azkarra.http.ExchangeHelper.sendTextValue;
import static io.streamthoughts.azkarra.http.utils.Constants.HTTP_QUERY_PARAM_FILTER_EMPTY;
import static io.streamthoughts.azkarra.http.utils.Constants.HTTP_QUERY_PARAM_FORMAT;
import static io.streamthoughts.azkarra.http.utils.Constants.HTTP_QUERY_PARAM_FORMAT_VALUE_PROMETHEUS;
Expand Down Expand Up @@ -111,9 +112,8 @@ public void handleRequest(final HttpServerExchange exchange) {
throw new MetricNotFoundException("{group=\"" + group.get() + "\"}");
}

boolean extractValue = exchange.getRelativePath().endsWith("/value");
if (name.isPresent() && extractValue) {
sendJsonResponse(exchange, metric.get().value());
if (name.isPresent() && exchange.getRelativePath().endsWith("/value")) {
sendTextValue(exchange, metric.get().value());
} else {
sendJsonResponse(exchange, groupSet);
}
Expand Down

0 comments on commit 36f0bb0

Please sign in to comment.