Configuration center metrics separate module (#12096)

* init config module

* remove old

* add pom

* add pom

* add in artifact

* use consts

* add comment

---------

Co-authored-by: x-shadow-man <1494445739@qq.com>
Co-authored-by: songxiaosheng <songxiaosheng@elastic.link>
Co-authored-by: Albumen Kevin <jhq0812@gmail.com>
This commit is contained in:
wxbty 2023-04-19 18:33:13 +08:00 committed by GitHub
parent 742e035a8a
commit d6f9d8d38f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 322 additions and 81 deletions

View File

@ -60,6 +60,7 @@ dubbo-metrics-default
dubbo-metrics-metadata
dubbo-metrics-prometheus
dubbo-metrics-registry
dubbo-metrics-config-center
dubbo-monitor
dubbo-monitor-api
dubbo-monitor-default

View File

@ -66,6 +66,12 @@
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-monitor-api</artifactId>
@ -227,5 +233,6 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.config.ReferenceCache;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory;
import org.apache.dubbo.common.config.configcenter.wrapper.CompositeDynamicConfiguration;
@ -50,8 +51,8 @@ import org.apache.dubbo.config.utils.CompositeReferenceCache;
import org.apache.dubbo.config.utils.ConfigValidationUtils;
import org.apache.dubbo.metadata.report.MetadataReportFactory;
import org.apache.dubbo.metadata.report.MetadataReportInstance;
import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.registry.event.RegistryEvent;
import org.apache.dubbo.metrics.report.MetricsReporter;
@ -799,8 +800,6 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
}
}
ApplicationModel applicationModel = getApplicationModel();
ConfigCenterMetricsCollector collector =
applicationModel.getBeanFactory().getOrRegisterBean(ConfigCenterMetricsCollector.class);
if (StringUtils.isNotEmpty(configCenter.getConfigFile())) {
String configContent = dynamicConfiguration.getProperties(configCenter.getConfigFile(), configCenter.getGroup());
@ -819,11 +818,11 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
environment.updateAppExternalConfigMap(appConfigMap);
// Add metrics
collector.increase4Initialized(configCenter.getConfigFile(), configCenter.getGroup(),
configCenter.getProtocol(), applicationModel.getApplicationName(), configMap.size());
MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, configCenter.getConfigFile(), configCenter.getGroup(),
configCenter.getProtocol(), ConfigChangeType.ADDED.name(), configMap.size()));
if (isNotEmpty(appGroup)) {
collector.increase4Initialized(appConfigFile, appGroup,
configCenter.getProtocol(), applicationModel.getApplicationName(), appConfigMap.size());
MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, appConfigFile, appGroup,
configCenter.getProtocol(), ConfigChangeType.ADDED.name(), appConfigMap.size()));
}
} catch (IOException e) {
throw new IllegalStateException("Failed to parse configurations from Config Center.", e);
@ -1153,6 +1152,7 @@ public class DefaultApplicationDeployer extends AbstractDeployer<ApplicationMode
collector.registryDefaultSample();
}
}
private void completeStartFuture(boolean success) {
if (startFuture != null) {
startFuture.complete(success);

View File

@ -63,5 +63,10 @@
<version>${apollo_mock_server_version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -16,15 +16,6 @@
*/
package org.apache.dubbo.configcenter.support.apollo;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigFile;
@ -33,7 +24,16 @@ import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.enums.ConfigSourceType;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.Arrays;
@ -52,6 +52,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.CONFIG_NAMESPACE
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FAILED_CLOSE_CONNECT_APOLLO;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FAILED_CONNECT_REGISTRY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_NOT_EFFECT_EMPTY_RULE_APOLLO;
import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
/**
* Apollo implementation, https://github.com/ctripcorp/apollo
@ -250,9 +251,8 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration {
ConfigChangedEvent event = new ConfigChangedEvent(key, change.getNamespace(), change.getNewValue(), getChangeType(change));
listeners.forEach(listener -> listener.process(event));
ConfigCenterMetricsCollector collector =
applicationModel.getBeanFactory().getBean(ConfigCenterMetricsCollector.class);
collector.increaseUpdated("apollo", applicationModel.getApplicationName(), event);
MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, event.getKey(), event.getGroup(),
ConfigCenterEvent.APOLLO_PROTOCOL, ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE));
}
}

View File

@ -56,5 +56,10 @@
<artifactId>dubbo-metrics-prometheus</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -17,14 +17,11 @@
package org.apache.dubbo.configcenter.support.nacos;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
@ -37,15 +34,18 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.common.utils.MD5Utils;
import org.apache.dubbo.common.utils.StringUtils;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
import com.alibaba.nacos.api.exception.NacosException;
import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
import static com.alibaba.nacos.api.PropertyKeyConst.PASSWORD;
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME;
@ -55,6 +55,7 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_INT
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
import static org.apache.dubbo.common.utils.StringConstantFieldValuePredicate.of;
import static org.apache.dubbo.common.utils.StringUtils.HYPHEN_CHAR;
import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
/**
* The nacos implementation of {@link DynamicConfiguration}
@ -345,9 +346,8 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
}
listeners.forEach(listener -> listener.process(event));
ConfigCenterMetricsCollector collector =
applicationModel.getBeanFactory().getOrRegisterBean(ConfigCenterMetricsCollector.class);
collector.increaseUpdated("nacos", applicationModel.getApplicationName(), event);
MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, event.getKey(), event.getGroup(),
ConfigCenterEvent.NACOS_PROTOCOL, ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE));
}
void addListener(ConfigurationListener configurationListener) {

View File

@ -75,6 +75,11 @@
<artifactId>dubbo-metrics-prometheus</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
<profiles>

View File

@ -20,7 +20,8 @@ import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.remoting.zookeeper.DataListener;
import org.apache.dubbo.remoting.zookeeper.EventType;
import org.apache.dubbo.rpc.model.ApplicationModel;
@ -28,6 +29,8 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
/**
* one path has multi configurationListeners
*/
@ -77,9 +80,8 @@ public class ZookeeperDataListener implements DataListener {
listeners.forEach(listener -> listener.process(configChangeEvent));
}
ConfigCenterMetricsCollector collector =
applicationModel.getBeanFactory().getBean(ConfigCenterMetricsCollector.class);
collector.increaseUpdated("zookeeper", applicationModel.getApplicationName(), configChangeEvent);
MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, configChangeEvent.getKey(), configChangeEvent.getGroup(),
ConfigCenterEvent.ZK_PROTOCOL, ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE));
}
}

View File

@ -211,6 +211,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- monitor -->
<dependency>
@ -521,6 +528,7 @@
<include>org.apache.dubbo:dubbo-metrics-default</include>
<include>org.apache.dubbo:dubbo-metrics-registry</include>
<include>org.apache.dubbo:dubbo-metrics-metadata</include>
<include>org.apache.dubbo:dubbo-metrics-config-center</include>
<include>org.apache.dubbo:dubbo-metrics-prometheus</include>
<include>org.apache.dubbo:dubbo-monitor-api</include>
<include>org.apache.dubbo:dubbo-monitor-default</include>

View File

@ -250,6 +250,11 @@
<artifactId>dubbo-metrics-metadata</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
<version>${project.version}</version>
</dependency>
<!-- monitor -->
<dependency>

View File

@ -18,5 +18,5 @@
package org.apache.dubbo.metrics.model.key;
public enum MetricsLevel {
APP,SERVICE
APP,SERVICE,CONFIG
}

View File

@ -25,6 +25,10 @@ public class TypeWrapper {
private final MetricsKey finishType;
private final MetricsKey errorType;
public TypeWrapper(MetricsLevel level, MetricsKey postType) {
this(level, postType, null, null);
}
public TypeWrapper(MetricsLevel level, MetricsKey postType, MetricsKey finishType, MetricsKey errorType) {
this.level = level;
this.postType = postType;
@ -36,10 +40,6 @@ public class TypeWrapper {
return level;
}
public MetricsKey getErrorType() {
return errorType;
}
public boolean isAssignableFrom(Object type) {
Assert.notNull(type, "Type can not be null");
return type.equals(postType) || type.equals(finishType) || type.equals(errorType);

View File

@ -0,0 +1,34 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>dubbo-metrics-config-center</artifactId>
<name>${project.artifactId}</name>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,26 @@
/*
* 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.config;
public interface ConfigCenterMetricsConstants {
String ATTACHMENT_KEY_CONFIG_FILE = "configFileKey";
String ATTACHMENT_KEY_CONFIG_GROUP = "configGroup";
String ATTACHMENT_KEY_CONFIG_PROTOCOL = "configProtocol";
String ATTACHMENT_KEY_CHANGE_TYPE = "configChangeType";
}

View File

@ -15,10 +15,16 @@
* limitations under the License.
*/
package org.apache.dubbo.metrics.collector;
package org.apache.dubbo.metrics.config.collector;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.metrics.collector.CombMetricsCollector;
import org.apache.dubbo.metrics.collector.MetricsCollector;
import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
import org.apache.dubbo.metrics.config.event.ConfigCenterMetricsDispatcher;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.ConfigCenterMetric;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
@ -28,25 +34,28 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_METRICS_CONFIGCENTER_ENABLE;
import static org.apache.dubbo.metrics.model.MetricsCategory.CONFIGCENTER;
public class ConfigCenterMetricsCollector implements MetricsCollector {
private boolean collectEnabled = true;
/**
* Config center implementation of {@link MetricsCollector}
*/
@Activate
public class ConfigCenterMetricsCollector extends CombMetricsCollector<TimeCounterEvent> {
private Boolean collectEnabled = null;
private final ApplicationModel applicationModel;
private final Map<ConfigCenterMetric, AtomicLong> updatedMetrics = new ConcurrentHashMap<>();
public ConfigCenterMetricsCollector(ApplicationModel applicationModel) {
super(null);
this.applicationModel = applicationModel;
// default is true, disable when config false
if ("false".equals(System.getProperty(DUBBO_METRICS_CONFIGCENTER_ENABLE))) {
collectEnabled = false;
}
super.setEventMulticaster(new ConfigCenterMetricsDispatcher(this));
}
public void setCollectEnabled(Boolean collectEnabled) {
@ -57,29 +66,21 @@ public class ConfigCenterMetricsCollector implements MetricsCollector {
@Override
public boolean isCollectEnabled() {
return collectEnabled;
if (collectEnabled == null) {
ConfigManager configManager = applicationModel.getApplicationConfigManager();
configManager.getMetrics().ifPresent(metricsConfig -> setCollectEnabled(metricsConfig.getEnableMetadata()));
}
return Optional.ofNullable(collectEnabled).orElse(true);
}
public void increase4Initialized(String key, String group, String protocol, String applicationName, int count) {
public void increase(String key, String group, String protocol, String changeTypeName, int size) {
if (!isCollectEnabled()) {
return;
}
if (count <= 0) {
return;
}
ConfigCenterMetric metric = new ConfigCenterMetric(applicationName, key, group, protocol, ConfigChangeType.ADDED.name());
AtomicLong aLong = updatedMetrics.computeIfAbsent(metric, k -> new AtomicLong(0L));
aLong.addAndGet(count);
ConfigCenterMetric metric = new ConfigCenterMetric(applicationModel.getApplicationName(), key, group, protocol, changeTypeName);
updatedMetrics.computeIfAbsent(metric, k -> new AtomicLong(0L)).addAndGet(size);
}
public void increaseUpdated(String protocol, String applicationName, ConfigChangedEvent event) {
if (!isCollectEnabled()) {
return;
}
ConfigCenterMetric metric = new ConfigCenterMetric(applicationName, event.getKey(), event.getGroup(), protocol, event.getChangeType().name());
AtomicLong count = updatedMetrics.computeIfAbsent(metric, k -> new AtomicLong(0L));
count.incrementAndGet();
}
@Override
public List<MetricSample> collect() {
@ -88,12 +89,14 @@ public class ConfigCenterMetricsCollector implements MetricsCollector {
if (!isCollectEnabled()) {
return list;
}
collect(list);
updatedMetrics.forEach((k, v) -> list.add(new GaugeMetricSample<>(MetricsKey.CONFIGCENTER_METRIC_TOTAL, k.getTags(), CONFIGCENTER, v, AtomicLong::get)));
return list;
}
private void collect(List<MetricSample> list) {
updatedMetrics.forEach((k, v) -> list.add(new GaugeMetricSample<>(MetricsKey.CONFIGCENTER_METRIC_TOTAL, k.getTags(), CONFIGCENTER, v, AtomicLong::get)));
@Override
public boolean isSupport(MetricsEvent event) {
return event instanceof ConfigCenterEvent;
}
}

View File

@ -0,0 +1,70 @@
/*
* 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.config.event;
import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.key.MetricsLevel;
import org.apache.dubbo.metrics.model.key.TypeWrapper;
import org.apache.dubbo.rpc.model.ApplicationModel;
import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CHANGE_TYPE;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_FILE;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_GROUP;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_PROTOCOL;
import static org.apache.dubbo.metrics.model.key.MetricsKey.CONFIGCENTER_METRIC_TOTAL;
/**
* Registry related events
* Triggered in three types of configuration centers (apollo, zk, nacos)
*/
public class ConfigCenterEvent extends TimeCounterEvent {
public static final String NACOS_PROTOCOL = "nacos";
public static final String APOLLO_PROTOCOL = "apollo";
public static final String ZK_PROTOCOL = "zookeeper";
public ConfigCenterEvent(ApplicationModel applicationModel, TypeWrapper typeWrapper) {
super(applicationModel);
super.typeWrapper = typeWrapper;
ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
ConfigCenterMetricsCollector collector;
if (!beanFactory.isDestroyed()) {
collector = beanFactory.getBean(ConfigCenterMetricsCollector.class);
super.setAvailable(collector != null && collector.isCollectEnabled());
}
}
public static ConfigCenterEvent toChangeEvent(ApplicationModel applicationModel, String key, String group, String protocol, String changeType, int count) {
ConfigCenterEvent configCenterEvent = new ConfigCenterEvent(applicationModel, new TypeWrapper(MetricsLevel.CONFIG, CONFIGCENTER_METRIC_TOTAL));
configCenterEvent.putAttachment(ATTACHMENT_KEY_CONFIG_FILE, key);
configCenterEvent.putAttachment(ATTACHMENT_KEY_CONFIG_GROUP, group);
configCenterEvent.putAttachment(ATTACHMENT_KEY_CONFIG_PROTOCOL, protocol);
configCenterEvent.putAttachment(ATTACHMENT_KEY_CHANGE_TYPE, changeType);
configCenterEvent.putAttachment(ATTACHMENT_KEY_SIZE, count);
return configCenterEvent;
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.config.event;
import org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.listener.AbstractMetricsListener;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CHANGE_TYPE;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_FILE;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_GROUP;
import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_PROTOCOL;
public final class ConfigCenterMetricsDispatcher extends SimpleMetricsEventMulticaster {
public ConfigCenterMetricsDispatcher(ConfigCenterMetricsCollector collector) {
super.addListener(new AbstractMetricsListener(MetricsKey.CONFIGCENTER_METRIC_TOTAL) {
@Override
public boolean isSupport(MetricsEvent event) {
return event instanceof ConfigCenterEvent;
}
@Override
public void onEvent(TimeCounterEvent event) {
collector.increase(
event.getAttachmentValue(ATTACHMENT_KEY_CONFIG_FILE),
event.getAttachmentValue(ATTACHMENT_KEY_CONFIG_GROUP),
event.getAttachmentValue(ATTACHMENT_KEY_CONFIG_PROTOCOL),
event.getAttachmentValue(ATTACHMENT_KEY_CHANGE_TYPE),
event.getAttachmentValue(ATTACHMENT_KEY_SIZE)
);
}
});
}
}

View File

@ -0,0 +1 @@
config-collector=org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector

View File

@ -20,6 +20,7 @@ package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
@ -33,6 +34,7 @@ import java.util.List;
import java.util.Map;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME;
import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
class ConfigCenterMetricsCollectorTest {
@ -59,8 +61,8 @@ class ConfigCenterMetricsCollectorTest {
ConfigCenterMetricsCollector collector = new ConfigCenterMetricsCollector(applicationModel);
collector.setCollectEnabled(true);
String applicationName = applicationModel.getApplicationName();
collector.increase4Initialized("key", "group", "nacos", applicationName, 1);
collector.increase4Initialized("key", "group", "nacos", applicationName, 1);
collector.increase("key", "group", "nacos", ConfigChangeType.ADDED.name(), 1);
collector.increase("key", "group", "nacos", ConfigChangeType.ADDED.name(), 1);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {
@ -80,9 +82,12 @@ class ConfigCenterMetricsCollectorTest {
String applicationName = applicationModel.getApplicationName();
ConfigChangedEvent event = new ConfigChangedEvent("key", "group", null, ConfigChangeType.ADDED);
collector.increaseUpdated("nacos", applicationName, event);
collector.increaseUpdated("nacos", applicationName, event);
collector.increase(event.getKey(), event.getGroup(),
"apollo", ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE);
collector.increase(event.getKey(), event.getGroup(),
"apollo", ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE);
List<MetricSample> samples = collector.collect();
for (MetricSample sample : samples) {

View File

@ -23,6 +23,7 @@
<module>dubbo-metrics-registry</module>
<module>dubbo-metrics-metadata</module>
<module>dubbo-metrics-prometheus</module>
<module>dubbo-metrics-config-center</module>
</modules>
<parent>
<groupId>org.apache.dubbo</groupId>

View File

@ -157,6 +157,11 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-metadata</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-config-center</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-prometheus</artifactId>