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.core.logging.servlet;
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;
30 import javax.servlet.Filter;
31 import javax.servlet.FilterChain;
32 import javax.servlet.FilterConfig;
33 import javax.servlet.ServletException;
34 import javax.servlet.ServletRequest;
35 import javax.servlet.ServletResponse;
38 * <p>Pushes information required by EELF onto MDC (Mapped Diagnostic Context).</p> <p>This is
39 * servlet filter that should be configured in <i>web.xml</i> to be used. Example:</p>
42 * <filter-name>LoggingServletFilter</filter-name>
43 * <filter-class>LoggingFilter</filter-class>
45 * <filter-mapping>
46 * <filter-name>LoggingServletFilter</filter-name>
47 * <url-pattern>/*</url-pattern>
48 * </filter-mapping>
51 public class LoggingFilter implements Filter {
53 // should be cashed to avoid low-level call, but with a timeout to account for IP or FQDN changes
54 private static final HostAddressCache HOST_ADDRESS = new HostAddressCache();
55 private static final String UNKNOWN = "UNKNOWN";
57 public void destroy() {
63 * @param request the request
65 public void doFilter(ServletRequest request, ServletResponse response,
66 FilterChain chain) throws IOException, ServletException {
72 MDC.put("RequestId", UUID.randomUUID().toString());
73 MDC.put("ServiceInstanceId", "N/A"); // not applicable
74 MDC.put("ServiceName", "SDC");
75 MDC.put("InstanceUUID", "N/A");
77 // For some reason chooses IPv4 or IPv6 in a random way
78 MDC.put("RemoteHost", request.getRemoteHost());
80 InetAddress host = HOST_ADDRESS.get();
88 ipAddress = host.getHostAddress();
89 hostName = host.getHostName();
92 MDC.put("ServerIPAddress", ipAddress);
93 MDC.put("ServerFQDN", hostName);
95 // TODO: Clarify what these stand for
96 // MDC.put("AlertSeverity", );
97 // MDC.put("Timer", );
99 chain.doFilter(request, response);
106 public void init(FilterConfig config) throws ServletException {
109 private static class HostAddressCache {
111 private static final long REFRESH_TIME = 1000L;
113 private AtomicLong lastUpdated = new AtomicLong(0L);
114 private InetAddress hostAddress;
116 public InetAddress get() {
118 long current = System.currentTimeMillis();
119 if (current - lastUpdated.get() > REFRESH_TIME) {
121 synchronized (this) {
124 lastUpdated.set(current); // set now to register the attempt even if failed
125 hostAddress = InetAddress.getLocalHost();
126 } catch (UnknownHostException unknownHostException) {