-
Notifications
You must be signed in to change notification settings - Fork 49
Add /health endpoint and standardize /version response #331
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
Open
vaishnavbhosale
wants to merge
9
commits into
PSMRI:main
Choose a base branch
from
vaishnavbhosale:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dbe7418
Add /health endpoint and standardize /version response
vaishnavbhosale 2a3678c
Add license headers and Javadocs to health and version controllers
vaishnavbhosale 4c01959
Enhance /health endpoint to check Database and Redis connectivity
vaishnavbhosale 13db52a
Improve /health endpoint HTTP status handling and logging
vaishnavbhosale 49edb56
Enhance database health check with validation query
vaishnavbhosale 25baf2f
Refactor health controller to constructor injection and constants
vaishnavbhosale cb36911
Refactor: Extract business logic to HealthService to keep controller β¦
vaishnavbhosale 2922428
Refactor: Extract business logic to HealthService to keep controller β¦
vaishnavbhosale d18922d
Fix: Use ObjectProvider for optional health dependencies
vaishnavbhosale 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
66 changes: 66 additions & 0 deletions
66
src/main/java/com/iemr/common/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,66 @@ | ||
| /* | ||
| * 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.common.controller.health; | ||
|
|
||
| import com.iemr.common.service.health.HealthService; | ||
| 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.RestController; | ||
|
|
||
| /** | ||
| * Health check controller for Common-API. | ||
| * Verifies application liveness and dependency health (DB, Redis). | ||
| * | ||
| * @author vaishnavbhosale | ||
| */ | ||
| @RestController | ||
| public class HealthController { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(HealthController.class); | ||
|
|
||
| private final HealthService healthService; | ||
|
|
||
| public HealthController(HealthService healthService) { | ||
| this.healthService = healthService; | ||
| } | ||
|
|
||
| @GetMapping("/health") | ||
| public ResponseEntity<Map<String, Object>> health() { | ||
| logger.info("Health check endpoint called"); | ||
|
|
||
|
|
||
| Map<String, Object> healthStatus = healthService.checkHealth(); | ||
|
|
||
| // Standard HTTP Status logic | ||
| String status = (String) healthStatus.get("status"); | ||
| HttpStatus httpStatus = "UP".equals(status) ? HttpStatus.OK : HttpStatus.SERVICE_UNAVAILABLE; | ||
|
|
||
| logger.info("Health check completed with status: {}", status); | ||
|
|
||
| return ResponseEntity.status(httpStatus).body(healthStatus); | ||
| } | ||
| } |
98 changes: 47 additions & 51 deletions
98
src/main/java/com/iemr/common/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 |
|---|---|---|
| @@ -1,77 +1,73 @@ | ||
| /* | ||
| * 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/. | ||
| */ | ||
| * 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/. | ||
| */ | ||
| /** | ||
| * REST controller exposing application version and build metadata. | ||
| * <p> | ||
| * Provides the <code>/version</code> endpoint which returns the | ||
| * Git commit hash and build timestamp in a standardized JSON format. | ||
| * </p> | ||
| * | ||
| * @author Vaishnav Bhosale | ||
| */ | ||
| package com.iemr.common.controller.version; | ||
|
|
||
| import java.io.BufferedReader; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.util.Properties; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestMethod; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import com.iemr.common.utils.response.OutputResponse; | ||
|
|
||
| import io.swagger.v3.oas.annotations.Operation; | ||
|
|
||
|
|
||
| @RestController | ||
| public class VersionController { | ||
|
|
||
| private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName()); | ||
| private final Logger logger = | ||
| LoggerFactory.getLogger(this.getClass().getSimpleName()); | ||
|
|
||
| @Operation(summary = "Get version") | ||
| @RequestMapping(value = "/version", method = { RequestMethod.GET }) | ||
| public String versionInformation() { | ||
| OutputResponse output = new OutputResponse(); | ||
| try { | ||
| logger.info("version Controller Start"); | ||
| output.setResponse(readGitProperties()); | ||
| } catch (Exception e) { | ||
| output.setError(e); | ||
| } | ||
|
|
||
| logger.info("version Controller End"); | ||
| return output.toString(); | ||
| } | ||
| public VersionInfo versionInformation() { | ||
|
|
||
| private String readGitProperties() throws Exception { | ||
| ClassLoader classLoader = getClass().getClassLoader(); | ||
| InputStream inputStream = classLoader.getResourceAsStream("git.properties"); | ||
| Properties properties = new Properties(); | ||
|
|
||
| return readFromInputStream(inputStream); | ||
| } | ||
| try (InputStream is = getClass() | ||
| .getClassLoader() | ||
| .getResourceAsStream("git.properties")) { | ||
|
|
||
| private String readFromInputStream(InputStream inputStream) throws IOException { | ||
| StringBuilder resultStringBuilder = new StringBuilder(); | ||
| try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { | ||
| String line; | ||
| while ((line = br.readLine()) != null) { | ||
| resultStringBuilder.append(line).append("\n"); | ||
| if (is != null) { | ||
| properties.load(is); | ||
| } | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error("Error reading git.properties", e); | ||
| } | ||
| return resultStringBuilder.toString(); | ||
|
|
||
| return new VersionInfo( | ||
| properties.getProperty("git.commit.id.abbrev", "unknown"), | ||
| properties.getProperty("git.build.time", "unknown") | ||
| ); | ||
| } | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/iemr/common/controller/version/VersionInfo.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,46 @@ | ||
| /* | ||
| * 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/. | ||
| */ | ||
| /** | ||
| * DTO for exposing build and version metadata. | ||
| * | ||
| * @author vaishnavbhosale | ||
| */ | ||
| package com.iemr.common.controller.version; | ||
|
|
||
| public class VersionInfo { | ||
|
|
||
| private String commitHash; | ||
| private String buildTime; | ||
|
|
||
| public VersionInfo(String commitHash, String buildTime) { | ||
| this.commitHash = commitHash; | ||
| this.buildTime = buildTime; | ||
| } | ||
|
|
||
| public String getCommitHash() { | ||
| return commitHash; | ||
| } | ||
|
|
||
| public String getBuildTime() { | ||
| return buildTime; | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
src/main/java/com/iemr/common/service/health/HealthService.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,114 @@ | ||
| /* | ||
| * 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.common.service.health; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.ObjectProvider; // <--- VITAL IMPORT | ||
| import org.springframework.data.redis.connection.RedisConnection; | ||
| import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import javax.sql.DataSource; | ||
| import java.sql.Connection; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Health check service for Common-API. | ||
| * Verifies application liveness and dependency health (DB, Redis). | ||
| * | ||
| * @author vaishnavbhosale | ||
| */ | ||
| @Service | ||
| public class HealthService { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(HealthService.class); | ||
|
|
||
| private static final String COMPONENT_DATABASE = "database"; | ||
| private static final String COMPONENT_REDIS = "redis"; | ||
|
|
||
| private final DataSource dataSource; | ||
| private final RedisConnectionFactory redisConnectionFactory; | ||
|
|
||
| // --- CORRECT CONSTRUCTOR START --- | ||
| public HealthService(ObjectProvider<DataSource> dataSourceProvider, | ||
| ObjectProvider<RedisConnectionFactory> redisProvider) { | ||
| // This allows them to be null without crashing the app | ||
| this.dataSource = dataSourceProvider.getIfAvailable(); | ||
| this.redisConnectionFactory = redisProvider.getIfAvailable(); | ||
| } | ||
| // --- CORRECT CONSTRUCTOR END --- | ||
|
|
||
| public Map<String, Object> checkHealth() { | ||
| Map<String, Object> response = new HashMap<>(); | ||
| Map<String, String> components = new HashMap<>(); | ||
|
|
||
| boolean dbUp = checkDatabase(components); | ||
| boolean redisUp = checkRedis(components); | ||
|
|
||
| boolean overallUp = dbUp && redisUp; | ||
|
|
||
| response.put("status", overallUp ? "UP" : "DOWN"); | ||
| response.put("components", components); | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| private boolean checkDatabase(Map<String, String> components) { | ||
| if (dataSource == null) { | ||
| components.put(COMPONENT_DATABASE, "NOT_CONFIGURED"); | ||
| return true; | ||
| } | ||
|
|
||
| try (Connection connection = dataSource.getConnection(); | ||
| var statement = connection.createStatement()) { | ||
|
|
||
| statement.execute("SELECT 1"); | ||
| components.put(COMPONENT_DATABASE, "UP"); | ||
| return true; | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error("Database health check failed", e); | ||
| components.put(COMPONENT_DATABASE, "DOWN"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private boolean checkRedis(Map<String, String> components) { | ||
| if (redisConnectionFactory == null) { | ||
| components.put(COMPONENT_REDIS, "NOT_CONFIGURED"); | ||
| return true; | ||
| } | ||
|
|
||
| try (RedisConnection connection = redisConnectionFactory.getConnection()) { | ||
| connection.ping(); | ||
| components.put(COMPONENT_REDIS, "UP"); | ||
| return true; | ||
|
|
||
| } catch (Exception e) { | ||
| logger.error("Redis health check failed", e); | ||
| components.put(COMPONENT_REDIS, "DOWN"); | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
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.