19
19
import com .alibaba .csp .sentinel .EntryType ;
20
20
import com .alibaba .csp .sentinel .ResourceTypeConstants ;
21
21
import com .alibaba .csp .sentinel .SphU ;
22
- import com .alibaba .csp .sentinel .Tracer ;
23
22
import com .alibaba .csp .sentinel .adapter .dubbo .config .DubboConfig ;
24
23
import com .alibaba .csp .sentinel .adapter .dubbo .fallback .DubboFallbackRegistry ;
25
24
import com .alibaba .csp .sentinel .log .RecordLog ;
26
25
import com .alibaba .csp .sentinel .slots .block .BlockException ;
27
26
import org .apache .dubbo .common .extension .Activate ;
28
- import org .apache .dubbo .rpc .Filter ;
29
27
import org .apache .dubbo .rpc .Invocation ;
28
+ import org .apache .dubbo .rpc .InvokeMode ;
30
29
import org .apache .dubbo .rpc .Invoker ;
31
30
import org .apache .dubbo .rpc .Result ;
31
+ import org .apache .dubbo .rpc .RpcContext ;
32
32
import org .apache .dubbo .rpc .RpcException ;
33
+ import org .apache .dubbo .rpc .support .RpcUtils ;
33
34
34
35
/**
35
36
* <p>Dubbo service consumer filter for Sentinel. Auto activated by default.</p>
43
44
* @author Eric Zhao
44
45
*/
45
46
@ Activate (group = "consumer" )
46
- public class SentinelDubboConsumerFilter implements Filter {
47
+ public class SentinelDubboConsumerFilter extends BaseSentinelDubboFilter {
47
48
48
49
public SentinelDubboConsumerFilter () {
49
50
RecordLog .info ("Sentinel Apache Dubbo consumer filter initialized" );
@@ -53,33 +54,28 @@ public SentinelDubboConsumerFilter() {
53
54
public Result invoke (Invoker <?> invoker , Invocation invocation ) throws RpcException {
54
55
Entry interfaceEntry = null ;
55
56
Entry methodEntry = null ;
57
+ RpcContext rpcContext = RpcContext .getContext ();
56
58
try {
57
- String resourceName = DubboUtils .getResourceName (invoker , invocation , DubboConfig .getDubboConsumerPrefix ());
58
- interfaceEntry = SphU . entry ( invoker .getInterface ().getName (),
59
- ResourceTypeConstants . COMMON_RPC , EntryType . OUT );
60
- methodEntry = SphU . entry ( resourceName , ResourceTypeConstants . COMMON_RPC , EntryType . OUT );
59
+ String methodResourceName = DubboUtils .getResourceName (invoker , invocation , DubboConfig .getDubboConsumerPrefix ());
60
+ String interfaceResourceName = DubboConfig . getDubboInterfaceGroupAndVersionEnabled () ? invoker .getUrl ().getColonSeparatedKey ()
61
+ : invoker . getInterface (). getName ( );
62
+ InvokeMode invokeMode = RpcUtils . getInvokeMode ( invoker . getUrl (), invocation );
61
63
62
- Result result = invoker .invoke (invocation );
63
- if (result .hasException ()) {
64
- Throwable e = result .getException ();
65
- // Record common exception.
66
- Tracer .traceEntry (e , interfaceEntry );
67
- Tracer .traceEntry (e , methodEntry );
64
+ if (InvokeMode .SYNC == invokeMode ) {
65
+ interfaceEntry = SphU .entry (interfaceResourceName , ResourceTypeConstants .COMMON_RPC , EntryType .OUT );
66
+ methodEntry = SphU .entry (methodResourceName , ResourceTypeConstants .COMMON_RPC , EntryType .OUT , invocation .getArguments ());
67
+ } else {
68
+ // should generate the AsyncEntry when the invoke model in future or async
69
+ interfaceEntry = SphU .asyncEntry (interfaceResourceName , ResourceTypeConstants .COMMON_RPC , EntryType .OUT );
70
+ methodEntry = SphU .asyncEntry (methodResourceName , ResourceTypeConstants .COMMON_RPC , EntryType .OUT , 1 , invocation .getArguments ());
68
71
}
69
- return result ;
72
+ rpcContext .set (DubboUtils .DUBBO_INTERFACE_ENTRY_KEY , interfaceEntry );
73
+ rpcContext .set (DubboUtils .DUBBO_METHOD_ENTRY_KEY , methodEntry );
74
+ return invoker .invoke (invocation );
70
75
} catch (BlockException e ) {
71
76
return DubboFallbackRegistry .getConsumerFallback ().handle (invoker , invocation , e );
72
- } catch (RpcException e ) {
73
- Tracer .traceEntry (e , interfaceEntry );
74
- Tracer .traceEntry (e , methodEntry );
75
- throw e ;
76
- } finally {
77
- if (methodEntry != null ) {
78
- methodEntry .exit ();
79
- }
80
- if (interfaceEntry != null ) {
81
- interfaceEntry .exit ();
82
- }
83
77
}
84
78
}
85
79
}
80
+
81
+
0 commit comments