/** * {@inheritDoc} */ @NonNull @Override public MetricSet getMetricSet() { Jenkins jenkins = Jenkins.getInstance(); if (jenkins == null) { throw new AssertionError("Jenkins is missing"); } HealthChecker c = jenkins.getExtensionList(PeriodicWork.class).get(HealthChecker.class); if (c == null) { throw new AssertionError("HealthChecker is missing"); } return metrics( metric(name("jenkins", "health-check", "duration"), c.getHealthCheckDuration()), metric(name("jenkins", "health-check", "count"), c.getHealthCheckCount()), metric(name("jenkins", "health-check", "score"), c.getHealthCheckScore()), metric(name("jenkins", "health-check", "inverse-score"), new DerivativeGauge<Double, Double>(c.getHealthCheckScore()) { @Override protected Double transform(Double value) { return value == null ? null : 1.0 - value; } }) ); }
/** * Get the current health check data. * * @return the current health check data or {@code null} if the health checks have not run yet. */ @CheckForNull public static HealthCheckData getHealthCheckData() { Jenkins jenkins = Jenkins.getInstance(); if (jenkins == null) { LOGGER.warning("Unable to get health check results, client master is not ready (startup or shutdown)"); return null; } HealthChecker healthChecker = jenkins.getExtensionList(PeriodicWork.class).get(HealthChecker.class); if (healthChecker == null) { LOGGER.warning("Unable to get health check results, HealthChecker is not available"); return null; } return healthChecker.getHealthCheckData(); }
/** * {@inheritDoc} */ public void generateResponse(StaplerRequest req, StaplerResponse resp, Object node) throws IOException, ServletException { boolean jsonp = StringUtils.isNotBlank(req.getParameter("callback")); String jsonpCallback = StringUtils.defaultIfBlank(req.getParameter("callback"), "callback"); String prefix = Util.fixEmptyAndTrim(req.getParameter("prefix")); String postfix = Util.fixEmptyAndTrim(req.getParameter("postfix")); resp.setHeader(CACHE_CONTROL, NO_CACHE); resp.setContentType(jsonp ? JSONP_CONTENT_TYPE : JSON_CONTENT_TYPE); resp.setStatus(HttpServletResponse.SC_OK); final OutputStream output = resp.getOutputStream(); try { if (jsonp) { output.write(jsonpCallback.getBytes("US-ASCII")); output.write("(".getBytes("US-ASCII")); } Jenkins jenkins = Jenkins.getInstance(); Sampler sampler = jenkins == null ? null : jenkins.getExtensionList(PeriodicWork.class).get(Sampler.class); Map<Date, Object> sample = sampler == null ? null : (prefix == null && postfix == null ? sampler.sample() : sampler.sample(prefix, postfix)); output.write(getWriter(metricsMapper, req).writeValueAsBytes(sample)); if (jsonp) { output.write(");".getBytes("US-ASCII")); } } finally { output.close(); } }