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 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");
39 public AbstractBaseMetricLogFilter() {
40 partnerName = getPartnerName();
43 protected abstract String getTargetServiceName(Request request);
45 protected abstract int getHttpStatusCode(Response response);
47 protected abstract String getResponseCode(Response response);
49 protected abstract String getTargetEntity(Request request);
51 protected void pre(Request request) {
56 additionalPre(request);
58 } catch (Exception e) {
59 logger.warn("Error in AbstractBaseMetricLogFilter pre", e);
63 protected void additionalPre(Request request) {
64 // override to add application specific logic
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);
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());
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);
84 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
89 setElapsedTimeInvokeTimestamp();
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);
103 protected void post(Request request, Response response) {
106 setElapsedTimeInvokeTimestamp();
107 setResponseStatusCode(getHttpStatusCode(response));
108 setResponseDescription(getHttpStatusCode(response));
109 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
110 additionalPost(request, response);
113 } catch (Exception e) {
114 logger.warn("Error in AbstractBaseMetricLogFilter post", e);
118 protected void additionalPost(Request request, Response response) {
119 // override to add application specific logic
122 protected String getPartnerName() {
123 return getProperty(Constants.Property.PARTNER_NAME);
126 protected void logRequest() {
127 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
130 protected void logResponse() {
131 logger.info(INVOKE_RETURN, "InvokeReturn");