add ability to specify instant precision
[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, getCurrentTimeStamp());
76         MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(request));
77         MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
78
79         if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) {
80             String targetEntity = getTargetEntity(request);
81             if (targetEntity != null) {
82                 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
83             } else {
84                 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
85             }
86         }
87         setServerFQDN();
88         setLogTimestamp();
89         setElapsedTimeInvokeTimestamp();
90     }
91
92     protected String extractRequestID() {
93         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
94         if (requestId == null || requestId.isEmpty()) {
95             requestId = UUID.randomUUID().toString();
96             logger.trace("No value found in MDC when checking key {} value will be set to {}",
97                     ONAPLogConstants.MDCs.REQUEST_ID, requestId);
98             MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
99         }
100         return requestId;
101     }
102
103     protected void post(Request request, Response response) {
104         try {
105             setLogTimestamp();
106             setElapsedTimeInvokeTimestamp();
107             setResponseStatusCode(getHttpStatusCode(response));
108             setResponseDescription(getHttpStatusCode(response));
109             MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
110             additionalPost(request, response);
111             logResponse();
112             clearClientMDCs();
113         } catch (Exception e) {
114             logger.warn("Error in AbstractBaseMetricLogFilter post", e);
115         }
116     }
117
118     protected void additionalPost(Request request, Response response) {
119         // override to add application specific logic
120     }
121
122     protected String getPartnerName() {
123         return getProperty(Constants.Property.PARTNER_NAME);
124     }
125
126     protected void logRequest() {
127         logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
128     }
129
130     protected void logResponse() {
131         logger.info(INVOKE_RETURN, "InvokeReturn");
132     }
133
134 }