Skip to content

Commit 7fc1e8b

Browse files
Cherry-pick health and version API enhancements to release-3.6.1 (#119)
* feat(health,version): add health and version endpoints * fix(health): removed duplicates from healthservices * fix: The DEGRADED status was incorrectly returning HTTP 503 * fix(health): run checks concurrently, prevent thread starvation, and harden timeouts * fix(health): add proper @deprecated metadata and javadoc for obsolete methods * fix(health): add proper @deprecated metadata and javadoc for obsolete methods * refactor(health): remove obsolete deprecated health check methods * fix(health): mark timed-out components DOWN and make status maps thread-safe * fix(health): removed duplicates from health services * fix(health): harden advanced MySQL checks and throttle execution * fix(health): harden advanced MySQL checks and reflect DEGRADED status * fix(health): avoid nested executor deadlock in advanced MySQL checks * fix(health): scope PROCESSLIST lock-wait check to application DB user * fix(health): avoid blocking DB I/O under write lock and restore interrupt flag
1 parent ce01105 commit 7fc1e8b

5 files changed

Lines changed: 694 additions & 34 deletions

File tree

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,32 @@
386386
</compilerArgs>
387387
</configuration>
388388
</plugin>
389+
<plugin>
390+
<groupId>io.github.git-commit-id</groupId>
391+
<artifactId>git-commit-id-maven-plugin</artifactId>
392+
<version>9.0.2</version>
393+
<executions>
394+
<execution>
395+
<id>get-the-git-infos</id>
396+
<goals>
397+
<goal>revision</goal>
398+
</goals>
399+
<phase>initialize</phase>
400+
</execution>
401+
</executions>
402+
<configuration>
403+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
404+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
405+
<includeOnlyProperties>
406+
<property>^git.branch$</property>
407+
<property>^git.commit.id.abbrev$</property>
408+
<property>^git.build.version$</property>
409+
<property>^git.build.time$</property>
410+
</includeOnlyProperties>
411+
<failOnNoGitDirectory>false</failOnNoGitDirectory>
412+
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
413+
</configuration>
414+
</plugin>
389415
<plugin>
390416
<groupId>org.springframework.boot</groupId>
391417
<artifactId>spring-boot-maven-plugin</artifactId>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
23+
package com.iemr.inventory.controller.health;
24+
25+
import java.time.Instant;
26+
import java.util.Map;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
import org.springframework.http.HttpStatus;
30+
import org.springframework.http.ResponseEntity;
31+
import org.springframework.web.bind.annotation.GetMapping;
32+
import org.springframework.web.bind.annotation.RequestMapping;
33+
import org.springframework.web.bind.annotation.RestController;
34+
import com.iemr.inventory.service.health.HealthService;
35+
import io.swagger.v3.oas.annotations.Operation;
36+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
37+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
38+
import io.swagger.v3.oas.annotations.tags.Tag;
39+
40+
@RestController
41+
@RequestMapping("/health")
42+
@Tag(name = "Health Check", description = "APIs for checking infrastructure health status")
43+
public class HealthController {
44+
45+
private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
46+
47+
private final HealthService healthService;
48+
49+
public HealthController(HealthService healthService) {
50+
this.healthService = healthService;
51+
}
52+
53+
@GetMapping
54+
@Operation(summary = "Check infrastructure health",
55+
description = "Returns the health status of MySQL, Redis, and other configured services")
56+
@ApiResponses({
57+
@ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"),
58+
@ApiResponse(responseCode = "503", description = "One or more critical services are DOWN")
59+
})
60+
public ResponseEntity<Map<String, Object>> checkHealth() {
61+
logger.info("Health check endpoint called");
62+
63+
try {
64+
Map<String, Object> healthStatus = healthService.checkHealth();
65+
String overallStatus = (String) healthStatus.get("status");
66+
67+
// Return 503 only if DOWN; 200 for both UP and DEGRADED (DEGRADED = operational with warnings)
68+
HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK;
69+
70+
logger.debug("Health check completed with status: {}", overallStatus);
71+
return new ResponseEntity<>(healthStatus, httpStatus);
72+
73+
} catch (Exception e) {
74+
logger.error("Unexpected error during health check", e);
75+
76+
Map<String, Object> errorResponse = Map.of(
77+
"status", "DOWN",
78+
"timestamp", Instant.now().toString()
79+
);
80+
81+
return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE);
82+
}
83+
}
84+
}
85+
86+
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* AMRIT – Accessible Medical Records via Integrated Technology
3-
* Integrated EHR (Electronic Health Records) Solution
2+
* AMRIT – Accessible Medical Records via Integrated Technology
3+
* Integrated EHR (Electronic Health Records) Solution
44
*
5-
* Copyright (C) "Piramal Swasthya Management and Research Institute"
5+
* Copyright (C) "Piramal Swasthya Management and Research Institute"
66
*
77
* This file is part of AMRIT.
88
*
@@ -21,57 +21,59 @@
2121
*/
2222
package com.iemr.inventory.controller.version;
2323

24-
import java.io.BufferedReader;
2524
import java.io.IOException;
2625
import java.io.InputStream;
27-
import java.io.InputStreamReader;
26+
import java.util.LinkedHashMap;
27+
import java.util.Map;
28+
import java.util.Properties;
2829

2930
import org.slf4j.Logger;
3031
import org.slf4j.LoggerFactory;
3132

32-
import org.springframework.web.bind.annotation.RequestMapping;
33-
import org.springframework.web.bind.annotation.RequestMethod;
33+
import org.springframework.http.MediaType;
34+
import org.springframework.http.ResponseEntity;
35+
import org.springframework.web.bind.annotation.GetMapping;
3436
import org.springframework.web.bind.annotation.RestController;
3537

36-
import com.iemr.inventory.utils.response.OutputResponse;
37-
38-
import io.swagger.annotations.ApiOperation;
38+
import io.swagger.v3.oas.annotations.Operation;
3939

4040
@RestController
4141
public class VersionController {
4242

43-
private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
43+
private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
44+
45+
private static final String UNKNOWN_VALUE = "unknown";
4446

45-
@ApiOperation(value = "Get version details", consumes = "application/json", produces = "application/json")
46-
@RequestMapping(value = "/version", method = { RequestMethod.GET })
47-
public String versionInformation() {
48-
OutputResponse output = new OutputResponse();
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<>();
4951
try {
5052
logger.info("version Controller Start");
51-
output.setResponse(readGitProperties());
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));
5258
} catch (Exception e) {
53-
output.setError(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);
5464
}
55-
5665
logger.info("version Controller End");
57-
return output.toString();
58-
}
59-
60-
private String readGitProperties() throws Exception {
61-
ClassLoader classLoader = getClass().getClassLoader();
62-
InputStream inputStream = classLoader.getResourceAsStream("git.properties");
63-
64-
return readFromInputStream(inputStream);
66+
return ResponseEntity.ok(response);
6567
}
6668

67-
private String readFromInputStream(InputStream inputStream) throws IOException {
68-
StringBuilder resultStringBuilder = new StringBuilder();
69-
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
70-
String line;
71-
while ((line = br.readLine()) != null) {
72-
resultStringBuilder.append(line).append("\n");
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);
7375
}
7476
}
75-
return resultStringBuilder.toString();
77+
return properties;
7678
}
7779
}

0 commit comments

Comments
 (0)