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 AbstractMetricLogFilter<Request, Response, RequestHeaders> extends MDCSetup {
35 protected static final Logger logger = LoggerFactory.getLogger(AbstractMetricLogFilter.class);
36 private final String partnerName;
37 private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
39 public AbstractMetricLogFilter() {
40 partnerName = getPartnerName();
43 protected abstract void addHeader(RequestHeaders requestHeaders, String headerName, String headerValue);
45 protected abstract String getTargetServiceName(Request request);
47 protected abstract int getHttpStatusCode(Response response);
49 protected abstract String getResponseCode(Response response);
51 protected abstract String getTargetEntity(Request request);
53 protected void pre(Request request, RequestHeaders requestHeaders) {
56 setupHeaders(request, requestHeaders);
57 additionalPre(request, requestHeaders);
58 logger.info(ONAPLogConstants.Markers.INVOKE, "Invoke");
59 } catch (Exception e) {
60 logger.warn("Error in AbstractMetricLogFilter pre", e);
64 protected void additionalPre(Request request, RequestHeaders requestHeaders) {
65 // override to add application specific logic
68 protected void setupHeaders(Request clientRequest, RequestHeaders requestHeaders) {
69 String requestId = extractRequestID();
70 String invocationId = setInvocationId();
71 addHeader(requestHeaders, ONAPLogConstants.Headers.REQUEST_ID, requestId);
72 addHeader(requestHeaders, Constants.HttpHeaders.HEADER_REQUEST_ID, requestId);
73 addHeader(requestHeaders, Constants.HttpHeaders.TRANSACTION_ID, requestId);
74 addHeader(requestHeaders, Constants.HttpHeaders.ECOMP_REQUEST_ID, requestId);
75 addHeader(requestHeaders, ONAPLogConstants.Headers.PARTNER_NAME, partnerName);
76 logger.trace("Setting X-InvocationID header for outgoing request: {}", invocationId);
77 addHeader(requestHeaders, ONAPLogConstants.Headers.INVOCATION_ID, invocationId);
81 protected String setInvocationId() {
82 String invocationId = UUID.randomUUID().toString();
83 MDC.put(ONAPLogConstants.MDCs.CLIENT_INVOCATION_ID, invocationId);
84 MDC.put(ONAPLogConstants.MDCs.INVOCATION_ID, invocationId);
88 protected void setupMDC(Request request) {
89 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
90 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
91 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, getTargetServiceName(request));
92 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
94 if (MDC.get(ONAPLogConstants.MDCs.TARGET_ENTITY) == null) {
95 String targetEntity = getTargetEntity(request);
96 if (targetEntity != null) {
97 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, targetEntity);
99 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, Constants.DefaultValues.UNKNOWN_TARGET_ENTITY);
105 protected String extractRequestID() {
106 String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
107 if (requestId == null || requestId.isEmpty()) {
108 requestId = UUID.randomUUID().toString();
110 setElapsedTimeInvokeTimestamp();
111 logger.warn("No value found in MDC when checking key {} value will be set to {}",
112 ONAPLogConstants.MDCs.REQUEST_ID, requestId);
113 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, requestId);
118 protected void post(Request request, Response response) {
121 setElapsedTimeInvokeTimestamp();
122 setResponseStatusCode(getHttpStatusCode(response));
123 setResponseDescription(getHttpStatusCode(response));
124 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, getResponseCode(response));
125 additionalPost(request, response);
126 logger.info(INVOKE_RETURN, "InvokeReturn");
128 } catch (Exception e) {
129 logger.warn("Error in AbstractMetricLogFilter post", e);
133 protected void additionalPost(Request request, Response response) {
134 // override to add application specific logic
137 protected String getPartnerName() {
138 return getProperty(Constants.Property.PARTNER_NAME);