001package io.prometheus.metrics.exporter.httpserver;
002
003import com.sun.net.httpserver.HttpExchange;
004import com.sun.net.httpserver.HttpHandler;
005
006import java.io.IOException;
007import java.nio.charset.StandardCharsets;
008
009/**
010 * Handler for the / endpoint
011 */
012public class DefaultHandler implements HttpHandler {
013
014    private final byte[] responseBytes;
015    private final String contentType;
016
017    public DefaultHandler() {
018        String responseString = "" +
019                "<html>\n" +
020                "<head><title>Prometheus Java Client</title></head>\n" +
021                "<body>\n" +
022                "<h1>Prometheus Java Client</h1>\n" +
023                "<h2>Metrics Path</h2>\n" +
024                "The metrics path is <a href=\"/metrics\">/metrics</a>.\n" +
025                "<h2>Name Filter</h2>\n" +
026                "If you want to scrape only specific metrics, use the <tt>name[]</tt> parameter like this:\n" +
027                "<ul>\n" +
028                "<li><a href=\"/metrics?name[]=my_metric\">/metrics?name[]=my_metric</a></li>\n" +
029                "</ul>\n" +
030                "You can also use multiple <tt>name[]</tt> parameters to query multiple metrics:\n" +
031                "<ul>\n" +
032                "<li><a href=\"/metrics?name[]=my_metric_a&name=my_metrics_b\">/metrics?name[]=my_metric_a&amp;name=[]=my_metric_b</a></li>\n" +
033                "</ul>\n" +
034                "The <tt>name[]</tt> parameter can be used by the Prometheus server for scraping. Add the following snippet to your scrape job configuration in <tt>prometheus.yaml</tt>:\n" +
035                "<pre>\n" +
036                "params:\n" +
037                "    name[]:\n" +
038                "        - my_metric_a\n" +
039                "        - my_metric_b\n" +
040                "</pre>\n" +
041                "<h2>Debug Parameter</h2>\n" +
042                "The Prometheus Java metrics library supports multiple exposition formats.\n" +
043                "The Prometheus server sends the <tt>Accept</tt> header to indicate which format it accepts.\n" +
044                "By default, the Prometheus server accepts OpenMetrics text format, unless the Prometheus server is started with feature flag <tt>--enable-feature=native-histograms</tt>,\n" +
045                "in which case the default is Prometheus protobuf.\n" +
046                "The Prometheus Java metrics library supports a <tt>debug</tt> query parameter for viewing the different formats in a Web browser:\n" +
047                "<ul>\n" +
048                "<li><a href=\"/metrics?debug=openmetrics\">/metrics?debug=openmetrics</a>: View OpenMetrics text format.</li>\n" +
049                "<li><a href=\"/metrics?debug=text\">/metrics?debug=text</a>: View Prometheus text format (this is the default when accessing the <a href=\"/metrics\">/metrics</a> endpoint with a Web browser).</li>\n" +
050                "<li><a href=\"/metrics?debug=prometheus-protobuf\">/metrics?debug=prometheus-protobuf</a>: View a text representation of the Prometheus protobuf format.</li>\n" +
051                "</ul>\n" +
052                "Note that the <tt>debug</tt> 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 <tt>Accept</tt> header for indicating which format it accepts.\n" +
053                "</body>\n" +
054                "</html>\n";
055        this.responseBytes = responseString.getBytes(StandardCharsets.UTF_8);
056        this.contentType = "text/html; charset=utf-8";
057    }
058
059    @Override
060    public void handle(HttpExchange exchange) throws IOException {
061        exchange.getResponseHeaders().set("Content-Type", contentType);
062        exchange.getResponseHeaders().set("Content-Length", Integer.toString(responseBytes.length));
063        exchange.sendResponseHeaders(200, responseBytes.length);
064        exchange.getResponseBody().write(responseBytes);
065        exchange.close();
066    }
067}