From cb1b7daace9f16ed54b3bc0b419e100f52320fde Mon Sep 17 00:00:00 2001 From: Michael Lando Date: Tue, 22 May 2018 20:43:41 +0300 Subject: fix swagger issue update the config servlt with mdc logs Change-Id: I0f0b9b4895ba16895ce5c11539904e1072a53782 Issue-ID: SDC-1321 Signed-off-by: Michael Lando --- .../org/openecomp/sdc/fe/impl/PluginStatusBL.java | 27 +++-- .../openecomp/sdc/fe/servlets/ConfigServlet.java | 116 +++++++-------------- .../openecomp/sdc/fe/servlets/FeProxyServlet.java | 51 --------- .../openecomp/sdc/fe/servlets/LoggingServlet.java | 102 ++++++++++++++++++ 4 files changed, 154 insertions(+), 142 deletions(-) create mode 100644 catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/LoggingServlet.java (limited to 'catalog-fe/src') diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java index 5ed4585e10..cdf965da63 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/impl/PluginStatusBL.java @@ -20,18 +20,24 @@ import com.google.gson.GsonBuilder; public class PluginStatusBL { - private static Logger log = LoggerFactory.getLogger(PluginStatusBL.class.getName()); - private static Gson gson = new GsonBuilder().setPrettyPrinting().create(); - private CloseableHttpClient client = null; - private PluginsConfiguration pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration(); + private static final Logger log = LoggerFactory.getLogger(PluginStatusBL.class.getName()); + private final Gson gson; + private final CloseableHttpClient client; + private final PluginsConfiguration pluginsConfiguration; private Integer connectionTimeout; + private RequestConfig requestConfig; public PluginStatusBL() { + this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration(); this.client = HttpClients.createDefault(); + this.gson = new GsonBuilder().setPrettyPrinting().create(); } public PluginStatusBL(CloseableHttpClient client) { + this.pluginsConfiguration = ConfigurationManager.getConfigurationManager().getPluginsConfiguration(); this.client = client; + + this.gson = new GsonBuilder().setPrettyPrinting().create(); } @@ -43,6 +49,10 @@ public class PluginStatusBL { } else { log.debug("The value returned from getConfig is {}", pluginsConfiguration); connectionTimeout = pluginsConfiguration.getConnectionTimeout(); + this.requestConfig = RequestConfig.custom() + .setSocketTimeout(connectionTimeout) + .setConnectTimeout(connectionTimeout) + .setConnectionRequestTimeout(connectionTimeout).build(); List availablePluginsList = new ArrayList<>(); @@ -58,17 +68,14 @@ public class PluginStatusBL { private boolean checkPluginAvailability(Plugin plugin) { boolean result = false; - + log.debug("sending head request to id:{} url:{}",plugin.getPluginId(),plugin.getPluginDiscoveryUrl()); HttpHead head = new HttpHead(plugin.getPluginDiscoveryUrl()); - RequestConfig requestConfig = RequestConfig.custom() - .setSocketTimeout(connectionTimeout) - .setConnectTimeout(connectionTimeout) - .setConnectionRequestTimeout(connectionTimeout).build(); - head.setConfig(requestConfig); + head.setConfig(this.requestConfig); try (CloseableHttpResponse response = this.client.execute(head)) { result = response != null && response.getStatusLine().getStatusCode() == 200; + log.debug("The plugin {} is online", plugin.getPluginId()); } catch (IOException e) { log.debug("The plugin {} is offline", plugin.getPluginId()); } diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/ConfigServlet.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/ConfigServlet.java index cbe5c43a45..437123618e 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/ConfigServlet.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/ConfigServlet.java @@ -20,113 +20,67 @@ package org.openecomp.sdc.fe.servlets; -import java.util.concurrent.TimeUnit; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.fe.config.FeEcompErrorManager; +import org.openecomp.sdc.fe.impl.PluginStatusBL; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.container.AsyncResponse; -import javax.ws.rs.container.Suspended; -import javax.ws.rs.container.TimeoutHandler; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; -import org.openecomp.sdc.common.api.ConfigurationSource; -import org.openecomp.sdc.common.api.Constants; -import org.openecomp.sdc.common.servlets.BasicServlet; -import org.openecomp.sdc.fe.config.Configuration; -import org.openecomp.sdc.fe.impl.PluginStatusBL; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Root resource (exposed at "/" path) */ @Path("/config") -public class ConfigServlet extends BasicServlet { - - private static final long serialVersionUID = 1L; - private static Logger log = LoggerFactory.getLogger(ConfigServlet.class.getName()); - - //@GET - //@Path("/get") - //@Produces(MediaType.APPLICATION_JSON) - public String getConfig(@Context final HttpServletRequest request) { - - String result = null; +public class ConfigServlet extends LoggingServlet { - ServletContext context = request.getSession().getServletContext(); + private static final Logger log = LoggerFactory.getLogger(ConfigServlet.class.getName()); - ConfigurationSource configurationSource = (ConfigurationSource) context - .getAttribute(Constants.CONFIGURATION_SOURCE_ATTR); - if (configurationSource != null) { - Configuration configuration = configurationSource.getAndWatchConfiguration(Configuration.class, null); - - if (configuration == null) { - log.warn("Configuration of type {} was not found", Configuration.class); - } - log.debug("{}", configuration); - if (log.isInfoEnabled()) { - log.info("Info level ENABLED..."); - } - log.info("The value returned from getConfig is {}", configuration); - - result = gson.toJson(configuration); - - } else { - log.warn("Source Configuration object was not initialized in the context."); - } - return result; + @GET + @Path("/ui/plugins") + @Produces(MediaType.APPLICATION_JSON) + public Response getPluginsConfiguration(@Context final HttpServletRequest request) { - } + try { + logFeRequest(request); - //@GET - //@Path("/asyncget") - public void asyncGet(@Suspended final AsyncResponse asyncResponse) { + ServletContext context = request.getSession().getServletContext(); - asyncResponse.setTimeoutHandler(new TimeoutHandler() { + PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT); - @Override - public void handleTimeout(AsyncResponse asyncResponse) { - asyncResponse.resume( - Response.status(Response.Status.SERVICE_UNAVAILABLE).entity("Operation time out.").build()); - } - }); - asyncResponse.setTimeout(3, TimeUnit.SECONDS); + String result = pluginStatusBL.checkPluginsListAvailability(); - new Thread(new Runnable() { - @Override - public void run() { - String result = veryExpensiveOperation(); - asyncResponse.resume(result); - } + Response response = Response.status(Status.OK).entity(result).build(); - private String veryExpensiveOperation() { + logFeResponse(request, response); - return "veryExpensiveOperation SUCCESS"; + return response; + } catch (Exception e) { + FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Response"); + log.error("Unexpected FE response logging error :", e); + return Response.status(Status.OK).entity(null).build(); + } - } - }).start(); } - @GET - @Path("/ui/plugins") - @Produces(MediaType.APPLICATION_JSON) - public Response getPluginsConfiguration(@Context final HttpServletRequest request) { - String result = null; - - ServletContext context = request.getSession().getServletContext(); - - PluginStatusBL pluginStatusBL = (PluginStatusBL) context.getAttribute(Constants.PLUGIN_BL_COMPONENT); - - result = pluginStatusBL.checkPluginsListAvailability(); - - return Response.status(Status.OK).entity(result).build(); - - } + protected void inHttpRequest(HttpServletRequest httpRequest) { + log.info("{} {} {}", httpRequest.getMethod(), httpRequest.getRequestURI(), httpRequest.getProtocol()); + } + + /** + * Extracted for purpose of clear method name, for logback %M parameter + * @param response http response + */ + protected void outHttpResponse(Response response) { + log.info("SC=\"{}\"", response.getStatus()); + } } diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java index 511592afe2..a8aa2069e1 100644 --- a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/FeProxyServlet.java @@ -47,21 +47,6 @@ public class FeProxyServlet extends SSLProxyServlet { private static final Logger log = LoggerFactory.getLogger(FeProxyServlet.class.getName()); private static Cache mdcDataCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build(); -// @Override -// public URI rewriteURI(HttpServletRequest request) { -// try { -// logFeRequest(request); -// } catch (Exception e) { -// FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Request"); -// log.error("Unexpected FE request logging error :", e); -// } -// String originalUrl = request.getRequestURL().toString(); -// String redirectedUrl = getModifiedUrl(request); -// -// log.debug("FeProxyServlet Redirecting request from: {} , to: {}", originalUrl, redirectedUrl); -// -// return URI.create(redirectedUrl); -// } @Override protected String rewriteTarget(HttpServletRequest request) { @@ -79,17 +64,6 @@ public class FeProxyServlet extends SSLProxyServlet { return redirectedUrl; } -// @Override -// protected void onResponseSuccess(HttpServletRequest request, HttpServletResponse response, Response proxyResponse) { -// try { -// logFeResponse(request, proxyResponse); -// } catch (Exception e) { -// FeEcompErrorManager.getInstance().logFeHttpLoggingError("FE Response"); -// log.error("Unexpected FE response logging error :", e); -// } -// super.onResponseSuccess(request, response, proxyResponse); -// } -//protected void onProxyResponseSuccess(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Response serverResponse) { @Override protected void onProxyResponseSuccess(HttpServletRequest request, HttpServletResponse proxyResponse, Response response) { try { @@ -209,31 +183,6 @@ public class FeProxyServlet extends SSLProxyServlet { -// String scheme = config.getBeProtocol(); -// String uri = request.getRequestURI().toString(); -// StringBuilder url = new StringBuilder(); -// url.append(scheme).append("://").append(config.getBeHost()); -// url.append(":"); -// if (config.getBeProtocol().equals(BeProtocol.HTTP.getProtocolName())) { -// url.append(config.getBeHttpPort()); -// } else { -// url.append(config.getBeSslPort()); -// } -// url.append(uri); -// String queryString = request.getQueryString(); // d=789 -// if (queryString != null) { -// url.append("?").append(queryString); -// } -// -// String redirectedUrl = url.toString(); -// String onboardingForwardContext = config.getOnboardingForwardContext(); -// if (onboardingForwardContext == null || onboardingForwardContext.isEmpty()) { -// onboardingForwardContext = "/onboarding-api"; -// } -// redirectedUrl = redirectedUrl.replace("/sdc1/feProxy/dcae-api", "/dcae"); -// redirectedUrl = redirectedUrl.replace("/sdc1/feProxy/onboarding-api", onboardingForwardContext); -// redirectedUrl = redirectedUrl.replace("/sdc1/feProxy", "/sdc2"); -// return redirectedUrl; } diff --git a/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/LoggingServlet.java b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/LoggingServlet.java new file mode 100644 index 0000000000..af07810f87 --- /dev/null +++ b/catalog-fe/src/main/java/org/openecomp/sdc/fe/servlets/LoggingServlet.java @@ -0,0 +1,102 @@ +package org.openecomp.sdc.fe.servlets; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import org.openecomp.sdc.common.api.Constants; +import org.openecomp.sdc.common.servlets.BasicServlet; +import org.openecomp.sdc.fe.mdc.MdcData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.MDC; + + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.core.Response; +import java.util.concurrent.TimeUnit; + +public abstract class LoggingServlet extends BasicServlet { + + private static final Logger log = LoggerFactory.getLogger(BasicServlet.class.getName()); + private static final Cache mdcDataCache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).build(); + + /** + * log incoming requests + * @param httpRequest the http request + */ + protected void logFeRequest(HttpServletRequest httpRequest) { + + MDC.clear(); + + Long transactionStartTime = System.currentTimeMillis(); + String uuid = httpRequest.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER); + String serviceInstanceID = httpRequest.getHeader(Constants.X_ECOMP_SERVICE_ID_HEADER); + + if (uuid != null && uuid.length() > 0) { + String userId = httpRequest.getHeader(Constants.USER_ID_HEADER); + + String remoteAddr = httpRequest.getRemoteAddr(); + String localAddr = httpRequest.getLocalAddr(); + + mdcDataCache.put(uuid, new MdcData(serviceInstanceID, userId, remoteAddr, localAddr, transactionStartTime)); + + updateMdc(uuid, serviceInstanceID, userId, remoteAddr, localAddr, null); + } + inHttpRequest(httpRequest); + } + + /** + * log response + * @param request orig request + * @param response returned response + */ + protected void logFeResponse(HttpServletRequest request, Response response) { + String uuid = request.getHeader(Constants.X_ECOMP_REQUEST_ID_HEADER); + String transactionRoundTime = null; + + if (uuid != null) { + MdcData mdcData = mdcDataCache.getIfPresent(uuid); + if (mdcData != null) { + Long transactionStartTime = mdcData.getTransactionStartTime(); + if (transactionStartTime != null) {// should'n ever be null, but + // just to be defensive + transactionRoundTime = Long.toString(System.currentTimeMillis() - transactionStartTime); + } + updateMdc(uuid, mdcData.getServiceInstanceID(), mdcData.getUserId(), mdcData.getRemoteAddr(), mdcData.getLocalAddr(), transactionRoundTime); + } + } + outHttpResponse(response); + + MDC.clear(); + } + + /** + * Extracted for purpose of clear method name, for logback %M parameter + * @param httpRequest http request + */ + protected abstract void inHttpRequest(HttpServletRequest httpRequest) ; + + + /** + * Extracted for purpose of clear method name, for logback %M parameter + * @param response http response + */ + protected abstract void outHttpResponse(Response response); + + /** + * update mdc with values from the request + * @param uuid service uuid + * @param serviceInstanceID serviceInstanceID + * @param userId userId + * @param remoteAddr remoteAddr + * @param localAddr localAddr + * @param transactionStartTime transactionStartTime + */ + private void updateMdc(String uuid, String serviceInstanceID, String userId, String remoteAddr, String localAddr, String transactionStartTime) { + MDC.put("uuid", uuid); + MDC.put("serviceInstanceID", serviceInstanceID); + MDC.put("userId", userId); + MDC.put("remoteAddr", remoteAddr); + MDC.put("localAddr", localAddr); + MDC.put("timer", transactionStartTime); + } +} -- cgit 1.2.3-korg