metric log updates and util fix
[logging-analytics.git] / reference / logging-filter / logging-filter-base / src / main / java / org / onap / logging / filter / base / AbstractBaseMetricLogFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - Logging
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.logging.filter.base;
22
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;
30 import org.slf4j.MDC;
31 import org.slf4j.Marker;
32 import org.slf4j.MarkerFactory;
33
34 public abstract class AbstractBaseMetricLogFilter<Request, Response> extends MDCSetup {
35     protected static final Logger logger = LoggerFactory.getLogger(AbstractBaseMetricLogFilter.class);
36     protected final String partnerName;
37     protected static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
38
39     public AbstractBaseMetricLogFilter() {
40         partnerName = getPartnerName();
41     }
42
43     protected abstract String getTargetServiceName(Request request);
44
45     protected abstract int getHttpStatusCode(Response response);
46
47     protected abstract String getResponseCode(Response response);
48
49     protected abstract String getTargetEntity(Request request);
50
51     protected void pre(Request request) {
52         try {
53             setupMDC(request);
54             extractRequestID();
55             setInvocationId();
56             additionalPre(request);
57             logRequest();
58         } catch (Exception e) {
59             logger.warn("Error in AbstractBaseMetricLogFilter pre", e);
60         }
61     }
62
63     protected void additionalPre(Request request) {
64         // override to add application specific logic
65     }
66
67     protected String setInvocationId() {
68         String invocationId = UUID.randomUUID().toString();
69         MDC.put(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID, invocationId);
70         MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
71         return invocationId;
72     }
73
74     protected void setupMDC(Request request) {
75         MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
76                 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
77         MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(request));
78         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
79
80         if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) {
81             String targetEntity = getTargetEntity(request);
82             if (targetEntity != null) {
83                 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
84             } else {
85                 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
86             }
87         }
88         setServerFQDN();
89     }
90
91     protected String extractRequestID() {
92         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
93         if (requestId == null || requestId.isEmpty()) {
94             requestId = UUID.randomUUID().toString();
95             setLogTimestamp();
96             setElapsedTimeInvokeTimestamp();
97             logger.trace("No value found in MDC when checking key {} value will be set to {}",
98                     ONAPLogConstants.MDCs.REQUEST_ID, requestId);
99             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
100         }
101         return requestId;
102     }
103
104     protected void post(Request request, Response response) {
105         try {
106             setLogTimestamp();
107             setElapsedTimeInvokeTimestamp();
108             setResponseStatusCode(getHttpStatusCode(response));
109             setResponseDescription(getHttpStatusCode(response));
110             MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
111             additionalPost(request, response);
112             logResponse();
113             clearClientMDCs();
114         } catch (Exception e) {
115             logger.warn("Error in AbstractBaseMetricLogFilter post", e);
116         }
117     }
118
119     protected void additionalPost(Request request, Response response) {
120         // override to add application specific logic
121     }
122
123     protected String getPartnerName() {
124         return getProperty(Constants.Property.PARTNER_NAME);
125     }
126
127     protected void logRequest() {
128         logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
129     }
130
131     protected void logResponse() {
132         logger.info(INVOKE_RETURN, "InvokeReturn");
133     }
134
135 }