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

[DO-NOT-MERGE] Added remote address to grpc status exception #642

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ private GoogleCloudStorageGrpcReadChannel(

private static IOException convertError(
StatusRuntimeException error, StorageResourceId resourceId) {
String msg = String.format("Error reading '%s'", resourceId);
switch (Status.fromThrowable(error).getCode()) {
Status status = error.getStatus();
String msg = String.format("Error reading '%s' got status: '%s'", resourceId, status);
switch (status.getCode()) {
case NOT_FOUND:
return GoogleCloudStorageExceptions.createFileNotFoundException(
resourceId.getBucketName(), resourceId.getObjectName(), new IOException(msg, error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.grpc.Context;
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
Expand All @@ -33,6 +34,7 @@
import io.grpc.alts.GoogleDefaultChannelBuilder;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.stub.AbstractStub;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -139,6 +141,35 @@ public void onClose(Status status, Metadata trailers) {
}
}

final class RemoteAddressDebuggingInterceptor implements ClientInterceptor {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
final ClientCall<ReqT, RespT> newCall = next.newCall(method, callOptions);
return new SimpleForwardingClientCall<ReqT, RespT>(newCall) {
@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
super.start(
new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onClose(Status status, Metadata trailers) {
if (!status.isOk()) {
SocketAddress remoteAddr =
newCall.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR);
if (remoteAddr != null) {
status =
status.augmentDescription("Remote Address: " + remoteAddr.toString());
}
}
super.onClose(status, trailers);
}
},
headers);
}
};
}
}

class ChannelAndRequestCounter {
private final ManagedChannel channel;
private final ActiveRequestCounter counter;
Expand Down Expand Up @@ -192,6 +223,7 @@ private ChannelAndRequestCounter buildManagedChannel() {
.createChannelBuilder(target)
.enableRetry()
.intercept(counter)
.intercept(new RemoteAddressDebuggingInterceptor())
.userAgent(userAgent)
.build();
return new ChannelAndRequestCounter(channel, counter);
Expand Down