31
31
import org .hypertrace .agent .core .config .InstrumentationConfig ;
32
32
import org .hypertrace .agent .core .instrumentation .HypertraceSemanticAttributes ;
33
33
import org .hypertrace .agent .filter .FilterRegistry ;
34
+ import org .slf4j .Logger ;
35
+ import org .slf4j .LoggerFactory ;
34
36
35
37
public class GrpcServerInterceptor implements ServerInterceptor {
36
38
39
+ private static final Logger log = LoggerFactory .getLogger (GrpcServerInterceptor .class );
40
+
37
41
@ Override
38
42
public <ReqT , RespT > ServerCall .Listener <ReqT > interceptCall (
39
43
ServerCall <ReqT , RespT > call , Metadata headers , ServerCallHandler <ReqT , RespT > next ) {
40
44
41
- InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
42
- if (!instrumentationConfig .isInstrumentationEnabled (
43
- GrpcInstrumentationName .PRIMARY , GrpcInstrumentationName .OTHER )) {
44
- return next .startCall (call , headers );
45
- }
45
+ try {
46
+ InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
47
+ if (!instrumentationConfig .isInstrumentationEnabled (
48
+ GrpcInstrumentationName .PRIMARY , GrpcInstrumentationName .OTHER )) {
49
+ return next .startCall (call , headers );
50
+ }
46
51
47
- Span currentSpan = Span .current ();
52
+ Span currentSpan = Span .current ();
48
53
49
- Map <String , String > mapHeaders = GrpcSpanDecorator .metadataToMap (headers );
54
+ Map <String , String > mapHeaders = GrpcSpanDecorator .metadataToMap (headers );
50
55
51
- if (instrumentationConfig .rpcMetadata ().request ()) {
52
- GrpcSpanDecorator .addMetadataAttributes (mapHeaders , currentSpan );
53
- }
56
+ if (instrumentationConfig .rpcMetadata ().request ()) {
57
+ GrpcSpanDecorator .addMetadataAttributes (mapHeaders , currentSpan );
58
+ }
54
59
55
- boolean block = FilterRegistry .getFilter ().evaluateRequestHeaders (currentSpan , mapHeaders );
56
- if (block ) {
57
- call .close (Status .PERMISSION_DENIED , new Metadata ());
58
- @ SuppressWarnings ("unchecked" )
59
- ServerCall .Listener <ReqT > noop = NoopServerCallListener .INSTANCE ;
60
- return noop ;
61
- }
60
+ boolean block = FilterRegistry .getFilter ().evaluateRequestHeaders (currentSpan , mapHeaders );
61
+ if (block ) {
62
+ call .close (Status .PERMISSION_DENIED , new Metadata ());
63
+ @ SuppressWarnings ("unchecked" )
64
+ ServerCall .Listener <ReqT > noop = NoopServerCallListener .INSTANCE ;
65
+ return noop ;
66
+ }
62
67
63
- Listener <ReqT > serverCall = next .startCall (new TracingServerCall <>(call , currentSpan ), headers );
64
- return new TracingServerCallListener <>(serverCall , currentSpan );
68
+ Listener <ReqT > serverCall =
69
+ next .startCall (new TracingServerCall <>(call , currentSpan ), headers );
70
+ return new TracingServerCallListener <>(serverCall , currentSpan );
71
+ } catch (Throwable t ) {
72
+ log .debug ("exception thrown during intercepting server call" , t );
73
+ return next .startCall (call , headers );
74
+ }
65
75
}
66
76
67
77
static final class TracingServerCall <ReqT , RespT >
@@ -78,21 +88,29 @@ static final class TracingServerCall<ReqT, RespT>
78
88
public void sendMessage (RespT message ) {
79
89
super .sendMessage (message );
80
90
81
- InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
82
- if (instrumentationConfig .rpcBody ().response ()) {
83
- GrpcSpanDecorator .addMessageAttribute (
84
- message , span , HypertraceSemanticAttributes .RPC_RESPONSE_BODY );
91
+ try {
92
+ InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
93
+ if (instrumentationConfig .rpcBody ().response ()) {
94
+ GrpcSpanDecorator .addMessageAttribute (
95
+ message , span , HypertraceSemanticAttributes .RPC_RESPONSE_BODY );
96
+ }
97
+ } catch (Throwable t ) {
98
+ log .debug ("exception thrown while capturing grpc server response body" , t );
85
99
}
86
100
}
87
101
88
102
@ Override
89
103
public void sendHeaders (Metadata headers ) {
90
104
super .sendHeaders (headers );
91
105
92
- InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
93
- if (instrumentationConfig .rpcMetadata ().response ()) {
94
- GrpcSpanDecorator .addMetadataAttributes (
95
- headers , span , HypertraceSemanticAttributes ::rpcResponseMetadata );
106
+ try {
107
+ InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
108
+ if (instrumentationConfig .rpcMetadata ().response ()) {
109
+ GrpcSpanDecorator .addMetadataAttributes (
110
+ headers , span , HypertraceSemanticAttributes ::rpcResponseMetadata );
111
+ }
112
+ } catch (Throwable t ) {
113
+ log .debug ("exception thrown while capturing grpc server response headers" , t );
96
114
}
97
115
}
98
116
}
@@ -111,10 +129,14 @@ static final class TracingServerCallListener<ReqT>
111
129
public void onMessage (ReqT message ) {
112
130
delegate ().onMessage (message );
113
131
114
- InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
115
- if (instrumentationConfig .rpcBody ().request ()) {
116
- GrpcSpanDecorator .addMessageAttribute (
117
- message , span , HypertraceSemanticAttributes .RPC_REQUEST_BODY );
132
+ try {
133
+ InstrumentationConfig instrumentationConfig = InstrumentationConfig .ConfigProvider .get ();
134
+ if (instrumentationConfig .rpcBody ().request ()) {
135
+ GrpcSpanDecorator .addMessageAttribute (
136
+ message , span , HypertraceSemanticAttributes .RPC_REQUEST_BODY );
137
+ }
138
+ } catch (Throwable t ) {
139
+ log .debug ("exception thrown while capturing grpc server request body" , t );
118
140
}
119
141
}
120
142
}
0 commit comments