add metrics enable switch (#12389)

* add metrics enable switch

* default false

* default false

* set default true and fix unit test

* xsd set default true

* rename enabled to enableRpc

* make enableRpc default value to true

* fix camel name

* remove default value

---------

Co-authored-by: songxiaosheng <songxiaosheng@elastic.link>
This commit is contained in:
TomlongTK 2023-06-07 18:54:20 +08:00 committed by GitHub
parent 9af6fbf1d0
commit c4702142bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 9 deletions

View File

@ -92,6 +92,7 @@ public class MetricsConfig extends AbstractConfig {
*/
private Boolean useGlobalRegistry;
private Boolean enableRpc;
public MetricsConfig() {
}
@ -214,4 +215,12 @@ public class MetricsConfig extends AbstractConfig {
public void setUseGlobalRegistry(Boolean useGlobalRegistry) {
this.useGlobalRegistry = useGlobalRegistry;
}
public Boolean getEnableRpc() {
return enableRpc;
}
public void setEnableRpc(Boolean enableRpc) {
this.enableRpc = enableRpc;
}
}

View File

@ -1098,6 +1098,12 @@
<xsd:documentation><![CDATA[ Deprecated. No longer use. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="enable-rpc" type="xsd:boolean" default="true">
<xsd:annotation>
<xsd:documentation><![CDATA[ Enable record rpc metrics. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:complexType name="tracingType">

View File

@ -19,6 +19,7 @@ package org.apache.dubbo.metrics.filter;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.event.RequestEvent;
import org.apache.dubbo.rpc.BaseFilter;
@ -40,20 +41,24 @@ import static org.apache.dubbo.metrics.DefaultConstants.METRIC_THROWABLE;
public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAware {
private ApplicationModel applicationModel;
private final static ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(MetricsFilter.class);
private static final ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(MetricsFilter.class);
private boolean rpcMetricsEnable;
@Override
public void setApplicationModel(ApplicationModel applicationModel) {
this.applicationModel = applicationModel;
this.rpcMetricsEnable = applicationModel.getApplicationConfigManager().getMetrics().map(MetricsConfig::getEnableRpc).orElse(true);
}
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
try {
RequestEvent requestEvent = RequestEvent.toRequestEvent(applicationModel, invocation);
MetricsEventBus.before(requestEvent, () -> invocation.put(METRIC_FILTER_EVENT, requestEvent));
} catch (Throwable t) {
LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when invoke.", t);
if (rpcMetricsEnable) {
try {
RequestEvent requestEvent = RequestEvent.toRequestEvent(applicationModel, invocation);
MetricsEventBus.before(requestEvent, () -> invocation.put(METRIC_FILTER_EVENT, requestEvent));
} catch (Throwable t) {
LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when invoke.", t);
}
}
return invoker.invoke(invocation);
}
@ -84,6 +89,4 @@ public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAwa
}
}
}

View File

@ -82,7 +82,6 @@ class MetricsFilterTest {
config.setName("MockMetrics");
applicationModel = ApplicationModel.defaultModel();
applicationModel.getApplicationConfigManager().setApplication(config);
invocation = new RpcInvocation();
filter = new MetricsFilter();