aboutsummaryrefslogtreecommitdiffstats
path: root/common/openecomp-sdc-artifact-generator-lib/openecomp-sdc-artifact-generator-core/src/main/java/org/openecomp/sdc/generator/aspect/MetricsAspect.java
blob: d226b00b05b9d391e2f24be914fc2c4abccb05f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.generator.aspect;

import static org.openecomp.sdc.generator.data.GeneratorConstants.ARTIFACT_MODEL_INFO;
import static org.openecomp.sdc.generator.data.GeneratorConstants.BEGIN_TIMESTAMP;
import static org.openecomp.sdc.generator.data.GeneratorConstants.ELAPSED_TIME;
import static org.openecomp.sdc.generator.data.GeneratorConstants.END_TIMESTAMP;
import static org.openecomp.sdc.generator.data.GeneratorConstants.ERROR_CODE;
import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC;
import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_FAILURE_RESPONSE_CODE;
import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_FAILURE_RESPONSE_DESC;
import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_SUCCESS_RESPONSE_CODE;
import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_SUCCESS_RESPONSE_DESC;
import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_METRICS_TARGET_ENTITY;
import static org.openecomp.sdc.generator.data.GeneratorConstants.REQUEST_ID;
import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_CODE;
import static org.openecomp.sdc.generator.data.GeneratorConstants.RESPONSE_DESCRIPTION;
import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_INSTANCE_ID;
import static org.openecomp.sdc.generator.data.GeneratorConstants.SERVICE_NAME;
import static org.openecomp.sdc.generator.data.GeneratorConstants.STATUS_CODE;
import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_ENTITY;
import static org.openecomp.sdc.generator.data.GeneratorConstants.TARGET_SERVICE_NAME;
import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.artifactGeneratorErrorLogProcessor;
import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.getLogUtcDateStringFromTimestamp;
import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.resetLoggingContext;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.openecomp.core.logging.api.Logger;
import org.openecomp.core.logging.api.LoggerFactory;
import org.openecomp.sdc.generator.data.GenerationData;
import org.openecomp.sdc.generator.logging.CategoryLogLevel;
import org.openecomp.sdc.generator.logging.StatusCode;
import org.slf4j.MDC;

import java.util.Date;

/**
 * Wraps around any method annotated with
 * {@link org.openecomp.sdc.generator.logging.annotations.Metrics} for logging metrics information
 * In order for the aspect to be used, AspectJ annotation processing must be
 * turned on and this particular aspect enabled.
 *
 * @see org.openecomp.sdc.generator.logging.annotations.Metrics
 */
@Aspect
public class MetricsAspect {

  /**
   * Log Audit information for the method.
   *
   * @param pjp the pjp
   * @return the object
   * @throws Throwable the throwable
   */
  @Around("@annotation(org.openecomp.sdc.generator.logging.annotations.Metrics)")
  public Object logMetrics(ProceedingJoinPoint pjp) throws Throwable {

    final Logger logger = LoggerFactory.getLogger(pjp.getSignature().getDeclaringTypeName());
    String [] modelInfo = MDC.get(ARTIFACT_MODEL_INFO).split(",");

    if (modelInfo.length == 2) {
      //Since ARTIFACT_MODEL_INFO is passed as String from caller "null" value is populated.
      //So resetting "null" to "" in logs
      String serviceInstanceId =  modelInfo[0].equals("null") ? "" : modelInfo[0];
      String requestId =  modelInfo[1].equals("null") ? "" : modelInfo[1];
      MDC.put(SERVICE_INSTANCE_ID, serviceInstanceId);
      MDC.put(REQUEST_ID, requestId);
    }

    MDC.put(TARGET_ENTITY, GENERATOR_METRICS_TARGET_ENTITY);
    MDC.put(TARGET_SERVICE_NAME, MDC.get(SERVICE_NAME) + " artifact generation");
    // check if metrics aspect is enabled
    if (logger.isMetricsEnabled()) {
      final long beginTimestamp = System.currentTimeMillis();
      try {
        MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_SUCCESS_RESPONSE_DESC, MDC
            .get(SERVICE_NAME)));
        Object obj = pjp.proceed();
        if (obj instanceof  GenerationData) {
          GenerationData data = (GenerationData) obj;
          if (data.getErrorData() != null && !data.getErrorData().isEmpty()) {
            MDC.put(STATUS_CODE, StatusCode.ERROR.name());
            artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR, data.getErrorData().get(
                "AAI").get(0));
            MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
            // MDC.put(RESPONSE_CODE, GENERATOR_METRICS_FAILURE_RESPONSE_CODE);
            // If not service name  found log no artifact type found error.
            if (MDC.get(SERVICE_NAME) == null) {
              MDC.put(RESPONSE_DESCRIPTION, GENERATOR_AUDIT_NO_ARTIFACT_TYPE_RESPONSE_DESC);
            } else {
              MDC.put(RESPONSE_DESCRIPTION,
                  String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
                      .get(SERVICE_NAME)));
            }
          }
        }
        return obj;


      } catch (Exception ex) {
        artifactGeneratorErrorLogProcessor(CategoryLogLevel.ERROR,ex.getMessage());
        MDC.put(STATUS_CODE, StatusCode.ERROR.name());
        MDC.put(RESPONSE_CODE, MDC.get(ERROR_CODE));
        MDC.put(RESPONSE_DESCRIPTION, String.format(GENERATOR_METRICS_FAILURE_RESPONSE_DESC, MDC
            .get(SERVICE_NAME)));
        throw ex;
      } finally {
        long endTimestamp = System.currentTimeMillis();
        MDC.put(BEGIN_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(beginTimestamp)));
        MDC.put(END_TIMESTAMP, getLogUtcDateStringFromTimestamp(new Date(endTimestamp)));
        MDC.put(ELAPSED_TIME, String.valueOf(endTimestamp - beginTimestamp));
        logger.metrics("");
        resetLoggingContext();
      }
    } else {
      return pjp.proceed();
    }
  }
}