From a81719786e5fcd58095fd3995c959760dfee5dbf Mon Sep 17 00:00:00 2001 From: peng-yongsheng <8082209@qq.com> Date: Thu, 8 Feb 2018 18:16:58 +0800 Subject: [PATCH] Provide the getSlowService query. --- .../storage/dao/ui/IServiceMetricUIDAO.java | 4 ++ .../apm/collector/storage/ui/alarm/Alarm.java | 10 +-- .../collector/storage/ui/alarm/AlarmItem.java | 2 +- .../storage/ui/service/ServiceMetric.java | 61 +++++++++++++++++++ .../es/dao/ui/ServiceMetricEsUIDAO.java | 60 ++++++++++++++++++ .../h2/dao/ui/ServiceMetricH2UIDAO.java | 9 ++- .../apm/collector/ui/query/AlarmQuery.java | 4 +- .../collector/ui/query/ApplicationQuery.java | 9 ++- .../ui/service/ApplicationService.java | 21 +++++++ .../ui-graphql/application-layer.graphqls | 2 +- 10 files changed, 168 insertions(+), 14 deletions(-) create mode 100644 apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceMetric.java diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceMetricUIDAO.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceMetricUIDAO.java index 4303aa797..0ca4c752a 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceMetricUIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/dao/ui/IServiceMetricUIDAO.java @@ -24,6 +24,7 @@ import org.apache.skywalking.apm.collector.storage.base.dao.DAO; import org.apache.skywalking.apm.collector.storage.table.MetricSource; import org.apache.skywalking.apm.collector.storage.ui.common.Node; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceMetric; import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; /** @@ -36,4 +37,7 @@ public interface IServiceMetricUIDAO extends DAO { List getServicesMetric(Step step, long startTime, long endTime, MetricSource metricSource, Collection serviceIds); + + List getSlowService(int applicationId, Step step, long start, long end, + Integer top, MetricSource metricSource); } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/Alarm.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/Alarm.java index 62c3dc102..001024a27 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/Alarm.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/Alarm.java @@ -25,7 +25,7 @@ import java.util.List; */ public class Alarm { private List items; - private Integer count; + private int total; public List getItems() { return items; @@ -35,11 +35,11 @@ public class Alarm { this.items = items; } - public Integer getCount() { - return count; + public int getTotal() { + return total; } - public void setCount(Integer count) { - this.count = count; + public void setTotal(int total) { + this.total = total; } } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/AlarmItem.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/AlarmItem.java index 1523ec931..1e47c71da 100644 --- a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/AlarmItem.java +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/alarm/AlarmItem.java @@ -25,6 +25,6 @@ public class AlarmItem { private String title; private String content; private String startTime; - private AlarmType alertType; + private AlarmType alarmType; private CauseType causeType; } diff --git a/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceMetric.java b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceMetric.java new file mode 100644 index 000000000..7db67f8b4 --- /dev/null +++ b/apm-collector/apm-collector-storage/collector-storage-define/src/main/java/org/apache/skywalking/apm/collector/storage/ui/service/ServiceMetric.java @@ -0,0 +1,61 @@ +/* + * 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. + * + */ + +package org.apache.skywalking.apm.collector.storage.ui.service; + +/** + * @author peng-yongsheng + */ +public class ServiceMetric { + private int id; + private String name; + private int avgResponseTime; + private int tps; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAvgResponseTime() { + return avgResponseTime; + } + + public void setAvgResponseTime(int avgResponseTime) { + this.avgResponseTime = avgResponseTime; + } + + public int getTps() { + return tps; + } + + public void setTps(int tps) { + this.tps = tps; + } +} diff --git a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceMetricEsUIDAO.java b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceMetricEsUIDAO.java index 43ed65f74..cd8cb02f6 100644 --- a/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceMetricEsUIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-es-provider/src/main/java/org/apache/skywalking/apm/collector/storage/es/dao/ui/ServiceMetricEsUIDAO.java @@ -19,8 +19,10 @@ package org.apache.skywalking.apm.collector.storage.es.dao.ui; import java.util.Collection; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient; import org.apache.skywalking.apm.collector.core.util.Const; import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceMetricUIDAO; @@ -29,6 +31,7 @@ import org.apache.skywalking.apm.collector.storage.table.MetricSource; import org.apache.skywalking.apm.collector.storage.table.service.ServiceMetricTable; import org.apache.skywalking.apm.collector.storage.ui.common.Node; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceMetric; import org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode; import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.apache.skywalking.apm.collector.storage.utils.TimePyramidTableNameBuilder; @@ -40,16 +43,21 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.sum.Sum; +import org.elasticsearch.search.aggregations.pipeline.InternalSimpleValue; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders; /** * @author peng-yongsheng */ public class ServiceMetricEsUIDAO extends EsDAO implements IServiceMetricUIDAO { + private static final String AVG_DURATION = "avg_duration"; + public ServiceMetricEsUIDAO(ElasticSearchClient client) { super(client); } @@ -143,4 +151,56 @@ public class ServiceMetricEsUIDAO extends EsDAO implements IServiceMetricUIDAO { }); return nodes; } + + @Override public List getSlowService(int applicationId, Step step, long start, long end, Integer top, + MetricSource metricSource) { + String tableName = TimePyramidTableNameBuilder.build(step, ServiceMetricTable.TABLE); + + SearchRequestBuilder searchRequestBuilder = getClient().prepareSearch(tableName); + searchRequestBuilder.setTypes(ServiceMetricTable.TABLE_TYPE); + searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH); + + BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); + boolQuery.must().add(QueryBuilders.rangeQuery(ServiceMetricTable.COLUMN_TIME_BUCKET).gte(start).lte(end)); + boolQuery.must().add(QueryBuilders.termQuery(ServiceMetricTable.COLUMN_APPLICATION_ID, applicationId)); + boolQuery.must().add(QueryBuilders.termQuery(ServiceMetricTable.COLUMN_SOURCE_VALUE, metricSource.getValue())); + + searchRequestBuilder.setQuery(boolQuery); + searchRequestBuilder.setSize(0); + + TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms(ServiceMetricTable.COLUMN_SERVICE_ID).field(ServiceMetricTable.COLUMN_SERVICE_ID).size(100); + aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceMetricTable.COLUMN_TRANSACTION_CALLS).field(ServiceMetricTable.COLUMN_TRANSACTION_CALLS)); + aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS).field(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS)); + aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceMetricTable.COLUMN_TRANSACTION_DURATION_SUM).field(ServiceMetricTable.COLUMN_TRANSACTION_DURATION_SUM)); + aggregationBuilder.subAggregation(AggregationBuilders.sum(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM).field(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM)); + + Map bucketsPathsMap = new HashMap<>(); + bucketsPathsMap.put(ServiceMetricTable.COLUMN_TRANSACTION_CALLS, ServiceMetricTable.COLUMN_TRANSACTION_CALLS); + bucketsPathsMap.put(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS, ServiceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS); + bucketsPathsMap.put(ServiceMetricTable.COLUMN_TRANSACTION_DURATION_SUM, ServiceMetricTable.COLUMN_TRANSACTION_DURATION_SUM); + bucketsPathsMap.put(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM, ServiceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM); + + String idOrCode = "(params." + ServiceMetricTable.COLUMN_TRANSACTION_DURATION_SUM + " - params." + ServiceMetricTable.COLUMN_TRANSACTION_ERROR_DURATION_SUM + ")" + + " / " + + "(params." + ServiceMetricTable.COLUMN_TRANSACTION_CALLS + " - params." + ServiceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS + ")"; + Script script = new Script(idOrCode); + aggregationBuilder.subAggregation(PipelineAggregatorBuilders.bucketScript(AVG_DURATION, bucketsPathsMap, script)); + + searchRequestBuilder.addAggregation(aggregationBuilder); + SearchResponse searchResponse = searchRequestBuilder.execute().actionGet(); + + List serviceMetrics = new LinkedList<>(); + Terms serviceIdTerms = searchResponse.getAggregations().get(ServiceMetricTable.COLUMN_SERVICE_ID); + serviceIdTerms.getBuckets().forEach(serviceIdTerm -> { + int serviceId = serviceIdTerm.getKeyAsNumber().intValue(); + + ServiceMetric serviceMetric = new ServiceMetric(); + InternalSimpleValue simpleValue = serviceIdTerm.getAggregations().get(AVG_DURATION); + + serviceMetric.setId(serviceId); + serviceMetric.setAvgResponseTime((int)simpleValue.getValue()); + serviceMetrics.add(serviceMetric); + }); + return serviceMetrics; + } } diff --git a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceMetricH2UIDAO.java b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceMetricH2UIDAO.java index 2b5628a10..0bab7e5b8 100644 --- a/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceMetricH2UIDAO.java +++ b/apm-collector/apm-collector-storage/collector-storage-h2-provider/src/main/java/org/apache/skywalking/apm/collector/storage/h2/dao/ui/ServiceMetricH2UIDAO.java @@ -33,6 +33,7 @@ import org.apache.skywalking.apm.collector.storage.table.MetricSource; import org.apache.skywalking.apm.collector.storage.table.service.ServiceMetricTable; import org.apache.skywalking.apm.collector.storage.ui.common.Node; import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceMetric; import org.apache.skywalking.apm.collector.storage.utils.DurationPoint; import org.apache.skywalking.apm.collector.storage.utils.TimePyramidTableNameBuilder; import org.slf4j.Logger; @@ -106,9 +107,13 @@ public class ServiceMetricH2UIDAO extends H2DAO implements IServiceMetricUIDAO { return trends; } - @Override - public List getServicesMetric(Step step, long startTime, long endTime, MetricSource metricSource, + @Override public List getServicesMetric(Step step, long startTime, long endTime, MetricSource metricSource, Collection serviceIds) { return null; } + + @Override public List getSlowService(int applicationId, Step step, long start, long end, Integer top, + MetricSource metricSource) { + return null; + } } diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/AlarmQuery.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/AlarmQuery.java index 820b41a19..61b1d2990 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/AlarmQuery.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/AlarmQuery.java @@ -29,9 +29,9 @@ import org.apache.skywalking.apm.collector.storage.ui.common.Pagination; */ public class AlarmQuery implements Query { - public Alarm loadAlertList(String keyword, AlarmType alarmType, Duration duration, Pagination pagination) { + public Alarm loadAlarmList(String keyword, AlarmType alarmType, Duration duration, Pagination paging) { Alarm alarm = new Alarm(); - alarm.setCount(0); + alarm.setTotal(0); return alarm; } } diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ApplicationQuery.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ApplicationQuery.java index 7a97ee968..121cdce78 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ApplicationQuery.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/query/ApplicationQuery.java @@ -26,7 +26,7 @@ import org.apache.skywalking.apm.collector.storage.ui.application.Application; import org.apache.skywalking.apm.collector.storage.ui.common.Duration; import org.apache.skywalking.apm.collector.storage.ui.common.Topology; import org.apache.skywalking.apm.collector.storage.ui.server.AppServerInfo; -import org.apache.skywalking.apm.collector.storage.ui.service.ServiceInfo; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceMetric; import org.apache.skywalking.apm.collector.ui.graphql.Query; import org.apache.skywalking.apm.collector.ui.service.ApplicationService; import org.apache.skywalking.apm.collector.ui.service.ApplicationTopologyService; @@ -73,8 +73,11 @@ public class ApplicationQuery implements Query { return getApplicationTopologyService().getApplicationTopology(duration.getStep(), applicationId, start, end); } - public List getSlowService(int applicationId, Duration duration, Integer top) { - return null; + public List getSlowService(int applicationId, Duration duration, Integer top) throws ParseException { + long start = DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getStart()); + long end = DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getEnd()); + + return getApplicationService().getSlowService(applicationId, duration.getStep(), start, end, top); } public List getServerThroughput(int applicationId, Duration duration, Integer top) { diff --git a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ApplicationService.java b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ApplicationService.java index 7f7ce816f..42c094fe8 100644 --- a/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ApplicationService.java +++ b/apm-collector/apm-collector-ui/collector-ui-jetty-provider/src/main/java/org/apache/skywalking/apm/collector/ui/service/ApplicationService.java @@ -18,13 +18,19 @@ package org.apache.skywalking.apm.collector.ui.service; +import java.text.ParseException; import java.util.List; import org.apache.skywalking.apm.collector.cache.CacheModule; import org.apache.skywalking.apm.collector.cache.service.ApplicationCacheService; +import org.apache.skywalking.apm.collector.cache.service.ServiceNameCacheService; import org.apache.skywalking.apm.collector.core.module.ModuleManager; import org.apache.skywalking.apm.collector.storage.StorageModule; import org.apache.skywalking.apm.collector.storage.dao.ui.IInstanceUIDAO; +import org.apache.skywalking.apm.collector.storage.dao.ui.IServiceMetricUIDAO; +import org.apache.skywalking.apm.collector.storage.table.MetricSource; import org.apache.skywalking.apm.collector.storage.ui.application.Application; +import org.apache.skywalking.apm.collector.storage.ui.common.Step; +import org.apache.skywalking.apm.collector.storage.ui.service.ServiceMetric; /** * @author peng-yongsheng @@ -32,11 +38,15 @@ import org.apache.skywalking.apm.collector.storage.ui.application.Application; public class ApplicationService { private final IInstanceUIDAO instanceDAO; + private final IServiceMetricUIDAO serviceMetricUIDAO; private final ApplicationCacheService applicationCacheService; + private final ServiceNameCacheService serviceNameCacheService; public ApplicationService(ModuleManager moduleManager) { this.instanceDAO = moduleManager.find(StorageModule.NAME).getService(IInstanceUIDAO.class); + this.serviceMetricUIDAO = moduleManager.find(StorageModule.NAME).getService(IServiceMetricUIDAO.class); this.applicationCacheService = moduleManager.find(CacheModule.NAME).getService(ApplicationCacheService.class); + this.serviceNameCacheService = moduleManager.find(CacheModule.NAME).getService(ServiceNameCacheService.class); } public List getApplications(long startTime, long endTime, int... applicationIds) { @@ -48,4 +58,15 @@ public class ApplicationService { }); return applications; } + + public List getSlowService(int applicationId, Step step, long start, long end, + Integer top) throws ParseException { + List slowServices = serviceMetricUIDAO.getSlowService(applicationId, step, start, end, top, MetricSource.Callee); + slowServices.forEach(slowService -> { + slowService.setName(serviceNameCacheService.get(slowService.getId()).getServiceName()); + //TODO + slowService.setTps(1); + }); + return slowServices; + } } diff --git a/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/application-layer.graphqls b/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/application-layer.graphqls index bb99a4910..67f435073 100644 --- a/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/application-layer.graphqls +++ b/apm-protocol/apm-ui-protocol/src/main/resources/ui-graphql/application-layer.graphqls @@ -43,6 +43,6 @@ type Application { extend type Query { getAllApplication(duration: Duration!): [Application!]! getApplicationTopology(applicationId: ID!, duration: Duration!): Topology - getSlowService(applicationId: ID!, duration: Duration!, top: Int!): [ServiceInfo!]! + getSlowService(applicationId: ID!, duration: Duration!, top: Int!): [ServiceMetric!]! getServerThroughput(applicationId: ID!, duration: Duration!, top: Int!): [AppServerInfo!]! }