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,
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());
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);
85 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
91 protected String extractRequestID() {
92 String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
93 if (requestId == null || requestId.isEmpty()) {
94 requestId = UUID.randomUUID().toString();
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);
104 protected void post(Request request, Response response) {
107 setElapsedTimeInvokeTimestamp();
108 setResponseStatusCode(getHttpStatusCode(response));
109 setResponseDescription(getHttpStatusCode(response));
110 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
111 additionalPost(request, response);
114 } catch (Exception e) {
115 logger.warn("Error in AbstractBaseMetricLogFilter post", e);
119 protected void additionalPost(Request request, Response response) {
120 // override to add application specific logic
123 protected String getPartnerName() {
124 return getProperty(Constants.Property.PARTNER_NAME);
127 protected void logRequest() {
128 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
131 protected void logResponse() {
132 logger.info(INVOKE_RETURN, "InvokeReturn");