kylixs commented on a change in pull request #8983: URL: https://github.com/apache/dubbo/pull/8983#discussion_r762411552
########## File path: dubbo-common/src/main/java/org/apache/dubbo/common/metrics/collector/DefaultMetricsCollector.java ########## @@ -0,0 +1,181 @@ +/* + * 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.dubbo.common.metrics.collector; + +import org.apache.dubbo.common.metrics.event.BaseMetricsEvent; +import org.apache.dubbo.common.metrics.event.NewRTEvent; +import org.apache.dubbo.common.metrics.event.NewRequestEvent; +import org.apache.dubbo.common.metrics.listener.MetricsListener; +import org.apache.dubbo.common.metrics.model.MethodMetric; +import org.apache.dubbo.common.metrics.model.sample.GaugeMetricSample; +import org.apache.dubbo.common.metrics.model.sample.MetricSample; +import org.apache.dubbo.rpc.model.ApplicationModel; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicLong; + +import static org.apache.dubbo.common.metrics.model.MetricsCategory.REQUESTS; +import static org.apache.dubbo.common.metrics.model.MetricsCategory.RT; + +/** + * Default implementation of {@link MetricsCollector} + */ +public class DefaultMetricsCollector implements MetricsCollector { + + private Boolean collectEnabled = false; + private final List<MetricsListener> listeners = new ArrayList<>(); + private final ApplicationModel applicationModel; + private final String applicationName; + + private final Map<MethodMetric, AtomicLong> totalRequests = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> succeedRequests = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> failedRequests = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> processingRequests = new HashMap<>(); + + private final Map<MethodMetric, AtomicLong> lastRT = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> minRT = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> maxRT = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> avgRT = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> totalRT = new HashMap<>(); + private final Map<MethodMetric, AtomicLong> rtCount = new HashMap<>(); + + public DefaultMetricsCollector(ApplicationModel applicationModel) { + this.applicationModel = applicationModel; + this.applicationName = applicationModel.tryGetApplicationName(); + } + + public void setCollectEnabled(Boolean collectEnabled) { + this.collectEnabled = collectEnabled; + } + + public Boolean isCollectEnabled() { + return collectEnabled; + } + + public void addListener(MetricsListener listener) { + listeners.add(listener); + } + + public void increaseTotalRequests(String interfaceName, String methodName, String parameterTypesDesc, String group, String version) { + if (isCollectEnabled()) { + MethodMetric metric = new MethodMetric(applicationName, interfaceName, methodName, parameterTypesDesc, group, version); + AtomicLong count = totalRequests.computeIfAbsent(metric, k -> new AtomicLong(0L)); + count.incrementAndGet(); + + publishEvent(new NewRequestEvent(metric, NewRequestEvent.Type.TOTAL)); + } + } + + public void increaseSucceedRequests(String interfaceName, String methodName, String parameterTypesDesc, String group, String version) { + if (isCollectEnabled()) { + MethodMetric metric = new MethodMetric(applicationName, interfaceName, methodName, parameterTypesDesc, group, version); + AtomicLong count = succeedRequests.computeIfAbsent(metric, k -> new AtomicLong(0L)); + count.incrementAndGet(); + + publishEvent(new NewRequestEvent(metric, NewRequestEvent.Type.SUCCEED)); + } + } + + public void increaseFailedRequests(String interfaceName, String methodName, String parameterTypesDesc, String group, String version) { + if (isCollectEnabled()) { + MethodMetric metric = new MethodMetric(applicationName, interfaceName, methodName, parameterTypesDesc, group, version); + AtomicLong count = failedRequests.computeIfAbsent(metric, k -> new AtomicLong(0L)); + count.incrementAndGet(); + + publishEvent(new NewRequestEvent(metric, NewRequestEvent.Type.FAILED)); + } + } + + public void increaseProcessingRequests(String interfaceName, String methodName, String parameterTypesDesc, String group, String version) { + if (isCollectEnabled()) { + MethodMetric metric = new MethodMetric(applicationName, interfaceName, methodName, parameterTypesDesc, group, version); + AtomicLong count = processingRequests.computeIfAbsent(metric, k -> new AtomicLong(0L)); + count.incrementAndGet(); + } + } + + public void decreaseProcessingRequests(String interfaceName, String methodName, String parameterTypesDesc, String group, String version) { + if (isCollectEnabled()) { + MethodMetric metric = new MethodMetric(applicationName, interfaceName, methodName, parameterTypesDesc, group, version); + AtomicLong count = processingRequests.computeIfAbsent(metric, k -> new AtomicLong(0L)); + count.decrementAndGet(); + } + } + + public void addRT(String interfaceName, String methodName, String parameterTypesDesc, String group, String version, Long responseTime) { + if (isCollectEnabled()) { + MethodMetric metric = new MethodMetric(applicationName, interfaceName, methodName, parameterTypesDesc, group, version); + + AtomicLong last = lastRT.computeIfAbsent(metric, k -> new AtomicLong()); + last.set(responseTime); + + AtomicLong min = minRT.computeIfAbsent(metric, k -> new AtomicLong(Long.MAX_VALUE)); + if (responseTime < min.longValue()) { + min.set(responseTime); + } Review comment: This kind of compare and set is unsafe when execute concurrently. See about: ```java LongAccumulator maxId = new LongAccumulator(Long::max, 0); //replace 0 with desired initial value maxId.accumulate(newValue); //from each thread ``` pick from: https://stackoverflow.com/a/49027525 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org