|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are |
| 6 | + * met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following disclaimer |
| 12 | + * in the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Google LLC nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +package com.google.api.gax.grpc; |
| 32 | + |
| 33 | +import static com.google.api.gax.logging.LoggingUtils.executeWithTryCatch; |
| 34 | +import static com.google.api.gax.logging.LoggingUtils.logRequest; |
| 35 | +import static com.google.api.gax.logging.LoggingUtils.logResponse; |
| 36 | +import static com.google.api.gax.logging.LoggingUtils.recordResponseHeaders; |
| 37 | +import static com.google.api.gax.logging.LoggingUtils.recordResponsePayload; |
| 38 | +import static com.google.api.gax.logging.LoggingUtils.recordServiceRpcAndRequestHeaders; |
| 39 | + |
| 40 | +import com.google.api.core.InternalApi; |
| 41 | +import com.google.api.gax.logging.LogData; |
| 42 | +import com.google.api.gax.logging.LoggerProvider; |
| 43 | +import io.grpc.CallOptions; |
| 44 | +import io.grpc.Channel; |
| 45 | +import io.grpc.ClientCall; |
| 46 | +import io.grpc.ClientInterceptor; |
| 47 | +import io.grpc.ForwardingClientCall; |
| 48 | +import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; |
| 49 | +import io.grpc.Metadata; |
| 50 | +import io.grpc.MethodDescriptor; |
| 51 | +import io.grpc.Status; |
| 52 | +import java.util.HashMap; |
| 53 | +import java.util.Map; |
| 54 | + |
| 55 | +@InternalApi |
| 56 | +public class GrpcLoggingInterceptor implements ClientInterceptor { |
| 57 | + |
| 58 | + private static final LoggerProvider LOGGER_PROVIDER = |
| 59 | + LoggerProvider.forClazz(GrpcLoggingInterceptor.class); |
| 60 | + |
| 61 | + ClientCall.Listener<?> currentListener; // expose for test setup |
| 62 | + |
| 63 | + @Override |
| 64 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 65 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 66 | + |
| 67 | + return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>( |
| 68 | + next.newCall(method, callOptions)) { |
| 69 | + LogData.Builder logDataBuilder = LogData.builder(); |
| 70 | + |
| 71 | + @Override |
| 72 | + public void start(Listener<RespT> responseListener, Metadata headers) { |
| 73 | + recordServiceRpcAndRequestHeaders( |
| 74 | + method.getServiceName(), |
| 75 | + method.getFullMethodName(), |
| 76 | + null, // endpoint is for http request only |
| 77 | + metadataHeadersToMap(headers), |
| 78 | + logDataBuilder, |
| 79 | + LOGGER_PROVIDER); |
| 80 | + SimpleForwardingClientCallListener<RespT> responseLoggingListener = |
| 81 | + new SimpleForwardingClientCallListener<RespT>(responseListener) { |
| 82 | + @Override |
| 83 | + public void onHeaders(Metadata headers) { |
| 84 | + recordResponseHeaders( |
| 85 | + metadataHeadersToMap(headers), logDataBuilder, LOGGER_PROVIDER); |
| 86 | + super.onHeaders(headers); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void onMessage(RespT message) { |
| 91 | + recordResponsePayload(message, logDataBuilder, LOGGER_PROVIDER); |
| 92 | + super.onMessage(message); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void onClose(Status status, Metadata trailers) { |
| 97 | + logResponse(status.getCode().toString(), logDataBuilder, LOGGER_PROVIDER); |
| 98 | + super.onClose(status, trailers); |
| 99 | + } |
| 100 | + }; |
| 101 | + currentListener = responseLoggingListener; |
| 102 | + super.start(responseLoggingListener, headers); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void sendMessage(ReqT message) { |
| 107 | + logRequest(message, logDataBuilder, LOGGER_PROVIDER); |
| 108 | + super.sendMessage(message); |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + // Helper methods for logging |
| 114 | + private static Map<String, String> metadataHeadersToMap(Metadata headers) { |
| 115 | + |
| 116 | + Map<String, String> headersMap = new HashMap<>(); |
| 117 | + executeWithTryCatch( |
| 118 | + () -> { |
| 119 | + for (String key : headers.keys()) { |
| 120 | + // grpc header values can be either ASCII strings or binary |
| 121 | + // https://grpc.io/docs/guides/metadata/#overview |
| 122 | + // this condition identified binary headers and skip for logging |
| 123 | + if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) { |
| 124 | + continue; |
| 125 | + } |
| 126 | + Metadata.Key<String> metadataKey = |
| 127 | + Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER); |
| 128 | + String headerValue = headers.get(metadataKey); |
| 129 | + |
| 130 | + headersMap.put(key, headerValue); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + return headersMap; |
| 135 | + } |
| 136 | +} |
0 commit comments