2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.logging.servlet;
19 import org.openecomp.sdc.logging.api.Logger;
20 import org.openecomp.sdc.logging.api.LoggerFactory;
23 import javax.servlet.*;
24 import javax.servlet.http.HttpServletRequest;
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.net.UnknownHostException;
28 import java.util.UUID;
29 import java.util.concurrent.atomic.AtomicLong;
34 * <p>Pushes information required by EELF onto MDC (Mapped Diagnostic Context).</p>
36 * <p>This is servlet filter that should be configured in <i>web.xml</i> to be used. Example:</p>
41 * <filter-name>LoggingServletFilter</filter-name>
42 * <filter-class>org.openecomp.sdc.logging.servlet.LoggingFilter</filter-class>
45 * <filter-mapping>
46 * <filter-name>LoggingServletFilter</filter-name>
47 * <url-pattern>/*</url-pattern>
48 * </filter-mapping>
55 public class LoggingFilter implements Filter {
57 // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
58 private static final HostAddressCache HOST_ADDRESS = new HostAddressCache();
59 private static final String UNKNOWN = "UNKNOWN";
61 private final static Logger LOGGER = LoggerFactory.getLogger(LoggingFilter.class);
63 public void destroy() {
66 public void doFilter(ServletRequest request, ServletResponse response,
67 FilterChain chain) throws IOException, ServletException {
73 MDC.put("RequestId", UUID.randomUUID().toString());
74 MDC.put("ServiceInstanceId", "N/A"); // not applicable
75 MDC.put("ServiceName", "ASDC");
76 MDC.put("InstanceUUID", "N/A");
78 // For some reason chooses IPv4 or IPv6 in a random way
79 MDC.put("RemoteHost", request.getRemoteHost());
81 InetAddress host = HOST_ADDRESS.get();
83 String ipAddress, hostName;
88 ipAddress = host.getHostAddress();
89 hostName = host.getHostName();
92 MDC.put("ServerIPAddress", ipAddress);
93 MDC.put("ServerFQDN", hostName);
95 if(request instanceof HttpServletRequest) {
96 String userName = ((HttpServletRequest) request).getHeader("USER_ID");
97 MDC.put("PartnerName", userName);
99 // TODO: Clarify what these stand for
100 // MDC.put("AlertSeverity", );
101 // MDC.put("Timer", );
103 chain.doFilter(request, response);
110 public void init(FilterConfig config) throws ServletException { }
112 private static class HostAddressCache {
114 private static final long REFRESH_TIME = 1000L;
116 private final AtomicLong lastUpdated = new AtomicLong(0L);
117 private InetAddress hostAddress;
119 public InetAddress get() {
121 long current = System.currentTimeMillis();
122 if (current - lastUpdated.get() > REFRESH_TIME) {
124 synchronized (this) {
127 lastUpdated.set(current); // set now to register the attempt even if failed
128 hostAddress = InetAddress.getLocalHost();
129 } catch (UnknownHostException e) {
130 LOGGER.error("Failed to retrieve local hostname for logging", e);