2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.logging.servlet;
23 import org.omg.CORBA.Request;
26 import javax.servlet.*;
27 import javax.servlet.http.HttpServletRequest;
28 import java.io.IOException;
29 import java.net.InetAddress;
30 import java.net.UnknownHostException;
31 import java.util.UUID;
32 import java.util.concurrent.atomic.AtomicLong;
37 * <p>Pushes information required by EELF onto MDC (Mapped Diagnostic Context).</p>
39 * <p>This is servlet filter that should be configured in <i>web.xml</i> to be used. Example:</p>
44 * <filter-name>LoggingServletFilter</filter-name>
45 * <filter-class>org.openecomp.sdc.logging.servlet.LoggingFilter</filter-class>
48 * <filter-mapping>
49 * <filter-name>LoggingServletFilter</filter-name>
50 * <url-pattern>/*</url-pattern>
51 * </filter-mapping>
58 public class LoggingFilter implements Filter {
60 // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
61 private static final HostAddressCache HOST_ADDRESS = new HostAddressCache();
62 private static final String UNKNOWN = "UNKNOWN";
64 public void destroy() {
67 public void doFilter(ServletRequest request, ServletResponse response,
68 FilterChain chain) throws IOException, ServletException {
74 MDC.put("RequestId", UUID.randomUUID().toString());
75 MDC.put("ServiceInstanceId", "N/A"); // not applicable
76 MDC.put("ServiceName", "ASDC");
77 MDC.put("InstanceUUID", "N/A");
79 // For some reason chooses IPv4 or IPv6 in a random way
80 MDC.put("RemoteHost", request.getRemoteHost());
82 InetAddress host = HOST_ADDRESS.get();
84 String ipAddress, hostName;
89 ipAddress = host.getHostAddress();
90 hostName = host.getHostName();
93 MDC.put("ServerIPAddress", ipAddress);
94 MDC.put("ServerFQDN", hostName);
96 if(request instanceof HttpServletRequest) {
97 String userName = ((HttpServletRequest) request).getHeader("USER_ID");
98 MDC.put("PartnerName", userName);
100 // TODO: Clarify what these stand for
101 // MDC.put("AlertSeverity", );
102 // MDC.put("Timer", );
104 chain.doFilter(request, response);
111 public void init(FilterConfig config) throws ServletException { }
113 private static class HostAddressCache {
115 private static final long REFRESH_TIME = 1000L;
117 private AtomicLong lastUpdated = new AtomicLong(0L);
118 private InetAddress hostAddress;
120 public InetAddress get() {
122 long current = System.currentTimeMillis();
123 if (current - lastUpdated.get() > REFRESH_TIME) {
125 synchronized (this) {
128 lastUpdated.set(current); // set now to register the attempt even if failed
129 hostAddress = InetAddress.getLocalHost();
130 } catch (UnknownHostException e) {