AlbumenJ commented on code in PR #11493: URL: https://github.com/apache/dubbo/pull/11493#discussion_r1105219059
########## dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/collector/RegistryMetricsCollector.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.metrics.registry.collector; + +import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.config.ReferenceConfigBase; +import org.apache.dubbo.config.context.ConfigManager; +import org.apache.dubbo.metrics.collector.ApplicationMetricsCollector; +import org.apache.dubbo.metrics.collector.MetricsCollector; +import org.apache.dubbo.metrics.event.MetricsEvent; +import org.apache.dubbo.metrics.event.MetricsEventMulticaster; +import org.apache.dubbo.metrics.listener.MetricsLifeListener; +import org.apache.dubbo.metrics.model.sample.MetricSample; +import org.apache.dubbo.metrics.registry.collector.stat.RegistryStatComposite; +import org.apache.dubbo.metrics.registry.event.RegistryEvent; +import org.apache.dubbo.metrics.registry.event.RegistryMetricsEventMulticaster; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.model.ConsumerModel; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Optional; + + +/** + * Registry implementation of {@link MetricsCollector} + */ +@Activate +public class RegistryMetricsCollector implements ApplicationMetricsCollector<RegistryEvent.Type>, MetricsLifeListener<RegistryEvent> { + + private Boolean collectEnabled = null; + private final RegistryStatComposite stats; + private final MetricsEventMulticaster registryMulticaster; + private final ApplicationModel applicationModel; + + public RegistryMetricsCollector(ApplicationModel applicationModel) { + this.stats = new RegistryStatComposite(); + this.registryMulticaster = new RegistryMetricsEventMulticaster(); + this.applicationModel = applicationModel; + } + + public void setCollectEnabled(Boolean collectEnabled) { + if (collectEnabled != null) { + this.collectEnabled = collectEnabled; + } + } + + public boolean isCollectEnabled() { + if (collectEnabled == null) { + ConfigManager configManager = applicationModel.getApplicationConfigManager(); + configManager.getMetrics().ifPresent(metricsConfig -> setCollectEnabled(metricsConfig.getEnableRegistry())); + } + return Optional.ofNullable(collectEnabled).orElse(false); + } + + public void setNum(RegistryEvent.Type registryType, String applicationName, Map<String, Integer> lastNumMap) { + lastNumMap.forEach((serviceKey, num) -> + this.stats.setServiceKey(registryType, applicationName, serviceKey, num)); + } + + + @Override + public void increment(RegistryEvent.Type registryType, String applicationName) { + this.stats.increment(registryType, applicationName); + } + + @Override + public void addRT(String applicationName, String registryOpType, Long responseTime) { + stats.calcRt(applicationName, registryOpType, responseTime); + } + + @Override + public List<MetricSample> collect() { + if (!isCollectEnabled()) { + new ArrayList<>(); + } + List<MetricSample> list = new ArrayList<>(); + list.addAll(stats.exportNumMetrics()); + list.addAll(stats.exportRtMetrics()); + //Dictionary url statistics + statsDictionary(); + list.addAll(stats.exportSkMetrics()); + + return list; + } + + private void statsDictionary() { + Collection<ConsumerModel> consumerModels = applicationModel.getApplicationServiceRepository().allConsumerModels(); + for (ConsumerModel consumerModel : consumerModels) { + ReferenceConfigBase<?> referenceConfig = consumerModel.getReferenceConfig(); + this.stats.setServiceKey(RegistryEvent.Type.D_TOTAL, applicationModel.getApplicationName(), consumerModel.getServiceKey(), referenceConfig.getInvokerNum()); Review Comment: Get invokers size from consumer model. See `org.apache.dubbo.qos.command.impl.Ls`. ########## dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java: ########## @@ -358,15 +365,33 @@ private void initMetricsService() { metricsServiceExporter.init(); } + @SuppressWarnings({"rawtypes"}) private void initMetricsReporter() { - DefaultMetricsCollector collector = - applicationModel.getFrameworkModel().getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class); + + ScopeBeanFactory beanFactory = applicationModel.getFrameworkModel().getBeanFactory(); MetricsConfig metricsConfig = configManager.getMetrics().orElse(null); + this.eventMulticaster = beanFactory.getOrRegisterBean(SimpleMetricsEventMulticaster.class); + // TODO compatible with old usage of metrics, remove protocol check after new metrics is ready for use. if (metricsConfig != null && PROTOCOL_PROMETHEUS.equals(metricsConfig.getProtocol())) { + + DefaultMetricsCollector collector = + beanFactory.getOrRegisterBean(DefaultMetricsCollector.class); collector.setCollectEnabled(true); - collector.addApplicationInfo(applicationModel.getApplicationName(), Version.getVersion()); + eventMulticaster.setAvailable(); + + List<MetricsListener> metricsListeners = applicationModel.getExtensionLoader(MetricsListener.class) + .getActivateExtensions(); + metricsListeners.forEach(this.eventMulticaster::addListener); + + List<MetricsCollector> customizeCollectors = applicationModel.getExtensionLoader(MetricsCollector.class) + .getActivateExtensions(); + for (MetricsCollector customizeCollector : customizeCollectors) { + beanFactory.registerBean(customizeCollector); + } Review Comment: Refactor `MetricsListener` and `MetricsCollector` to scope bean. If there is more that one implmentation, introduce a factory to hold `MetricsListener` and `MetricsCollector`. Depends on `tExtensionLoader`'s siglton may cause some unexpected result. ########## dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ReferenceConfig.java: ########## @@ -772,4 +774,24 @@ public Invoker<?> getInvoker() { public Runnable getDestroyRunner() { return this::destroy; } + + @Override + public int getInvokerNum() { + Invoker<?> invoker = getInvoker(); + if (invoker instanceof MigrationInvoker) { + AbstractDirectory<?> directory = (AbstractDirectory<?>) ((MigrationInvoker<?>) invoker).getDirectory(); + return directory.getAllInvokers().size(); + } + return super.getInvokerNum(); + } + + @Override + public int getValidInvokerNum() { + Invoker<?> invoker = getInvoker(); + if (invoker instanceof MigrationInvoker) { + AbstractDirectory<?> directory = (AbstractDirectory<?>) ((MigrationInvoker<?>) invoker).getDirectory(); + return directory.getValidInvokers().size(); + } + return super.getValidInvokerNum(); + } } Review Comment: `ReferenceConfig` itself should not depends on `MigrationInvoker`. Get invokers size from consumer model. See `org.apache.dubbo.qos.command.impl.Ls`. -- 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