Search in sources :

Example 1 with ServiceNode

use of org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode in project incubator-skywalking by apache.

the class ServiceMetricEsUIDAO method getServicesMetric.

@Override
public List<Node> getServicesMetric(Step step, long startTime, long endTime, MetricSource metricSource, Collection<Integer> serviceIds) {
    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(startTime).lte(endTime));
    boolQuery.must().add(QueryBuilders.termsQuery(ServiceMetricTable.COLUMN_SERVICE_ID, serviceIds));
    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));
    searchRequestBuilder.addAggregation(aggregationBuilder);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    List<Node> nodes = new LinkedList<>();
    Terms serviceIdTerms = searchResponse.getAggregations().get(ServiceMetricTable.COLUMN_SERVICE_ID);
    serviceIdTerms.getBuckets().forEach(serviceIdBucket -> {
        int serviceId = serviceIdBucket.getKeyAsNumber().intValue();
        Sum callsSum = serviceIdBucket.getAggregations().get(ServiceMetricTable.COLUMN_TRANSACTION_CALLS);
        Sum errorCallsSum = serviceIdBucket.getAggregations().get(ServiceMetricTable.COLUMN_TRANSACTION_ERROR_CALLS);
        ServiceNode serviceNode = new ServiceNode();
        serviceNode.setId(serviceId);
        serviceNode.setCalls((long) callsSum.getValue());
        serviceNode.setSla((int) (((callsSum.getValue() - errorCallsSum.getValue()) / callsSum.getValue()) * 10000));
        nodes.add(serviceNode);
    });
    return nodes;
}
Also used : ServiceNode(org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Node(org.apache.skywalking.apm.collector.storage.ui.common.Node) ServiceNode(org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) DurationPoint(org.apache.skywalking.apm.collector.storage.utils.DurationPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

Node (org.apache.skywalking.apm.collector.storage.ui.common.Node)1 ServiceNode (org.apache.skywalking.apm.collector.storage.ui.service.ServiceNode)1 DurationPoint (org.apache.skywalking.apm.collector.storage.utils.DurationPoint)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)1 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)1 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)1