Skip to content

Commit c2be201

Browse files
feat(health,version): add health and version endpoints (#155)
* feat(health,version): add health and version endpoints * fix(health): restore interrupt flag when InterruptedException occurs * fix(health): shutdown executor on destroy and sanitize infra errors in /health * fix(health): MySQL health check with timeout and sanitize errors * fix(health): harden advanced MySQL checks and throttle execution * fix(health): remove unused imports * fix(health): fux deadlock detection issue * fix(health): fix deadline timeout issue * fix(health): scope PROCESSLIST lock-wait check to application DB user * refactor(health): extract MySQL basic health query into helper method * fix(health): avoid sharing JDBC connections across threads in advanced MySQL checks * fix(health): avoid blocking DB I/O under write lock and restore interrupt flag * fix: add missing close brace
1 parent 456b23a commit c2be201

6 files changed

Lines changed: 783 additions & 9 deletions

File tree

pom.xml

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,7 @@
255255
<groupId>org.springframework.session</groupId>
256256
<artifactId>spring-session-data-redis</artifactId>
257257
</dependency>
258-
<!-- Actuator -->
259-
<dependency>
260-
<groupId>org.springframework.boot</groupId>
261-
<artifactId>spring-boot-starter-actuator</artifactId>
262-
</dependency>
258+
263259
<dependency>
264260
<groupId>org.jacoco</groupId>
265261
<artifactId>jacoco-maven-plugin</artifactId>
@@ -297,7 +293,7 @@
297293
</dependency>
298294
</dependencies>
299295
<build>
300-
<finalName>${artifactId}-${version}</finalName>
296+
<finalName>${project.artifactId}-${project.version}</finalName>
301297
<plugins>
302298
<plugin>
303299
<groupId>org.apache.maven.plugins</groupId>
@@ -434,7 +430,6 @@
434430
</dependencies>
435431
<configuration>
436432
<configLocation>checkstyle.xml</configLocation>
437-
<encoding>UTF-8</encoding>
438433
<consoleOutput>true</consoleOutput>
439434
<failsOnError>true</failsOnError>
440435
<linkXRef>false</linkXRef>
@@ -449,6 +444,32 @@
449444
</execution>
450445
</executions>
451446
</plugin>
447+
<plugin>
448+
<groupId>io.github.git-commit-id</groupId>
449+
<artifactId>git-commit-id-maven-plugin</artifactId>
450+
<version>7.0.0</version>
451+
<executions>
452+
<execution>
453+
<id>get-the-git-infos</id>
454+
<goals>
455+
<goal>revision</goal>
456+
</goals>
457+
<phase>initialize</phase>
458+
</execution>
459+
</executions>
460+
<configuration>
461+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
462+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
463+
<includeOnlyProperties>
464+
<property>^git.branch$</property>
465+
<property>^git.commit.id.abbrev$</property>
466+
<property>^git.build.version$</property>
467+
<property>^git.build.time$</property>
468+
</includeOnlyProperties>
469+
<failOnNoGitDirectory>false</failOnNoGitDirectory>
470+
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
471+
</configuration>
472+
</plugin>
452473
<plugin>
453474
<groupId>org.springframework.boot</groupId>
454475
<artifactId>spring-boot-maven-plugin</artifactId>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.mmu.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.mmu.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.debug("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+
}
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.mmu.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)