/** * Gets the {@link ServletContextHandler} of the metrics servlet. * * @return the {@link ServletContextHandler} object */ public ServletContextHandler getHandler() { ServletContextHandler contextHandler = new ServletContextHandler(); contextHandler.setContextPath(SERVLET_PATH); contextHandler.addServlet(new ServletHolder(new MetricsServlet(mCollectorRegistry)), "/"); return contextHandler; }
@Override public void terminate() { running = false; try { instanceExports.terminate(); if (clientProfiler.isStart()) { clientProfiler.stop(); } profiler().setInstanceProfiler(NOP); if (server != null) { server.stop(); } } catch (Throwable t) { logger.warn("Something happened while terminating.", t); } }
@Override public void initialize() { try { logger.info("Start prometheus HTTPServer on port {}.", port); //TODO 2.Https? server = new HTTPServer(port); } catch (IOException e) { logger.warn("Unable to start prometheus HTTPServer.", e); return; } try { // JVM exports DefaultExports.initialize(); instanceExports.initialize(); if (!clientProfiler.isStart()) { clientProfiler.start(); } profiler().setInstanceProfiler(clientProfiler); } catch (Throwable t) { logger.warn("Unable to initialize server exports.", t); } running = true; }
private void delete() { try { this.pushGateway.delete(this.job, this.groupingKey); } catch (Throwable ex) { logger.error("Unable to delete metrics from Prometheus Pushgateway", ex); } }
/** * Start a HTTP server serving Prometheus metrics from the given registry. */ public HTTPServer(InetSocketAddress addr, CollectorRegistry registry, boolean daemon) throws IOException { server = HttpServer.create(); server.bind(addr, 3); HttpHandler mHandler = new HTTPMetricHandler(registry); server.createContext("/", mHandler); server.createContext("/metrics", mHandler); executorService = Executors.newFixedThreadPool(5, NamedDaemonThreadFactory.defaultThreadFactory(daemon)); server.setExecutor(executorService); start(daemon); }
@Override protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }
/** * Deletes metrics from the Pushgateway. * <p> * Deletes metrics with the provided job and grouping key. * This uses the DELETE HTTP method. */ public void delete(String job, Map<String, String> groupingKey) throws IOException { doRequest(null, job, groupingKey, "DELETE"); }
/** * Construct a Pushgateway, with the given address. * <p> * @param address host:port or ip:port of the Pushgateway. */ public PushGateway(String address) { this(createURLSneakily("http://" + address)); }
public BasicAuthHttpConnectionFactory(HttpConnectionFactory connectionFactory, String user, String password) { this.originConnectionFactory = connectionFactory; this.basicAuthHeader = encode(user, password); }
public BasicAuthHttpConnectionFactory(String user, String password) { this(new DefaultHttpConnectionFactory(), user, password); }
@Override public HttpURLConnection create(String url) throws IOException { HttpURLConnection connection = originConnectionFactory.create(url); connection.setRequestProperty("Authorization", basicAuthHeader); return connection; }
static ThreadFactory defaultThreadFactory(boolean daemon) { return new NamedDaemonThreadFactory(Executors.defaultThreadFactory(), daemon); } }
public void startServer() throws Exception { if (this.bindAddress != null) { this.server = new Server(new InetSocketAddress(InetAddress.getByName(this.bindAddress), port)); } else { this.server = new Server(this.port); } ServletContextHandler handler = new ServletContextHandler(this.server, pathPrefix); if (metricsRegistries != null) { // TODO: there is a way to wire these up automagically via the AdminServlet, but it escapes me right now handler.addServlet(new ServletHolder(new MetricsServlet(metricsRegistries.metricRegistry)), "/metrics"); handler.addServlet(new ServletHolder(new io.prometheus.client.exporter.MetricsServlet()), "/prometheus"); handler.addServlet(new ServletHolder(new HealthCheckServlet(metricsRegistries.healthCheckRegistry)), "/healthcheck"); handler.addServlet(new ServletHolder(new PingServlet()), "/ping"); } if (diagnosticContext != null) { handler.addServlet(new ServletHolder(new DiagnosticHealthCheck(diagnosticContext)), "/diagnostic"); } this.server.start(); this.server.join(); }
public PrometheusQmqMetricRegistry() { DynamicConfig config = DynamicConfigLoader.load("qmq.prometheus.properties", false); String type = config.getString("monitor.type", "prometheus"); if ("prometheus".equals(type)) { String action = config.getString("monitor.action", "pull"); if ("pull".equals(action)) { try { HTTPServer server = new HTTPServer(config.getInt("monitor.port", 3333)); } catch (IOException e) { e.printStackTrace(); } } } else if ("graphite".equals(type)) { String host = config.getString("graphite.host"); int port = config.getInt("graphite.port"); Graphite graphite = new Graphite(host, port); graphite.start(CollectorRegistry.defaultRegistry, 60); } }
@Override public void destroy() { if (httpServer == null) { return; } httpServer.stop(); httpServer = null; LOGGER.info("Prometheus httpServer stopped."); } }
/** * Pushes all metrics in a registry, replacing only previously pushed metrics of the same name, job and grouping key. * <p> * This uses the POST HTTP method. */ public void pushAdd(CollectorRegistry registry, String job, Map<String, String> groupingKey) throws IOException { doRequest(registry, job, groupingKey, "POST"); }
/** * Deletes metrics from the Pushgateway. * <p> * Deletes metrics with no grouping key and the provided job. * This uses the DELETE HTTP method. */ public void delete(String job) throws IOException { doRequest(null, job, null, "DELETE"); }
/** * Pushes all metrics in a registry, replacing only previously pushed metrics of the same name and job and no grouping key. * <p> * This uses the POST HTTP method. */ public void pushAdd(CollectorRegistry registry, String job) throws IOException { doRequest(registry, job, null, "POST"); }
/** * Pushes all metrics in a registry, replacing all those with the same job and no grouping key. * <p> * This uses the PUT HTTP method. */ public void push(CollectorRegistry registry, String job) throws IOException { doRequest(registry, job, null, "PUT"); }
/** * Pushes all metrics in a registry, replacing all those with the same job and grouping key. * <p> * This uses the PUT HTTP method. */ public void push(CollectorRegistry registry, String job, Map<String, String> groupingKey) throws IOException { doRequest(registry, job, groupingKey, "PUT"); }