diff --git a/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/client/GrpcClientInterceptor.java b/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/client/GrpcClientInterceptor.java index eaf91e627..3c59e3062 100644 --- a/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/client/GrpcClientInterceptor.java +++ b/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/client/GrpcClientInterceptor.java @@ -30,22 +30,31 @@ import io.opentelemetry.javaagent.instrumentation.hypertrace.grpc.v1_6.GrpcSpanDecorator; import org.hypertrace.agent.core.config.InstrumentationConfig; import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class GrpcClientInterceptor implements ClientInterceptor { + private static final Logger log = LoggerFactory.getLogger(GrpcClientInterceptor.class); + @Override public ClientCall interceptCall( MethodDescriptor method, CallOptions callOptions, Channel next) { - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (!instrumentationConfig.isInstrumentationEnabled( - GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) { + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (!instrumentationConfig.isInstrumentationEnabled( + GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) { + return next.newCall(method, callOptions); + } + + Span currentSpan = Span.current(); + ClientCall clientCall = next.newCall(method, callOptions); + return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan); + } catch (Throwable t) { + log.debug("exception thrown while intercepting grpc client call", t); return next.newCall(method, callOptions); } - - Span currentSpan = Span.current(); - ClientCall clientCall = next.newCall(method, callOptions); - return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan); } static final class TracingClientCall @@ -62,10 +71,14 @@ static final class TracingClientCall public void start(Listener responseListener, Metadata headers) { super.start(new TracingClientCallListener<>(responseListener, span), headers); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcMetadata().request()) { - GrpcSpanDecorator.addMetadataAttributes( - headers, span, HypertraceSemanticAttributes::rpcRequestMetadata); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcMetadata().request()) { + GrpcSpanDecorator.addMetadataAttributes( + headers, span, HypertraceSemanticAttributes::rpcRequestMetadata); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc client request metadata", t); } } @@ -73,10 +86,14 @@ public void start(Listener responseListener, Metadata headers) { public void sendMessage(ReqT message) { super.sendMessage(message); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcBody().request()) { - GrpcSpanDecorator.addMessageAttribute( - message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcBody().request()) { + GrpcSpanDecorator.addMessageAttribute( + message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc client request body", t); } } } @@ -94,10 +111,14 @@ static final class TracingClientCallListener public void onMessage(RespT message) { delegate().onMessage(message); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcBody().response()) { - GrpcSpanDecorator.addMessageAttribute( - message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcBody().response()) { + GrpcSpanDecorator.addMessageAttribute( + message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc client response body", t); } } @@ -105,10 +126,14 @@ public void onMessage(RespT message) { public void onHeaders(Metadata headers) { super.onHeaders(headers); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcMetadata().response()) { - GrpcSpanDecorator.addMetadataAttributes( - headers, span, HypertraceSemanticAttributes::rpcResponseMetadata); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcMetadata().response()) { + GrpcSpanDecorator.addMetadataAttributes( + headers, span, HypertraceSemanticAttributes::rpcResponseMetadata); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc client response metadata", t); } } } diff --git a/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java b/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java index ee1630a2a..bfdcc559c 100644 --- a/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java +++ b/instrumentation/grpc-1.6/src/main/java/io/opentelemetry/javaagent/instrumentation/hypertrace/grpc/v1_6/server/GrpcServerInterceptor.java @@ -31,37 +31,47 @@ import org.hypertrace.agent.core.config.InstrumentationConfig; import org.hypertrace.agent.core.instrumentation.HypertraceSemanticAttributes; import org.hypertrace.agent.filter.FilterRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class GrpcServerInterceptor implements ServerInterceptor { + private static final Logger log = LoggerFactory.getLogger(GrpcServerInterceptor.class); + @Override public ServerCall.Listener interceptCall( ServerCall call, Metadata headers, ServerCallHandler next) { - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (!instrumentationConfig.isInstrumentationEnabled( - GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) { - return next.startCall(call, headers); - } + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (!instrumentationConfig.isInstrumentationEnabled( + GrpcInstrumentationName.PRIMARY, GrpcInstrumentationName.OTHER)) { + return next.startCall(call, headers); + } - Span currentSpan = Span.current(); + Span currentSpan = Span.current(); - Map mapHeaders = GrpcSpanDecorator.metadataToMap(headers); + Map mapHeaders = GrpcSpanDecorator.metadataToMap(headers); - if (instrumentationConfig.rpcMetadata().request()) { - GrpcSpanDecorator.addMetadataAttributes(mapHeaders, currentSpan); - } + if (instrumentationConfig.rpcMetadata().request()) { + GrpcSpanDecorator.addMetadataAttributes(mapHeaders, currentSpan); + } - boolean block = FilterRegistry.getFilter().evaluateRequestHeaders(currentSpan, mapHeaders); - if (block) { - call.close(Status.PERMISSION_DENIED, new Metadata()); - @SuppressWarnings("unchecked") - ServerCall.Listener noop = NoopServerCallListener.INSTANCE; - return noop; - } + boolean block = FilterRegistry.getFilter().evaluateRequestHeaders(currentSpan, mapHeaders); + if (block) { + call.close(Status.PERMISSION_DENIED, new Metadata()); + @SuppressWarnings("unchecked") + ServerCall.Listener noop = NoopServerCallListener.INSTANCE; + return noop; + } - Listener serverCall = next.startCall(new TracingServerCall<>(call, currentSpan), headers); - return new TracingServerCallListener<>(serverCall, currentSpan); + Listener serverCall = + next.startCall(new TracingServerCall<>(call, currentSpan), headers); + return new TracingServerCallListener<>(serverCall, currentSpan); + } catch (Throwable t) { + log.debug("exception thrown during intercepting server call", t); + return next.startCall(call, headers); + } } static final class TracingServerCall @@ -78,10 +88,14 @@ static final class TracingServerCall public void sendMessage(RespT message) { super.sendMessage(message); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcBody().response()) { - GrpcSpanDecorator.addMessageAttribute( - message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcBody().response()) { + GrpcSpanDecorator.addMessageAttribute( + message, span, HypertraceSemanticAttributes.RPC_RESPONSE_BODY); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc server response body", t); } } @@ -89,10 +103,14 @@ public void sendMessage(RespT message) { public void sendHeaders(Metadata headers) { super.sendHeaders(headers); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcMetadata().response()) { - GrpcSpanDecorator.addMetadataAttributes( - headers, span, HypertraceSemanticAttributes::rpcResponseMetadata); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcMetadata().response()) { + GrpcSpanDecorator.addMetadataAttributes( + headers, span, HypertraceSemanticAttributes::rpcResponseMetadata); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc server response headers", t); } } } @@ -111,10 +129,14 @@ static final class TracingServerCallListener public void onMessage(ReqT message) { delegate().onMessage(message); - InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); - if (instrumentationConfig.rpcBody().request()) { - GrpcSpanDecorator.addMessageAttribute( - message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY); + try { + InstrumentationConfig instrumentationConfig = InstrumentationConfig.ConfigProvider.get(); + if (instrumentationConfig.rpcBody().request()) { + GrpcSpanDecorator.addMessageAttribute( + message, span, HypertraceSemanticAttributes.RPC_REQUEST_BODY); + } + } catch (Throwable t) { + log.debug("exception thrown while capturing grpc server request body", t); } } }