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 void start() throws Exception { ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); context.setContextPath("/"); jettyServer.setHandler(context); context.addEventListener(new HealthCheckServletContextListener(healthCheckRegistry)); context.addEventListener(new MetricsServletContextListener(metricRegistry)); context.addServlet(new ServletHolder(new AdminServlet()), "/*"); jettyServer.start(); }
@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); }
@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); }
@Bean public MetricsServlet metricsServlet(MetricRegistry metricRegistry) { return new MetricsServlet(metricRegistry); }
@Override public void contextInitialized(ServletContextEvent event) { final ServletContext context = event.getServletContext(); context.setAttribute(METRICS_REGISTRY, getMetricRegistry()); context.setAttribute(RATE_UNIT, getRateUnit()); context.setAttribute(DURATION_UNIT, getDurationUnit()); context.setAttribute(ALLOWED_ORIGIN, getAllowedOrigin()); }
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final String uri = req.getPathInfo(); if (uri == null || uri.equals("/")) { super.service(req, resp); } else if (uri.equals(healthcheckUri)) { healthCheckServlet.service(req, resp); } else if (uri.startsWith(metricsUri)) { metricsServlet.service(req, resp); } else if (uri.equals(pingUri)) { pingServlet.service(req, resp); } else if (uri.equals(threadsUri)) { threadDumpServlet.service(req, resp); } else { resp.sendError(HttpServletResponse.SC_NOT_FOUND); } }
@Override public void contextInitialized(ServletContextEvent servletContextEvent) { final ServletContext context = servletContextEvent.getServletContext(); context.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, getHealthCheckRegistry()); context.setAttribute(HealthCheckServlet.HEALTH_CHECK_EXECUTOR, getExecutorService()); context.setAttribute(MetricsServlet.METRICS_REGISTRY, getMetricRegistry()); }
@Override public void contextInitialized(ServletContextEvent event) { final ServletContext context = event.getServletContext(); context.setAttribute(HEALTH_CHECK_REGISTRY, getHealthCheckRegistry()); context.setAttribute(HEALTH_CHECK_EXECUTOR, getExecutorService()); }
@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='threads.txt'"); } super.doGet(req, resp); } }
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { if (! tokenValidator.validateBearerAccessToken(req, resp)) { return; // invalid or missing token, or web API disabled } super.doGet(req, resp); }
@Override protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { if (! tokenValidator.validateBearerAccessToken(req, resp)) { return; // invalid or missing token, or web API disabled } super.service(req, resp); } }
@Override public void init(final ServletConfig config) throws ServletException { super.init(config); tokenValidator = createAccessTokenValidator(config); LogManager.getLogger("MAIN").info("[CM7110] Loaded monitor API servlet"); }
protected Handler createAdminServlet(Server server, MutableServletContextHandler handler, MetricRegistry metrics, HealthCheckRegistry healthChecks) { configureSessionsAndSecurity(handler, server); handler.setServer(server); handler.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metrics); handler.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthChecks); handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*"); final String allowedMethodsParam = allowedMethods.stream() .collect(Collectors.joining(",")); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, allowedMethodsParam); return handler; }
@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; } }
protected Handler createAdminServlet(Server server, MutableServletContextHandler handler, MetricRegistry metrics, HealthCheckRegistry healthChecks) { configureSessionsAndSecurity(handler, server); handler.setServer(server); handler.getServletContext().setAttribute(MetricsServlet.METRICS_REGISTRY, metrics); handler.getServletContext().setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY, healthChecks); handler.addServlet(new NonblockingServletHolder(new AdminServlet()), "/*"); handler.addFilter(AllowedMethodsFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST)) .setInitParameter(AllowedMethodsFilter.ALLOWED_METHODS_PARAM, Joiner.on(',').join(allowedMethods)); return handler; }
@Bean public ServletRegistrationBean servletRegistrationBean(){ return new ServletRegistrationBean(new AdminServlet(),"/metrics/admin/*"); }
@Bean public ServletRegistrationBean<AdminServlet> getMetricsServlet() { return new ServletRegistrationBean<>(new AdminServlet(), path + "/*"); }
@Bean public ServletRegistrationBean axisServletRegistrationBean() { ServletRegistrationBean registration = new ServletRegistrationBean(new AxisServlet(), "/services/*"); registration.addUrlMappings("*.jws"); return registration; } @Bean public ServletRegistrationBean adminServletRegistrationBean() { return new ServletRegistrationBean(new AdminServlet(), "/servlet/AdminServlet"); }
public class YourApplication extends Application<YourConfiguration> { // ... @Override public void run(YourConfiguration configuration, Environment environment) throws Exception { environment.jersey().register(new MyResource()); environment.getApplicationContext().setAttribute( MetricsServlet.METRICS_REGISTRY, environment.metrics()); environment.getApplicationContext().setAttribute( HealthCheckServlet.HEALTH_CHECK_REGISTRY, environment.healthChecks()); environment.getApplicationContext().addServlet( new NonblockingServletHolder(new AdminServlet()), "/admin/*"); } }