metadata listener refactor (#12016)

Co-authored-by: x-shadow-man <1494445739@qq.com>
This commit is contained in:
wxbty 2023-04-06 09:38:56 +08:00 committed by GitHub
parent 5d1b68b995
commit 60799ec3d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 369 additions and 306 deletions

View File

@ -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;

View File

@ -0,0 +1,27 @@
/*
* 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;
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";
}

View File

@ -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);

View File

@ -0,0 +1,27 @@
/*
* 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;
public interface MetadataConstants {
String OP_TYPE_PUSH = "push";
String OP_TYPE_SUBSCRIBE = "subscribe";
String OP_TYPE_STORE_PROVIDER_INTERFACE = "store.provider.interface";
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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,6 +51,18 @@ public class MetadataEvent extends TimeCounterEvent {
}
}
@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 void putAttachment(String key, Object value) {
attachment.put(key, value);
}
public ApplicationModel getSource() {
return source;
}
@ -48,95 +71,18 @@ public class MetadataEvent extends TimeCounterEvent {
return collector;
}
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 static MetadataEvent toPushEvent(ApplicationModel applicationModel) {
return new MetadataEvent(applicationModel, new TypeWrapper(MetricsLevel.APP, ApplicationType.P_TOTAL, ApplicationType.P_SUCCEED, ApplicationType.P_FAILED));
}
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 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 PushEvent extends MetadataEvent {
public PushEvent(ApplicationModel applicationModel) {
super(applicationModel);
}
}
public static class SubscribeEvent extends MetadataEvent {
public SubscribeEvent(ApplicationModel applicationModel) {
super(applicationModel);
}
}
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;
}
}

View File

@ -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);
}
};
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,50 @@
/*
* 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 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;
}
}

View File

@ -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,
() -> {

View File

@ -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

View File

@ -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";

View File

@ -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;
}

View File

@ -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) {

View File

@ -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 {

View File

@ -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);