Skip to content

Commit 4b44045

Browse files
Add advance health check for database (#361)
* chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * chore(swagger): automate swagger sync to amrit-docs * fix(swagger): update the workflow and fix the running issue * fix(swagger): fix the swagger json workflow * chore(swagger): add fixed branch name in workflow * chore(ci): prevent multiple swagger sync PRs by using fixed branch * chore(swagger): add Dev/UAT/Demo servers to OpenAPI config * chore(swagger): avoid default server URLs * chore(swagger): remove field injection and inject URLs into OpenAPI bean * feat(health,version): update version and health endpoints and add advance check for database * fix(health): normalize severity and fix slow query false positives * fix(health): avoid false CRITICAL on single long-running MySQL transaction * fix(health): enforce 3s DB connection timeout via HikariCP
1 parent a12c8ad commit 4b44045

4 files changed

Lines changed: 415 additions & 63 deletions

File tree

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,32 @@
526526
<build>
527527
<finalName>${artifactId}-${version}</finalName>
528528
<plugins>
529+
<plugin>
530+
<groupId>io.github.git-commit-id</groupId>
531+
<artifactId>git-commit-id-maven-plugin</artifactId>
532+
<version>9.0.2</version>
533+
<executions>
534+
<execution>
535+
<id>get-the-git-infos</id>
536+
<goals>
537+
<goal>revision</goal>
538+
</goals>
539+
<phase>initialize</phase>
540+
</execution>
541+
</executions>
542+
<configuration>
543+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
544+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
545+
<includeOnlyProperties>
546+
<property>^git.branch$</property>
547+
<property>^git.commit.id.abbrev$</property>
548+
<property>^git.build.version$</property>
549+
<property>^git.build.time$</property>
550+
</includeOnlyProperties>
551+
<failOnNoGitDirectory>false</failOnNoGitDirectory>
552+
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
553+
</configuration>
554+
</plugin>
529555
<plugin>
530556
<groupId>org.apache.maven.plugins</groupId>
531557
<artifactId>maven-jar-plugin</artifactId>

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

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,25 @@
2222
/**
2323
* REST controller exposing application version and build metadata.
2424
* <p>
25-
* Provides the <code>/version</code> endpoint which returns the
26-
* Git commit hash and build timestamp in a standardized JSON format.
25+
* Provides the <code>/version</code> endpoint which returns Git metadata
26+
* in a standardized JSON format consistent across all AMRIT APIs.
2727
* </p>
2828
*
2929
* @author Vaishnav Bhosale
3030
*/
3131
package com.iemr.common.controller.version;
3232

3333
import java.io.InputStream;
34+
import java.io.IOException;
35+
import java.util.LinkedHashMap;
36+
import java.util.Map;
3437
import java.util.Properties;
3538

3639
import org.slf4j.Logger;
3740
import org.slf4j.LoggerFactory;
38-
import org.springframework.web.bind.annotation.RequestMapping;
39-
import org.springframework.web.bind.annotation.RequestMethod;
41+
import org.springframework.http.MediaType;
42+
import org.springframework.http.ResponseEntity;
43+
import org.springframework.web.bind.annotation.GetMapping;
4044
import org.springframework.web.bind.annotation.RestController;
4145

4246
import io.swagger.v3.oas.annotations.Operation;
@@ -46,28 +50,39 @@ public class VersionController {
4650

4751
private final Logger logger =
4852
LoggerFactory.getLogger(this.getClass().getSimpleName());
53+
54+
private static final String UNKNOWN_VALUE = "unknown";
4955

50-
@Operation(summary = "Get version")
51-
@RequestMapping(value = "/version", method = { RequestMethod.GET })
52-
public VersionInfo versionInformation() {
56+
@Operation(summary = "Get version information")
57+
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
58+
public ResponseEntity<Map<String, String>> versionInformation() {
59+
Map<String, String> response = new LinkedHashMap<>();
60+
try {
61+
logger.info("version Controller Start");
62+
Properties gitProperties = loadGitProperties();
63+
response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE));
64+
response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE));
65+
response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE));
66+
response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE));
67+
} catch (Exception e) {
68+
logger.error("Failed to load version information", e);
69+
response.put("buildTimestamp", UNKNOWN_VALUE);
70+
response.put("version", UNKNOWN_VALUE);
71+
response.put("branch", UNKNOWN_VALUE);
72+
response.put("commitHash", UNKNOWN_VALUE);
73+
}
74+
logger.info("version Controller End");
75+
return ResponseEntity.ok(response);
76+
}
5377

78+
private Properties loadGitProperties() throws IOException {
5479
Properties properties = new Properties();
55-
56-
try (InputStream is = getClass()
57-
.getClassLoader()
80+
try (InputStream input = getClass().getClassLoader()
5881
.getResourceAsStream("git.properties")) {
59-
60-
if (is != null) {
61-
properties.load(is);
82+
if (input != null) {
83+
properties.load(input);
6284
}
63-
64-
} catch (Exception e) {
65-
logger.error("Error reading git.properties", e);
6685
}
67-
68-
return new VersionInfo(
69-
properties.getProperty("git.commit.id.abbrev", "unknown"),
70-
properties.getProperty("git.build.time", "unknown")
71-
);
86+
return properties;
7287
}
7388
}

0 commit comments

Comments
 (0)