3131import org .hypertrace .agent .core .config .InstrumentationConfig ;
3232import org .hypertrace .agent .core .instrumentation .HypertraceSemanticAttributes ;
3333import org .hypertrace .agent .filter .FilterRegistry ;
34+ import org .slf4j .Logger ;
35+ import org .slf4j .LoggerFactory ;
3436
3537public class GrpcServerInterceptor implements ServerInterceptor {
3638
39+ private static final Logger log = LoggerFactory .getLogger (GrpcServerInterceptor .class );
40+
3741 @ Override
3842 public <ReqT , RespT > ServerCall .Listener <ReqT > interceptCall (
3943 ServerCall <ReqT , RespT > call , Metadata headers , ServerCallHandler <ReqT , RespT > next ) {
4044
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+ }
4651
47- Span currentSpan = Span .current ();
52+ Span currentSpan = Span .current ();
4853
49- Map <String , String > mapHeaders = GrpcSpanDecorator .metadataToMap (headers );
54+ Map <String , String > mapHeaders = GrpcSpanDecorator .metadataToMap (headers );
5055
51- if (instrumentationConfig .rpcMetadata ().request ()) {
52- GrpcSpanDecorator .addMetadataAttributes (mapHeaders , currentSpan );
53- }
56+ if (instrumentationConfig .rpcMetadata ().request ()) {
57+ GrpcSpanDecorator .addMetadataAttributes (mapHeaders , currentSpan );
58+ }
5459
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+ }
6267
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+ }
6575 }
6676
6777 static final class TracingServerCall <ReqT , RespT >
@@ -78,21 +88,29 @@ static final class TracingServerCall<ReqT, RespT>
7888 public void sendMessage (RespT message ) {
7989 super .sendMessage (message );
8090
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 );
8599 }
86100 }
87101
88102 @ Override
89103 public void sendHeaders (Metadata headers ) {
90104 super .sendHeaders (headers );
91105
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 );
96114 }
97115 }
98116 }
@@ -111,10 +129,14 @@ static final class TracingServerCallListener<ReqT>
111129 public void onMessage (ReqT message ) {
112130 delegate ().onMessage (message );
113131
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 );
118140 }
119141 }
120142 }
0 commit comments