|
41 | 41 | import java.util.List; |
42 | 42 | import java.util.Map; |
43 | 43 | import java.util.Optional; |
| 44 | +import java.util.Properties; |
44 | 45 | import java.util.Set; |
45 | 46 | import java.util.SortedMap; |
46 | 47 | import java.util.TimeZone; |
|
58 | 59 | import javax.xml.stream.XMLStreamWriter; |
59 | 60 |
|
60 | 61 | import com.fasterxml.jackson.core.JsonParseException; |
| 62 | +import com.fasterxml.jackson.databind.ObjectMapper; |
61 | 63 | import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
62 | 64 | import com.google.common.base.CharMatcher; |
63 | 65 | import com.google.common.base.Splitter; |
@@ -293,9 +295,10 @@ public final void doHandle(HttpServletRequest baseRequest, |
293 | 295 | String uri = request.getRequestURI(); |
294 | 296 | String originalUri = request.getRequestURI(); |
295 | 297 |
|
296 | | - // Check for the /version endpoint |
297 | | - if ("/healthz".equals(uri) && "GET".equalsIgnoreCase(method)) { |
298 | | - handleVersionRequest(response); |
| 298 | + String healthzUri = servicePath.isEmpty() ? "/healthz" : |
| 299 | + servicePath + "/healthz"; |
| 300 | + if (healthzUri.equals(uri) && "GET".equalsIgnoreCase(method)) { |
| 301 | + handleStatuszRequest(response); |
299 | 302 | return; |
300 | 303 | } |
301 | 304 |
|
@@ -2035,19 +2038,56 @@ private void handlePutBlob(HttpServletRequest request, |
2035 | 2038 |
|
2036 | 2039 | response.addHeader(HttpHeaders.ETAG, maybeQuoteETag(eTag)); |
2037 | 2040 | } |
2038 | | - private void handleVersionRequest(HttpServletResponse response) throws IOException { |
| 2041 | + |
| 2042 | + private void handleStatuszRequest(HttpServletResponse response) |
| 2043 | + throws IOException { |
2039 | 2044 | response.setStatus(HttpServletResponse.SC_OK); |
2040 | 2045 | response.setContentType("application/json"); |
2041 | | - response.setCharacterEncoding("UTF-8"); |
2042 | | - |
2043 | | - String versionInfo = "{ \"status\": \"OK\"}"; |
2044 | | - |
| 2046 | + response.setCharacterEncoding(UTF_8); |
| 2047 | + |
| 2048 | + var statusBody = ImmutableMap.of("gitHash", StatusMetadata.INSTANCE.gitHash); |
| 2049 | + String versionInfo = StatusMetadata.JSON_MAPPER.writeValueAsString(statusBody); |
| 2050 | + |
2045 | 2051 | try (PrintWriter writer = response.getWriter()) { |
2046 | 2052 | writer.write(versionInfo); |
2047 | 2053 | writer.flush(); |
2048 | 2054 | } |
2049 | 2055 | } |
2050 | 2056 |
|
| 2057 | + private static final class StatusMetadata { |
| 2058 | + private static final ObjectMapper JSON_MAPPER = new ObjectMapper(); |
| 2059 | + private static final StatusMetadata INSTANCE = StatusMetadata.load(); |
| 2060 | + private static final String UNKNOWN_VALUE = "unknown"; |
| 2061 | + private final String gitHash; |
| 2062 | + |
| 2063 | + private StatusMetadata(String gitHash) { |
| 2064 | + this.gitHash = gitHash; |
| 2065 | + } |
| 2066 | + |
| 2067 | + static StatusMetadata load() { |
| 2068 | + String gitHash = loadGitHash(); |
| 2069 | + return new StatusMetadata(gitHash); |
| 2070 | + } |
| 2071 | + |
| 2072 | + private static String loadGitHash() { |
| 2073 | + try (InputStream stream = S3ProxyHandler.class.getClassLoader() |
| 2074 | + .getResourceAsStream("git.properties")) { |
| 2075 | + if (stream == null) { |
| 2076 | + return UNKNOWN_VALUE; |
| 2077 | + } |
| 2078 | + Properties properties = new Properties(); |
| 2079 | + properties.load(stream); |
| 2080 | + return Optional.ofNullable( |
| 2081 | + properties.getProperty("git.commit.id.abbrev")) |
| 2082 | + .orElseGet(() -> properties.getProperty("git.commit.id", |
| 2083 | + UNKNOWN_VALUE)); |
| 2084 | + } catch (IOException ioe) { |
| 2085 | + logger.debug("Unable to load git.properties", ioe); |
| 2086 | + return UNKNOWN_VALUE; |
| 2087 | + } |
| 2088 | + } |
| 2089 | + } |
| 2090 | + |
2051 | 2091 | private void handlePostBlob(HttpServletRequest request, |
2052 | 2092 | HttpServletResponse response, InputStream is, BlobStore blobStore, |
2053 | 2093 | String containerName) |
|
0 commit comments