2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.onap.logging.filter.base;
23 import java.time.ZoneOffset;
24 import java.time.ZonedDateTime;
25 import java.time.format.DateTimeFormatter;
26 import java.util.UUID;
27 import org.onap.logging.ref.slf4j.ONAPLogConstants;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import org.slf4j.Marker;
32 import org.slf4j.MarkerFactory;
34 public abstract class AbstractMetricLogFilter<Request, Response, RequestHeaders> extends MDCSetup {
35 protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class);
36 private final String partnerName;
37 private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
39 public AbstractMetricLogFilter() {
40 partnerName = getPartnerName();
43 protected abstract void addHeader(RequestHeaders requestHeaders, String headerName, String headerValue);
45 protected abstract String getTargetServiceName(Request request);
47 protected abstract String getServiceName(Request request);
49 protected abstract int getHttpStatusCode(Response response);
51 protected abstract String getResponseCode(Response response);
53 protected abstract String getTargetEntity(Request request);
55 protected void pre(Request request, RequestHeaders requestHeaders) {
58 setupHeaders(request, requestHeaders);
59 additionalPre(request, requestHeaders);
60 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
61 } catch (Exception e) {
62 logger.warn("Error in AbstractMetricLogFilter pre", e);
66 protected void additionalPre(Request request, RequestHeaders requestHeaders) {
67 // override to add application specific logic
70 protected void setupHeaders(Request clientRequest, RequestHeaders requestHeaders) {
71 String requestId = extractRequestID();
72 String invocationId = UUID.randomUUID().toString();
73 addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
74 addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
75 addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
76 addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
77 addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, partnerName);
78 logger.info("Setting X-InvocationID header for outgoing request: {}", invocationId);
79 addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, invocationId);
83 protected void setupMDC(Request request) {
84 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
85 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
86 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(request));
87 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
88 setInvocationIdFromMDC();
90 if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) {
91 String targetEntity = getTargetEntity(request);
92 if (targetEntity != null) {
93 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
95 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
99 if (MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME) == null) {
100 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, getServiceName(request));
105 protected String extractRequestID() {
106 String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
107 if (requestId == null || requestId.isEmpty()) {
108 requestId = UUID.randomUUID().toString();
110 setElapsedTimeInvokeTimestamp();
111 logger.warn("No value found in MDC when checking key {} value will be set to {}",
112 ONAPLogConstants.MDCs.REQUEST_ID, requestId);
113 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
118 protected void post(Request request, Response response) {
121 setElapsedTimeInvokeTimestamp();
122 setResponseStatusCode(getHttpStatusCode(response));
123 setResponseDescription(getHttpStatusCode(response));
124 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
125 additionalPost(request, response);
126 logger.info(INVOKE_RETURN, "InvokeReturn");
128 } catch (Exception e) {
129 logger.warn("Error in AbstractMetricLogFilter post", e);
133 protected void additionalPost(Request request, Response response) {
134 // override to add application specific logic
137 protected String getPartnerName() {
138 return getProperty(Constants.Property.PARTNER_NAME);