|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage.it; |
| 18 | + |
| 19 | +import com.google.api.gax.grpc.GrpcInterceptorProvider; |
| 20 | +import com.google.common.collect.ImmutableList; |
| 21 | +import com.google.protobuf.ByteString; |
| 22 | +import com.google.protobuf.MessageOrBuilder; |
| 23 | +import com.google.storage.v2.WriteObjectRequest; |
| 24 | +import io.grpc.CallOptions; |
| 25 | +import io.grpc.Channel; |
| 26 | +import io.grpc.ClientCall; |
| 27 | +import io.grpc.ClientInterceptor; |
| 28 | +import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; |
| 29 | +import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; |
| 30 | +import io.grpc.Metadata; |
| 31 | +import io.grpc.MethodDescriptor; |
| 32 | +import io.grpc.Status; |
| 33 | +import java.util.List; |
| 34 | +import java.util.logging.Level; |
| 35 | +import java.util.logging.Logger; |
| 36 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 37 | + |
| 38 | +/** |
| 39 | + * Client side interceptor which will log gRPC request, response and status messages in plain text, |
| 40 | + * rather than the byte encoded text io.grpc.netty.shaded.io.grpc.netty.NettyClientHandler does. |
| 41 | + * |
| 42 | + * <p>This interceptor does not include the other useful information that NettyClientHandler |
| 43 | + * provides such as headers, method names, peers etc. |
| 44 | + */ |
| 45 | +public final class GrpcPlainRequestLoggingInterceptor implements ClientInterceptor { |
| 46 | + |
| 47 | + private static final Logger LOGGER = |
| 48 | + Logger.getLogger(GrpcPlainRequestLoggingInterceptor.class.getName()); |
| 49 | + |
| 50 | + private static final GrpcPlainRequestLoggingInterceptor INSTANCE = |
| 51 | + new GrpcPlainRequestLoggingInterceptor(); |
| 52 | + |
| 53 | + private GrpcPlainRequestLoggingInterceptor() {} |
| 54 | + |
| 55 | + public static GrpcPlainRequestLoggingInterceptor getInstance() { |
| 56 | + return INSTANCE; |
| 57 | + } |
| 58 | + |
| 59 | + public static GrpcInterceptorProvider getInterceptorProvider() { |
| 60 | + return InterceptorProvider.INSTANCE; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 65 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 66 | + ClientCall<ReqT, RespT> call = next.newCall(method, callOptions); |
| 67 | + return new SimpleForwardingClientCall<ReqT, RespT>(call) { |
| 68 | + @Override |
| 69 | + public void start(Listener<RespT> responseListener, Metadata headers) { |
| 70 | + SimpleForwardingClientCallListener<RespT> listener = |
| 71 | + new SimpleForwardingClientCallListener<RespT>(responseListener) { |
| 72 | + @Override |
| 73 | + public void onMessage(RespT message) { |
| 74 | + LOGGER.log( |
| 75 | + Level.CONFIG, |
| 76 | + () -> |
| 77 | + String.format( |
| 78 | + "<<< %s{%n%s}", message.getClass().getSimpleName(), fmtProto(message))); |
| 79 | + super.onMessage(message); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void onClose(Status status, Metadata trailers) { |
| 84 | + LOGGER.log( |
| 85 | + Level.CONFIG, |
| 86 | + () -> |
| 87 | + String.format( |
| 88 | + "<<< status = %s, trailers = %s", |
| 89 | + status.toString(), trailers.toString())); |
| 90 | + super.onClose(status, trailers); |
| 91 | + } |
| 92 | + }; |
| 93 | + super.start(listener, headers); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void sendMessage(ReqT message) { |
| 98 | + LOGGER.log( |
| 99 | + Level.CONFIG, |
| 100 | + () -> |
| 101 | + String.format( |
| 102 | + ">>> %s{%n%s}", message.getClass().getSimpleName(), fmtProto(message))); |
| 103 | + super.sendMessage(message); |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + @NonNull |
| 109 | + static String fmtProto(@NonNull Object obj) { |
| 110 | + if (obj instanceof WriteObjectRequest) { |
| 111 | + return fmtProto((WriteObjectRequest) obj); |
| 112 | + } else if (obj instanceof MessageOrBuilder) { |
| 113 | + return fmtProto((MessageOrBuilder) obj); |
| 114 | + } else { |
| 115 | + return obj.toString(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @NonNull |
| 120 | + static String fmtProto(@NonNull final MessageOrBuilder msg) { |
| 121 | + return msg.toString(); |
| 122 | + } |
| 123 | + |
| 124 | + @NonNull |
| 125 | + static String fmtProto(@NonNull WriteObjectRequest msg) { |
| 126 | + if (msg.hasChecksummedData()) { |
| 127 | + ByteString content = msg.getChecksummedData().getContent(); |
| 128 | + if (content.size() > 20) { |
| 129 | + WriteObjectRequest.Builder b = msg.toBuilder(); |
| 130 | + ByteString snip = ByteString.copyFromUtf8(String.format("<snip (%d)>", content.size())); |
| 131 | + ByteString trim = content.substring(0, 20).concat(snip); |
| 132 | + b.getChecksummedDataBuilder().setContent(trim); |
| 133 | + |
| 134 | + return b.build().toString(); |
| 135 | + } |
| 136 | + } |
| 137 | + return msg.toString(); |
| 138 | + } |
| 139 | + |
| 140 | + private static final class InterceptorProvider implements GrpcInterceptorProvider { |
| 141 | + private static final InterceptorProvider INSTANCE = new InterceptorProvider(); |
| 142 | + |
| 143 | + private final List<ClientInterceptor> interceptors; |
| 144 | + |
| 145 | + private InterceptorProvider() { |
| 146 | + this.interceptors = ImmutableList.of(GrpcPlainRequestLoggingInterceptor.INSTANCE); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public List<ClientInterceptor> getInterceptors() { |
| 151 | + return interceptors; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments