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