Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gapic-libraries-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.google.cloud</groupId>
<artifactId>gapic-libraries-bom</artifactId>
<packaging>pom</packaging>
<version>1.85.1</version><!-- {x-version-update:google-cloud-java:current} -->
<version>1.85.0</version><!-- {x-version-update:google-cloud-java:current} -->
<name>Google Cloud Java BOM</name>
<description>
BOM for the libraries in google-cloud-java repository. Users should not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
package com.google.api.gax.grpc;

import static com.google.api.gax.logging.LoggingUtils.executeWithTryCatch;
import static com.google.api.gax.logging.LoggingUtils.isLoggingEnabled;
import static com.google.api.gax.logging.LoggingUtils.logRequest;
import static com.google.api.gax.logging.LoggingUtils.logResponse;
import static com.google.api.gax.logging.LoggingUtils.recordResponseHeaders;
Expand Down Expand Up @@ -70,19 +71,23 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(

@Override
public void start(Listener<RespT> responseListener, Metadata headers) {
recordServiceRpcAndRequestHeaders(
method.getServiceName(),
method.getFullMethodName(),
null, // endpoint is for http request only
metadataHeadersToMap(headers),
logDataBuilder,
LOGGER_PROVIDER);
if (isLoggingEnabled()) {
recordServiceRpcAndRequestHeaders(
method.getServiceName(),
method.getFullMethodName(),
null, // endpoint is for http request only
metadataHeadersToMap(headers),
logDataBuilder,
LOGGER_PROVIDER);
}
SimpleForwardingClientCallListener<RespT> responseLoggingListener =
new SimpleForwardingClientCallListener<RespT>(responseListener) {
@Override
public void onHeaders(Metadata headers) {
recordResponseHeaders(
metadataHeadersToMap(headers), logDataBuilder, LOGGER_PROVIDER);
if (isLoggingEnabled()) {
recordResponseHeaders(
metadataHeadersToMap(headers), logDataBuilder, LOGGER_PROVIDER);
}
super.onHeaders(headers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

package com.google.api.gax.grpc;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
Expand All @@ -44,6 +45,8 @@
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import java.lang.reflect.Method;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -57,6 +60,17 @@ class GrpcLoggingInterceptorTest {
@Mock private ClientCall<String, Integer> call;

private static final MethodDescriptor<String, Integer> method = FakeMethodDescriptor.create();
private boolean originalLoggingEnabled;

@org.junit.jupiter.api.BeforeEach
void setUpLoggingState() throws Exception {
originalLoggingEnabled = isLoggingEnabled();
}

@AfterEach
void tearDown() throws Exception {
setLoggingEnabled(originalLoggingEnabled);
}

@Test
void testInterceptor_basic() {
Expand Down Expand Up @@ -101,4 +115,43 @@ void testInterceptor_responseListener() {
Status status = Status.OK;
interceptor.currentListener.onClose(status, new Metadata());
}

@Test
void testInterceptor_skipsMetadataMaterializationWhenLoggingDisabled() throws Exception {
setLoggingEnabled(false);
when(channel.newCall(Mockito.<MethodDescriptor<String, Integer>>any(), any(CallOptions.class)))
.thenReturn(call);

GrpcLoggingInterceptor interceptor = new GrpcLoggingInterceptor();
Channel intercepted = ClientInterceptors.intercept(channel, interceptor);

@SuppressWarnings("unchecked")
ClientCall.Listener<Integer> listener = mock(ClientCall.Listener.class);

Metadata requestHeaders = mock(Metadata.class);
when(requestHeaders.keys()).thenThrow(new AssertionError("request headers should not be read"));
ClientCall<String, Integer> interceptedCall = intercepted.newCall(method, CallOptions.DEFAULT);

assertDoesNotThrow(() -> interceptedCall.start(listener, requestHeaders));

Metadata responseHeaders = mock(Metadata.class);
when(responseHeaders.keys())
.thenThrow(new AssertionError("response headers should not be read"));

assertDoesNotThrow(() -> interceptor.currentListener.onHeaders(responseHeaders));
}

private static void setLoggingEnabled(boolean enabled) throws Exception {
Class<?> loggingUtils = Class.forName("com.google.api.gax.logging.LoggingUtils");
Method method = loggingUtils.getDeclaredMethod("setLoggingEnabled", boolean.class);
method.setAccessible(true);
method.invoke(null, enabled);
}

private static boolean isLoggingEnabled() throws Exception {
Class<?> loggingUtils = Class.forName("com.google.api.gax.logging.LoggingUtils");
Method method = loggingUtils.getDeclaredMethod("isLoggingEnabled");
method.setAccessible(true);
return (boolean) method.invoke(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
class Slf4jUtils {

private static final Logger NO_OP_LOGGER = org.slf4j.helpers.NOPLogger.NOP_LOGGER;
private static final boolean loggingEnabled = LoggingUtils.isLoggingEnabled();

private static final boolean isSLF4J2x;

static {
Expand All @@ -70,7 +68,7 @@ static Logger getLogger(Class<?> clazz) {

// constructor with LoggerFactoryProvider to make testing easier
static Logger getLogger(Class<?> clazz, LoggerFactoryProvider factoryProvider) {
if (loggingEnabled) {
if (LoggingUtils.isLoggingEnabled()) {
ILoggerFactory loggerFactory = factoryProvider.getLoggerFactory();
return loggerFactory.getLogger(clazz.getName());
} else {
Expand Down
Loading