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(); }
@Override public void init(ServletConfig config) throws ServletException { super.init(config); this.healthCheckServlet = new HealthCheckServlet(); healthCheckServlet.init(config); this.metricsServlet = new MetricsServlet(); metricsServlet.init(config); this.pingServlet = new PingServlet(); pingServlet.init(config); this.threadDumpServlet = new ThreadDumpServlet(); threadDumpServlet.init(config); this.metricsUri = getParam(config.getInitParameter("metrics-uri"), DEFAULT_METRICS_URI); this.pingUri = getParam(config.getInitParameter("ping-uri"), DEFAULT_PING_URI); this.threadsUri = getParam(config.getInitParameter("threads-uri"), DEFAULT_THREADS_URI); this.healthcheckUri = getParam(config.getInitParameter("healthcheck-uri"), DEFAULT_HEALTHCHECK_URI); this.serviceName = getParam(config.getInitParameter("service-name"), null); }
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { boolean download = Boolean.parseBoolean(req.getParameter("download")); if (download) { resp.addHeader(CONTENT_DISPOSITION, "attachment; filename='metrics.json'"); } super.doGet(req, resp); } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType(CONTENT_TYPE); if (allowedOrigin != null) { resp.setHeader("Access-Control-Allow-Origin", allowedOrigin); } resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store"); resp.setStatus(HttpServletResponse.SC_OK); final OutputStream output = resp.getOutputStream(); try { getWriter(req).writeValue(output, registry); } finally { output.close(); } }
@Override public void init(ServletConfig config) throws ServletException { super.init(config); final ServletContext context = config.getServletContext(); if (null == registry) { final Object registryAttr = context.getAttribute(METRICS_REGISTRY); if (registryAttr instanceof MetricRegistry) { this.registry = (MetricRegistry) registryAttr; } else { throw new ServletException("Couldn't find a MetricRegistry instance."); } } final TimeUnit rateUnit = parseTimeUnit(context.getInitParameter(RATE_UNIT), TimeUnit.SECONDS); final TimeUnit durationUnit = parseTimeUnit(context.getInitParameter(DURATION_UNIT), TimeUnit.SECONDS); final boolean showSamples = Boolean.parseBoolean(context.getInitParameter(SHOW_SAMPLES)); this.mapper = new ObjectMapper().registerModule(new MetricsModule(rateUnit, durationUnit, showSamples)); this.allowedOrigin = config.getInitParameter(ALLOWED_ORIGIN); }
@Bean public MetricsServlet metricsServlet(MetricRegistry metricRegistry) { return new MetricsServlet(metricRegistry); }
@Configuration public class MetricsConfiguration { @Bean public ServletRegistrationBean servletRegistrationBean() { DefaultExports.initialize(); return new ServletRegistrationBean(new MetricsServlet(), "/prometheus"); } @Bean public SpringBootMetricsCollector springBootMetricsCollector(Collection<PublicMetrics> publicMetrics) { SpringBootMetricsCollector springBootMetricsCollector = new SpringBootMetricsCollector( publicMetrics); springBootMetricsCollector.register(); return springBootMetricsCollector; } }
/** * Appends endpoint servlets to the provided {@code context} which will serve codahale-style and prometheus-style * metrics. */ public static void configureMetricsEndpoints( ServletContextHandler context, String codahaleMetricsEndpoint, String prometheusEndpoint) { // Metrics ServletHolder codahaleMetricsServlet = new ServletHolder("default", new com.codahale.metrics.servlets.MetricsServlet(METRICS)); context.addServlet(codahaleMetricsServlet, codahaleMetricsEndpoint); // Prometheus CollectorRegistry collectorRegistry = new CollectorRegistry(); collectorRegistry.register(new DropwizardExports(METRICS)); ServletHolder prometheusServlet = new ServletHolder("prometheus", new io.prometheus.client.exporter.MetricsServlet(collectorRegistry)); context.addServlet(prometheusServlet, prometheusEndpoint); }
@Activate protected void activate() throws NamespaceException, ServletException { httpService.registerServlet(METRICS_URI, new MetricsServlet(metricRegistry), null, null); httpService.registerServlet(HEALTH_URI, new HealthCheckServlet(healthCheckRegistry), null, null); httpService.registerServlet(PING_URI, new PingServlet(), null, null); httpService.registerServlet(THREADS_URI, new ThreadDumpServlet(), null, null); }
public Map<String, HttpServlet> getServletMappings() { final MetricsServlet metricsServlet = new MetricsServlet(serverConfiguration.getMetricRegistry()); final HealthCheckServlet healthCheckServlet = new HealthCheckServlet(serverConfiguration.getHealthCheckRegistry()); final GearmanServlet gearmanServlet = new GearmanServlet(serverConfiguration.getJobQueueMonitor(), serverConfiguration.getJobManager(), serverConfiguration.getQueueMetrics()); final DashboardServlet dashboardServlet = new DashboardServlet(serverConfiguration.getJobQueueMonitor(), serverConfiguration.getQueueMetrics()); final JobQueueServlet jobQueueServlet = new JobQueueServlet(serverConfiguration.getJobManager()); final ClusterServlet clusterServlet = new ClusterServlet(serverConfiguration); final ExceptionsServlet exceptionsServlet = new ExceptionsServlet(serverConfiguration.getExceptionStorageEngine()); Map<String, HttpServlet> mappings = new HashMap<>(); mappings.put("/gearman/*", gearmanServlet); mappings.put("/queues/*", jobQueueServlet); mappings.put("/metrics/*", metricsServlet); mappings.put("/health/*", healthCheckServlet); mappings.put("/cluster/*", clusterServlet); mappings.put("/exceptions/*", exceptionsServlet); mappings.put("/", dashboardServlet); return mappings; }
webAppApi.getMetricsAndMonitoring(); serve(SYSTEM_HEALTHCHECK).with(new HealthCheckServlet(monitoring.getHealth())); serve(SYSTEM_METRICS).with(new MetricsServlet(monitoring.getMetrics())); serve(SYSTEM_PING).with(new PingServlet()); serve(SYSTEM_THREADS).with(new ThreadDumpServlet());
servletContextHandler.addServlet(servletHolder, "/*"); servletContextHandler.addServlet(new ServletHolder(new MetricsServlet(metrics)), STATUS_METRICS_PATH); servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), STATUS_THREADS_PATH);
webAppApi.getMetricsAndMonitoring(); serve(SYSTEM_HEALTHCHECK).with(new HealthCheckServlet(monitoring.getHealth())); serve(SYSTEM_METRICS).with(new MetricsServlet(monitoring.getMetrics())); serve(SYSTEM_PING).with(new PingServlet()); serve(SYSTEM_THREADS).with(new ThreadDumpServlet());