001package io.prometheus.metrics.exporter.servlet.jakarta;
002
003import io.prometheus.metrics.annotations.StableApi;
004import io.prometheus.metrics.config.PrometheusProperties;
005import io.prometheus.metrics.exporter.common.PrometheusScrapeHandler;
006import io.prometheus.metrics.model.registry.PrometheusRegistry;
007import jakarta.servlet.http.HttpServlet;
008import jakarta.servlet.http.HttpServletRequest;
009import jakarta.servlet.http.HttpServletResponse;
010import java.io.IOException;
011
012/**
013 * Initial example exporter so that we can try the new metrics library out.
014 *
015 * <p>We'll add a Jakarta servlet, the built-in HTTPServer, etc. soon, and likely move common code
016 * into a common module.
017 */
018@StableApi
019public class PrometheusMetricsServlet extends HttpServlet {
020
021  private static final long serialVersionUID = 0L;
022
023  private final PrometheusScrapeHandler handler;
024
025  public PrometheusMetricsServlet() {
026    this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry);
027  }
028
029  public PrometheusMetricsServlet(PrometheusRegistry registry) {
030    this(PrometheusProperties.get(), registry);
031  }
032
033  public PrometheusMetricsServlet(PrometheusProperties config) {
034    this(config, PrometheusRegistry.defaultRegistry);
035  }
036
037  public PrometheusMetricsServlet(PrometheusProperties config, PrometheusRegistry registry) {
038    this.handler = new PrometheusScrapeHandler(config, registry);
039  }
040
041  @Override
042  protected void doGet(HttpServletRequest request, HttpServletResponse response)
043      throws IOException {
044    handler.handleRequest(new HttpExchangeAdapter(request, response));
045  }
046}