Skip to content

Commit b643b6b

Browse files
feat(health,version): add health and version endpoints without auth (#120)
* feat(health,version): add health and version endpoints without auth * fix(health,version): remove unused git properties * refactor(health): simplify MySQL health check and remove sensitive details * fix(health): fix healthservices timeout issue * fix(health): include sanitized error in DOWN responses * fix(health): harden advanced MySQL checks and throttle execution * fix(health): scope PROCESSLIST lock-wait check to application DB user * fix(health): cancel timed-out advanced MySQL checks to avoid orphaned tasks * fix(health): avoid blocking DB I/O under write lock and restore interrupt flag
1 parent 28b50ee commit b643b6b

5 files changed

Lines changed: 715 additions & 1 deletion

File tree

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,32 @@
424424
</execution>
425425
</executions>
426426
</plugin>
427+
<plugin>
428+
<groupId>io.github.git-commit-id</groupId>
429+
<artifactId>git-commit-id-maven-plugin</artifactId>
430+
<version>7.0.0</version>
431+
<executions>
432+
<execution>
433+
<id>get-the-git-infos</id>
434+
<goals>
435+
<goal>revision</goal>
436+
</goals>
437+
<phase>initialize</phase>
438+
</execution>
439+
</executions>
440+
<configuration>
441+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
442+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
443+
<includeOnlyProperties>
444+
<property>^git.branch$</property>
445+
<property>^git.commit.id.abbrev$</property>
446+
<property>^git.build.version$</property>
447+
<property>^git.build.time$</property>
448+
</includeOnlyProperties>
449+
<failOnNoGitDirectory>false</failOnNoGitDirectory>
450+
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
451+
</configuration>
452+
</plugin>
427453
<plugin>
428454
<groupId>org.springframework.boot</groupId>
429455
<artifactId>spring-boot-maven-plugin</artifactId>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.iemr.ecd.controller.health;
2+
3+
import java.time.Instant;
4+
import java.util.Map;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import com.iemr.ecd.service.health.HealthService;
13+
import io.swagger.v3.oas.annotations.Operation;
14+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
15+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
16+
import io.swagger.v3.oas.annotations.tags.Tag;
17+
18+
@RestController
19+
@RequestMapping("/health")
20+
@Tag(name = "Health Check", description = "APIs for checking infrastructure health status")
21+
public class HealthController {
22+
23+
private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
24+
25+
private final HealthService healthService;
26+
27+
public HealthController(HealthService healthService) {
28+
this.healthService = healthService;
29+
}
30+
31+
@GetMapping
32+
@Operation(summary = "Check infrastructure health",
33+
description = "Returns the health status of MySQL, Redis, and other configured services")
34+
@ApiResponses({
35+
@ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"),
36+
@ApiResponse(responseCode = "503", description = "One or more critical services are DOWN")
37+
})
38+
public ResponseEntity<Map<String, Object>> checkHealth() {
39+
logger.info("Health check endpoint called");
40+
41+
try {
42+
Map<String, Object> healthStatus = healthService.checkHealth();
43+
String overallStatus = (String) healthStatus.get("status");
44+
45+
// Return 503 only if DOWN; 200 for both UP and DEGRADED (DEGRADED = operational with warnings)
46+
HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK;
47+
48+
logger.debug("Health check completed with status: {}", overallStatus);
49+
return new ResponseEntity<>(healthStatus, httpStatus);
50+
51+
} catch (Exception e) {
52+
logger.error("Unexpected error during health check", e);
53+
54+
Map<String, Object> errorResponse = Map.of(
55+
"status", "DOWN",
56+
"timestamp", Instant.now().toString()
57+
);
58+
59+
return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE);
60+
}
61+
}
62+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* AMRIT – Accessible Medical Records via Integrated Technology
3+
* Integrated EHR (Electronic Health Records) Solution
4+
*
5+
* Copyright (C) "Piramal Swasthya Management and Research Institute"
6+
*
7+
* This file is part of AMRIT.
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see https://www.gnu.org/licenses/.
21+
*/
22+
package com.iemr.ecd.controller.version;
23+
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.util.LinkedHashMap;
27+
import java.util.Map;
28+
import java.util.Properties;
29+
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
import org.springframework.http.MediaType;
34+
import org.springframework.http.ResponseEntity;
35+
import org.springframework.web.bind.annotation.GetMapping;
36+
import org.springframework.web.bind.annotation.RestController;
37+
38+
import io.swagger.v3.oas.annotations.Operation;
39+
40+
@RestController
41+
public class VersionController {
42+
43+
private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
44+
45+
private static final String UNKNOWN_VALUE = "unknown";
46+
47+
@Operation(summary = "Get version information")
48+
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
49+
public ResponseEntity<Map<String, String>> versionInformation() {
50+
Map<String, String> response = new LinkedHashMap<>();
51+
try {
52+
logger.info("version Controller Start");
53+
Properties gitProperties = loadGitProperties();
54+
response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE));
55+
response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE));
56+
response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE));
57+
response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE));
58+
} catch (Exception e) {
59+
logger.error("Failed to load version information", e);
60+
response.put("buildTimestamp", UNKNOWN_VALUE);
61+
response.put("version", UNKNOWN_VALUE);
62+
response.put("branch", UNKNOWN_VALUE);
63+
response.put("commitHash", UNKNOWN_VALUE);
64+
}
65+
logger.info("version Controller End");
66+
return ResponseEntity.ok(response);
67+
}
68+
69+
private Properties loadGitProperties() throws IOException {
70+
Properties properties = new Properties();
71+
try (InputStream input = getClass().getClassLoader()
72+
.getResourceAsStream("git.properties")) {
73+
if (input != null) {
74+
properties.load(input);
75+
}
76+
}
77+
return properties;
78+
}
79+
}

0 commit comments

Comments
 (0)