DeepAnalysis.java 6.9 KB
Newer Older
wu-sheng's avatar
wu-sheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 *
 */

wu-sheng's avatar
wu-sheng 已提交
19
package org.apache.skywalking.oal.rt.parser;
wu-sheng's avatar
wu-sheng 已提交
20 21

import java.lang.annotation.Annotation;
22 23 24
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
wu-sheng's avatar
wu-sheng 已提交
25
import java.util.List;
wu-sheng's avatar
wu-sheng 已提交
26
import org.apache.skywalking.oal.rt.util.ClassMethodUtil;
27
import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
28 29 30 31
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Arg;
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.ConstOne;
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Entrance;
import org.apache.skywalking.oap.server.core.analysis.metrics.annotation.SourceFrom;
wu-sheng's avatar
wu-sheng 已提交
32 33
import org.apache.skywalking.oap.server.core.storage.annotation.Column;

34 35
import static java.util.Objects.isNull;

wu-sheng's avatar
wu-sheng 已提交
36 37
public class DeepAnalysis {
    public AnalysisResult analysis(AnalysisResult result) {
38
        // 1. Set sub package name by source.metrics
wu-sheng's avatar
wu-sheng 已提交
39 40
        result.setPackageName(result.getSourceName().toLowerCase());

41
        Class<? extends Metrics> metricsClass = MetricsHolder.find(result.getAggregationFunctionName());
42
        String metricsClassSimpleName = metricsClass.getSimpleName();
wu-sheng's avatar
wu-sheng 已提交
43

44
        result.setMetricsClassName(metricsClassSimpleName);
wu-sheng's avatar
wu-sheng 已提交
45 46 47 48 49

        // Optional for filter
        List<ConditionExpression> expressions = result.getFilterExpressionsParserResult();
        if (expressions != null && expressions.size() > 0) {
            for (ConditionExpression expression : expressions) {
50 51 52 53 54 55 56 57 58 59 60
                final FilterMatchers.MatcherInfo matcherInfo = FilterMatchers.INSTANCE.find(expression.getExpressionType());

                final String getter = matcherInfo.isBooleanType()
                    ? ClassMethodUtil.toIsMethod(expression.getAttribute())
                    : ClassMethodUtil.toGetMethod(expression.getAttribute());

                final Expression filterExpression = new Expression();
                filterExpression.setExpressionObject(matcherInfo.getMatcher().getName());
                filterExpression.setLeft("source." + getter + "()");
                filterExpression.setRight(expression.getValue());
                result.addFilterExpressions(filterExpression);
wu-sheng's avatar
wu-sheng 已提交
61 62 63
            }
        }

64
        // 3. Find Entrance method of this metrics
65
        Class<?> c = metricsClass;
wu-sheng's avatar
wu-sheng 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
        Method entranceMethod = null;
        SearchEntrance:
        while (!c.equals(Object.class)) {
            for (Method method : c.getMethods()) {
                Entrance annotation = method.getAnnotation(Entrance.class);
                if (annotation != null) {
                    entranceMethod = method;
                    break SearchEntrance;
                }
            }
            c = c.getSuperclass();
        }
        if (entranceMethod == null) {
79
            throw new IllegalArgumentException("Can't find Entrance method in class: " + metricsClass.getName());
wu-sheng's avatar
wu-sheng 已提交
80 81 82 83 84 85 86
        }
        EntryMethod entryMethod = new EntryMethod();
        result.setEntryMethod(entryMethod);
        entryMethod.setMethodName(entranceMethod.getName());

        // 4. Use parameter's annotation of entrance method to generate aggregation entrance.
        for (Parameter parameter : entranceMethod.getParameters()) {
wu-sheng's avatar
wu-sheng 已提交
87
            Class<?> parameterType = parameter.getType();
wu-sheng's avatar
wu-sheng 已提交
88 89
            Annotation[] parameterAnnotations = parameter.getAnnotations();
            if (parameterAnnotations == null || parameterAnnotations.length == 0) {
90 91
                throw new IllegalArgumentException(
                    "Entrance method:" + entranceMethod + " doesn't include the annotation.");
wu-sheng's avatar
wu-sheng 已提交
92 93 94
            }
            Annotation annotation = parameterAnnotations[0];
            if (annotation instanceof SourceFrom) {
95 96
                entryMethod.addArg(
                    parameterType, "source." + ClassMethodUtil.toGetMethod(result.getSourceAttribute()) + "()");
wu-sheng's avatar
wu-sheng 已提交
97
            } else if (annotation instanceof ConstOne) {
wu-sheng's avatar
wu-sheng 已提交
98 99
                entryMethod.addArg(parameterType, "1");
            } else if (annotation instanceof org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Expression) {
100 101 102 103
                if (isNull(result.getFuncConditionExpressions()) || result.getFuncConditionExpressions().isEmpty()) {
                    throw new IllegalArgumentException("Entrance method:" + entranceMethod + " argument can't find funcParamExpression.");
                } else {
                    ConditionExpression expression = result.getNextFuncConditionExpression();
104 105 106 107 108
                    final FilterMatchers.MatcherInfo matcherInfo = FilterMatchers.INSTANCE.find(expression.getExpressionType());

                    final String getter = matcherInfo.isBooleanType()
                        ? ClassMethodUtil.toIsMethod(expression.getAttribute())
                        : ClassMethodUtil.toGetMethod(expression.getAttribute());
wu-sheng's avatar
wu-sheng 已提交
109

110 111 112 113
                    final Expression argExpression = new Expression();
                    argExpression.setRight(expression.getValue());
                    argExpression.setExpressionObject(matcherInfo.getMatcher().getName());
                    argExpression.setLeft("source." + getter + "()");
wu-sheng's avatar
wu-sheng 已提交
114 115

                    entryMethod.addArg(argExpression);
wu-sheng's avatar
wu-sheng 已提交
116 117
                }
            } else if (annotation instanceof Arg) {
wu-sheng's avatar
wu-sheng 已提交
118
                entryMethod.addArg(parameterType, result.getNextFuncArg());
wu-sheng's avatar
wu-sheng 已提交
119
            } else {
120 121
                throw new IllegalArgumentException(
                    "Entrance method:" + entranceMethod + " doesn't the expected annotation.");
wu-sheng's avatar
wu-sheng 已提交
122 123 124
            }
        }

125 126
        // 5. Get all column declared in MetricsHolder class.
        c = metricsClass;
wu-sheng's avatar
wu-sheng 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        while (!c.equals(Object.class)) {
            for (Field field : c.getDeclaredFields()) {
                Column column = field.getAnnotation(Column.class);
                if (column != null) {
                    result.addPersistentField(field.getName(), column.columnName(), field.getType());
                }
            }
            c = c.getSuperclass();
        }

        // 6. Based on Source, generate default columns
        List<SourceColumn> columns = SourceColumnsFactory.getColumns(result.getSourceName());
        result.setFieldsFromSource(columns);

        result.generateSerializeFields();

        return result;
    }

}