001package io.prometheus.metrics.exporter.httpserver;
002
003import com.sun.net.httpserver.HttpExchange;
004import com.sun.net.httpserver.HttpHandler;
005import java.io.IOException;
006import java.nio.charset.StandardCharsets;
007
008/** Handler for the /-/healthy endpoint */
009public class HealthyHandler implements HttpHandler {
010
011  private final byte[] responseBytes;
012  private final String contentType;
013
014  public HealthyHandler() {
015    String responseString = "Exporter is healthy.\n";
016    this.responseBytes = responseString.getBytes(StandardCharsets.UTF_8);
017    this.contentType = "text/plain; charset=utf-8";
018  }
019
020  @Override
021  public void handle(HttpExchange exchange) throws IOException {
022    try {
023      exchange.getResponseHeaders().set("Content-Type", contentType);
024      exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length));
025      exchange.sendResponseHeaders(200, responseBytes.length);
026      exchange.getResponseBody().write(responseBytes);
027    } finally {
028      exchange.close();
029    }
030  }
031}