Skip to content

Commit 0683b41

Browse files
feat(health,version): add health and version endpoints (#120)
* feat(health,version): add health and version endponts * fix(health): add constant and remove duplicates * fix(health): avoid permanent DEGRADED from historical deadlocks * fix(health): Removed the unnecessary boolean literal * fix(health): Fixed the broken lock-wait detection * fix(health): avoid blocking DB I/O under write lock and restore interrupt flag * fix(health): add cancelFutures in healthservice * fix(health): close basic DB connection before advanced checks and remove shared-map race
1 parent 5b960cc commit 0683b41

5 files changed

Lines changed: 565 additions & 95 deletions

File tree

pom.xml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,10 @@
296296
<artifactId>maven-jar-plugin</artifactId>
297297
<version>3.0.2</version>
298298
</plugin>
299-
300299
<plugin>
301-
<groupId>pl.project13.maven</groupId>
302-
<artifactId>git-commit-id-plugin</artifactId>
303-
<version>4.9.10</version>
300+
<groupId>io.github.git-commit-id</groupId>
301+
<artifactId>git-commit-id-maven-plugin</artifactId>
302+
<version>9.0.2</version>
304303
<executions>
305304
<execution>
306305
<id>get-the-git-infos</id>
@@ -314,11 +313,13 @@
314313
<generateGitPropertiesFile>true</generateGitPropertiesFile>
315314
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
316315
<includeOnlyProperties>
317-
<includeOnlyProperty>git.commit.id</includeOnlyProperty>
318-
<includeOnlyProperty>git.build.time</includeOnlyProperty>
316+
<property>^git.branch$</property>
317+
<property>^git.commit.id.abbrev$</property>
318+
<property>^git.build.version$</property>
319+
<property>^git.build.time$</property>
319320
</includeOnlyProperties>
320-
<commitIdGenerationMode>full</commitIdGenerationMode>
321-
<format>properties</format>
321+
<failOnNoGitDirectory>false</failOnNoGitDirectory>
322+
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
322323
</configuration>
323324
</plugin>
324325
<plugin>

src/main/java/com/iemr/admin/controller/health/HealthController.java

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,66 @@
2121
*/
2222
package com.iemr.admin.controller.health;
2323

24+
import java.time.Instant;
2425
import java.util.Map;
2526

2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
28-
import org.springframework.beans.factory.annotation.Autowired;
2929
import org.springframework.http.HttpStatus;
3030
import org.springframework.http.ResponseEntity;
3131
import org.springframework.web.bind.annotation.GetMapping;
32+
import org.springframework.web.bind.annotation.RequestMapping;
3233
import org.springframework.web.bind.annotation.RestController;
3334

3435
import com.iemr.admin.service.health.HealthService;
3536

3637
import io.swagger.v3.oas.annotations.Operation;
38+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
39+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
40+
import io.swagger.v3.oas.annotations.tags.Tag;
3741

3842
@RestController
43+
@RequestMapping("/health")
44+
@Tag(name = "Health Check", description = "APIs for checking infrastructure health status")
3945
public class HealthController {
4046

4147
private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
4248

43-
@Autowired
44-
private HealthService healthService;
49+
private final HealthService healthService;
50+
51+
public HealthController(HealthService healthService) {
52+
this.healthService = healthService;
53+
}
4554

46-
@Operation(summary = "Health check endpoint")
47-
@GetMapping("/health")
48-
public ResponseEntity<Map<String, Object>> health() {
49-
logger.info("Health check endpoint called");
50-
51-
Map<String, Object> healthStatus = healthService.checkHealth();
55+
@GetMapping
56+
@Operation(summary = "Check infrastructure health",
57+
description = "Returns the health status of MySQL, Redis, and other configured services")
58+
@ApiResponses({
59+
@ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"),
60+
@ApiResponse(responseCode = "503", description = "One or more critical services are DOWN")
61+
})
62+
public ResponseEntity<Map<String, Object>> checkHealth() {
63+
logger.debug("Health check endpoint called");
5264

53-
// Return 503 if any service is down, 200 if all are up
54-
String status = (String) healthStatus.get("status");
55-
HttpStatus httpStatus = "UP".equals(status) ? HttpStatus.OK : HttpStatus.SERVICE_UNAVAILABLE;
56-
57-
logger.info("Health check completed with status: {}", status);
58-
return ResponseEntity.status(httpStatus).body(healthStatus);
65+
try {
66+
Map<String, Object> healthStatus = healthService.checkHealth();
67+
String overallStatus = (String) healthStatus.get("status");
68+
69+
HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK;
70+
71+
logger.debug("Health check completed with status: {}", overallStatus);
72+
return new ResponseEntity<>(healthStatus, httpStatus);
73+
74+
} catch (Exception e) {
75+
logger.error("Unexpected error during health check", e);
76+
77+
Map<String, Object> errorResponse = Map.of(
78+
"status", "DOWN",
79+
"timestamp", Instant.now().toString()
80+
);
81+
82+
return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE);
83+
}
5984
}
6085
}
86+

src/main/java/com/iemr/admin/controller/version/VersionController.java

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,59 @@
1919
* You should have received a copy of the GNU General Public License
2020
* along with this program. If not, see https://www.gnu.org/licenses/.
2121
*/
22+
2223
package com.iemr.admin.controller.version;
2324

2425
import java.util.Map;
25-
2626
import org.slf4j.Logger;
2727
import org.slf4j.LoggerFactory;
28-
import org.springframework.beans.factory.annotation.Autowired;
29-
import org.springframework.http.MediaType;
28+
import org.springframework.http.ResponseEntity;
3029
import org.springframework.web.bind.annotation.GetMapping;
3130
import org.springframework.web.bind.annotation.RestController;
32-
33-
import com.iemr.admin.service.version.VersionService;
34-
3531
import io.swagger.v3.oas.annotations.Operation;
32+
import java.io.IOException;
33+
import java.io.InputStream;
34+
import java.util.LinkedHashMap;
35+
import java.util.Properties;
36+
import org.springframework.http.MediaType;
3637

3738
@RestController
3839
public class VersionController {
3940

40-
private static final Logger logger = LoggerFactory.getLogger(VersionController.class);
41+
private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
42+
43+
private static final String UNKNOWN_VALUE = "unknown";
4144

42-
@Autowired
43-
private VersionService versionService;
45+
@Operation(summary = "Get version information")
46+
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
47+
public ResponseEntity<Map<String, String>> versionInformation() {
48+
Map<String, String> response = new LinkedHashMap<>();
49+
try {
50+
logger.info("version Controller Start");
51+
Properties gitProperties = loadGitProperties();
52+
response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE));
53+
response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE));
54+
response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE));
55+
response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE));
56+
} catch (Exception e) {
57+
logger.error("Failed to load version information", e);
58+
response.put("buildTimestamp", UNKNOWN_VALUE);
59+
response.put("version", UNKNOWN_VALUE);
60+
response.put("branch", UNKNOWN_VALUE);
61+
response.put("commitHash", UNKNOWN_VALUE);
62+
}
63+
logger.info("version Controller End");
64+
return ResponseEntity.ok(response);
65+
}
4466

45-
@Operation(summary = "Version information")
46-
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
47-
public Map<String, String> versionInformation() {
48-
logger.info("version Controller Start");
49-
Map<String, String> versionInfo = versionService.getVersionInfo();
50-
logger.info("version Controller End");
51-
return versionInfo;
52-
}
67+
private Properties loadGitProperties() throws IOException {
68+
Properties properties = new Properties();
69+
try (InputStream input = getClass().getClassLoader()
70+
.getResourceAsStream("git.properties")) {
71+
if (input != null) {
72+
properties.load(input);
73+
}
74+
}
75+
return properties;
76+
}
5377
}

0 commit comments

Comments
 (0)