-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging interceptor and error augmentation interceptor
- Loading branch information
Showing
3 changed files
with
194 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
google-cloud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/LoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.spanner.spi.v1; | ||
|
||
import io.grpc.CallOptions; | ||
import io.grpc.Channel; | ||
import io.grpc.ClientCall; | ||
import io.grpc.ClientInterceptor; | ||
import io.grpc.ForwardingClientCall; | ||
import io.grpc.ForwardingClientCallListener; | ||
import io.grpc.Metadata; | ||
import io.grpc.MethodDescriptor; | ||
import io.grpc.Status; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import javax.annotation.Nullable; | ||
|
||
/** Adds logging to rpc calls */ | ||
class LoggingInterceptor implements ClientInterceptor { | ||
|
||
private final Logger logger; | ||
private final Level level; | ||
|
||
LoggingInterceptor(Logger logger, Level level) { | ||
this.logger = logger; | ||
this.level = level; | ||
} | ||
|
||
private class CallLogger { | ||
|
||
private final MethodDescriptor<?, ?> method; | ||
|
||
CallLogger(MethodDescriptor<?, ?> method) { | ||
this.method = method; | ||
} | ||
|
||
void log(String message) { | ||
logger.log( | ||
level, | ||
"{0}[{1}]: {2}", | ||
new Object[] { | ||
method.getFullMethodName(), Integer.toHexString(System.identityHashCode(this)), message | ||
}); | ||
} | ||
|
||
void logfmt(String message, Object... params) { | ||
log(String.format(message, params)); | ||
} | ||
} | ||
|
||
@Override | ||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( | ||
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { | ||
if (!logger.isLoggable(level)) { | ||
return next.newCall(method, callOptions); | ||
} | ||
|
||
final CallLogger callLogger = new CallLogger(method); | ||
callLogger.log("Start"); | ||
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>( | ||
next.newCall(method, callOptions)) { | ||
@Override | ||
public void start(Listener<RespT> responseListener, Metadata headers) { | ||
super.start( | ||
new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>( | ||
responseListener) { | ||
@Override | ||
public void onMessage(RespT message) { | ||
callLogger.logfmt("Received:\n%s", message); | ||
super.onMessage(message); | ||
} | ||
|
||
@Override | ||
public void onClose(Status status, Metadata trailers) { | ||
callLogger.logfmt("Closed with status %s and trailers %s", status, trailers); | ||
super.onClose(status, trailers); | ||
} | ||
}, | ||
headers); | ||
} | ||
|
||
@Override | ||
public void sendMessage(ReqT message) { | ||
callLogger.logfmt("Send:\n%s", message); | ||
super.sendMessage(message); | ||
} | ||
|
||
@Override | ||
public void cancel(@Nullable String message, @Nullable Throwable cause) { | ||
callLogger.logfmt("Cancelled with message %s", message); | ||
super.cancel(message, cause); | ||
} | ||
}; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...oud-spanner/src/main/java/com/google/cloud/spanner/spi/v1/SpannerInterceptorProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2018 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.cloud.spanner.spi.v1; | ||
|
||
import com.google.api.gax.grpc.GrpcInterceptorProvider; | ||
import com.google.common.collect.ImmutableList; | ||
import io.grpc.ClientInterceptor; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* For internal use only. | ||
*/ | ||
class SpannerInterceptorProvider implements GrpcInterceptorProvider { | ||
|
||
private static final List<ClientInterceptor> clientInterceptors = | ||
ImmutableList.of( | ||
new SpannerErrorInterceptor(), | ||
new LoggingInterceptor(Logger.getLogger(GrpcSpannerRpc.class.getName()), Level.FINER)); | ||
|
||
@Override | ||
public List<ClientInterceptor> getInterceptors() { | ||
return clientInterceptors; | ||
} | ||
|
||
} |