Skip to content

Commit

Permalink
log the full version name of CDA
Browse files Browse the repository at this point in the history
this can be collected by dashboarding in order to compare metrics between version changes
  • Loading branch information
adamkorynta authored and MikeNeilson committed Nov 7, 2023
1 parent 33caa6c commit 16bca49
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cwms-data-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ dependencies {
tomcatLibs "org.apache.tomcat.embed:tomcat-embed-jasper:$tomcatVersion"
tomcatLibs "org.apache.tomcat:tomcat-juli:$tomcatVersion"
tomcatLibs "org.apache.tomcat:tomcat-jdbc:$tomcatVersion"
tomcatLibs "com.google.flogger:flogger:0.7.4"
tomcatLibs "com.google.flogger:flogger-system-backend:0.7.4"

testImplementation "com.github.h-thurow:tomcat8jndi:1.0.0"

Expand Down Expand Up @@ -180,6 +182,10 @@ war {
from "$buildDir/extra"
from "src/resources/"

manifest {
attributes "build-version": project.version
}

doLast {
println(war.archiveFileName.toString())
println("::set-output name=WARFILE::${project.name}-${project.version}.war")
Expand Down
20 changes: 19 additions & 1 deletion cwms-data-api/src/main/java/cwms/cda/ApiServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,17 @@
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.DateTimeException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.jar.Manifest;

import static io.javalin.apibuilder.ApiBuilder.*;

Expand Down Expand Up @@ -178,6 +183,7 @@ public void destroy() {

@Override
public void init(ServletConfig config) throws ServletException {
logger.atInfo().log("Initializing CWMS Data API Version: " + obtainFullVersion(config));
metrics = (MetricRegistry)config.getServletContext()
.getAttribute(MetricsServlet.METRICS_REGISTRY);
totalRequests = metrics.meter("cwms.dataapi.total_requests");
Expand All @@ -187,7 +193,6 @@ public void init(ServletConfig config) throws ServletException {
@SuppressWarnings({"java:S125","java:S2095"}) // closed in destroy handler
@Override
public void init() {
logger.atInfo().log("Initializing API");
JavalinValidation.register(UnitSystem.class, UnitSystem::systemFor);
JavalinValidation.register(JooqDao.DeleteMethod.class, Controllers::getDeleteMethod);
ObjectMapper om = new ObjectMapper();
Expand Down Expand Up @@ -301,6 +306,19 @@ public void init() {
.javalinServlet();
}

private String obtainFullVersion(ServletConfig servletConfig) throws ServletException {
String relativeWARPath = "/META-INF/MANIFEST.MF";
String absoluteDiskPath = servletConfig.getServletContext().getRealPath(relativeWARPath);
Path path = Paths.get(absoluteDiskPath);

try (InputStream inputStream = Files.newInputStream(path)) {
Manifest manifest = new Manifest(inputStream);
return manifest.getMainAttributes().getValue("build-version");
} catch (IOException e) {
throw new ServletException("Error obtaining servlet version", e);
}
}

private CdaAccessManager buildAccessManager(String provider) {
try {
AccessManagers ams = new AccessManagers();
Expand Down

0 comments on commit 16bca49

Please sign in to comment.