Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics event split #12888

Merged
merged 17 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dubbo-metrics-prometheus
dubbo-metrics-registry
dubbo-metrics-config-center
dubbo-metrics-netty
dubbo-metrics-event
dubbo-monitor
dubbo-monitor-api
dubbo-monitor-common
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -60,7 +61,6 @@
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.CLUSTER_AVAILABLE_CHECK_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.INVOCATION_NEED_MOCK;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.same;
Expand Down Expand Up @@ -107,7 +107,7 @@ public static void afterClass() {
@SuppressWarnings({"unchecked"})
@BeforeEach
public void setUp() throws Exception {
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsDispatcher.class);
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
Map<String, Object> attributes = new HashMap<>();
attributes.put("application", "abstractClusterInvokerTest");
url = url.putAttribute(REFER_KEY, attributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
Expand Down Expand Up @@ -72,7 +73,7 @@ class ConnectivityValidationTest {

@BeforeEach
public void setup() {
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsDispatcher.class);
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
invoker1 = Mockito.mock(Invoker.class);
invoker2 = Mockito.mock(Invoker.class);
invoker3 = Mockito.mock(Invoker.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.rpc.cluster.support;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.AsyncRpcResult;
Expand Down Expand Up @@ -67,7 +68,7 @@ class FailoverClusterInvokerTest {

@BeforeEach
public void setUp() throws Exception {
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsDispatcher.class);
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
dic = mock(Directory.class);

given(dic.getUrl()).willReturn(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -33,7 +34,6 @@
import org.apache.dubbo.rpc.cluster.directory.StaticDirectory;
import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
import org.apache.dubbo.rpc.model.ApplicationModel;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -67,7 +67,7 @@ class ScopeClusterInvokerTest {

@BeforeEach
void beforeMonth() {
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsDispatcher.class);
ApplicationModel.defaultModel().getBeanFactory().registerBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
}

@AfterEach
Expand Down
8 changes: 8 additions & 0 deletions dubbo-distribution/dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@
</dependency>

<!-- metrics -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-event</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-api</artifactId>
Expand Down Expand Up @@ -690,6 +697,7 @@
<include>org.apache.dubbo:dubbo-metadata-report-nacos</include>
<include>org.apache.dubbo:dubbo-metadata-report-redis</include>
<include>org.apache.dubbo:dubbo-metadata-report-zookeeper</include>
<include>org.apache.dubbo:dubbo-metrics-event</include>
<include>org.apache.dubbo:dubbo-metrics-api</include>
<include>org.apache.dubbo:dubbo-metrics-default</include>
<include>org.apache.dubbo:dubbo-metrics-registry</include>
Expand Down
5 changes: 5 additions & 0 deletions dubbo-distribution/dubbo-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@
<artifactId>dubbo-metrics-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-event</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-default</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions dubbo-metrics/dubbo-metrics-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<artifactId>dubbo-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics-event</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void setup() {
aggregationConfig.setTimeWindowSeconds(120);
metricsConfig.setAggregation(aggregationConfig);
applicationModel.getApplicationConfigManager().setMetrics(metricsConfig);
metricsDispatcher = applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
metricsDispatcher = applicationModel.getBeanFactory().getOrRegisterBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
defaultCollector = applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class);
collector = applicationModel.getBeanFactory().getOrRegisterBean(AggregateMetricsCollector.class);
collector.setCollectEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.TestMetricsInvoker;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.metrics.event.RequestEvent;
Expand Down Expand Up @@ -88,7 +89,7 @@ public void setup() {
config.setName("MockMetrics");

applicationModel.getApplicationConfigManager().setApplication(config);
metricsDispatcher = applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
metricsDispatcher = applicationModel.getBeanFactory().getOrRegisterBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
defaultCollector = applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class);
defaultCollector.setCollectEnabled(true);

Expand Down Expand Up @@ -130,7 +131,7 @@ public void teardown() {
@Test
void testRequestEventNoRt() {

applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
applicationModel.getBeanFactory().getOrRegisterBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
DefaultMetricsCollector collector = applicationModel.getBeanFactory().getOrRegisterBean(DefaultMetricsCollector.class);
collector.setCollectEnabled(true);

Expand Down
42 changes: 42 additions & 0 deletions dubbo-metrics/dubbo-metrics-event/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metrics</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>dubbo-metrics-event</artifactId>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>The metrics event-related api module of dubbo project</description>
<properties>
<skip_maven_deploy>false</skip_maven_deploy>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface MetricsConstants {
String ATTACHMENT_KEY_SIZE = "size";
String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
String ATTACHMENT_DIRECTORY_MAP = "dirNum";
String METRICS_DISPATCHER_NAME = "MetricsDispatcher";

int SELF_INCREMENT_SIZE = 1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package org.apache.dubbo.metrics.event;

import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.exception.MetricsNeverHappenException;
import org.apache.dubbo.metrics.model.MethodMetric;
import org.apache.dubbo.metrics.model.key.TypeWrapper;
import org.apache.dubbo.rpc.model.ApplicationModel;

Expand All @@ -32,21 +32,21 @@
public abstract class MetricsEvent {

/**
* Metric object. (eg. {@link MethodMetric})
* Metric object. (eg. MethodMetric)
*/
protected transient final ApplicationModel source;
private boolean available = true;
private final TypeWrapper typeWrapper;
private final String appName;
private final MetricsDispatcher metricsDispatcher;
private final MetricsEventMulticaster MetricsEventMulticaster;

private final Map<String, Object> attachment = new IdentityHashMap<>(8);

public MetricsEvent(ApplicationModel source, TypeWrapper typeWrapper) {
this(source, null, null, typeWrapper);
}

public MetricsEvent(ApplicationModel source, String appName, MetricsDispatcher metricsDispatcher, TypeWrapper typeWrapper) {
public MetricsEvent(ApplicationModel source, String appName, MetricsEventMulticaster MetricsEventMulticaster, TypeWrapper typeWrapper) {
namelessssssssssss marked this conversation as resolved.
Show resolved Hide resolved
this.typeWrapper = typeWrapper;
if (source == null) {
this.source = ApplicationModel.defaultModel();
Expand All @@ -55,20 +55,20 @@ public MetricsEvent(ApplicationModel source, String appName, MetricsDispatcher m
} else {
this.source = source;
}
if (metricsDispatcher == null) {
if (MetricsEventMulticaster == null) {
if (this.source.isDestroyed()) {
this.metricsDispatcher = null;
this.MetricsEventMulticaster = null;
} else {
ScopeBeanFactory beanFactory = this.source.getBeanFactory();
if (beanFactory.isDestroyed()) {
this.metricsDispatcher = null;
this.MetricsEventMulticaster = null;
} else {
MetricsDispatcher dispatcher = beanFactory.getBean(MetricsDispatcher.class);
this.metricsDispatcher = dispatcher;
MetricsEventMulticaster dispatcher = beanFactory.getBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsEventMulticaster.class);
this.MetricsEventMulticaster = dispatcher;
}
}
} else {
this.metricsDispatcher = metricsDispatcher;
this.MetricsEventMulticaster = MetricsEventMulticaster;
}
if (appName == null) {
this.appName = this.source.tryGetApplicationName();
Expand Down Expand Up @@ -106,8 +106,8 @@ public ApplicationModel getSource() {
return source;
}

public MetricsDispatcher getMetricsDispatcher() {
return metricsDispatcher;
public MetricsEventMulticaster getMetricsEventMulticaster() {
return MetricsEventMulticaster;
}

public String appName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static void publish(MetricsEvent event) {
if (event.getSource() == null) {
return;
}
MetricsDispatcher dispatcher = event.getMetricsDispatcher();
MetricsEventMulticaster dispatcher = event.getMetricsEventMulticaster();
Optional.ofNullable(dispatcher).ifPresent(d -> d.publishEvent(event));
}

Expand Down Expand Up @@ -89,26 +89,27 @@ public static <T> T post(MetricsEvent event, Supplier<T> targetSupplier, Functio
* eventSaveRunner saves the event, so that the calculation rt is introverted
*/
public static void before(MetricsEvent event) {
MetricsDispatcher dispatcher = validate(event);
MetricsEventMulticaster dispatcher = validate(event);

if (dispatcher == null) return;
dispatcher.publishEvent(event);
}

public static void after(MetricsEvent event, Object result) {
MetricsDispatcher dispatcher = validate(event);
MetricsEventMulticaster dispatcher = validate(event);
if (dispatcher == null) return;
event.customAfterPost(result);
dispatcher.publishFinishEvent((TimeCounterEvent) event);
}

public static void error(MetricsEvent event) {
MetricsDispatcher dispatcher = validate(event);
MetricsEventMulticaster dispatcher = validate(event);
if (dispatcher == null) return;
dispatcher.publishErrorEvent((TimeCounterEvent) event);
}

private static MetricsDispatcher validate(MetricsEvent event) {
MetricsDispatcher dispatcher = event.getMetricsDispatcher();
private static MetricsEventMulticaster validate(MetricsEvent event) {
MetricsEventMulticaster dispatcher = event.getMetricsEventMulticaster();
if (dispatcher == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TimeCounterEvent(ApplicationModel source, TypeWrapper typeWrapper) {
this.timePair = TimePair.start();
}

public TimeCounterEvent(ApplicationModel source, String appName, MetricsDispatcher metricsDispatcher, TypeWrapper typeWrapper) {
public TimeCounterEvent(ApplicationModel source, String appName,MetricsEventMulticaster metricsDispatcher, TypeWrapper typeWrapper) {
super(source, appName, metricsDispatcher, typeWrapper);
this.timePair = TimePair.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.dubbo.metrics.metadata;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.metrics.event.MetricsEventBus;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void setup() {
config.setName("MockMetrics");

applicationModel.getApplicationConfigManager().setApplication(config);
applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
applicationModel.getBeanFactory().getOrRegisterBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
namelessssssssssss marked this conversation as resolved.
Show resolved Hide resolved

collector = applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
collector.setCollectEnabled(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.dubbo.metrics.registry.metrics.collector;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.model.TimePair;
Expand Down Expand Up @@ -61,7 +62,7 @@ public void setup() {
config.setName("MockMetrics");

applicationModel.getApplicationConfigManager().setApplication(config);
applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
applicationModel.getBeanFactory().getOrRegisterBean(MetricsConstants.METRICS_DISPATCHER_NAME,MetricsDispatcher.class);
collector = applicationModel.getBeanFactory().getOrRegisterBean(RegistryMetricsCollector.class);
collector.setCollectEnabled(true);
}
Expand Down
1 change: 1 addition & 0 deletions dubbo-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<modelVersion>4.0.0</modelVersion>
<modules>
<module>dubbo-metrics-api</module>
<module>dubbo-metrics-event</module>
<module>dubbo-metrics-default</module>
<module>dubbo-metrics-registry</module>
<module>dubbo-metrics-metadata</module>
Expand Down