2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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.aai.aailog.logs;
23 import org.onap.aai.logging.AaiElsErrorCode;
24 import org.onap.logging.filter.base.MDCSetup;
25 import org.onap.logging.ref.slf4j.ONAPLogConstants;
28 import java.time.ZoneOffset;
29 import java.time.ZonedDateTime;
30 import java.time.format.DateTimeFormatter;
31 import java.util.regex.PatternSyntaxException;
33 public class AaiDmaapMetricLog extends MDCSetup {
35 protected static final Logger logger = LoggerFactory.getLogger(AaiDmaapMetricLog.class);
36 private static final Marker INVOKE_RETURN = MarkerFactory.getMarker("INVOKE-RETURN");
37 private static final String TARGET_ENTITY = "DMaaP";
39 public AaiDmaapMetricLog() {
41 public void pre(String targetServiceName, String event, String transactionId, String serviceName) {
44 MDC.put(ONAPLogConstants.MDCs.INVOKE_TIMESTAMP,
45 ZonedDateTime.now(ZoneOffset.UTC).format(DateTimeFormatter.ISO_INSTANT));
47 setElapsedTimeInvokeTimestamp();
48 MDC.put(ONAPLogConstants.MDCs.TARGET_SERVICE_NAME, targetServiceName);
49 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, ONAPLogConstants.ResponseStatus.INPROGRESS.toString());
50 MDC.put(ONAPLogConstants.MDCs.TARGET_ENTITY, TARGET_ENTITY);
51 if (transactionId != null && !(transactionId.isEmpty())) {
52 MDC.put(ONAPLogConstants.MDCs.REQUEST_ID, transactionId);
54 if (serviceName != null && !(serviceName.isEmpty())) {
55 MDC.put(ONAPLogConstants.MDCs.SERVICE_NAME, serviceName);
57 setInvocationIdFromMDC();
58 logger.info(ONAPLogConstants.Markers.INVOKE, event );
60 } catch (Exception e) {
61 logger.warn("Error in AaiDmaapMetricLog pre", e.getMessage());
65 public void post(String responseCode, String errorDescription) {
69 setElapsedTimeInvokeTimestamp();
70 setResponseStatusCode(responseCode, errorDescription);
71 logger.info(INVOKE_RETURN, "InvokeReturn");
73 } catch (Exception e) {
74 logger.warn("Error in AaiDmaapMetricLog post", e.getMessage());
78 public void setResponseStatusCode(String aaiElsErrorCode, String errorDescription) {
80 if (AaiElsErrorCode.SUCCESS.equals(aaiElsErrorCode)) {
81 statusCode = ONAPLogConstants.ResponseStatus.COMPLETE.toString();
84 statusCode = ONAPLogConstants.ResponseStatus.ERROR.toString();
85 MDC.put(ONAPLogConstants.MDCs.ERROR_CODE, aaiElsErrorCode);
86 MDC.put(ONAPLogConstants.MDCs.ERROR_DESC, errorDescription);
88 MDC.put(ONAPLogConstants.MDCs.RESPONSE_STATUS_CODE, statusCode);
89 MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, aaiElsErrorCode);