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;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import org.slf4j.Marker;
33 import org.slf4j.MarkerFactory;
35 public abstract class AbstractMetricLogFilter<Request, Response, RequestHeaders> extends MDCSetup {
36 protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class);
37 private final String partnerName;
38 private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
40 public AbstractMetricLogFilter() {
41 partnerName = getPartnerName();
44 protected abstract void addHeader(RequestHeaders requestHeaders, String headerName, String headerValue);
46 protected abstract String getTargetServiceName(Request request);
48 protected abstract String getServiceName(Request request);
50 protected abstract int getHttpStatusCode(Response response);
52 protected abstract String getResponseCode(Response response);
54 protected abstract String getTargetEntity(Request request);
56 protected void pre(Request request, RequestHeaders requestHeaders) {
59 setupHeaders(request, requestHeaders);
60 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
61 } catch (Exception e) {
62 logger.warn("Error in AbstractMetricLogFilter pre", e);
66 protected void setupHeaders(Request clientRequest, RequestHeaders requestHeaders) {
67 String requestId = extractRequestID();
68 addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
69 addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
70 addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
71 addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
72 addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, MDC.get(ONAPLogConstants.MDCs.INVOCATION_ID));
73 addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, partnerName);
76 protected void setupMDC(Request request) {
77 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
78 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
79 // setup time stamp defaults
81 setElapsedTimeInvokeTimestamp();
82 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(request));
83 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
84 setInvocationIdFromMDC();
86 if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) {
87 String targetEntity = getTargetEntity(request);
88 if (targetEntity != null) {
89 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
91 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
95 if (MDC.get(ONAPLogConstants.MDCs.SERVICE_NAME) == null) {
96 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, getServiceName(request));
101 protected String extractRequestID() {
102 String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
103 if (requestId == null || requestId.isEmpty()) {
104 requestId = UUID.randomUUID().toString();
106 setElapsedTimeInvokeTimestamp();
107 logger.warn("No value found in MDC when checking key {} value will be set to {}",
108 ONAPLogConstants.MDCs.REQUEST_ID, requestId);
109 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
114 protected void post(Request request, Response response) {
117 setElapsedTimeInvokeTimestamp();
118 setResponseStatusCode(getHttpStatusCode(response));
119 setResponseDescription(getHttpStatusCode(response));
120 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
121 logger.info(INVOKE_RETURN, "InvokeReturn");
123 } catch (Exception e) {
124 logger.warn("Error in AbstractMetricLogFilter post", e);
128 protected String getPartnerName() {
129 return getProperty(Constants.Property.PARTNER_NAME);
132 protected void logInvoke() {
133 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");