Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catch exceptions in grpc #392

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> 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<ReqT, RespT> 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<ReqT, RespT> clientCall = next.newCall(method, callOptions);
return new GrpcClientInterceptor.TracingClientCall<>(clientCall, currentSpan);
}

static final class TracingClientCall<ReqT, RespT>
Expand All @@ -62,21 +71,29 @@ static final class TracingClientCall<ReqT, RespT>
public void start(Listener<RespT> 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);
}
}

@Override
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);
}
}
}
Expand All @@ -94,21 +111,29 @@ static final class TracingClientCallListener<RespT>
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);
}
}

@Override
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> 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<String, String> mapHeaders = GrpcSpanDecorator.metadataToMap(headers);
Map<String, String> 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<ReqT> 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<ReqT> noop = NoopServerCallListener.INSTANCE;
return noop;
}

Listener<ReqT> serverCall = next.startCall(new TracingServerCall<>(call, currentSpan), headers);
return new TracingServerCallListener<>(serverCall, currentSpan);
Listener<ReqT> 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<ReqT, RespT>
Expand All @@ -78,21 +88,29 @@ static final class TracingServerCall<ReqT, RespT>
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);
}
}

@Override
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);
}
}
}
Expand All @@ -111,10 +129,14 @@ static final class TracingServerCallListener<ReqT>
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);
}
}
}
Expand Down
Loading