/** * Returns a healthy {@link Result} with a formatted message. * * Message formatting follows the same rules as * {@link String#format(String, Object...)}. * * @param message a message format * @param args the arguments apply to the message format * @return a healthy {@link Result} with an additional message * @see String#format(String, Object...) */ public static Result healthy(String message, Object... args) { return healthy(String.format(message, args)); }
if (result.isHealthy()) { if (result.getMessage() != null) { writer.format("* %s: OK\n %s\n", entry.getKey(), result.getMessage()); } else { writer.format("* %s: OK\n", entry.getKey()); if (result.getMessage() != null) { writer.format("! %s: ERROR\n! %s\n", entry.getKey(), result.getMessage()); final Throwable error = result.getError(); if (error != null) { writer.println();
/** * Returns an unhealthy {@link Result} with a formatted message. * * Message formatting follows the same rules as * {@link String#format(String, Object...)}. * * @param message a message format * @param args the arguments apply to the message format * @return an unhealthy {@link Result} with an additional message * @see String#format(String, Object...) */ public static Result unhealthy(String message, Object... args) { return unhealthy(String.format(message, args)); }
@Override public Result check() { Handle handle = null; try { handle = dbi.open(); final int test = handle.createQuery("select 1 as test").map(IntegerMapper.FIRST).first(); if (test == 1) { return Result.healthy(); } else { return Result.unhealthy(String.format("%s != 1", test)); } } catch (Throwable t) { return Result.unhealthy(t.toString()); } finally { if (handle != null) { handle.close(); } } }
/** * Executes the health check, catching and handling any exceptions raised by {@link #check()}. * * @return if the component is healthy, a healthy {@link Result}; otherwise, an unhealthy * {@link Result} with a descriptive error message or exception */ public Result execute() { try { return check(); } catch (Error e) { throw e; } catch (Throwable e) { return Result.unhealthy(e); } } }
@Override protected Result check() throws Exception { return Result.healthy(); } }
@Override protected Result check() throws Exception { mongo.getDatabaseNames(); return Result.healthy("MongoDB is running... :)"); }
@Managed public boolean isHealthy() { return check().isHealthy(); } }
@Override protected Result check() throws Exception { // The service is always healthy, right? return Result.healthy(); } }
private static boolean isAllHealthy(Map<String, HealthCheck.Result> results) { for (HealthCheck.Result result : results.values()) { if (!result.isHealthy()) { return false; } } return true; } }
@Override protected Result check() throws Exception { return Result.healthy(); } }
@Override protected Result check() throws Exception { verifyConnection(); return Result.healthy(); }
@Override protected Result check() throws Exception { return Result.healthy(); } }
@Override protected Result check() throws Exception { final Set<String> threads = vm.deadlockedThreads(); if (threads.isEmpty()) { return Result.healthy(); } final StringBuilder builder = new StringBuilder("Deadlocked threads detected:\n"); for (String thread : threads) { builder.append(thread).append('\n'); } return Result.unhealthy(builder.toString()); } }
/** * Returns an unhealthy {@link Result} with the given error. * * @param error an exception thrown during the health check * @return an unhealthy {@link Result} with the given error */ public static Result unhealthy(Throwable error) { return new Result(false, error.getMessage(), error); }
@Test public void successfulCallReturnsHealthy() throws Exception { when(facade.getOrg("orgName")).thenReturn(new GHOrg("orgName", "http://a.b")); assertThat(healthCheck.check(), equalTo(HealthCheck.Result.healthy())); }
/** * Returns a healthy {@link Result} with an additional message. * * @param message an informative message * @return a healthy {@link Result} with an additional message */ public static Result healthy(String message) { return new Result(true, message, null); }
/** * Returns an unhealthy {@link Result} with the given message. * * @param message an informative message describing how the health check failed * @return an unhealthy {@link Result} with the given message */ public static Result unhealthy(String message) { return new Result(false, message, null); }