2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.openecomp.sdc.generator.aspect;
23 import static org.openecomp.sdc.generator.data.GeneratorConstants.ARTIFACT_MODEL_INFO;
24 import static org.openecomp.sdc.generator.data.GeneratorConstants.BEGIN_TIMESTAMP;
25 import static org.openecomp.sdc.generator.data.GeneratorConstants.ELAPSED_TIME;
26 import static org.openecomp.sdc.generator.data.GeneratorConstants.END_TIMESTAMP;
27 import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_CODE;
28 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC;
29 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_FAILURE_RESPONSE_CODE;
30 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_FAILURE_RESPONSE_DESC;
31 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_SUCCESS_RESPONSE_CODE;
32 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_SUCCESS_RESPONSE_DESC;
33 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_TARGET_ENTITY;
34 import static org.openecomp.sdc.generator.data.GeneratorConstants.REQUEST_ID;
35 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_CODE;
36 import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_DESCRIPTION;
37 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_INSTANCE_ID;
38 import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_NAME;
39 import static org.openecomp.sdc.generator.data.GeneratorConstants.STATUS_CODE;
40 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_ENTITY;
41 import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_SERVICE_NAME;
42 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.artifactGeneratorErrorLogProcessor;
43 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.getLogUtcDateStringFromTimestamp;
44 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.resetLoggingContext;
46 import org.aspectj.lang.ProceedingJoinPoint;
47 import org.aspectj.lang.annotation.Around;
48 import org.aspectj.lang.annotation.Aspect;
49 import org.openecomp.core.logging.api.Logger;
50 import org.openecomp.core.logging.api.LoggerFactory;
51 import org.openecomp.sdc.generator.data.GenerationData;
52 import org.openecomp.sdc.generator.logging.CategoryLogLevel;
53 import org.openecomp.sdc.generator.logging.StatusCode;
56 import java.util.Date;
59 * Wraps around any method annotated with
60 * {@link org.openecomp.sdc.generator.logging.annotations.Metrics} for logging metrics information
61 * In order for the aspect to be used, AspectJ annotation processing must be
62 * turned on and this particular aspect enabled.
64 * @see org.openecomp.sdc.generator.logging.annotations.Metrics
67 public class MetricsAspect {
70 * Log Audit information for the method.
74 * @throws Throwable the throwable
76 @Around("@annotation(org.openecomp.sdc.generator.logging.annotations.Metrics)")
77 public Object logMetrics(ProceedingJoinPoint pjp) throws Throwable {
79 final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
80 String [] modelInfo = MDC.get(ARTIFACT_MODEL_INFO).split(",");
82 if (modelInfo.length == 2) {
83 //Since ARTIFACT_MODEL_INFO is passed as String from caller "null" value is populated.
84 //So resetting "null" to "" in logs
85 String serviceInstanceId = modelInfo[0].equals("null") ? "" : modelInfo[0];
86 String requestId = modelInfo[1].equals("null") ? "" : modelInfo[1];
87 MDC.put(SERVICE_INSTANCE_ID, serviceInstanceId);
88 MDC.put(REQUEST_ID, requestId);
91 MDC.put(TARGET_ENTITY, GENERATOR_METRICS_TARGET_ENTITY);
92 MDC.put(TARGET_SERVICE_NAME, MDC.get(SERVICE_NAME) + " artifact generation");
93 // check if metrics aspect is enabled
94 if (logger.isMetricsEnabled()) {
95 final long beginTimestamp = System.currentTimeMillis();
97 MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_SUCCESS_RESPONSE_DESC, MDC
99 Object obj = pjp.proceed();
100 if (obj instanceof GenerationData) {
101 GenerationData data = (GenerationData) obj;
102 if (data.getErrorData() != null && !data.getErrorData().isEmpty()) {
103 MDC.put(STATUS_CODE, StatusCode.ERROR.name());
104 artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR, data.getErrorData().get(
106 MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
107 // MDC.put(RESPONSE_CODE, GENERATOR_METRICS_FAILURE_RESPONSE_CODE);
108 // If not service name found log no artifact type found error.
109 if (MDC.get(SERVICE_NAME) == null) {
110 MDC.put(RESPONSE_DESCRIPTION, GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC);
112 MDC.put(RESPONSE_DESCRIPTION,
113 String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
114 .get(SERVICE_NAME)));
121 } catch (Exception ex) {
122 artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR,ex.getMessage());
123 MDC.put(STATUS_CODE, StatusCode.ERROR.name());
124 MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
125 MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
126 .get(SERVICE_NAME)));
129 long endTimestamp = System.currentTimeMillis();
130 MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
131 MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
132 MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
134 resetLoggingContext();
137 return pjp.proceed();