-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Add health check endpoint #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...d-spanner-cassandra/src/main/java/com/google/cloud/spanner/adapter/HealthCheckServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| Copyright 2025 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package com.google.cloud.spanner.adapter; | ||
|
|
||
| import com.sun.net.httpserver.HttpExchange; | ||
| import com.sun.net.httpserver.HttpHandler; | ||
| import com.sun.net.httpserver.HttpServer; | ||
| import java.io.IOException; | ||
| import java.io.OutputStream; | ||
| import java.net.InetAddress; | ||
| import java.net.InetSocketAddress; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * A simple, lightweight HTTP server for health check monitoring. | ||
| * | ||
| * <p>It listens on a configured host and port, responding to {@code GET} requests on the {@code | ||
| * /debug/health} endpoint. | ||
| */ | ||
| final class HealthCheckServer { | ||
| private static final Logger LOG = LoggerFactory.getLogger(HealthCheckServer.class); | ||
| private static final int HTTP_OK_STATUS = 200; | ||
| private static final int HTTP_UNAVAILABLE_STATUS = 503; | ||
| private static final int HTTP_METHOD_NOT_ALLOWED = 405; | ||
|
|
||
| private final HttpServer server; | ||
| private final AtomicBoolean isReady = new AtomicBoolean(false); | ||
|
|
||
| HealthCheckServer(InetAddress address, int port) throws IOException { | ||
| this.server = HttpServer.create(new InetSocketAddress(address, port), 0); | ||
| this.server.createContext("/debug/health", new HealthCheckHandler()); | ||
| } | ||
|
|
||
| void start() { | ||
| server.start(); | ||
| LOG.info("Health check server started on {}/debug/health", server.getAddress()); | ||
| } | ||
|
|
||
| void stop() { | ||
| server.stop(0); | ||
| LOG.info("Health check server stopped."); | ||
| } | ||
|
|
||
| InetSocketAddress getAddress() { | ||
| return server.getAddress(); | ||
| } | ||
|
|
||
| void setReady(boolean ready) { | ||
| isReady.set(ready); | ||
| } | ||
|
|
||
| /** This handler responds to health check requests. */ | ||
| private class HealthCheckHandler implements HttpHandler { | ||
| @Override | ||
| public void handle(HttpExchange exchange) throws IOException { | ||
| if (!"GET".equalsIgnoreCase(exchange.getRequestMethod())) { | ||
| // Respond with 405 Method Not Allowed for non-GET requests | ||
| exchange.sendResponseHeaders(HTTP_METHOD_NOT_ALLOWED, -1); | ||
| return; | ||
| } | ||
| if (isReady.get()) { | ||
| sendResponse(exchange, HTTP_OK_STATUS, "All listeners are up and running"); | ||
| } else { | ||
| sendResponse(exchange, HTTP_UNAVAILABLE_STATUS, "Service Unavailable"); | ||
| } | ||
| } | ||
|
|
||
| private void sendResponse(HttpExchange exchange, int statusCode, String response) | ||
| throws IOException { | ||
| byte[] responseBytes = response.getBytes(StandardCharsets.UTF_8); | ||
| exchange.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8"); | ||
| exchange.sendResponseHeaders(statusCode, responseBytes.length); | ||
| try (OutputStream os = exchange.getResponseBody()) { | ||
| os.write(responseBytes); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...anner-cassandra/src/test/java/com/google/cloud/spanner/adapter/HealthCheckServerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| Copyright 2025 Google LLC | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
| package com.google.cloud.spanner.adapter; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.InetAddress; | ||
| import org.apache.hc.client5.http.classic.methods.HttpGet; | ||
| import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
| import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
| import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
| import org.apache.hc.core5.http.io.entity.EntityUtils; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public final class HealthCheckServerTest { | ||
| private HealthCheckServer server; | ||
| private CloseableHttpClient client; | ||
| private String baseUri; | ||
|
|
||
| @Before | ||
| public void setUp() throws IOException { | ||
| // Start the server on an ephemeral port (port 0) to avoid conflicts. | ||
| server = new HealthCheckServer(InetAddress.getLoopbackAddress(), 0); | ||
| server.start(); | ||
|
|
||
| // Get the actual port the server is listening on to build the request URI. | ||
| final int port = server.getAddress().getPort(); | ||
| baseUri = "http://localhost:" + port; | ||
|
|
||
| // Create a default HttpClient instance. | ||
| client = HttpClients.createDefault(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws IOException { | ||
| if (server != null) { | ||
| server.stop(); | ||
| } | ||
| if (client != null) { | ||
| client.close(); // Close the client to release resources. | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void server_whenNotReady_returns503() throws IOException { | ||
| final HttpGet request = new HttpGet(baseUri + "/debug/health"); | ||
|
|
||
| client.execute( | ||
| request, | ||
| response -> { | ||
| assertThat(response.getCode()).isEqualTo(503); | ||
| assertThat(EntityUtils.toString(response.getEntity())).isEqualTo("Service Unavailable"); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void server_whenSetToReady_returns200() throws IOException { | ||
| server.setReady(true); | ||
| final HttpGet request = new HttpGet(baseUri + "/debug/health"); | ||
|
|
||
| client.execute( | ||
| request, | ||
| response -> { | ||
| assertThat(response.getCode()).isEqualTo(200); | ||
| assertThat(EntityUtils.toString(response.getEntity())) | ||
| .isEqualTo("All listeners are up and running"); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void server_whenToggledToNotReady_returns503() throws IOException { | ||
| server.setReady(true); | ||
| server.setReady(false); | ||
| final HttpGet request = new HttpGet(baseUri + "/debug/health"); | ||
|
|
||
| client.execute( | ||
| request, | ||
| response -> { | ||
| assertThat(response.getCode()).isEqualTo(503); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void server_withPostRequest_returns405() throws IOException { | ||
| final HttpPost request = new HttpPost(baseUri + "/debug/health"); | ||
|
|
||
| client.execute( | ||
| request, | ||
| response -> { | ||
| assertThat(response.getCode()).isEqualTo(405); | ||
| return null; | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void server_withInvalidPath_returns404() throws IOException { | ||
| final HttpGet request = new HttpGet(baseUri + "/invalid/path"); | ||
|
|
||
| client.execute( | ||
| request, | ||
| response -> { | ||
| assertThat(response.getCode()).isEqualTo(404); | ||
| return null; | ||
| }); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.