@Override public void processGauge(MetricName name, Gauge<?> gauge, Context context) throws Exception { final JsonGenerator json = context.json; json.writeStartObject(); { json.writeStringField("type", "gauge"); json.writeObjectField("value", evaluateGauge(gauge)); } json.writeEndObject(); }
@Override public void processTimer(MetricName name, Timer timer, Context context) throws Exception { final JsonGenerator json = context.json; json.writeStartObject(); { json.writeStringField("type", "timer"); json.writeFieldName("duration"); json.writeStartObject(); { json.writeStringField("unit", timer.durationUnit().toString().toLowerCase()); writeSummarizable(timer, json); writeSampling(timer, json); if (context.showFullSamples) { json.writeObjectField("values", timer.getSnapshot().getValues()); } } json.writeEndObject(); json.writeFieldName("rate"); json.writeStartObject(); { writeMeteredFields(timer, json); } json.writeEndObject(); } json.writeEndObject(); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { final String classPrefix = req.getParameter("class"); final boolean pretty = Boolean.parseBoolean(req.getParameter("pretty")); final boolean showFullSamples = Boolean.parseBoolean(req.getParameter("full-samples")); resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(CONTENT_TYPE); final OutputStream output = resp.getOutputStream(); final JsonGenerator json = factory.createJsonGenerator(output, JsonEncoding.UTF8); if (pretty) { json.useDefaultPrettyPrinter(); } json.writeStartObject(); { if (showJvmMetrics && ("jvm".equals(classPrefix) || classPrefix == null)) { writeVmMetrics(json); } writeRegularMetrics(json, classPrefix, showFullSamples); } json.writeEndObject(); json.close(); }
@Override public void processHistogram(MetricName name, Histogram histogram, Context context) throws Exception { final JsonGenerator json = context.json; json.writeStartObject(); { json.writeStringField("type", "histogram"); json.writeNumberField("count", histogram.count()); writeSummarizable(histogram, json); writeSampling(histogram, json); if (context.showFullSamples) { json.writeObjectField("values", histogram.getSnapshot().getValues()); } } json.writeEndObject(); }
/** * Method: init * Purpose: Initializes the embedded Jetty Server with including the metrics servlets. */ private void init() { LOG.info("Initializing Kafka Http Metrics Reporter"); // creating the socket address for binding to the specified address and port InetSocketAddress inetSocketAddress = new InetSocketAddress(bindAddress, port); // create new Jetty server server = new Server(inetSocketAddress); // creating the servlet context handler ServletContextHandler servletContextHandler = new ServletContextHandler(); // setting the context path servletContextHandler.setContextPath("/"); // adding the codahale metrics servlet to the servlet context servletContextHandler.addServlet(new ServletHolder(new AdminServlet()), "/api"); servletContextHandler.addServlet(new ServletHolder(new MetricsServlet()), "/api/metrics"); servletContextHandler.addServlet(new ServletHolder(new ThreadDumpServlet()), "/api/threads"); servletContextHandler.addServlet(new ServletHolder(new HealthCheckServlet()), "/api/healthcheck"); servletContextHandler.addServlet(new ServletHolder(new PingServlet()), "/api/ping"); // adding the configured servlet context handler to the Jetty Server server.setHandler(servletContextHandler); LOG.info("Finished initializing Kafka Http Metrics Reporter"); }
@Override public void processMeter(MetricName name, Metered meter, Context context) throws Exception { final JsonGenerator json = context.json; json.writeStartObject(); { json.writeStringField("type", "meter"); json.writeStringField("event_type", meter.eventType()); writeMeteredFields(meter, json); } json.writeEndObject(); }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store"); final String uri = req.getPathInfo(); final String path = this.contextPath + req.getServletPath(); if (uri == null || uri.equals("/")) { resp.setStatus(HttpServletResponse.SC_OK); resp.setContentType(CONTENT_TYPE); final PrintWriter writer = resp.getWriter(); try { writer.println(MessageFormat.format(TEMPLATE, path, metricsUri, path, pingUri, path, threadsUri, path, healthcheckUri)); } finally { writer.close(); } } 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 init(ServletConfig config) throws ServletException { super.init(config); healthCheckServlet.init(config); metricsServlet.init(config); pingServlet.init(config); threadDumpServlet.init(config); final ServletContext context = config.getServletContext(); this.contextPath = context.getContextPath(); this.metricsUri = getParam(config.getInitParameter("metrics-uri"), this.metricsUri); this.pingUri = getParam(config.getInitParameter("ping-uri"), this.pingUri); this.threadsUri = getParam(config.getInitParameter("threads-uri"), this.threadsUri); this.healthcheckUri = getParam(config.getInitParameter("healthcheck-uri"), this.healthcheckUri); }
public AdminServlet(HealthCheckServlet healthCheckServlet, MetricsServlet metricsServlet, PingServlet pingServlet, ThreadDumpServlet threadDumpServlet, String healthcheckUri, String metricsUri, String pingUri, String threadsUri) { this.healthCheckServlet = healthCheckServlet; this.metricsServlet = metricsServlet; this.pingServlet = pingServlet; this.threadDumpServlet = threadDumpServlet; this.metricsUri = metricsUri; this.pingUri = pingUri; this.threadsUri = threadsUri; this.healthcheckUri = healthcheckUri; }
@Override public AdminServlet get() { for (HealthCheck healthCheck : healthChecks) { healthCheckRegistry.register(healthCheck); } final JsonFactory factory = jsonFactory == null ? new JsonFactory(new ObjectMapper()) : jsonFactory; return new AdminServlet(new HealthCheckServlet(healthCheckRegistry), new MetricsServlet(Clock.defaultClock(), VirtualMachineMetrics.getInstance(), metricsRegistry, factory, true), new PingServlet(), new ThreadDumpServlet(VirtualMachineMetrics.getInstance()), healthcheckUri, metricsUri, pingUri, threadsUri); } }