-
Notifications
You must be signed in to change notification settings - Fork 31
feat(health,version): add health and version endpoints #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
46ff34a
feat(health,version): add health and version endpoints
DurgaPrasad-54 7ae39fb
fix(health): restore interrupt flag when InterruptedException occurs
DurgaPrasad-54 5971077
fix(health): shutdown executor on destroy and sanitize infra errors i…
DurgaPrasad-54 9e96b2e
fix(health): MySQL health check with timeout and sanitize errors
DurgaPrasad-54 d7c8a9f
fix(health): harden advanced MySQL checks and throttle execution
DurgaPrasad-54 422b832
fix(health): remove unused imports
DurgaPrasad-54 e151b3c
fix(health): fux deadlock detection issue
DurgaPrasad-54 5aa5234
fix(health): fix deadline timeout issue
DurgaPrasad-54 26ae608
fix(health): scope PROCESSLIST lock-wait check to application DB user
DurgaPrasad-54 b0cfa28
refactor(health): extract MySQL basic health query into helper method
DurgaPrasad-54 040349e
fix(health): avoid sharing JDBC connections across threads in advance…
DurgaPrasad-54 792a2a8
fix(health): avoid blocking DB I/O under write lock and restore inter…
DurgaPrasad-54 290740e
fix: add missing close brace
DurgaPrasad-54 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/com/iemr/mmu/controller/health/HealthController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * AMRIT – Accessible Medical Records via Integrated Technology | ||
| * Integrated EHR (Electronic Health Records) Solution | ||
| * | ||
| * Copyright (C) "Piramal Swasthya Management and Research Institute" | ||
| * | ||
| * This file is part of AMRIT. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| */ | ||
|
|
||
| package com.iemr.mmu.controller.health; | ||
|
|
||
| import java.time.Instant; | ||
| import java.util.Map; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
| import com.iemr.mmu.service.health.HealthService; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/health") | ||
| @Tag(name = "Health Check", description = "APIs for checking infrastructure health status") | ||
| public class HealthController { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(HealthController.class); | ||
|
|
||
| private final HealthService healthService; | ||
|
|
||
| public HealthController(HealthService healthService) { | ||
| this.healthService = healthService; | ||
| } | ||
|
|
||
| @GetMapping | ||
| @Operation(summary = "Check infrastructure health", | ||
| description = "Returns the health status of MySQL, Redis, and other configured services") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "Services are UP or DEGRADED (operational with warnings)"), | ||
| @ApiResponse(responseCode = "503", description = "One or more critical services are DOWN") | ||
| }) | ||
| public ResponseEntity<Map<String, Object>> checkHealth() { | ||
| logger.debug("Health check endpoint called"); | ||
|
|
||
| try { | ||
| Map<String, Object> healthStatus = healthService.checkHealth(); | ||
| String overallStatus = (String) healthStatus.get("status"); | ||
|
|
||
| // Return 503 only if DOWN; 200 for both UP and DEGRADED (DEGRADED = operational with warnings) | ||
| HttpStatus httpStatus = "DOWN".equals(overallStatus) ? HttpStatus.SERVICE_UNAVAILABLE : HttpStatus.OK; | ||
|
|
||
| logger.debug("Health check completed with status: {}", overallStatus); | ||
| return new ResponseEntity<>(healthStatus, httpStatus); | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error("Unexpected error during health check", e); | ||
|
|
||
| Map<String, Object> errorResponse = Map.of( | ||
| "status", "DOWN", | ||
| "timestamp", Instant.now().toString() | ||
| ); | ||
|
|
||
| return new ResponseEntity<>(errorResponse, HttpStatus.SERVICE_UNAVAILABLE); | ||
| } | ||
| } | ||
| } | ||
79 changes: 79 additions & 0 deletions
79
src/main/java/com/iemr/mmu/controller/version/VersionController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * AMRIT – Accessible Medical Records via Integrated Technology | ||
| * Integrated EHR (Electronic Health Records) Solution | ||
| * | ||
| * Copyright (C) "Piramal Swasthya Management and Research Institute" | ||
| * | ||
| * This file is part of AMRIT. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see https://www.gnu.org/licenses/. | ||
| */ | ||
| package com.iemr.mmu.controller.version; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Properties; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.springframework.http.MediaType; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
|
|
||
| @RestController | ||
| public class VersionController { | ||
|
|
||
| private final Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); | ||
|
|
||
| private static final String UNKNOWN_VALUE = "unknown"; | ||
|
|
||
| @Operation(summary = "Get version information") | ||
| @GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE) | ||
| public ResponseEntity<Map<String, String>> versionInformation() { | ||
| Map<String, String> response = new LinkedHashMap<>(); | ||
| try { | ||
| logger.info("version Controller Start"); | ||
| Properties gitProperties = loadGitProperties(); | ||
| response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE)); | ||
| response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE)); | ||
| response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE)); | ||
| response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE)); | ||
| } catch (Exception e) { | ||
| logger.error("Failed to load version information", e); | ||
| response.put("buildTimestamp", UNKNOWN_VALUE); | ||
| response.put("version", UNKNOWN_VALUE); | ||
| response.put("branch", UNKNOWN_VALUE); | ||
| response.put("commitHash", UNKNOWN_VALUE); | ||
| } | ||
| logger.info("version Controller End"); | ||
| return ResponseEntity.ok(response); | ||
| } | ||
|
|
||
| private Properties loadGitProperties() throws IOException { | ||
| Properties properties = new Properties(); | ||
| try (InputStream input = getClass().getClassLoader() | ||
| .getResourceAsStream("git.properties")) { | ||
| if (input != null) { | ||
| properties.load(input); | ||
| } | ||
| } | ||
| return properties; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.