Skip to content

Commit 8f4d68c

Browse files
authored
catch exceptions in grpc (#392)
1 parent 6e851f8 commit 8f4d68c

File tree

2 files changed

+101
-54
lines changed

2 files changed

+101
-54
lines changed

instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/client/GrpcClientInterceptor.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,31 @@
3030
import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_6.GrpcSpanDecorator;
3131
import org.hypertrace.agent.core.config.InstrumentationConfig;
3232
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
3335

3436
public class GrpcClientInterceptor implements ClientInterceptor {
3537

38+
private static final Logger log = LoggerFactory.getLogger(GrpcClientInterceptor.class);
39+
3640
@Override
3741
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
3842
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
3943

40-
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
41-
if (!instrumentationConfig.isInstrumentationEnabled(
42-
GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) {
44+
try {
45+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
46+
if (!instrumentationConfig.isInstrumentationEnabled(
47+
GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) {
48+
return next.newCall(method, callOptions);
49+
}
50+
51+
Span currentSpan = Span.current();
52+
ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
53+
return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan);
54+
} catch (Throwable t) {
55+
log.debug("exception thrown while intercepting grpc client call", t);
4356
return next.newCall(method, callOptions);
4457
}
45-
46-
Span currentSpan = Span.current();
47-
ClientCall<ReqT, RespT> clientCall = next.newCall(method, callOptions);
48-
return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan);
4958
}
5059

5160
static final class TracingClientCall<ReqT, RespT>
@@ -62,21 +71,29 @@ static final class TracingClientCall<ReqT, RespT>
6271
public void start(Listener<RespT> responseListener, Metadata headers) {
6372
super.start(new TracingClientCallListener<>(responseListener, span), headers);
6473

65-
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
66-
if (instrumentationConfig.rpcMetadata().request()) {
67-
GrpcSpanDecorator.addMetadataAttributes(
68-
headers, span, HypertraceSemanticAttributes::rpcRequestMetadata);
74+
try {
75+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
76+
if (instrumentationConfig.rpcMetadata().request()) {
77+
GrpcSpanDecorator.addMetadataAttributes(
78+
headers, span, HypertraceSemanticAttributes::rpcRequestMetadata);
79+
}
80+
} catch (Throwable t) {
81+
log.debug("exception thrown while capturing grpc client request metadata", t);
6982
}
7083
}
7184

7285
@Override
7386
public void sendMessage(ReqT message) {
7487
super.sendMessage(message);
7588

76-
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
77-
if (instrumentationConfig.rpcBody().request()) {
78-
GrpcSpanDecorator.addMessageAttribute(
79-
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
89+
try {
90+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
91+
if (instrumentationConfig.rpcBody().request()) {
92+
GrpcSpanDecorator.addMessageAttribute(
93+
message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY);
94+
}
95+
} catch (Throwable t) {
96+
log.debug("exception thrown while capturing grpc client request body", t);
8097
}
8198
}
8299
}
@@ -94,21 +111,29 @@ static final class TracingClientCallListener<RespT>
94111
public void onMessage(RespT message) {
95112
delegate().onMessage(message);
96113

97-
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
98-
if (instrumentationConfig.rpcBody().response()) {
99-
GrpcSpanDecorator.addMessageAttribute(
100-
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
114+
try {
115+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
116+
if (instrumentationConfig.rpcBody().response()) {
117+
GrpcSpanDecorator.addMessageAttribute(
118+
message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY);
119+
}
120+
} catch (Throwable t) {
121+
log.debug("exception thrown while capturing grpc client response body", t);
101122
}
102123
}
103124

104125
@Override
105126
public void onHeaders(Metadata headers) {
106127
super.onHeaders(headers);
107128

108-
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
109-
if (instrumentationConfig.rpcMetadata().response()) {
110-
GrpcSpanDecorator.addMetadataAttributes(
111-
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
129+
try {
130+
InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get();
131+
if (instrumentationConfig.rpcMetadata().response()) {
132+
GrpcSpanDecorator.addMetadataAttributes(
133+
headers, span, HypertraceSemanticAttributes::rpcResponseMetadata);
134+
}
135+
} catch (Throwable t) {
136+
log.debug("exception thrown while capturing grpc client response metadata", t);
112137
}
113138
}
114139
}

instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,47 @@
3131
import org.hypertrace.agent.core.config.InstrumentationConfig;
3232
import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes;
3333
import org.hypertrace.agent.filter.FilterRegistry;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436

3537
public 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

Comments
 (0)