DeepAnalysis { public AnalysisResult analysis(AnalysisResult result) { result.setPackageName(result.getSourceName().toLowerCase()); Class<? extends Metrics> metricsClass = MetricsHolder.find(result.getAggregationFunctionName()); String metricsClassSimpleName = metricsClass.getSimpleName(); result.setMetricsClassName(metricsClassSimpleName); List<ConditionExpression> expressions = result.getFilterExpressionsParserResult(); if (expressions != null && expressions.size() > 0) { for (ConditionExpression expression : expressions) { 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); } } Class<?> c = metricsClass; 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) { throw new IllegalArgumentException("Can't find Entrance method in class: " + metricsClass.getName()); } EntryMethod entryMethod = new EntryMethod(); result.setEntryMethod(entryMethod); entryMethod.setMethodName(entranceMethod.getName()); for (Parameter parameter : entranceMethod.getParameters()) { Class<?> parameterType = parameter.getType(); Annotation[] parameterAnnotations = parameter.getAnnotations(); if (parameterAnnotations == null || parameterAnnotations.length == 0) { throw new IllegalArgumentException( "Entrance method:" + entranceMethod + " doesn't include the annotation."); } Annotation annotation = parameterAnnotations[0]; if (annotation instanceof SourceFrom) { entryMethod.addArg( parameterType, "source." + ClassMethodUtil.toGetMethod(result.getSourceAttribute()) + "()"); } else if (annotation instanceof ConstOne) { entryMethod.addArg(parameterType, "1"); } else if (annotation instanceof org.apache.skywalking.oap.server.core.analysis.metrics.annotation.Expression) { if (isNull(result.getFuncConditionExpressions()) || result.getFuncConditionExpressions().isEmpty()) { throw new IllegalArgumentException("Entrance method:" + entranceMethod + " argument can't find funcParamExpression."); } else { ConditionExpression expression = result.getNextFuncConditionExpression(); final FilterMatchers.MatcherInfo matcherInfo = FilterMatchers.INSTANCE.find(expression.getExpressionType()); final String getter = matcherInfo.isBooleanType() ? ClassMethodUtil.toIsMethod(expression.getAttribute()) : ClassMethodUtil.toGetMethod(expression.getAttribute()); final Expression argExpression = new Expression(); argExpression.setRight(expression.getValue()); argExpression.setExpressionObject(matcherInfo.getMatcher().getName()); argExpression.setLeft("source." + getter + "()"); entryMethod.addArg(argExpression); } } else if (annotation instanceof Arg) { entryMethod.addArg(parameterType, result.getNextFuncArg()); } else { throw new IllegalArgumentException( "Entrance method:" + entranceMethod + " doesn't the expected annotation."); } } c = metricsClass; 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(); } List<SourceColumn> columns = SourceColumnsFactory.getColumns(result.getSourceName()); result.setFieldsFromSource(columns); result.generateSerializeFields(); return result; }  AnalysisResult analysis(AnalysisResult result);  }
@Test public void testServiceAnalysis() { AnalysisResult result = new AnalysisResult(); result.setSourceName("Service"); result.setPackageName("service.serviceavg"); result.setSourceAttribute("latency"); result.setMetricsName("ServiceAvg"); result.setAggregationFunctionName("longAvg"); DeepAnalysis analysis = new DeepAnalysis(); result = analysis.analysis(result); EntryMethod method = result.getEntryMethod(); Assert.assertEquals("combine", method.getMethodName()); Assert.assertEquals("(long)(source.getLatency())", method.getArgsExpressions().get(0)); Assert.assertEquals("(long)(1)", method.getArgsExpressions().get(1)); List<SourceColumn> source = result.getFieldsFromSource(); Assert.assertEquals(1, source.size()); List<DataColumn> persistentFields = result.getPersistentFields(); Assert.assertEquals(4, persistentFields.size()); }
@Test public void testEndpointAnalysis() { AnalysisResult result = new AnalysisResult(); result.setSourceName("Endpoint"); result.setPackageName("endpoint.endpointavg"); result.setSourceAttribute("latency"); result.setMetricsName("EndpointAvg"); result.setAggregationFunctionName("longAvg"); DeepAnalysis analysis = new DeepAnalysis(); result = analysis.analysis(result); EntryMethod method = result.getEntryMethod(); Assert.assertEquals("combine", method.getMethodName()); Assert.assertEquals("(long)(source.getLatency())", method.getArgsExpressions().get(0)); Assert.assertEquals("(long)(1)", method.getArgsExpressions().get(1)); List<SourceColumn> source = result.getFieldsFromSource(); Assert.assertEquals(2, source.size()); List<DataColumn> persistentFields = result.getPersistentFields(); Assert.assertEquals(4, persistentFields.size()); }
@Test public void testFilterAnalysis() { AnalysisResult result = new AnalysisResult(); result.setSourceName("Endpoint"); result.setPackageName("endpoint.endpointavg"); result.setSourceAttribute("latency"); result.setMetricsName("EndpointAvg"); result.setAggregationFunctionName("longAvg"); ConditionExpression expression = new ConditionExpression(); expression.setExpressionType("stringMatch"); expression.setAttribute("name"); expression.setValue("\"/service/prod/save\""); result.addFilterExpressionsParserResult(expression); DeepAnalysis analysis = new DeepAnalysis(); result = analysis.analysis(result); EntryMethod method = result.getEntryMethod(); Assert.assertEquals("combine", method.getMethodName()); Assert.assertEquals("(long)(source.getLatency())", method.getArgsExpressions().get(0)); Assert.assertEquals("(long)(1)", method.getArgsExpressions().get(1)); List<SourceColumn> source = result.getFieldsFromSource(); Assert.assertEquals(2, source.size()); List<DataColumn> persistentFields = result.getPersistentFields(); Assert.assertEquals(4, persistentFields.size()); List<Expression> filterExpressions = result.getFilterExpressions(); Assert.assertEquals(1, filterExpressions.size()); Expression filterExpression = filterExpressions.get(0); Assert.assertEquals(EqualMatch.class.getName(), filterExpression.getExpressionObject()); Assert.assertEquals("source.getName()", filterExpression.getLeft()); Assert.assertEquals("\"/service/prod/save\"", filterExpression.getRight()); }
@Test public void shouldUseCorrectMatcher() { AnalysisResult result = new AnalysisResult(); result.setSourceName("Endpoint"); result.setPackageName("endpoint.endpointavg"); result.setSourceAttribute("latency"); result.setMetricsName("EndpointAvg"); result.setAggregationFunctionName("longAvg"); DeepAnalysis analysis = new DeepAnalysis(); result.setFilterExpressions(null); result.setFilterExpressionsParserResult(null); result.addFilterExpressionsParserResult(new ConditionExpression("booleanMatch", "valid", "")); result = analysis.analysis(result); assertTrue(result.getFilterExpressions().size() > 0); assertEquals(BooleanMatch.class.getName(), result.getFilterExpressions().get(0).getExpressionObject()); assertEquals("source.isValid()", result.getFilterExpressions().get(0).getLeft()); result.setFilterExpressions(null); result.setFilterExpressionsParserResult(null); result.addFilterExpressionsParserResult(new ConditionExpression("stringMatch", "type", "")); result = analysis.analysis(result); assertTrue(result.getFilterExpressions().size() > 0); assertEquals(EqualMatch.class.getName(), result.getFilterExpressions().get(0).getExpressionObject()); assertEquals("source.getType()", result.getFilterExpressions().get(0).getLeft()); result.setFilterExpressions(null); result.setFilterExpressionsParserResult(null); result.addFilterExpressionsParserResult(new ConditionExpression("notEqualMatch", "type", "")); result = analysis.analysis(result); assertTrue(result.getFilterExpressions().size() > 0); assertEquals(NotEqualMatch.class.getName(), result.getFilterExpressions().get(0).getExpressionObject()); assertEquals("source.getType()", result.getFilterExpressions().get(0).getLeft()); result.setFilterExpressions(null); result.setFilterExpressionsParserResult(null); result.addFilterExpressionsParserResult(new ConditionExpression("booleanNotEqualMatch", "type", "")); result = analysis.analysis(result); assertTrue(result.getFilterExpressions().size() > 0); assertEquals(BooleanNotEqualMatch.class.getName(), result.getFilterExpressions().get(0).getExpressionObject()); assertEquals("source.isType()", result.getFilterExpressions().get(0).getLeft()); }