io.prometheus.metrics.exporter.httpserver.DefaultHandler Maven / Gradle / Ivy
package io.prometheus.metrics.exporter.httpserver;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* Handler for the / endpoint
*/
public class DefaultHandler implements HttpHandler {
private final byte[] responseBytes;
private final String contentType;
public DefaultHandler() {
String responseString = "" +
"\n" +
"Prometheus Java Client \n" +
"\n" +
"Prometheus Java Client
\n" +
"Metrics Path
\n" +
"The metrics path is /metrics.\n" +
"Name Filter
\n" +
"If you want to scrape only specific metrics, use the name[] parameter like this:\n" +
"\n" +
"- /metrics?name[]=my_metric
\n" +
"
\n" +
"You can also use multiple name[] parameters to query multiple metrics:\n" +
"\n" +
"- /metrics?name[]=my_metric_a&name=[]=my_metric_b
\n" +
"
\n" +
"The name[] parameter can be used by the Prometheus server for scraping. Add the following snippet to your scrape job configuration in prometheus.yaml:\n" +
"\n" +
"params:\n" +
" name[]:\n" +
" - my_metric_a\n" +
" - my_metric_b\n" +
"
\n" +
"Debug Parameter
\n" +
"The Prometheus Java metrics library supports multiple exposition formats.\n" +
"The Prometheus server sends the Accept header to indicate which format it accepts.\n" +
"By default, the Prometheus server accepts OpenMetrics text format, unless the Prometheus server is started with feature flag --enable-feature=native-histograms,\n" +
"in which case the default is Prometheus protobuf.\n" +
"The Prometheus Java metrics library supports a debug query parameter for viewing the different formats in a Web browser:\n" +
"\n" +
"- /metrics?debug=openmetrics: View OpenMetrics text format.
\n" +
"- /metrics?debug=text: View Prometheus text format (this is the default when accessing the /metrics endpoint with a Web browser).
\n" +
"- /metrics?debug=prometheus-protobuf: View a text representation of the Prometheus protobuf format.
\n" +
"
\n" +
"Note that the debug parameter is only for viewing different formats in a Web browser, it should not be used by the Prometheus server for scraping. The Prometheus server uses the Accept header for indicating which format it accepts.\n" +
"\n" +
"\n";
this.responseBytes = responseString.getBytes(StandardCharsets.UTF_8);
this.contentType = "text/html; charset=utf-8";
}
@Override
public void handle(HttpExchange exchange) throws IOException {
exchange.getResponseHeaders().set("Content-Type", contentType);
exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length));
exchange.sendResponseHeaders(200, responseBytes.length);
exchange.getResponseBody().write(responseBytes);
exchange.close();
}
}