1
0
mirror of https://github.com/apache/dubbo.git synced 2024-10-23 07:04:37 +08:00

Fixed metrics collection getting wrong method name when generalize called (#11012)

* Fixes #11001, fixed metrics collection getting wrong method name when generalize called

* avoid array out of bounds
This commit is contained in:
twz007 2022-11-28 09:27:51 +08:00 committed by GitHub
parent 0e95ac4e39
commit b860358b94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -18,6 +18,7 @@
package org.apache.dubbo.metrics.filter;
import static org.apache.dubbo.common.constants.MetricsConstants.METRIC_FILTER_START_TIME;
import static org.apache.dubbo.rpc.support.RpcUtils.isGenericCall;
import java.util.function.Supplier;
@ -25,6 +26,7 @@ import org.apache.dubbo.common.metrics.collector.DefaultMetricsCollector;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;
public class MetricsCollectExecutor {
@ -88,6 +90,12 @@ public class MetricsCollectExecutor {
private void init(Invocation invocation) {
String serviceUniqueName = invocation.getTargetServiceUniqueName();
String methodName = invocation.getMethodName();
if (invocation instanceof RpcInvocation
&& isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodName)
&& invocation.getArguments() != null
&& invocation.getArguments().length == 3) {
methodName = ((String) invocation.getArguments()[0]).trim();
}
String group = null;
String interfaceAndVersion;
String[] arr = serviceUniqueName.split("/");

View File

@ -38,6 +38,7 @@ import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.apache.dubbo.common.constants.CommonConstants.*;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_GROUP_KEY;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY;
@ -234,6 +235,31 @@ class MetricsFilterTest {
Assertions.assertNull(tags.get(TAG_VERSION_KEY));
}
@Test
void testGenericCall() {
collector.setCollectEnabled(true);
given(invoker.invoke(invocation)).willReturn(new AppResponse("success"));
invocation.setTargetServiceUniqueName(INTERFACE_NAME);
invocation.setMethodName(METHOD_NAME);
invocation.setParameterTypes(new Class[]{String.class});
Result result = filter.invoke(invoker, invocation);
invocation.setMethodName($INVOKE);
invocation.setParameterTypesDesc(GENERIC_PARAMETER_DESC);
invocation.setArguments(new Object[]{METHOD_NAME, new String[]{"java.lang.String"}, new Object[]{"mock"}});
filter.onResponse(result, invoker, invocation);
Map<String, MetricSample> metricsMap = getMetricsMap();
MetricSample sample = metricsMap.get("requests.processing");
Map<String, String> tags = sample.getTags();
Assertions.assertEquals(tags.get(TAG_INTERFACE_KEY), INTERFACE_NAME);
Assertions.assertEquals(tags.get(TAG_METHOD_KEY), METHOD_NAME);
}
private void initParam() {
invocation.setTargetServiceUniqueName(GROUP + "/" + INTERFACE_NAME + ":" + VERSION);
invocation.setMethodName(METHOD_NAME);