diff --git a/.github/workflows/publish-deploy.yml b/.github/workflows/publish-deploy.yml index a6857fe..7b16304 100644 --- a/.github/workflows/publish-deploy.yml +++ b/.github/workflows/publish-deploy.yml @@ -9,7 +9,7 @@ jobs: publish: uses: "epimorphics/github-workflows/.github/workflows/publish.yml@reusable" secrets: - # Repostory specific + # Repository specific aws_access_key_id: "${{ secrets.BUILD_HMLR_AWS_ACCESS_KEY_ID }}" aws_secret_access_key: "${{ secrets.BUILD_HMLR_AWS_SECRET_ACCESS_KEY }}" # Fixed @@ -18,7 +18,7 @@ jobs: needs: "publish" uses: "epimorphics/github-workflows/.github/workflows/deploy.yml@reusable" with: - # Repostory specific + # Repository specific ansible_repo: epimorphics/hmlr-ansible-deployment ansible_repo_ref: master host_prefix: hmlr @@ -27,7 +27,7 @@ jobs: key: "${{ needs.publish.outputs.key }}" tag: "${{ needs.publish.outputs.tag }}" secrets: - # Repostory specific + # Repository specific ansible_vault_password: "${{ secrets.HMLR_ANSIBLE_VAULT_PASSWORD }}" aws_access_key_id: "${{ secrets.BUILD_HMLR_AWS_ACCESS_KEY_ID }}" aws_secret_access_key: "${{ secrets.BUILD_HMLR_AWS_SECRET_ACCESS_KEY }}" diff --git a/pom.xml b/pom.xml index 2f0b8c6..efaf6d1 100644 --- a/pom.xml +++ b/pom.xml @@ -17,7 +17,7 @@ UTF-8 1.7.5 1.2.6 - 0.1.5 + 6.6 @@ -118,21 +118,21 @@ org.apache.tomcat.embed tomcat-embed-core 7.0.107 - provided + org.apache.tomcat.embed tomcat-embed-logging-log4j 7.0.64 - provided + org.apache.tomcat.embed tomcat-embed-jasper 7.0.64 - provided + @@ -175,23 +175,11 @@ - ch.qos.logback.contrib - logback-json-classic + net.logstash.logback + logstash-logback-encoder ${logback-json.version} - - ch.qos.logback.contrib - logback-jackson - ${logback-json.version} - - - - com.fasterxml.jackson.core - jackson-databind - 2.9.10.8 - - diff --git a/src/main/java/com/epimorphics/standardReports/webapi/LogRequestFilter.java b/src/main/java/com/epimorphics/standardReports/webapi/LogRequestFilter.java new file mode 100644 index 0000000..85da314 --- /dev/null +++ b/src/main/java/com/epimorphics/standardReports/webapi/LogRequestFilter.java @@ -0,0 +1,81 @@ +package com.epimorphics.standardReports.webapi; + +import java.io.IOException; +import java.util.concurrent.atomic.AtomicLong; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.ws.rs.core.Response.Status; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.epimorphics.appbase.webapi.WebApiException; +import com.epimorphics.util.NameUtils; +import org.slf4j.MDC; + +/** + * A Filter that can be added to filter chain to log all incoming requests and + * the corresponding response (with response code and execution time). Assigns a + * simple request number to each request and includes that in the response headers + * for diagnosis. Not robust against restarts but easier to work with than UUIDs. + */ +public class LogRequestFilter implements Filter { + public static final String TRANSACTION_ATTRIBUTE = "transaction"; + public static final String START_TIME_ATTRIBUTE = "startTime"; + public static final String RESPONSE_ID_HEADER = "X-Response-ID"; + public static final String REQUEST_ID_HEADER = "X-Request-ID"; + + static final Logger log = LoggerFactory.getLogger( LogRequestFilter.class ); + + protected static AtomicLong transactionCount = new AtomicLong(0); + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, + FilterChain chain) throws IOException, ServletException { + HttpServletRequest httpRequest = (HttpServletRequest)request; + HttpServletResponse httpResponse = (HttpServletResponse)response; + String path = httpRequest.getRequestURI(); + String query = httpRequest.getQueryString(); + String requestID = httpRequest.getHeader(REQUEST_ID_HEADER); + if (requestID == null || requestID.isEmpty()) { + requestID = Long.toString(transactionCount.incrementAndGet()); + } + MDC.put("request_id", requestID); + long start = System.currentTimeMillis(); + + String fullpath = path + (query == null ? "" : ("?" + query)); + MDC.put("path", fullpath); + log.info( String.format("Request [%s] : %s", requestID, fullpath) ); + httpResponse.addHeader(RESPONSE_ID_HEADER, requestID); + chain.doFilter(request, response); + log.info( String.format("Response [%s] : %d (%s)", requestID, httpResponse.getStatus(), + NameUtils.formatDuration(System.currentTimeMillis() - start)) ); + } + + @Override + public void destroy() { + } + + public static WebApiException badRequestException(String message) { + log.warn("Bad request: " + message); + return new WebApiException(Status.BAD_REQUEST, message); + } + + public static WebApiException serverErrorException(String message) { + log.warn("Server error: " + message); + return new WebApiException(Status.INTERNAL_SERVER_ERROR, message); + } + +} + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 44b3695..fa0663a 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -1,15 +1,25 @@ - - - - - true - yyyy-MM-dd'T'HH:mm:ss.SSSZ - + + + X-B3-SpanId + X-B3-TraceId + X-Span-Export + spanExportable + UTC + + ts + version + + + 30 + 2048 + 20 + true + - + diff --git a/src/main/webapp/WEB-INF/app.conf b/src/main/webapp/WEB-INF/app.conf index 29913bc..b7a3201 100644 --- a/src/main/webapp/WEB-INF/app.conf +++ b/src/main/webapp/WEB-INF/app.conf @@ -31,8 +31,8 @@ reportManager.workDir = /tmp/sr-workspace # Data source ssource = com.epimorphics.appbase.data.impl.RemoteSparqlSource -ssource.endpoint = http://localhost:3030/landregistry_to/query -#ssource.endpoint = http://landregistry.data.gov.uk/landregistry/query +#ssource.endpoint = http://localhost:3030/landregistry_to/query +ssource.endpoint = http://landregistry.data.gov.uk/landregistry/query ssource.connectTimeout = 10000 ssource.remoteTimeout = 3600000 diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 8ec7a34..ddcc4ec 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -64,7 +64,7 @@ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns LogRequestFilter - com.epimorphics.simpleAPI.webapi.LogRequestFilter + com.epimorphics.standardReports.webapi.LogRequestFilter