1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
/**
* ============LICENSE_START=======================================================
* org.onap.aai
* ================================================================================
* Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.aai.aailog.filter;
import com.sun.jersey.api.client.ClientHandler;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.ClientRequest;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.filter.ClientFilter;
import org.onap.aai.aailog.logs.ServiceName;
import org.onap.logging.filter.base.Constants;
import org.onap.logging.filter.base.MDCSetup;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.slf4j.*;
import javax.ws.rs.core.MultivaluedMap;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;
public class RestControllerClientLoggingInterceptor extends ClientFilter {
private static final Logger logger = LoggerFactory.getLogger(RestControllerClientLoggingInterceptor.class);
private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
private final MDCSetup mdcSetup;
private final String partnerName;
public RestControllerClientLoggingInterceptor () {
mdcSetup = new MDCSetup();
partnerName = getPartnerName();
}
@Override
public ClientResponse handle(ClientRequest clientRequest) throws ClientHandlerException {
ClientResponse clientResponse = null;
pre(clientRequest);
// Call the next client handler in the filter chain
ClientHandler nextHandler = getNext();
if (nextHandler != null) {
clientResponse = nextHandler.handle(clientRequest);
}
if (clientResponse != null) {
post(clientResponse);
}
return clientResponse;
}
protected String getTargetServiceName(ClientRequest clientRequest) {
return getServiceName(clientRequest);
}
protected String getServiceName(ClientRequest clientRequest) {
String path = clientRequest.getURI().getRawPath();
return ServiceName.extractServiceName(path);
}
protected int getHttpStatusCode(ClientResponse response) {
return response.getStatus();
}
protected String getResponseCode(ClientResponse clientResponse) {
return String.valueOf(clientResponse.getStatus());
}
protected String getTargetEntity(ClientRequest ClientRequest) {
return Constants.DefaultValues.UNKNOWN_TARGET_ENTITY;
};
protected void pre(ClientRequest clientRequest) {
try {
setInvocationId(clientRequest);
setupMDC(clientRequest);
setupHeaders(clientRequest);
logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
} catch (Exception e) {
logger.warn("Error in RestControllerClientLoggingInterceptor pre", e.getMessage());
}
}
public void setInvocationId(ClientRequest clientRequest) {
String invocationId = null;
MultivaluedMap<String, Object> requestHeaders = clientRequest.getHeaders();
Object id = requestHeaders.get(ONAPLogConstants.Headers.INVOCATION_ID);
if (id != null) {
invocationId = (String)id;
}
requestHeaders.remove(ONAPLogConstants.Headers.INVOCATION_ID);
if (invocationId == null) {
invocationId = UUID.randomUUID().toString();
}
MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
}
protected void setupHeaders(ClientRequest clientRequest) {
String requestId = extractRequestID(clientRequest);
MultivaluedMap<String, Object> requestHeaders = clientRequest.getHeaders();
addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
Object requestIdObj = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
if (requestIdObj == null) {
addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
}
addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
if (partnerName != null && (!partnerName.isEmpty())){
addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, partnerName);
}
}
protected void setupMDC(ClientRequest clientRequest) {
MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(clientRequest));
MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
mdcSetup.setInvocationIdFromMDC();
if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) {
String targetEntity = getTargetEntity(clientRequest);
if (targetEntity != null) {
MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
} else {
MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
}
}
if (MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME) == null) {
MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, getServiceName(clientRequest));
}
mdcSetup.setServerFQDN();
}
protected String extractRequestID(ClientRequest clientRequest) {
String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
if (requestId == null || requestId.isEmpty()) {
MultivaluedMap<String, Object> requestHeaders = clientRequest.getHeaders();
Object requestIdObj = requestHeaders.getFirst(Constants.HttpHeaders.TRANSACTION_ID);
if (requestIdObj != null) {
requestId = (String)requestIdObj;
}
if ( requestId == null || requestId.isEmpty() ) {
requestId = UUID.randomUUID().toString();
}
mdcSetup.setLogTimestamp();
mdcSetup.setElapsedTimeInvokeTimestamp();
logger.warn("No value found in MDC when checking key {} value will be set to {}",
ONAPLogConstants.MDCs.REQUEST_ID, requestId);
MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
}
return requestId;
}
protected void post(ClientResponse clientResponse) {
try {
mdcSetup.setLogTimestamp();
mdcSetup.setElapsedTimeInvokeTimestamp();
mdcSetup.setResponseStatusCode(getHttpStatusCode(clientResponse));
mdcSetup.setResponseDescription(getHttpStatusCode(clientResponse));
MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(clientResponse));
logger.info(INVOKE_RETURN, "InvokeReturn");
mdcSetup.clearClientMDCs();
} catch (Exception e) {
logger.warn("Error in RestControllerClientLoggingInterceptor post", e.getMessage());
}
}
protected String getPartnerName() {
return mdcSetup.getProperty(Constants.Property.PARTNER_NAME);
}
protected void addHeader(MultivaluedMap<String, Object> requestHeaders, String headerName, String headerValue) {
requestHeaders.add(headerName, headerValue);
}
}
|