Java Code Examples for org.eclipse.microprofile.health.HealthCheckResponseBuilder#withData()

The following examples show how to use org.eclipse.microprofile.health.HealthCheckResponseBuilder#withData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DBHealthCheck.java    From boost with Eclipse Public License 1.0 7 votes vote down vote up
@Override
public HealthCheckResponse call() {

    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("membership");
    try {
        Connection connection = datasource.getConnection();
        boolean isValid = connection.isValid(5);

        DatabaseMetaData metaData = connection.getMetaData();

        responseBuilder = responseBuilder
                    .withData("databaseProductName", metaData.getDatabaseProductName())
                    .withData("databaseProductVersion", metaData.getDatabaseProductVersion())
                    .withData("driverName", metaData.getDriverName())
                    .withData("driverVersion", metaData.getDriverVersion())
                    .withData("isValid", isValid);

        return responseBuilder.state(isValid).build();


    } catch(SQLException  e) {
        responseBuilder = responseBuilder
               .withData("exceptionMessage", e.getMessage());
        return responseBuilder.down().build();
    }
}
 
Example 2
Source File: SocketHealthCheck.java    From smallrye-health with Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    HealthCheckResponseBuilder healthCheckResponseBuilder = HealthCheckResponse
            .named(name);
    healthCheckResponseBuilder.withData("host", String.format("%s:%d", this.host, this.port));
    try (Socket s = new Socket()) {
        final SocketAddress socketAddress = new InetSocketAddress(host, port);
        s.connect(socketAddress, timeout);
        healthCheckResponseBuilder.up();
    } catch (IOException ex) {
        HealthChecksLogging.log.socketHealthCheckError(ex);

        healthCheckResponseBuilder.withData("error", ex.getMessage());
        healthCheckResponseBuilder.down();
    }
    return healthCheckResponseBuilder.build();
}
 
Example 3
Source File: InetAddressHealthCheck.java    From smallrye-health with Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    final HealthCheckResponseBuilder healthCheckResponseBuilder = HealthCheckResponse.named(this.name);
    healthCheckResponseBuilder.withData("host", this.host);
    try {
        InetAddress addr = InetAddress.getByName(this.host);
        final boolean reachable = addr.isReachable(this.timeout);
        if (!reachable) {
            healthCheckResponseBuilder.withData("error", String.format("Host %s not reachable.", this.host));
        }

        healthCheckResponseBuilder.state(reachable);
    } catch (IOException e) {
        HealthChecksLogging.log.inetAddressHealthCheckError(e);

        healthCheckResponseBuilder.withData("error", e.getMessage());
        healthCheckResponseBuilder.down();
    }

    return healthCheckResponseBuilder.build();
}
 
Example 4
Source File: SmallRyeHealthReporter.java    From smallrye-health with Apache License 2.0 6 votes vote down vote up
private HealthCheckResponse handleFailure(String name, Throwable e) {
    // Log Stacktrace to server log so an error is not just in Health Check response
    HealthLogging.log.healthCheckError(e);

    HealthCheckResponseBuilder response = HealthCheckResponse.named(name).down();

    if (null != uncheckedExceptionDataStyle) {
        switch (uncheckedExceptionDataStyle) {
            case ROOT_CAUSE:
                response.withData(ROOT_CAUSE, getRootCause(e).getMessage());
                break;
            case STACK_TRACE:
                response.withData(STACK_TRACE, getStackTrace(e));
                break;
            default:
                // don't add anything
        }
    }

    return response.build();
}
 
Example 5
Source File: GrpcHealthCheck.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    ServingStatus servingStatus = healthService.getStatuses().get(GrpcHealthStorage.DEFAULT_SERVICE_NAME);

    HealthCheckResponseBuilder builder = HealthCheckResponse.named("gRPC Server health check").up();
    builder.name("gRPC Server");

    if (isUp(servingStatus)) {
        builder.up();
    } else {
        builder.down();
    }

    for (Map.Entry<String, ServingStatus> statusEntry : healthService.getStatuses().entrySet()) {
        String serviceName = statusEntry.getKey();
        if (!serviceName.equals(GrpcHealthStorage.DEFAULT_SERVICE_NAME)) {
            builder.withData(serviceName, isUp(statusEntry.getValue()));
        }
    }

    return builder.build();
}
 
Example 6
Source File: KafkaStreamsTopicsHealthCheck.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {
    HealthCheckResponseBuilder builder = HealthCheckResponse.named("Kafka Streams topics health check").up();

    try {
        Set<String> missingTopics = manager.getMissingTopics(trimmedTopics);
        List<String> availableTopics = new ArrayList<>(trimmedTopics);
        availableTopics.removeAll(missingTopics);

        if (!availableTopics.isEmpty()) {
            builder.withData("available_topics", String.join(",", availableTopics));
        }
        if (!missingTopics.isEmpty()) {
            builder.down().withData("missing_topics", String.join(",", missingTopics));
        }
    } catch (InterruptedException e) {
        LOGGER.error("error when retrieving missing topics", e);
        builder.down().withData("technical_error", e.getMessage());
    }

    return builder.build();
}
 
Example 7
Source File: DBHealthCheck.java    From microprofile-sandbox with Apache License 2.0 6 votes vote down vote up
@Override
public HealthCheckResponse call() {

    HealthCheckResponseBuilder responseBuilder = HealthCheckResponse.named("membership");
    try {
        Connection connection = datasource.getConnection();
        boolean isValid = connection.isValid(5);

        DatabaseMetaData metaData = connection.getMetaData();

        responseBuilder = responseBuilder
                    .withData("databaseProductName", metaData.getDatabaseProductName())
                    .withData("databaseProductVersion", metaData.getDatabaseProductVersion())
                    .withData("driverName", metaData.getDriverName())
                    .withData("driverVersion", metaData.getDriverVersion())
                    .withData("isValid", isValid);

        return responseBuilder.state(isValid).build();


    } catch(SQLException  e) {
        responseBuilder = responseBuilder
               .withData("exceptionMessage", e.getMessage());
        return responseBuilder.down().build();
    }
}
 
Example 8
Source File: LivenessProbe.java    From trader with Apache License 2.0 6 votes vote down vote up
public HealthCheckResponse call() {
	HealthCheckResponse response = null;
	String message = "Live";
	try {
		HealthCheckResponseBuilder builder = HealthCheckResponse.named("Trader");

		if (Summary.error) { //can't run without these env vars
			builder = builder.down();
			message = Summary.message;
			logger.warning("Returning NOT live!");
		} else {
			builder = builder.up();
			logger.fine("Returning live!");
		}

		builder = builder.withData("message", message);

		response = builder.build(); 
	} catch (Throwable t) {
		logger.warning("Exception occurred during health check: "+t.getMessage());
		logException(t);
		throw t;
	}

	return response;
}
 
Example 9
Source File: ReadinessProbe.java    From trader with Apache License 2.0 6 votes vote down vote up
public HealthCheckResponse call() {
	HealthCheckResponse response = null;
	String message = "Ready";
	try {
		HealthCheckResponseBuilder builder = HealthCheckResponse.named("Trader");

		if ((jwtAudience==null) || (jwtIssuer==null)) { //can't run without these env vars
			builder = builder.down();
			message = "JWT environment variables not set!";
			logger.warning("Returning NOT ready!");
		} else {
			builder = builder.up();
			logger.fine("Returning ready!");
		}

		builder = builder.withData("message", message);

		response = builder.build(); 
	} catch (Throwable t) {
		logger.warning("Exception occurred during health check: "+t.getMessage());
		logException(t);
		throw t;
	}

	return response;
}
 
Example 10
Source File: UrlHealthCheck.java    From smallrye-health with Apache License 2.0 5 votes vote down vote up
@Override
public HealthCheckResponse call() {
    final HealthCheckResponseBuilder healthCheckResponseBuilder = HealthCheckResponse
            .named(name);
    healthCheckResponseBuilder.withData("host", String.format("%s %s", this.requestMethod, this.url));
    try {
        final HttpURLConnection httpUrlConn = (HttpURLConnection) new URL(this.url)
                .openConnection();

        httpUrlConn.setRequestMethod(requestMethod);

        httpUrlConn.setConnectTimeout(timeout);
        httpUrlConn.setReadTimeout(timeout);

        final boolean isUp = httpUrlConn.getResponseCode() == statusCode;

        if (!isUp) {
            healthCheckResponseBuilder.withData("error", String.format("Expected response code %d but actual is %d",
                    statusCode, httpUrlConn.getResponseCode()));
        }

        healthCheckResponseBuilder.state(isUp);

    } catch (Exception e) {
        HealthChecksLogging.log.urlHealthCheckError(e);

        healthCheckResponseBuilder.withData("error",
                String.format("%s: %s", e.getClass().getCanonicalName(), e.getMessage()));
        healthCheckResponseBuilder.down();
    }

    return healthCheckResponseBuilder.build();
}
 
Example 11
Source File: Neo4jHealthCheck.java    From quarkus with Apache License 2.0 5 votes vote down vote up
/**
 * Applies the given {@link ResultSummary} to the {@link HealthCheckResponseBuilder builder} and calls {@code build}
 * afterwards.
 *
 * @param resultSummary the result summary returned by the server
 * @param builder the health builder to be modified
 * @return the final {@link HealthCheckResponse health check response}
 */
private static HealthCheckResponse buildStatusUp(ResultSummary resultSummary, HealthCheckResponseBuilder builder) {
    ServerInfo serverInfo = resultSummary.server();

    builder.withData("server", serverInfo.version() + "@" + serverInfo.address());

    String databaseName = resultSummary.database().name();
    if (!(databaseName == null || databaseName.trim().isEmpty())) {
        builder.withData("database", databaseName.trim());
    }

    return builder.build();
}