Skip to content

Commit cdc2d85

Browse files
authored
metrics code optimization (#11352)
1 parent bce9082 commit cdc2d85

13 files changed

Lines changed: 358 additions & 277 deletions

File tree

dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/stat/MetricsStatHandler.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717

1818
package org.apache.dubbo.metrics.collector.stat;
1919

20+
import org.apache.dubbo.metrics.event.MetricsEvent;
2021
import org.apache.dubbo.metrics.model.MethodMetric;
22+
import org.apache.dubbo.rpc.Invocation;
2123

2224
import java.util.Map;
2325
import java.util.concurrent.atomic.AtomicLong;
2426

2527
public interface MetricsStatHandler {
2628
Map<MethodMetric, AtomicLong> get();
27-
void increase(String applicationName, String interfaceName, String methodName, String group, String version);
28-
void decrease(String applicationName, String interfaceName, String methodName, String group, String version);
29+
30+
MetricsEvent increase(String applicationName, Invocation invocation);
31+
32+
MetricsEvent decrease(String applicationName, Invocation invocation);
2933
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.metrics.event;
19+
20+
/**
21+
* EmptyEvent, do nothing.
22+
*/
23+
public class EmptyEvent extends MetricsEvent {
24+
25+
private static final EmptyEvent empty = new EmptyEvent(new Object());
26+
27+
public EmptyEvent(Object source) {
28+
super(source);
29+
}
30+
31+
public static EmptyEvent instance() {
32+
return empty;
33+
}
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.metrics.event;
19+
20+
import org.apache.dubbo.metrics.listener.MetricsListener;
21+
22+
public interface MetricsEventMulticaster {
23+
24+
void addListener(MetricsListener listener);
25+
26+
void publishEvent(MetricsEvent event);
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.dubbo.metrics.event;
19+
20+
import org.apache.dubbo.metrics.listener.MetricsListener;
21+
22+
import java.util.ArrayList;
23+
import java.util.Collections;
24+
import java.util.List;
25+
26+
public final class SimpleMetricsEventMulticaster implements MetricsEventMulticaster {
27+
28+
@SuppressWarnings("java:S3077")
29+
private static volatile SimpleMetricsEventMulticaster instance;
30+
31+
private SimpleMetricsEventMulticaster() {
32+
}
33+
34+
private final List<MetricsListener> listeners = Collections.synchronizedList(new ArrayList<>());
35+
36+
public static SimpleMetricsEventMulticaster getInstance() {
37+
if (instance == null) {
38+
synchronized (SimpleMetricsEventMulticaster.class) {
39+
if (instance == null) {
40+
instance = new SimpleMetricsEventMulticaster();
41+
}
42+
}
43+
}
44+
return instance;
45+
}
46+
47+
@Override
48+
public void addListener(MetricsListener listener) {
49+
listeners.add(listener);
50+
}
51+
52+
@Override
53+
public void publishEvent(MetricsEvent event) {
54+
if (event instanceof EmptyEvent) {
55+
return;
56+
}
57+
for (MetricsListener listener : listeners) {
58+
listener.onEvent(event);
59+
}
60+
}
61+
}

dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.apache.dubbo.metrics.model;
1919

20+
import org.apache.dubbo.rpc.Invocation;
21+
import org.apache.dubbo.rpc.RpcInvocation;
22+
2023
import java.util.HashMap;
2124
import java.util.Map;
2225
import java.util.Objects;
@@ -30,6 +33,7 @@
3033
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY;
3134
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
3235
import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName;
36+
import static org.apache.dubbo.rpc.support.RpcUtils.isGenericCall;
3337

3438
/**
3539
* Metric class for method.
@@ -45,12 +49,9 @@ public MethodMetric() {
4549

4650
}
4751

48-
public MethodMetric(String applicationName, String interfaceName, String methodName, String group, String version) {
52+
public MethodMetric(String applicationName, Invocation invocation) {
4953
this.applicationName = applicationName;
50-
this.interfaceName = interfaceName;
51-
this.methodName = methodName;
52-
this.group = group;
53-
this.version = version;
54+
init(invocation);
5455
}
5556

5657
public String getInterfaceName() {
@@ -121,4 +122,33 @@ public String toString() {
121122
", version='" + version + '\'' +
122123
'}';
123124
}
125+
126+
private void init(Invocation invocation) {
127+
String serviceUniqueName = invocation.getTargetServiceUniqueName();
128+
String methodName = invocation.getMethodName();
129+
if (invocation instanceof RpcInvocation
130+
&& isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodName)
131+
&& invocation.getArguments() != null
132+
&& invocation.getArguments().length == 3) {
133+
methodName = ((String) invocation.getArguments()[0]).trim();
134+
}
135+
String group = null;
136+
String interfaceAndVersion;
137+
String[] arr = serviceUniqueName.split("/");
138+
if (arr.length == 2) {
139+
group = arr[0];
140+
interfaceAndVersion = arr[1];
141+
} else {
142+
interfaceAndVersion = arr[0];
143+
}
144+
145+
String[] ivArr = interfaceAndVersion.split(":");
146+
String interfaceName = ivArr[0];
147+
String version = ivArr.length == 2 ? ivArr[1] : null;
148+
149+
this.interfaceName = interfaceName;
150+
this.methodName = methodName;
151+
this.group = group;
152+
this.version = version;
153+
}
124154
}

0 commit comments

Comments
 (0)