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