import com.codahale.metrics.MetricRegistry;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import io.prometheus.client.exporter.MetricsServlet;
import io.prometheus.client.hotspot.DefaultExports;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;web
public class PrometheusReporter {服務器
//建立Server,加載Connectors,加載handlers,加載Servlets等,啓動服務start,最後加入服務器join。
//webappcontext 繼承於servletContextHandler,servletContextHandler繼承於contexthandler
//server 用於存儲handler,handler分不少種,此處是servlet 運行在servletcontext 中,servlet 的工做是由handler來指揮和安排,handler是遵從於外界的web 請求app
//當外界web 請求過來時,首先接到這個請求的是handler,contexthandler 會根據請求的path找到and交由內部對應的servlet 來處理eclipse
//serlvet的存在和建立(即須要如下步驟完成的狀況下才可能存在):包括建立server ,設置server端口,建立context ,設置contextpath,設置contexthandlerwebapp
//添加servlet :需serlvet的存在和建立下能夠操做的url
public static void start( MetricRegistry registry ) throws Exception {視頻
// Hook the Dropwizard registry into the Prometheus registry
// via the DropwizardExports collector.server
//啓動汽車前,先把汽車的監控鏈接到對的監控視屏上,方便監控視頻能夠採集到汽車的數據
CollectorRegistry.defaultRegistry.register(new DropwizardExports(registry));繼承
// Expose Prometheus metrics.
Server server = new Server(9404);//將本身的端口設置爲9094
ServletContextHandler context = new ServletContextHandler();//聲明一個context
context.setContextPath("/");////設置contextPath
server.setHandler(context);//設置handler
context.addServlet(new ServletHolder(new MetricsServlet()), "/metrics");//指定servlet和servlet匹配的url
//context.addServlet(new ServletHolder(new MetricsServlet()), "/hhh");
DefaultExports.initialize();
// Start the webserver.
server.start();
server.join();
}
}ip