Skip to content

Commit 1426e72

Browse files
authored
Add extended interface for metric extension hook to support distinguishing traffic type (alibaba#1665)
- Add EntryType args to all hook methods
1 parent 83bdf23 commit 1426e72

File tree

6 files changed

+285
-35
lines changed

6 files changed

+285
-35
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.alibaba.csp.sentinel.metric.extension;
2+
3+
import com.alibaba.csp.sentinel.EntryType;
4+
import com.alibaba.csp.sentinel.slots.block.BlockException;
5+
6+
/**
7+
* Advanced {@link MetricExtension} extending input parameters of each metric
8+
* collection method with the name of {@link EntryType}.
9+
*
10+
* @author bill_yip
11+
* @since 1.8.0
12+
*/
13+
public interface AdvancedMetricExtension extends MetricExtension {
14+
/**
15+
* Add current pass count of the resource name.
16+
*
17+
* @param n count to add
18+
* @param resource resource name
19+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as consumer.
20+
* @param args additional arguments of the resource, eg. if the resource is
21+
* a method name, the args will be the parameters of the
22+
* method.
23+
*/
24+
void addPass(String resource, String entryType, int n, Object... args);
25+
26+
/**
27+
* Add current block count of the resource name.
28+
*
29+
* @param n count to add
30+
* @param resource resource name
31+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as
32+
* consumer.
33+
* @param origin the original invoker.
34+
* @param blockException block exception related.
35+
* @param args additional arguments of the resource, eg. if the
36+
* resource is a method name, the args will be the
37+
* parameters of the method.
38+
*/
39+
void addBlock(String resource, String entryType, int n, String origin, BlockException blockException,
40+
Object... args);
41+
42+
/**
43+
* Add current completed count of the resource name.
44+
*
45+
* @param n count to add
46+
* @param resource resource name
47+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as consumer.
48+
* @param args additional arguments of the resource, eg. if the resource is
49+
* a method name, the args will be the parameters of the
50+
* method.
51+
*/
52+
void addSuccess(String resource, String entryType, int n, Object... args);
53+
54+
/**
55+
* Add current exception count of the resource name.
56+
*
57+
* @param n count to add
58+
* @param resource resource name
59+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as consumer.
60+
* @param throwable exception related.
61+
*/
62+
void addException(String resource, String entryType, int n, Throwable throwable);
63+
64+
/**
65+
* Add response time of the resource name.
66+
*
67+
* @param rt response time in millisecond
68+
* @param resource resource name
69+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as consumer.
70+
* @param args additional arguments of the resource, eg. if the resource is
71+
* a method name, the args will be the parameters of the
72+
* method.
73+
*/
74+
void addRt(String resource, String entryTypeTag, long rt, Object... args);
75+
76+
/**
77+
* Increase current thread count of the resource name.
78+
*
79+
* @param resource resource name
80+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as consumer.
81+
* @param args additional arguments of the resource, eg. if the resource is
82+
* a method name, the args will be the parameters of the
83+
* method.
84+
*/
85+
void increaseThreadNum(String resource, String entryType, Object... args);
86+
87+
/**
88+
* Decrease current thread count of the resource name.
89+
*
90+
* @param resource resource name
91+
* @param entryType {@link EntryType} name, [IN] as provider, [OUT] as consumer.
92+
* @param args additional arguments of the resource, eg. if the resource is
93+
* a method name, the args will be the parameters of the
94+
* method.
95+
*/
96+
void decreaseThreadNum(String resource, String entryType, Object... args);
97+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.alibaba.csp.sentinel.metric.extension.callback;
22

33
import com.alibaba.csp.sentinel.context.Context;
4-
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
4+
import com.alibaba.csp.sentinel.metric.extension.AdvancedMetricExtension;
55
import com.alibaba.csp.sentinel.metric.extension.MetricExtension;
6+
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
67
import com.alibaba.csp.sentinel.node.DefaultNode;
78
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotEntryCallback;
89
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
@@ -15,20 +16,32 @@
1516
* @since 1.6.1
1617
*/
1718
public class MetricEntryCallback implements ProcessorSlotEntryCallback<DefaultNode> {
18-
@Override
19-
public void onPass(Context context, ResourceWrapper resourceWrapper, DefaultNode param,
20-
int count, Object... args) throws Exception {
21-
for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
22-
m.increaseThreadNum(resourceWrapper.getName(), args);
23-
m.addPass(resourceWrapper.getName(), count, args);
24-
}
25-
}
19+
@Override
20+
public void onPass(Context context, ResourceWrapper resourceWrapper, DefaultNode param, int count, Object... args)
21+
throws Exception {
22+
for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
23+
if (m instanceof AdvancedMetricExtension) {
24+
((AdvancedMetricExtension) m).increaseThreadNum(resourceWrapper.getName(),
25+
resourceWrapper.getEntryType().name(), args);
26+
((AdvancedMetricExtension) m).addPass(resourceWrapper.getName(), resourceWrapper.getEntryType().name(),
27+
count, args);
28+
} else {
29+
m.increaseThreadNum(resourceWrapper.getName(), args);
30+
m.addPass(resourceWrapper.getName(), count, args);
31+
}
32+
}
33+
}
2634

27-
@Override
28-
public void onBlocked(BlockException ex, Context context, ResourceWrapper resourceWrapper,
29-
DefaultNode param, int count, Object... args) {
30-
for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
31-
m.addBlock(resourceWrapper.getName(), count, context.getOrigin(), ex, args);
32-
}
33-
}
35+
@Override
36+
public void onBlocked(BlockException ex, Context context, ResourceWrapper resourceWrapper, DefaultNode param,
37+
int count, Object... args) {
38+
for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
39+
if (m instanceof AdvancedMetricExtension) {
40+
((AdvancedMetricExtension) m).addBlock(resourceWrapper.getName(), resourceWrapper.getEntryType().name(),
41+
count, context.getOrigin(), ex, args);
42+
} else {
43+
m.addBlock(resourceWrapper.getName(), count, context.getOrigin(), ex, args);
44+
}
45+
}
46+
}
3447
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.alibaba.csp.sentinel.metric.extension.callback;
22

33
import com.alibaba.csp.sentinel.context.Context;
4-
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
4+
import com.alibaba.csp.sentinel.metric.extension.AdvancedMetricExtension;
55
import com.alibaba.csp.sentinel.metric.extension.MetricExtension;
6+
import com.alibaba.csp.sentinel.metric.extension.MetricExtensionProvider;
67
import com.alibaba.csp.sentinel.slotchain.ProcessorSlotExitCallback;
78
import com.alibaba.csp.sentinel.slotchain.ResourceWrapper;
89
import com.alibaba.csp.sentinel.util.TimeUtil;
@@ -15,22 +16,31 @@
1516
*/
1617
public class MetricExitCallback implements ProcessorSlotExitCallback {
1718

18-
@Override
19-
public void onExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
20-
for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
21-
if (context.getCurEntry().getBlockError() != null) {
22-
continue;
23-
}
24-
String resource = resourceWrapper.getName();
25-
long realRt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTimestamp();
26-
m.addRt(resource, realRt, args);
27-
m.addSuccess(resource, count, args);
28-
m.decreaseThreadNum(resource, args);
29-
30-
Throwable ex = context.getCurEntry().getError();
31-
if (ex != null) {
32-
m.addException(resource, count, ex);
33-
}
34-
}
35-
}
19+
@Override
20+
public void onExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
21+
for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
22+
if (context.getCurEntry().getBlockError() != null) {
23+
continue;
24+
}
25+
String resource = resourceWrapper.getName();
26+
String entryType = resourceWrapper.getEntryType().name();
27+
Throwable ex = context.getCurEntry().getError();
28+
long realRt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTimestamp();
29+
if (m instanceof AdvancedMetricExtension) {
30+
((AdvancedMetricExtension) m).addRt(resource, entryType, realRt, args);
31+
((AdvancedMetricExtension) m).addSuccess(resource, entryType, count, args);
32+
((AdvancedMetricExtension) m).decreaseThreadNum(resource, entryType, args);
33+
if (null != ex) {
34+
((AdvancedMetricExtension) m).addException(resource, entryType, count, ex);
35+
}
36+
} else {
37+
m.addRt(resource, realRt, args);
38+
m.addSuccess(resource, count, args);
39+
m.decreaseThreadNum(resource, args);
40+
if (null != ex) {
41+
m.addException(resource, count, ex);
42+
}
43+
}
44+
}
45+
}
3646
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.alibaba.csp.sentinel.metric.extension.callback;
2+
3+
import com.alibaba.csp.sentinel.metric.extension.AdvancedMetricExtension;
4+
import com.alibaba.csp.sentinel.slots.block.BlockException;
5+
6+
class FakeAdvancedMetricExtension implements AdvancedMetricExtension {
7+
long pass = 0;
8+
long block = 0;
9+
long success = 0;
10+
long exception = 0;
11+
long rt = 0;
12+
long thread = 0;
13+
14+
@Override
15+
public void addPass(String resource, int n, Object... args) {
16+
// Do nothing because of using the enhanced one
17+
}
18+
19+
@Override
20+
public void addBlock(String resource, int n, String origin, BlockException blockException, Object... args) {
21+
// Do nothing because of using the enhanced one
22+
}
23+
24+
@Override
25+
public void addSuccess(String resource, int n, Object... args) {
26+
// Do nothing because of using the enhanced one
27+
}
28+
29+
@Override
30+
public void addException(String resource, int n, Throwable throwable) {
31+
// Do nothing because of using the enhanced one
32+
}
33+
34+
@Override
35+
public void addRt(String resource, long rt, Object... args) {
36+
// Do nothing because of using the enhanced one
37+
}
38+
39+
@Override
40+
public void increaseThreadNum(String resource, Object... args) {
41+
// Do nothing because of using the enhanced one
42+
}
43+
44+
@Override
45+
public void decreaseThreadNum(String resource, Object... args) {
46+
// Do nothing because of using the enhanced one
47+
}
48+
49+
@Override
50+
public void addPass(String resource, String entryType, int n, Object... args) {
51+
pass += n;
52+
}
53+
54+
@Override
55+
public void addBlock(String resource, String entryType, int n, String origin, BlockException blockException,
56+
Object... args) {
57+
block += n;
58+
}
59+
60+
@Override
61+
public void addSuccess(String resource, String entryType, int n, Object... args) {
62+
success += n;
63+
}
64+
65+
@Override
66+
public void addException(String resource, String entryType, int n, Throwable throwable) {
67+
exception += n;
68+
}
69+
70+
@Override
71+
public void addRt(String resource, String entryTypeTag, long rt, Object... args) {
72+
this.rt += rt;
73+
}
74+
75+
@Override
76+
public void increaseThreadNum(String resource, String entryType, Object... args) {
77+
thread ++;
78+
}
79+
80+
@Override
81+
public void decreaseThreadNum(String resource, String entryType, Object... args) {
82+
thread --;
83+
}
84+
85+
}

sentinel-core/src/test/java/com/alibaba/csp/sentinel/metric/extension/callback/MetricEntryCallbackTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,30 @@ public class MetricEntryCallbackTest {
2020
@Test
2121
public void onPass() throws Exception {
2222
FakeMetricExtension extension = new FakeMetricExtension();
23+
FakeAdvancedMetricExtension advancedExtension = new FakeAdvancedMetricExtension();
2324
MetricExtensionProvider.addMetricExtension(extension);
25+
MetricExtensionProvider.addMetricExtension(advancedExtension);
2426

2527
MetricEntryCallback entryCallback = new MetricEntryCallback();
2628
StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
2729
int count = 2;
2830
Object[] args = {"args1", "args2"};
2931
entryCallback.onPass(null, resourceWrapper, null, count, args);
32+
// assert extension
3033
Assert.assertEquals(extension.pass, count);
3134
Assert.assertEquals(extension.thread, 1);
35+
36+
// assert advancedExtension
37+
Assert.assertEquals(advancedExtension.pass, count);
38+
Assert.assertEquals(advancedExtension.thread, 1);
3239
}
3340

3441
@Test
3542
public void onBlocked() throws Exception {
3643
FakeMetricExtension extension = new FakeMetricExtension();
44+
FakeAdvancedMetricExtension advancedExtension = new FakeAdvancedMetricExtension();
3745
MetricExtensionProvider.addMetricExtension(extension);
46+
MetricExtensionProvider.addMetricExtension(advancedExtension);
3847

3948
MetricEntryCallback entryCallback = new MetricEntryCallback();
4049
StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
@@ -43,6 +52,9 @@ public void onBlocked() throws Exception {
4352
int count = 2;
4453
Object[] args = {"args1", "args2"};
4554
entryCallback.onBlocked(new FlowException("xx"), context, resourceWrapper, null, count, args);
55+
// assert extension
4656
Assert.assertEquals(extension.block, count);
57+
// assert advancedExtension
58+
Assert.assertEquals(advancedExtension.block, count);
4759
}
4860
}

sentinel-core/src/test/java/com/alibaba/csp/sentinel/metric/extension/callback/MetricExitCallbackTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,37 @@ public void onExit() {
6262
Assert.assertEquals(extension.success, 6 + count);
6363
Assert.assertEquals(extension.thread, 10 - 1);
6464
}
65+
66+
/**
67+
* @author bill_yip
68+
*/
69+
@Test
70+
public void advancedExtensionOnExit() {
71+
FakeAdvancedMetricExtension extension = new FakeAdvancedMetricExtension();
72+
MetricExtensionProvider.addMetricExtension(extension);
73+
74+
MetricExitCallback exitCallback = new MetricExitCallback();
75+
StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
76+
int count = 2;
77+
Object[] args = {"args1", "args2"};
78+
long prevRt = 20;
79+
extension.rt = prevRt;
80+
extension.success = 6;
81+
extension.thread = 10;
82+
Context context = mock(Context.class);
83+
Entry entry = mock(Entry.class);
84+
85+
// Mock current time
86+
long curMillis = System.currentTimeMillis();
87+
setCurrentMillis(curMillis);
88+
89+
int deltaMs = 100;
90+
when(entry.getError()).thenReturn(null);
91+
when(entry.getCreateTimestamp()).thenReturn(curMillis - deltaMs);
92+
when(context.getCurEntry()).thenReturn(entry);
93+
exitCallback.onExit(context, resourceWrapper, count, args);
94+
Assert.assertEquals(prevRt + deltaMs, extension.rt);
95+
Assert.assertEquals(extension.success, 6 + count);
96+
Assert.assertEquals(extension.thread, 10 - 1);
97+
}
6598
}

0 commit comments

Comments
 (0)