ServiceReferenceMetricSpanListener.java 10.8 KB
Newer Older
P
peng-yongsheng 已提交
1
/*
wu-sheng's avatar
wu-sheng 已提交
2 3 4 5 6 7
 * 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
P
peng-yongsheng 已提交
8 9 10 11 12 13 14 15 16 17 18
 *
 *     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.
 *
 */

19
package org.apache.skywalking.apm.collector.analysis.metric.provider.worker.service.refmetric;
P
peng-yongsheng 已提交
20 21 22

import java.util.LinkedList;
import java.util.List;
P
peng-yongsheng 已提交
23
import org.apache.skywalking.apm.collector.analysis.metric.define.graph.MetricGraphIdDefine;
P
peng-yongsheng 已提交
24 25 26 27 28
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.decorator.ReferenceDecorator;
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.decorator.SpanDecorator;
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.listener.EntrySpanListener;
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.listener.ExitSpanListener;
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.listener.FirstSpanListener;
29 30
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.listener.SpanListener;
import org.apache.skywalking.apm.collector.analysis.segment.parser.define.listener.SpanListenerFactory;
31
import org.apache.skywalking.apm.collector.cache.CacheModule;
32
import org.apache.skywalking.apm.collector.cache.service.ApplicationCacheService;
33
import org.apache.skywalking.apm.collector.cache.service.InstanceCacheService;
34 35
import org.apache.skywalking.apm.collector.core.graph.Graph;
import org.apache.skywalking.apm.collector.core.graph.GraphManager;
36
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
37 38 39
import org.apache.skywalking.apm.collector.core.util.Const;
import org.apache.skywalking.apm.collector.core.util.ObjectUtils;
import org.apache.skywalking.apm.collector.core.util.TimeBucketUtils;
P
peng-yongsheng 已提交
40
import org.apache.skywalking.apm.collector.storage.table.MetricSource;
41 42
import org.apache.skywalking.apm.collector.storage.table.service.ServiceReferenceMetric;
import org.apache.skywalking.apm.network.proto.SpanLayer;
P
peng-yongsheng 已提交
43 44 45 46 47 48
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author peng-yongsheng
 */
P
peng-yongsheng 已提交
49
public class ServiceReferenceMetricSpanListener implements FirstSpanListener, EntrySpanListener, ExitSpanListener {
P
peng-yongsheng 已提交
50

P
peng-yongsheng 已提交
51
    private final Logger logger = LoggerFactory.getLogger(ServiceReferenceMetricSpanListener.class);
P
peng-yongsheng 已提交
52

53 54 55 56
    private final InstanceCacheService instanceCacheService;
    private final ApplicationCacheService applicationCacheService;
    private final List<ServiceReferenceMetric> entryReferenceMetric;
    private List<ServiceReferenceMetric> exitReferenceMetric;
P
peng-yongsheng 已提交
57
    private SpanDecorator entrySpanDecorator;
P
peng-yongsheng 已提交
58 59
    private long timeBucket;

60
    ServiceReferenceMetricSpanListener(ModuleManager moduleManager) {
61 62
        this.entryReferenceMetric = new LinkedList<>();
        this.exitReferenceMetric = new LinkedList<>();
63
        this.instanceCacheService = moduleManager.find(CacheModule.NAME).getService(InstanceCacheService.class);
64
        this.applicationCacheService = moduleManager.find(CacheModule.NAME).getService(ApplicationCacheService.class);
65 66
    }

P
peng-yongsheng 已提交
67 68 69
    @Override
    public void parseFirst(SpanDecorator spanDecorator, int applicationId, int instanceId,
        String segmentId) {
P
peng-yongsheng 已提交
70
        this.timeBucket = TimeBucketUtils.INSTANCE.getMinuteTimeBucket(spanDecorator.getStartTime());
P
peng-yongsheng 已提交
71 72 73 74 75
    }

    @Override
    public void parseEntry(SpanDecorator spanDecorator, int applicationId, int instanceId,
        String segmentId) {
P
peng-yongsheng 已提交
76 77 78
        if (spanDecorator.getRefsCount() > 0) {
            for (int i = 0; i < spanDecorator.getRefsCount(); i++) {
                ReferenceDecorator reference = spanDecorator.getRefs(i);
79
                ServiceReferenceMetric serviceReferenceMetric = new ServiceReferenceMetric();
P
peng-yongsheng 已提交
80
                serviceReferenceMetric.setFrontServiceId(reference.getParentServiceId());
P
peng-yongsheng 已提交
81 82

                if (spanDecorator.getSpanLayer().equals(SpanLayer.MQ)) {
83 84
                    int applicationIdByPeerId = applicationCacheService.getApplicationIdByAddressId(reference.getNetworkAddressId());
                    int instanceIdByPeerId = instanceCacheService.getInstanceIdByAddressId(applicationIdByPeerId, reference.getNetworkAddressId());
P
peng-yongsheng 已提交
85 86 87 88 89 90
                    serviceReferenceMetric.setFrontInstanceId(instanceIdByPeerId);
                    serviceReferenceMetric.setFrontApplicationId(applicationIdByPeerId);
                } else {
                    serviceReferenceMetric.setFrontInstanceId(reference.getParentApplicationInstanceId());
                    serviceReferenceMetric.setFrontApplicationId(instanceCacheService.getApplicationId(reference.getParentApplicationInstanceId()));
                }
P
peng-yongsheng 已提交
91 92
                serviceReferenceMetric.setBehindServiceId(spanDecorator.getOperationNameId());
                serviceReferenceMetric.setBehindInstanceId(instanceId);
93
                serviceReferenceMetric.setBehindApplicationId(applicationId);
P
peng-yongsheng 已提交
94
                serviceReferenceMetric.setSourceValue(MetricSource.Callee.getValue());
95
                calculateDuration(serviceReferenceMetric, spanDecorator);
P
peng-yongsheng 已提交
96 97 98
                entryReferenceMetric.add(serviceReferenceMetric);
            }
        } else {
99
            ServiceReferenceMetric serviceReferenceMetric = new ServiceReferenceMetric();
P
peng-yongsheng 已提交
100
            serviceReferenceMetric.setFrontServiceId(Const.NONE_SERVICE_ID);
101 102
            serviceReferenceMetric.setFrontInstanceId(Const.NONE_INSTANCE_ID);
            serviceReferenceMetric.setFrontApplicationId(Const.NONE_APPLICATION_ID);
P
peng-yongsheng 已提交
103
            serviceReferenceMetric.setBehindServiceId(spanDecorator.getOperationNameId());
104
            serviceReferenceMetric.setBehindInstanceId(instanceId);
105
            serviceReferenceMetric.setBehindApplicationId(applicationId);
P
peng-yongsheng 已提交
106
            serviceReferenceMetric.setSourceValue(MetricSource.Callee.getValue());
P
peng-yongsheng 已提交
107

108
            calculateDuration(serviceReferenceMetric, spanDecorator);
P
peng-yongsheng 已提交
109 110 111
            entryReferenceMetric.add(serviceReferenceMetric);
        }
        this.entrySpanDecorator = spanDecorator;
P
peng-yongsheng 已提交
112 113
    }

P
peng-yongsheng 已提交
114
    @Override public void parseExit(SpanDecorator spanDecorator, int applicationId, int instanceId, String segmentId) {
115
        ServiceReferenceMetric serviceReferenceMetric = new ServiceReferenceMetric();
116

117 118 119 120 121
        int peerId = spanDecorator.getPeerId();
        int behindApplicationId = applicationCacheService.getApplicationIdByAddressId(peerId);
        int behindInstanceId = instanceCacheService.getInstanceIdByAddressId(behindApplicationId, peerId);

        serviceReferenceMetric.setFrontServiceId(Const.NONE_SERVICE_ID);
P
peng-yongsheng 已提交
122
        serviceReferenceMetric.setFrontInstanceId(instanceId);
123
        serviceReferenceMetric.setFrontApplicationId(applicationId);
P
peng-yongsheng 已提交
124
        serviceReferenceMetric.setBehindServiceId(spanDecorator.getOperationNameId());
125 126
        serviceReferenceMetric.setBehindInstanceId(behindInstanceId);
        serviceReferenceMetric.setBehindApplicationId(behindApplicationId);
P
peng-yongsheng 已提交
127
        serviceReferenceMetric.setSourceValue(MetricSource.Caller.getValue());
128
        calculateDuration(serviceReferenceMetric, spanDecorator);
P
peng-yongsheng 已提交
129 130 131
        exitReferenceMetric.add(serviceReferenceMetric);
    }

132
    private void calculateDuration(ServiceReferenceMetric serviceReferenceMetric, SpanDecorator spanDecorator) {
133
        long duration = spanDecorator.getEndTime() - spanDecorator.getStartTime();
134

P
peng-yongsheng 已提交
135
        if (spanDecorator.getIsError()) {
136 137
            serviceReferenceMetric.setTransactionErrorCalls(1L);
            serviceReferenceMetric.setTransactionErrorDurationSum(duration);
P
peng-yongsheng 已提交
138
        }
P
peng-yongsheng 已提交
139 140
        serviceReferenceMetric.setTransactionCalls(1L);
        serviceReferenceMetric.setTransactionDurationSum(duration);
P
peng-yongsheng 已提交
141

P
peng-yongsheng 已提交
142 143 144 145
        if (SpanLayer.MQ.equals(spanDecorator.getSpanLayer())) {
            if (spanDecorator.getIsError()) {
                serviceReferenceMetric.setMqTransactionErrorCalls(1L);
                serviceReferenceMetric.setMqTransactionErrorDurationSum(duration);
P
peng-yongsheng 已提交
146
            }
P
peng-yongsheng 已提交
147 148
            serviceReferenceMetric.setMqTransactionCalls(1L);
            serviceReferenceMetric.setMqTransactionDurationSum(duration);
149 150 151 152 153 154 155
        } else {
            if (spanDecorator.getIsError()) {
                serviceReferenceMetric.setBusinessTransactionErrorCalls(1L);
                serviceReferenceMetric.setBusinessTransactionErrorDurationSum(duration);
            }
            serviceReferenceMetric.setBusinessTransactionCalls(1L);
            serviceReferenceMetric.setBusinessTransactionDurationSum(duration);
P
peng-yongsheng 已提交
156 157 158
        }
    }

P
peng-yongsheng 已提交
159 160
    @Override public void build() {
        logger.debug("service reference listener build");
P
peng-yongsheng 已提交
161
        Graph<ServiceReferenceMetric> graph = GraphManager.INSTANCE.findGraph(MetricGraphIdDefine.SERVICE_REFERENCE_METRIC_GRAPH_ID, ServiceReferenceMetric.class);
P
peng-yongsheng 已提交
162
        entryReferenceMetric.forEach(serviceReferenceMetric -> {
163 164
            String metricId = serviceReferenceMetric.getFrontServiceId() + Const.ID_SPLIT + serviceReferenceMetric.getBehindServiceId() + Const.ID_SPLIT + serviceReferenceMetric.getSourceValue();
            String id = timeBucket + Const.ID_SPLIT + metricId;
P
peng-yongsheng 已提交
165

P
peng-yongsheng 已提交
166
            serviceReferenceMetric.setId(id);
167
            serviceReferenceMetric.setMetricId(metricId);
P
peng-yongsheng 已提交
168 169
            serviceReferenceMetric.setTimeBucket(timeBucket);
            logger.debug("push to service reference aggregation worker, id: {}", serviceReferenceMetric.getId());
P
peng-yongsheng 已提交
170

P
peng-yongsheng 已提交
171 172
            graph.start(serviceReferenceMetric);
        });
P
peng-yongsheng 已提交
173

P
peng-yongsheng 已提交
174 175 176 177 178 179
        exitReferenceMetric.forEach(serviceReferenceMetric -> {
            if (ObjectUtils.isNotEmpty(entrySpanDecorator)) {
                serviceReferenceMetric.setFrontServiceId(entrySpanDecorator.getOperationNameId());
            } else {
                serviceReferenceMetric.setFrontServiceId(Const.NONE_SERVICE_ID);
            }
P
peng-yongsheng 已提交
180

181 182
            String metricId = serviceReferenceMetric.getFrontServiceId() + Const.ID_SPLIT + serviceReferenceMetric.getBehindServiceId() + Const.ID_SPLIT + serviceReferenceMetric.getSourceValue();
            String id = timeBucket + Const.ID_SPLIT + metricId;
P
peng-yongsheng 已提交
183
            serviceReferenceMetric.setId(id);
184
            serviceReferenceMetric.setMetricId(metricId);
P
peng-yongsheng 已提交
185
            serviceReferenceMetric.setTimeBucket(timeBucket);
P
peng-yongsheng 已提交
186

P
peng-yongsheng 已提交
187 188
            graph.start(serviceReferenceMetric);
        });
P
peng-yongsheng 已提交
189
    }
190 191 192

    public static class Factory implements SpanListenerFactory {
        @Override public SpanListener create(ModuleManager moduleManager) {
193
            return new ServiceReferenceMetricSpanListener(moduleManager);
194 195
        }
    }
P
peng-yongsheng 已提交
196
}