This is an automated email from the ASF dual-hosted git repository.

albumenj pushed a commit to branch 3.2
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.2 by this push:
     new 60799ec3d9 metadata listener refactor (#12016)
60799ec3d9 is described below

commit 60799ec3d929dabcdc937cbd8e918cf1ad19889c
Author: wxbty <[email protected]>
AuthorDate: Thu Apr 6 09:38:56 2023 +0800

    metadata listener refactor (#12016)
    
    Co-authored-by: x-shadow-man <[email protected]>
---
 .../report/support/AbstractMetadataReport.java     |   3 +-
 .../apache/dubbo/metrics/MetricsConstants.java}    |  11 +-
 .../event/SimpleMetricsEventMulticaster.java       |  10 --
 .../dubbo/metrics/metadata/MetadataConstants.java} |  15 +--
 .../collector/MetadataMetricsCollector.java        |   8 +-
 .../collector/stat/MetadataStatComposite.java      |  31 +++---
 .../metrics/metadata/event/MetadataEvent.java      | 120 ++++++---------------
 .../metrics/metadata/event/MetadataListener.java   |  67 ++++++++++++
 .../event/MetadataMetricsEventMulticaster.java     |  64 ++++++++++-
 .../metadata/event/MetricsPushListener.java        |  49 ---------
 .../metadata/event/MetricsSubscribeListener.java   |  49 ---------
 .../event/StoreProviderMetadataListener.java       |  49 ---------
 .../metrics/metadata/type/ApplicationType.java     |  51 +++++++++
 .../ServiceType.java}                              |  35 ++++--
 .../metadata/MetadataMetricsCollectorTest.java     |  20 ++--
 .../metadata/MetadataStatCompositeTest.java        |  10 +-
 .../dubbo/metrics/registry/RegistryConstants.java  |   5 -
 .../metrics/registry/event/RegistryEvent.java      |  21 ++--
 .../event/RegistryMetricsEventMulticaster.java     |   7 +-
 .../registry/event/support/DirectorSupport.java    |   3 +-
 .../registry/client/AbstractServiceDiscovery.java  |   4 +-
 21 files changed, 298 insertions(+), 334 deletions(-)

diff --git 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
index 4b3d48d77b..99fe2fdfbb 100644
--- 
a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
+++ 
b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/report/support/AbstractMetadataReport.java
@@ -279,8 +279,7 @@ public abstract class AbstractMetadataReport implements 
MetadataReport {
 
     private void storeProviderMetadataTask(MetadataIdentifier 
providerMetadataIdentifier, ServiceDefinition serviceDefinition) {
 
-        String interfaceMethodName = serviceDefinition.getCanonicalName();
-        MetadataEvent metadataEvent = new 
MetadataEvent.StoreProviderMetadataEvent(applicationModel, interfaceMethodName);
+        MetadataEvent metadataEvent = 
MetadataEvent.toServiceSubscribeEvent(applicationModel, 
serviceDefinition.getCanonicalName());
         MetricsEventBus.post(metadataEvent, () ->
             {
                 boolean result = true;
diff --git 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
 
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java
similarity index 75%
copy from 
dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
copy to 
dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java
index 341ee385f9..a8becf252a 100644
--- 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
+++ 
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java
@@ -15,20 +15,13 @@
  * limitations under the License.
  */
 
-package org.apache.dubbo.metrics.registry;
+package org.apache.dubbo.metrics;
 
-public interface RegistryConstants {
+public interface MetricsConstants {
 
     String ATTACHMENT_KEY_SERVICE = "serviceKey";
     String ATTACHMENT_KEY_SIZE = "size";
     String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
     String ATTACHMENT_KEY_DIR_NUM = "dirNum";
 
-    String OP_TYPE_REGISTER = "register";
-    String OP_TYPE_SUBSCRIBE = "subscribe";
-    String OP_TYPE_NOTIFY = "notify";
-    String OP_TYPE_REGISTER_SERVICE = "register.service";
-    String OP_TYPE_SUBSCRIBE_SERVICE = "subscribe.service";
-
-
 }
diff --git 
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticaster.java
 
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticaster.java
index cc3843246f..70179a7445 100644
--- 
a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticaster.java
+++ 
b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticaster.java
@@ -31,16 +31,6 @@ import java.util.function.Consumer;
 public class SimpleMetricsEventMulticaster implements MetricsEventMulticaster {
     private final List<MetricsListener<?>> listeners = 
Collections.synchronizedList(new ArrayList<>());
 
-    private boolean available = false;
-
-    public void setAvailable() {
-        this.available = true;
-    }
-
-    public boolean isAvailable() {
-        return available;
-    }
-
     @Override
     public void addListener(MetricsListener<?> listener) {
         listeners.add(listener);
diff --git 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/MetadataConstants.java
similarity index 64%
copy from 
dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
copy to 
dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/MetadataConstants.java
index 341ee385f9..32c74d87a9 100644
--- 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/MetadataConstants.java
@@ -15,20 +15,13 @@
  * limitations under the License.
  */
 
-package org.apache.dubbo.metrics.registry;
+package org.apache.dubbo.metrics.metadata;
 
-public interface RegistryConstants {
+public interface MetadataConstants {
 
-    String ATTACHMENT_KEY_SERVICE = "serviceKey";
-    String ATTACHMENT_KEY_SIZE = "size";
-    String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
-    String ATTACHMENT_KEY_DIR_NUM = "dirNum";
-
-    String OP_TYPE_REGISTER = "register";
+    String OP_TYPE_PUSH = "push";
     String OP_TYPE_SUBSCRIBE = "subscribe";
-    String OP_TYPE_NOTIFY = "notify";
-    String OP_TYPE_REGISTER_SERVICE = "register.service";
-    String OP_TYPE_SUBSCRIBE_SERVICE = "subscribe.service";
+    String OP_TYPE_STORE_PROVIDER_INTERFACE = "store.provider.interface";
 
 
 }
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java
index a63946ae23..f34d34002d 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/MetadataMetricsCollector.java
@@ -24,8 +24,10 @@ 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.metadata.collector.stat.MetadataStatComposite;
+import org.apache.dubbo.metrics.metadata.type.ApplicationType;
 import org.apache.dubbo.metrics.metadata.event.MetadataEvent;
 import org.apache.dubbo.metrics.metadata.event.MetadataMetricsEventMulticaster;
+import org.apache.dubbo.metrics.metadata.type.ServiceType;
 import org.apache.dubbo.metrics.model.sample.MetricSample;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 
@@ -38,7 +40,7 @@ import java.util.Optional;
  * Registry implementation of {@link MetricsCollector}
  */
 @Activate
-public class MetadataMetricsCollector implements 
ApplicationMetricsCollector<MetadataEvent.ApplicationType, MetadataEvent> {
+public class MetadataMetricsCollector implements 
ApplicationMetricsCollector<ApplicationType, MetadataEvent> {
 
     private Boolean collectEnabled = null;
     private final MetadataStatComposite stats;
@@ -67,11 +69,11 @@ public class MetadataMetricsCollector implements 
ApplicationMetricsCollector<Met
     }
 
     @Override
-    public void increment(String applicationName, 
MetadataEvent.ApplicationType registryType) {
+    public void increment(String applicationName, ApplicationType 
registryType) {
         this.stats.increment(registryType, applicationName);
     }
 
-    public void incrementServiceKey(String applicationName, String serviceKey, 
MetadataEvent.ServiceType registryType, int size) {
+    public void incrementServiceKey(String applicationName, String serviceKey, 
ServiceType registryType, int size) {
         this.stats.incrementServiceKey(registryType, applicationName, 
serviceKey, size);
     }
 
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
index a01b34b03a..302c78344d 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/collector/stat/MetadataStatComposite.java
@@ -19,7 +19,8 @@ package org.apache.dubbo.metrics.metadata.collector.stat;
 
 import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
 import org.apache.dubbo.metrics.collector.MetricsCollector;
-import org.apache.dubbo.metrics.metadata.event.MetadataEvent;
+import org.apache.dubbo.metrics.metadata.type.ApplicationType;
+import org.apache.dubbo.metrics.metadata.type.ServiceType;
 import org.apache.dubbo.metrics.model.ApplicationMetric;
 import org.apache.dubbo.metrics.model.MetricsCategory;
 import org.apache.dubbo.metrics.model.MetricsKey;
@@ -40,6 +41,10 @@ import java.util.concurrent.atomic.LongAccumulator;
 import java.util.function.Function;
 import java.util.stream.Collectors;
 
+import static org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_PUSH;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_STORE_PROVIDER_INTERFACE;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_SUBSCRIBE;
+
 /**
  * As a data aggregator, use internal data containers calculates and classifies
  * the registry data collected by {@link MetricsCollector MetricsCollector}, 
and
@@ -48,20 +53,18 @@ import java.util.stream.Collectors;
 public class MetadataStatComposite implements MetricsExport {
 
 
-    public Map<MetadataEvent.ApplicationType, Map<String, AtomicLong>> 
applicationNumStats = new ConcurrentHashMap<>();
-    public Map<MetadataEvent.ServiceType, Map<ServiceKeyMetric, AtomicLong>> 
serviceNumStats = new ConcurrentHashMap<>();
+    public Map<ApplicationType, Map<String, AtomicLong>> applicationNumStats = 
new ConcurrentHashMap<>();
+    public Map<ServiceType, Map<ServiceKeyMetric, AtomicLong>> serviceNumStats 
= new ConcurrentHashMap<>();
     public List<LongContainer<? extends Number>> appRtStats = new 
ArrayList<>();
 
     public List<LongContainer<? extends Number>> serviceRtStats = new 
ArrayList<>();
-    public static String OP_TYPE_PUSH = "push";
-    public static String OP_TYPE_SUBSCRIBE = "subscribe";
-    public static String OP_TYPE_STORE_PROVIDER_INTERFACE = 
"store.provider.interface";
+
 
     public MetadataStatComposite() {
-        for (MetadataEvent.ApplicationType applicationType : 
MetadataEvent.ApplicationType.values()) {
+        for (ApplicationType applicationType : ApplicationType.values()) {
             applicationNumStats.put(applicationType, new 
ConcurrentHashMap<>());
         }
-        for (MetadataEvent.ServiceType serviceType : 
MetadataEvent.ServiceType.values()) {
+        for (ServiceType serviceType : ServiceType.values()) {
             serviceNumStats.put(serviceType, new ConcurrentHashMap<>());
         }
 
@@ -90,18 +93,18 @@ public class MetadataStatComposite implements MetricsExport 
{
         return singleRtStats;
     }
 
-    public void increment(MetadataEvent.ApplicationType type, String 
applicationName) {
+    public void increment(ApplicationType type, String applicationName) {
         incrementSize(type, applicationName, 1);
     }
 
-    public void incrementServiceKey(MetadataEvent.ServiceType type, String 
applicationName, String serviceKey, int size) {
+    public void incrementServiceKey(ServiceType type, String applicationName, 
String serviceKey, int size) {
         if (!serviceNumStats.containsKey(type)) {
             return;
         }
         serviceNumStats.get(type).computeIfAbsent(new 
ServiceKeyMetric(applicationName, serviceKey), k -> new 
AtomicLong(0L)).getAndAdd(size);
     }
 
-    public void incrementSize(MetadataEvent.ApplicationType type, String 
applicationName, int size) {
+    public void incrementSize(ApplicationType type, String applicationName, 
int size) {
         if (!applicationNumStats.containsKey(type)) {
             return;
         }
@@ -138,7 +141,7 @@ public class MetadataStatComposite implements MetricsExport 
{
     @SuppressWarnings("rawtypes")
     public List<GaugeMetricSample> exportNumMetrics() {
         List<GaugeMetricSample> list = new ArrayList<>();
-        for (MetadataEvent.ApplicationType type : 
applicationNumStats.keySet()) {
+        for (ApplicationType type : applicationNumStats.keySet()) {
             Map<String, AtomicLong> stringAtomicLongMap = 
applicationNumStats.get(type);
             for (String applicationName : stringAtomicLongMap.keySet()) {
                 list.add(convertToSample(applicationName, type, 
MetricsCategory.METADATA, stringAtomicLongMap.get(applicationName)));
@@ -159,7 +162,7 @@ public class MetadataStatComposite implements MetricsExport 
{
     @SuppressWarnings({"rawtypes"})
     public List<GaugeMetricSample> exportServiceNumMetrics() {
         List<GaugeMetricSample> list = new ArrayList<>();
-        for (MetadataEvent.ServiceType type : serviceNumStats.keySet()) {
+        for (ServiceType type : serviceNumStats.keySet()) {
             Map<ServiceKeyMetric, AtomicLong> stringAtomicLongMap = 
serviceNumStats.get(type);
             for (ServiceKeyMetric serviceKeyMetric : 
stringAtomicLongMap.keySet()) {
                 list.add(new GaugeMetricSample<>(type.getMetricsKey(), 
serviceKeyMetric.getTags(), MetricsCategory.METADATA, stringAtomicLongMap, 
value -> value.get(serviceKeyMetric).get()));
@@ -169,7 +172,7 @@ public class MetadataStatComposite implements MetricsExport 
{
     }
 
     @SuppressWarnings("rawtypes")
-    public GaugeMetricSample convertToSample(String applicationName, 
MetadataEvent.ApplicationType type, MetricsCategory category, AtomicLong 
targetNumber) {
+    public GaugeMetricSample convertToSample(String applicationName, 
ApplicationType type, MetricsCategory category, AtomicLong targetNumber) {
         return new GaugeMetricSample<>(type.getMetricsKey(), 
ApplicationMetric.getTagsByName(applicationName), category, targetNumber, 
AtomicLong::get);
     }
 
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataEvent.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataEvent.java
index c3ec36081a..c5b1f64f74 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataEvent.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataEvent.java
@@ -19,18 +19,29 @@ package org.apache.dubbo.metrics.metadata.event;
 
 import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
 import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.exception.MetricsNeverHappenException;
 import org.apache.dubbo.metrics.metadata.collector.MetadataMetricsCollector;
-import org.apache.dubbo.metrics.model.MetricsKey;
+import org.apache.dubbo.metrics.metadata.type.ApplicationType;
+import org.apache.dubbo.metrics.metadata.type.ServiceType;
+import org.apache.dubbo.metrics.model.MetricsLevel;
+import org.apache.dubbo.metrics.model.TypeWrapper;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
+
 /**
  * Registry related events
  */
 public class MetadataEvent extends TimeCounterEvent {
     private final MetadataMetricsCollector collector;
+    private final Map<String, Object> attachment = new HashMap<>(8);
 
-    public MetadataEvent(ApplicationModel applicationModel) {
+    public MetadataEvent(ApplicationModel applicationModel, TypeWrapper 
typeWrapper) {
         super(applicationModel);
+        super.typeWrapper = typeWrapper;
         ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
         if (beanFactory.isDestroyed()) {
             this.collector = null;
@@ -40,103 +51,38 @@ public class MetadataEvent extends TimeCounterEvent {
         }
     }
 
-    public ApplicationModel getSource() {
-        return source;
+    @SuppressWarnings("unchecked")
+    public <T> T getAttachmentValue(String key) {
+        if (!attachment.containsKey(key)) {
+            throw new MetricsNeverHappenException("Attachment key [" + key + 
"] not found");
+        }
+        return (T) attachment.get(key);
     }
 
-    public MetadataMetricsCollector getCollector() {
-        return collector;
+    public void putAttachment(String key, Object value) {
+        attachment.put(key, value);
     }
 
-    public enum ApplicationType {
-        P_TOTAL(MetricsKey.METADATA_PUSH_METRIC_NUM),
-        P_SUCCEED(MetricsKey.METADATA_PUSH_METRIC_NUM_SUCCEED),
-        P_FAILED(MetricsKey.METADATA_PUSH_METRIC_NUM_FAILED),
-
-        S_TOTAL(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM),
-        S_SUCCEED(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM_SUCCEED),
-        S_FAILED(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM_FAILED),
-
-        ;
-        private final MetricsKey metricsKey;
-        private final boolean isIncrement;
-
-        ApplicationType(MetricsKey metricsKey) {
-            this(metricsKey, true);
-        }
-
-        ApplicationType(MetricsKey metricsKey, boolean isIncrement) {
-            this.metricsKey = metricsKey;
-            this.isIncrement = isIncrement;
-        }
-
-        public MetricsKey getMetricsKey() {
-            return metricsKey;
-        }
-
-        public boolean isIncrement() {
-            return isIncrement;
-        }
+    public ApplicationModel getSource() {
+        return source;
     }
 
-    public enum ServiceType {
-
-        S_P_TOTAL(MetricsKey.STORE_PROVIDER_METADATA),
-        S_P_SUCCEED(MetricsKey.STORE_PROVIDER_METADATA_SUCCEED),
-        S_P_FAILED(MetricsKey.STORE_PROVIDER_METADATA_FAILED),
-
-        ;
-
-        private final MetricsKey metricsKey;
-        private final boolean isIncrement;
-
-
-        ServiceType(MetricsKey metricsKey) {
-            this(metricsKey, true);
-        }
-
-        ServiceType(MetricsKey metricsKey, boolean isIncrement) {
-            this.metricsKey = metricsKey;
-            this.isIncrement = isIncrement;
-        }
-
-        public MetricsKey getMetricsKey() {
-            return metricsKey;
-        }
-
-        public boolean isIncrement() {
-            return isIncrement;
-        }
+    public MetadataMetricsCollector getCollector() {
+        return collector;
     }
 
-    public static class PushEvent extends MetadataEvent {
-
-        public PushEvent(ApplicationModel applicationModel) {
-            super(applicationModel);
-        }
-
+    public static MetadataEvent toPushEvent(ApplicationModel applicationModel) 
{
+        return new MetadataEvent(applicationModel, new 
TypeWrapper(MetricsLevel.APP, ApplicationType.P_TOTAL, 
ApplicationType.P_SUCCEED, ApplicationType.P_FAILED));
     }
 
-    public static class SubscribeEvent extends MetadataEvent {
-
-        public SubscribeEvent(ApplicationModel applicationModel) {
-            super(applicationModel);
-        }
-
+    public static MetadataEvent toSubscribeEvent(ApplicationModel 
applicationModel) {
+        return new MetadataEvent(applicationModel, new 
TypeWrapper(MetricsLevel.APP, ApplicationType.S_TOTAL, 
ApplicationType.S_SUCCEED, ApplicationType.S_FAILED));
     }
 
-    public static class StoreProviderMetadataEvent extends MetadataEvent {
-        private final String serviceKey;
-
-        public StoreProviderMetadataEvent(ApplicationModel applicationModel, 
String serviceKey) {
-            super(applicationModel);
-            this.serviceKey = serviceKey;
-        }
-
-        public String getServiceKey() {
-            return serviceKey;
-        }
-
+    public static MetadataEvent toServiceSubscribeEvent(ApplicationModel 
applicationModel, String serviceKey) {
+        MetadataEvent metadataEvent = new MetadataEvent(applicationModel, new 
TypeWrapper(MetricsLevel.APP, ServiceType.S_P_TOTAL, ServiceType.S_P_SUCCEED, 
ServiceType.S_P_FAILED));
+        metadataEvent.putAttachment(ATTACHMENT_KEY_SERVICE, serviceKey);
+        return metadataEvent;
     }
 
 }
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataListener.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataListener.java
new file mode 100644
index 0000000000..68a8574fe7
--- /dev/null
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataListener.java
@@ -0,0 +1,67 @@
+/*
+ * 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.metadata.event;
+
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.listener.MetricsLifeListener;
+
+import java.util.function.BiConsumer;
+
+public abstract class MetadataListener implements 
MetricsLifeListener<MetadataEvent> {
+
+    private final Object enumType;
+
+    public MetadataListener(Object enumType) {
+        this.enumType = enumType;
+    }
+
+    @Override
+    public boolean isSupport(MetricsEvent event) {
+        return event.isAvailable() && event.isAssignableFrom(enumType);
+    }
+
+    static <T> MetadataListener onEvent(T enumType, BiConsumer<MetadataEvent, 
T> postFunc) {
+
+        return new MetadataListener(enumType) {
+            @Override
+            public void onEvent(MetadataEvent event) {
+                postFunc.accept(event, enumType);
+            }
+        };
+    }
+
+    static <T> MetadataListener onFinish(T enumType, BiConsumer<MetadataEvent, 
T> finishFunc) {
+
+        return new MetadataListener(enumType) {
+            @Override
+            public void onEventFinish(MetadataEvent event) {
+                finishFunc.accept(event, enumType);
+            }
+        };
+    }
+
+    static <T> MetadataListener onError(T enumType, BiConsumer<MetadataEvent, 
T> errorFunc) {
+
+        return new MetadataListener(enumType) {
+            @Override
+            public void onEventError(MetadataEvent event) {
+                errorFunc.accept(event, enumType);
+            }
+        };
+    }
+}
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
index 7bc3197b67..6e9694a78a 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
@@ -17,15 +17,71 @@
 
 package org.apache.dubbo.metrics.metadata.event;
 
+import org.apache.dubbo.metrics.MetricsConstants;
 import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
+import org.apache.dubbo.metrics.metadata.type.ApplicationType;
+import org.apache.dubbo.metrics.metadata.type.ServiceType;
+
+import static org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_PUSH;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_STORE_PROVIDER_INTERFACE;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_SUBSCRIBE;
 
 public final class MetadataMetricsEventMulticaster extends 
SimpleMetricsEventMulticaster {
 
     public MetadataMetricsEventMulticaster() {
-        super.addListener(new MetricsPushListener());
-        super.addListener(new MetricsSubscribeListener());
-        super.addListener(new StoreProviderMetadataListener());
-        setAvailable();
+        // MetricsPushListener
+        super.addListener(onPostEventBuild(ApplicationType.P_TOTAL));
+        super.addListener(onFinishEventBuild(ApplicationType.P_SUCCEED, 
OP_TYPE_PUSH));
+        super.addListener(onErrorEventBuild(ApplicationType.P_FAILED, 
OP_TYPE_PUSH));
+
+        // MetricsSubscribeListener
+        super.addListener(onPostEventBuild(ApplicationType.S_TOTAL));
+        super.addListener(onFinishEventBuild(ApplicationType.S_SUCCEED, 
OP_TYPE_SUBSCRIBE));
+        super.addListener(onErrorEventBuild(ApplicationType.S_FAILED, 
OP_TYPE_SUBSCRIBE));
+
+        // StoreProviderMetadataListener
+        super.addListener(MetadataListener.onEvent(ServiceType.S_P_TOTAL,
+            this::incrServiceKey
+        ));
+        super.addListener(MetadataListener.onFinish(ServiceType.S_P_SUCCEED,
+            this::incrAndRt
+        ));
+        super.addListener(MetadataListener.onError(ServiceType.S_P_FAILED,
+            this::incrAndRt
+        ));
+
+    }
+
+    private void incrAndRt(MetadataEvent event, ServiceType type) {
+        incrServiceKey(event, type);
+        
event.getCollector().addServiceKeyRT(event.getSource().getApplicationName(), 
event.getAttachmentValue(MetricsConstants.ATTACHMENT_KEY_SERVICE), 
OP_TYPE_STORE_PROVIDER_INTERFACE, event.getTimePair().calc());
+    }
+
+    private void incrServiceKey(MetadataEvent event, ServiceType type) {
+        
event.getCollector().incrementServiceKey(event.getSource().getApplicationName(),
 event.getAttachmentValue(MetricsConstants.ATTACHMENT_KEY_SERVICE), type, 1);
     }
 
+
+    private MetadataListener onPostEventBuild(ApplicationType applicationType) 
{
+        return MetadataListener.onEvent(applicationType,
+            (event, type) -> 
event.getCollector().increment(event.getSource().getApplicationName(), type)
+        );
+    }
+
+    private MetadataListener onFinishEventBuild(ApplicationType 
applicationType, String registryOpType) {
+        return MetadataListener.onFinish(applicationType,
+            (event, type) -> incrAndRt(event, applicationType, registryOpType)
+        );
+    }
+
+    private MetadataListener onErrorEventBuild(ApplicationType 
applicationType, String registryOpType) {
+        return MetadataListener.onError(applicationType,
+            (event, type) -> incrAndRt(event, applicationType, registryOpType)
+        );
+    }
+
+    private void incrAndRt(MetadataEvent event, ApplicationType 
applicationType, String registryOpType) {
+        event.getCollector().increment(event.getSource().getApplicationName(), 
applicationType);
+        
event.getCollector().addApplicationRT(event.getSource().getApplicationName(), 
registryOpType, event.getTimePair().calc());
+    }
 }
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetricsPushListener.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetricsPushListener.java
deleted file mode 100644
index a1ceaee3b1..0000000000
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetricsPushListener.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.metadata.event;
-
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.listener.MetricsLifeListener;
-
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_PUSH;
-
-public class MetricsPushListener implements 
MetricsLifeListener<MetadataEvent.PushEvent> {
-
-
-    @Override
-    public boolean isSupport(MetricsEvent event) {
-        return event instanceof MetadataEvent.PushEvent && ((MetadataEvent) 
event).isAvailable();
-    }
-
-    @Override
-    public void onEvent(MetadataEvent.PushEvent event) {
-        event.getCollector().increment(event.getSource().getApplicationName(), 
MetadataEvent.ApplicationType.P_TOTAL);
-    }
-
-    @Override
-    public void onEventFinish(MetadataEvent.PushEvent event) {
-        event.getCollector().increment(event.getSource().getApplicationName(), 
MetadataEvent.ApplicationType.P_SUCCEED);
-        
event.getCollector().addApplicationRT(event.getSource().getApplicationName(), 
OP_TYPE_PUSH, event.getTimePair().calc());
-    }
-
-    @Override
-    public void onEventError(MetadataEvent.PushEvent event) {
-        event.getCollector().increment(event.getSource().getApplicationName(), 
MetadataEvent.ApplicationType.P_FAILED);
-        
event.getCollector().addApplicationRT(event.getSource().getApplicationName(), 
OP_TYPE_PUSH, event.getTimePair().calc());
-    }
-}
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetricsSubscribeListener.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetricsSubscribeListener.java
deleted file mode 100644
index 7de568663b..0000000000
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetricsSubscribeListener.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.metadata.event;
-
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.listener.MetricsLifeListener;
-
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_SUBSCRIBE;
-
-public class MetricsSubscribeListener implements 
MetricsLifeListener<MetadataEvent.SubscribeEvent> {
-
-    @Override
-    public boolean isSupport(MetricsEvent event) {
-        return event instanceof MetadataEvent.SubscribeEvent && 
((MetadataEvent) event).isAvailable();
-    }
-
-    @Override
-    public void onEvent(MetadataEvent.SubscribeEvent event) {
-        event.getCollector().increment(event.getSource().getApplicationName(), 
MetadataEvent.ApplicationType.S_TOTAL);
-    }
-
-    @Override
-    public void onEventFinish(MetadataEvent.SubscribeEvent event) {
-        event.getCollector().increment(event.getSource().getApplicationName(), 
MetadataEvent.ApplicationType.S_SUCCEED);
-        
event.getCollector().addApplicationRT(event.getSource().getApplicationName(), 
OP_TYPE_SUBSCRIBE, event.getTimePair().calc());
-    }
-
-    @Override
-    public void onEventError(MetadataEvent.SubscribeEvent event) {
-        event.getCollector().increment(event.getSource().getApplicationName(), 
MetadataEvent.ApplicationType.S_FAILED);
-        
event.getCollector().addApplicationRT(event.getSource().getApplicationName(), 
OP_TYPE_SUBSCRIBE, event.getTimePair().calc());
-    }
-
-}
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/StoreProviderMetadataListener.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/StoreProviderMetadataListener.java
deleted file mode 100644
index 9c69556471..0000000000
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/StoreProviderMetadataListener.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.metadata.event;
-
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.listener.MetricsLifeListener;
-
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_STORE_PROVIDER_INTERFACE;
-
-public class StoreProviderMetadataListener implements 
MetricsLifeListener<MetadataEvent.StoreProviderMetadataEvent> {
-
-
-    @Override
-    public boolean isSupport(MetricsEvent event) {
-        return event instanceof MetadataEvent.StoreProviderMetadataEvent && 
event.isAvailable();
-    }
-
-    @Override
-    public void onEvent(MetadataEvent.StoreProviderMetadataEvent event) {
-        
event.getCollector().incrementServiceKey(event.getSource().getApplicationName(),
 event.getServiceKey(), MetadataEvent.ServiceType.S_P_TOTAL, 1);
-    }
-
-    @Override
-    public void onEventFinish(MetadataEvent.StoreProviderMetadataEvent event) {
-        
event.getCollector().incrementServiceKey(event.getSource().getApplicationName(),
 event.getServiceKey(), MetadataEvent.ServiceType.S_P_SUCCEED, 1);
-        
event.getCollector().addServiceKeyRT(event.getSource().getApplicationName(), 
event.getServiceKey(), OP_TYPE_STORE_PROVIDER_INTERFACE, 
event.getTimePair().calc());
-    }
-
-    @Override
-    public void onEventError(MetadataEvent.StoreProviderMetadataEvent event) {
-        
event.getCollector().incrementServiceKey(event.getSource().getApplicationName(),
 event.getServiceKey(), MetadataEvent.ServiceType.S_P_FAILED, 1);
-        
event.getCollector().addServiceKeyRT(event.getSource().getApplicationName(), 
event.getServiceKey(), OP_TYPE_STORE_PROVIDER_INTERFACE, 
event.getTimePair().calc());
-    }
-}
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/type/ApplicationType.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/type/ApplicationType.java
new file mode 100644
index 0000000000..88e45fbfce
--- /dev/null
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/type/ApplicationType.java
@@ -0,0 +1,51 @@
+/*
+ * 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.metadata.type;
+
+import org.apache.dubbo.metrics.model.MetricsKey;
+
+public enum ApplicationType {
+    P_TOTAL(MetricsKey.METADATA_PUSH_METRIC_NUM),
+    P_SUCCEED(MetricsKey.METADATA_PUSH_METRIC_NUM_SUCCEED),
+    P_FAILED(MetricsKey.METADATA_PUSH_METRIC_NUM_FAILED),
+
+    S_TOTAL(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM),
+    S_SUCCEED(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM_SUCCEED),
+    S_FAILED(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM_FAILED),
+
+    ;
+    private final MetricsKey metricsKey;
+    private final boolean isIncrement;
+
+    ApplicationType(MetricsKey metricsKey) {
+        this(metricsKey, true);
+    }
+
+    ApplicationType(MetricsKey metricsKey, boolean isIncrement) {
+        this.metricsKey = metricsKey;
+        this.isIncrement = isIncrement;
+    }
+
+    public MetricsKey getMetricsKey() {
+        return metricsKey;
+    }
+
+    public boolean isIncrement() {
+        return isIncrement;
+    }
+}
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/type/ServiceType.java
similarity index 52%
copy from 
dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
copy to 
dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/type/ServiceType.java
index 7bc3197b67..400769da70 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/MetadataMetricsEventMulticaster.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/type/ServiceType.java
@@ -15,17 +15,36 @@
  * limitations under the License.
  */
 
-package org.apache.dubbo.metrics.metadata.event;
+package org.apache.dubbo.metrics.metadata.type;
 
-import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
+import org.apache.dubbo.metrics.model.MetricsKey;
 
-public final class MetadataMetricsEventMulticaster extends 
SimpleMetricsEventMulticaster {
+public enum ServiceType {
 
-    public MetadataMetricsEventMulticaster() {
-        super.addListener(new MetricsPushListener());
-        super.addListener(new MetricsSubscribeListener());
-        super.addListener(new StoreProviderMetadataListener());
-        setAvailable();
+    S_P_TOTAL(MetricsKey.STORE_PROVIDER_METADATA),
+    S_P_SUCCEED(MetricsKey.STORE_PROVIDER_METADATA_SUCCEED),
+    S_P_FAILED(MetricsKey.STORE_PROVIDER_METADATA_FAILED),
+
+    ;
+
+    private final MetricsKey metricsKey;
+    private final boolean isIncrement;
+
+
+    ServiceType(MetricsKey metricsKey) {
+        this(metricsKey, true);
     }
 
+    ServiceType(MetricsKey metricsKey, boolean isIncrement) {
+        this.metricsKey = metricsKey;
+        this.isIncrement = isIncrement;
+    }
+
+    public MetricsKey getMetricsKey() {
+        return metricsKey;
+    }
+
+    public boolean isIncrement() {
+        return isIncrement;
+    }
 }
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
index 447ecdb7b6..d19c8fb157 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
@@ -41,9 +41,9 @@ import java.util.Objects;
 import java.util.stream.Collectors;
 
 import static 
org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME;
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_PUSH;
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_SUBSCRIBE;
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_STORE_PROVIDER_INTERFACE;
+import static org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_PUSH;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_STORE_PROVIDER_INTERFACE;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_SUBSCRIBE;
 
 
 class MetadataMetricsCollectorTest {
@@ -73,7 +73,7 @@ class MetadataMetricsCollectorTest {
         MetadataMetricsCollector collector = 
applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
         collector.setCollectEnabled(true);
 
-        MetadataEvent.PushEvent pushEvent = new 
MetadataEvent.PushEvent(applicationModel);
+        MetadataEvent pushEvent = MetadataEvent.toPushEvent(applicationModel);
         MetricsEventBus.post(pushEvent,
             () -> {
                 List<MetricSample> metricSamples = collector.collect();
@@ -92,7 +92,7 @@ class MetadataMetricsCollectorTest {
         Assertions.assertEquals(7, metricSamples.size());
         long c1 = pushEvent.getTimePair().calc();
 
-        pushEvent = new MetadataEvent.PushEvent(applicationModel);
+        pushEvent = MetadataEvent.toPushEvent(applicationModel);
         TimePair lastTimePair = pushEvent.getTimePair();
         MetricsEventBus.post(pushEvent,
             () -> {
@@ -134,7 +134,7 @@ class MetadataMetricsCollectorTest {
         MetadataMetricsCollector collector = 
applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
         collector.setCollectEnabled(true);
 
-        MetadataEvent.SubscribeEvent subscribeEvent = new 
MetadataEvent.SubscribeEvent(applicationModel);
+        MetadataEvent subscribeEvent = 
MetadataEvent.toSubscribeEvent(applicationModel);
         MetricsEventBus.post(subscribeEvent,
             () -> {
                 List<MetricSample> metricSamples = collector.collect();
@@ -152,7 +152,7 @@ class MetadataMetricsCollectorTest {
         List<MetricSample> metricSamples = collector.collect();
         //num(total+success) + rt(5) = 7
         Assertions.assertEquals(7, metricSamples.size());
-        subscribeEvent = new MetadataEvent.SubscribeEvent(applicationModel);
+        subscribeEvent = MetadataEvent.toSubscribeEvent(applicationModel);
         TimePair lastTimePair = subscribeEvent.getTimePair();
         MetricsEventBus.post(subscribeEvent,
             () -> {
@@ -190,14 +190,14 @@ class MetadataMetricsCollectorTest {
 
 
     @Test
-    void testStoreProviderMetadataMetrics() throws InterruptedException {
+    void testStoreProviderMetadataMetrics() {
 
         
applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
         MetadataMetricsCollector collector = 
applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
         collector.setCollectEnabled(true);
 
         String serviceKey = "store.provider.test";
-        MetadataEvent metadataEvent = new 
MetadataEvent.StoreProviderMetadataEvent(applicationModel, serviceKey);
+        MetadataEvent metadataEvent = 
MetadataEvent.toServiceSubscribeEvent(applicationModel, serviceKey);
         MetricsEventBus.post(metadataEvent,
             () -> {
                 List<MetricSample> metricSamples = collector.collect();
@@ -216,7 +216,7 @@ class MetadataMetricsCollectorTest {
         //num(total+success) + rt(5) = 7
         Assertions.assertEquals(7, metricSamples.size());
         long c1 = metadataEvent.getTimePair().calc();
-        metadataEvent = new 
MetadataEvent.StoreProviderMetadataEvent(applicationModel, serviceKey);
+        metadataEvent = 
MetadataEvent.toServiceSubscribeEvent(applicationModel, serviceKey);
         TimePair lastTimePair = metadataEvent.getTimePair();
         MetricsEventBus.post(metadataEvent,
             () -> {
diff --git 
a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java
 
b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java
index 7d7d26a89a..0b412fe486 100644
--- 
a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java
+++ 
b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataStatCompositeTest.java
@@ -18,7 +18,7 @@
 package org.apache.dubbo.metrics.metadata;
 
 import org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite;
-import org.apache.dubbo.metrics.metadata.event.MetadataEvent;
+import org.apache.dubbo.metrics.metadata.type.ApplicationType;
 import org.apache.dubbo.metrics.model.container.LongContainer;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
@@ -27,7 +27,7 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.concurrent.ConcurrentHashMap;
 
-import static 
org.apache.dubbo.metrics.metadata.collector.stat.MetadataStatComposite.OP_TYPE_SUBSCRIBE;
+import static 
org.apache.dubbo.metrics.metadata.MetadataConstants.OP_TYPE_SUBSCRIBE;
 
 public class MetadataStatCompositeTest {
 
@@ -36,7 +36,7 @@ public class MetadataStatCompositeTest {
     @Test
     void testInit() {
         MetadataStatComposite statComposite = new MetadataStatComposite();
-        Assertions.assertEquals(statComposite.applicationNumStats.size(), 
MetadataEvent.ApplicationType.values().length);
+        Assertions.assertEquals(statComposite.applicationNumStats.size(), 
ApplicationType.values().length);
         //(rt)5 * (push,subscribe)2
         Assertions.assertEquals(5 * 2, statComposite.appRtStats.size());
         statComposite.applicationNumStats.values().forEach((v ->
@@ -52,8 +52,8 @@ public class MetadataStatCompositeTest {
     @Test
     void testIncrement() {
         MetadataStatComposite statComposite = new MetadataStatComposite();
-        statComposite.increment(MetadataEvent.ApplicationType.P_TOTAL, 
applicationName);
-        Assertions.assertEquals(1L, 
statComposite.applicationNumStats.get(MetadataEvent.ApplicationType.P_TOTAL).get(applicationName).get());
+        statComposite.increment(ApplicationType.P_TOTAL, applicationName);
+        Assertions.assertEquals(1L, 
statComposite.applicationNumStats.get(ApplicationType.P_TOTAL).get(applicationName).get());
     }
 
     @Test
diff --git 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
index 341ee385f9..c0d7bffd5e 100644
--- 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
+++ 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/RegistryConstants.java
@@ -19,11 +19,6 @@ package org.apache.dubbo.metrics.registry;
 
 public interface RegistryConstants {
 
-    String ATTACHMENT_KEY_SERVICE = "serviceKey";
-    String ATTACHMENT_KEY_SIZE = "size";
-    String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
-    String ATTACHMENT_KEY_DIR_NUM = "dirNum";
-
     String OP_TYPE_REGISTER = "register";
     String OP_TYPE_SUBSCRIBE = "subscribe";
     String OP_TYPE_NOTIFY = "notify";
diff --git 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
index 8b7164ea4a..bd1fad1e7a 100644
--- 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
+++ 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryEvent.java
@@ -30,16 +30,16 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
 import java.util.HashMap;
 import java.util.Map;
 
-import static 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_LAST_NUM_MAP;
-import static 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SERVICE;
-import static 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SIZE;
+import static 
org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_LAST_NUM_MAP;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
 
 /**
  * Registry related events
  */
 public class RegistryEvent extends TimeCounterEvent {
     private final RegistryMetricsCollector collector;
-    protected Map<String, Object> attachment = new HashMap<>(8);
+    private final Map<String, Object> attachment = new HashMap<>(8);
 
     public RegistryEvent(ApplicationModel applicationModel, TypeWrapper 
typeWrapper) {
         super(applicationModel);
@@ -74,11 +74,6 @@ public class RegistryEvent extends TimeCounterEvent {
         attachment.put(key, value);
     }
 
-
-
-
-
-
     public void setLastNum(ServiceType type) {
         getCollector().setNum(type, getSource().getApplicationName(), 
getAttachmentValue(ATTACHMENT_KEY_LAST_NUM_MAP));
     }
@@ -123,21 +118,21 @@ public class RegistryEvent extends TimeCounterEvent {
         return new RegistryEvent(applicationModel, new 
TypeWrapper(MetricsLevel.APP, ApplicationType.N_TOTAL, ServiceType.N_LAST_NUM, 
null)) {
             @Override
             public void customAfterPost(Object postResult) {
-                super.attachment.put(ATTACHMENT_KEY_LAST_NUM_MAP, postResult);
+                super.putAttachment(ATTACHMENT_KEY_LAST_NUM_MAP, postResult);
             }
         };
     }
 
     public static RegistryEvent toRsEvent(ApplicationModel applicationModel, 
String serviceKey, int size) {
         RegistryEvent ddEvent = new RegistryEvent(applicationModel, new 
TypeWrapper(MetricsLevel.SERVICE, ServiceType.R_SERVICE_TOTAL, 
ServiceType.R_SERVICE_SUCCEED, ServiceType.R_SERVICE_FAILED));
-        ddEvent.attachment.put(ATTACHMENT_KEY_SERVICE, serviceKey);
-        ddEvent.attachment.put(ATTACHMENT_KEY_SIZE, size);
+        ddEvent.putAttachment(ATTACHMENT_KEY_SERVICE, serviceKey);
+        ddEvent.putAttachment(ATTACHMENT_KEY_SIZE, size);
         return ddEvent;
     }
 
     public static RegistryEvent toSsEvent(ApplicationModel applicationModel, 
String serviceKey) {
         RegistryEvent ddEvent = new RegistryEvent(applicationModel, new 
TypeWrapper(MetricsLevel.SERVICE, ServiceType.S_SERVICE_TOTAL, 
ServiceType.S_SERVICE_SUCCEED, ServiceType.S_SERVICE_FAILED));
-        ddEvent.attachment.put(ATTACHMENT_KEY_SERVICE, serviceKey);
+        ddEvent.putAttachment(ATTACHMENT_KEY_SERVICE, serviceKey);
         return ddEvent;
     }
 
diff --git 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
index 83590cde15..38be359ffd 100644
--- 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
+++ 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/RegistryMetricsEventMulticaster.java
@@ -21,8 +21,9 @@ import 
org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
 import org.apache.dubbo.metrics.registry.event.type.ApplicationType;
 import org.apache.dubbo.metrics.registry.event.type.ServiceType;
 
-import static 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_DIR_NUM;
-import static 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SERVICE;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_DIR_NUM;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
 import static 
org.apache.dubbo.metrics.registry.RegistryConstants.OP_TYPE_NOTIFY;
 import static 
org.apache.dubbo.metrics.registry.RegistryConstants.OP_TYPE_REGISTER;
 import static 
org.apache.dubbo.metrics.registry.RegistryConstants.OP_TYPE_REGISTER_SERVICE;
@@ -110,7 +111,7 @@ public final class RegistryMetricsEventMulticaster extends 
SimpleMetricsEventMul
     }
 
     private void incrSkSize(RegistryEvent event, ServiceType type) {
-        event.incrementServiceKey(type, ATTACHMENT_KEY_SERVICE, 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_SIZE);
+        event.incrementServiceKey(type, ATTACHMENT_KEY_SERVICE, 
ATTACHMENT_KEY_SIZE);
     }
 
     private void onRtEvent(RegistryEvent event, ServiceType type) {
diff --git 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
index cf5dabdb22..c54210a1b4 100644
--- 
a/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
+++ 
b/dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/support/DirectorSupport.java
@@ -23,7 +23,8 @@ import org.apache.dubbo.metrics.registry.event.RegistryEvent;
 import org.apache.dubbo.metrics.registry.event.type.ApplicationType;
 import org.apache.dubbo.rpc.model.ApplicationModel;
 
-import static 
org.apache.dubbo.metrics.registry.RegistryConstants.ATTACHMENT_KEY_DIR_NUM;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_DIR_NUM;
+
 
 public class DirectorSupport {
 
diff --git 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
index 19905498a9..86a5b0fa7a 100644
--- 
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
+++ 
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java
@@ -228,7 +228,7 @@ public abstract class AbstractServiceDiscovery implements 
ServiceDiscovery {
             int triedTimes = 0;
             while (triedTimes < 3) {
 
-                metadata = MetricsEventBus.post(new 
MetadataEvent.SubscribeEvent(applicationModel),
+                metadata = 
MetricsEventBus.post(MetadataEvent.toSubscribeEvent(applicationModel),
                     () -> MetadataUtils.getRemoteMetadata(revision, instances, 
metadataReport),
                     result -> result != MetadataInfo.EMPTY
                 );
@@ -361,7 +361,7 @@ public abstract class AbstractServiceDiscovery implements 
ServiceDiscovery {
         if (metadataReport != null) {
             SubscriberMetadataIdentifier identifier = new 
SubscriberMetadataIdentifier(serviceName, metadataInfo.getRevision());
             if ((DEFAULT_METADATA_STORAGE_TYPE.equals(metadataType) && 
metadataReport.shouldReportMetadata()) || 
REMOTE_METADATA_STORAGE_TYPE.equals(metadataType)) {
-                MetricsEventBus.post(new 
MetadataEvent.PushEvent(applicationModel),
+                
MetricsEventBus.post(MetadataEvent.toPushEvent(applicationModel),
                     () ->
                     {
                         metadataReport.publishAppMetadata(identifier, 
metadataInfo);

Reply via email to