Skip to content

Commit e359aa4

Browse files
committed
fix: Decrease log level for directpath warnings outside GCE
1 parent 8be3f9d commit e359aa4

3 files changed

Lines changed: 84 additions & 46 deletions

File tree

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -441,43 +441,47 @@ public boolean isDirectPathXdsEnabled() {
441441
// builder or createSingleChannel, only in getTransportChannel which creates the first channel
442442
// for a client.
443443
private void logDirectPathMisconfig() {
444-
if (isDirectPathXdsEnabled()) {
445-
if (!isDirectPathEnabled()) {
446-
// This misconfiguration occurs when Direct Path xDS is enabled, but Direct Path is not
447-
// Direct Path xDS can be enabled two ways: via environment variable or via builder.
448-
// Case 1: Direct Path is only enabled via xDS env var. We will _warn_ the user that this is
449-
// a misconfiguration if they intended to set the env var.
450-
if (isDirectPathXdsEnabledViaEnv()) {
451-
LOG.log(
452-
Level.WARNING,
453-
"Env var "
454-
+ DIRECT_PATH_ENV_ENABLE_XDS
455-
+ " was found and set to TRUE, but DirectPath was not enabled for this client. If this is intended for "
456-
+ "this client, please note that this is a misconfiguration and set the attemptDirectPath option as well.");
457-
}
458-
// Case 2: Direct Path xDS was enabled via Builder. Direct Path Traffic Director must be set
459-
// (enabled with `setAttemptDirectPath(true)`) along with xDS.
460-
// Here we warn the user about this.
461-
else if (isDirectPathXdsEnabledViaBuilderOption()) {
462-
LOG.log(
463-
Level.WARNING,
464-
"DirectPath is misconfigured. The DirectPath XDS option was set, but the attemptDirectPath option was not. Please set both the attemptDirectPath and attemptDirectPathXds options.");
465-
}
466-
} else {
467-
// Case 3: credential is not correctly set
468-
if (!isCredentialDirectPathCompatible()) {
469-
LOG.log(
470-
Level.WARNING,
471-
"DirectPath is misconfigured. Please make sure the credential is an instance of "
472-
+ ComputeEngineCredentials.class.getName()
473-
+ " .");
474-
}
475-
// Case 4: not running on GCE
476-
if (!isOnComputeEngine()) {
477-
LOG.log(
478-
Level.WARNING,
479-
"DirectPath is misconfigured. DirectPath is only available in a GCE environment.");
480-
}
444+
if (!isDirectPathXdsEnabled()) {
445+
return;
446+
}
447+
448+
Level level = isOnComputeEngine() ? Level.WARNING : Level.FINE;
449+
450+
if (!isDirectPathEnabled()) {
451+
// This misconfiguration occurs when Direct Path xDS is enabled, but Direct Path is not
452+
// Direct Path xDS can be enabled two ways: via environment variable or via builder.
453+
// Case 1: Direct Path is only enabled via xDS env var. We will _warn_ the user that this is
454+
// a misconfiguration if they intended to set the env var.
455+
if (isDirectPathXdsEnabledViaEnv()) {
456+
LOG.log(
457+
level,
458+
"Env var "
459+
+ DIRECT_PATH_ENV_ENABLE_XDS
460+
+ " was found and set to TRUE, but DirectPath was not enabled for this client. If this is intended for "
461+
+ "this client, please note that this is a misconfiguration and set the attemptDirectPath option as well.");
462+
}
463+
// Case 2: Direct Path xDS was enabled via Builder. Direct Path Traffic Director must be set
464+
// (enabled with `setAttemptDirectPath(true)`) along with xDS.
465+
// Here we warn the user about this.
466+
else if (isDirectPathXdsEnabledViaBuilderOption()) {
467+
LOG.log(
468+
level,
469+
"DirectPath is misconfigured. The DirectPath XDS option was set, but the attemptDirectPath option was not. Please set both the attemptDirectPath and attemptDirectPathXds options.");
470+
}
471+
} else {
472+
// Case 3: credential is not correctly set
473+
if (!isCredentialDirectPathCompatible()) {
474+
LOG.log(
475+
level,
476+
"DirectPath is misconfigured. Please make sure the credential is an instance of "
477+
+ ComputeEngineCredentials.class.getName()
478+
+ " .");
479+
}
480+
// Case 4: not running on GCE
481+
if (!isOnComputeEngine()) {
482+
LOG.log(
483+
level,
484+
"DirectPath is misconfigured. DirectPath is only available in a GCE environment.");
481485
}
482486
}
483487
}

gax-java/gax-grpc/src/test/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProviderTest.java

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
import java.util.concurrent.TimeUnit;
7878
import java.util.function.Function;
7979
import java.util.logging.Handler;
80+
import java.util.logging.Level;
8081
import java.util.logging.LogRecord;
8182
import java.util.stream.Collectors;
8283
import javax.annotation.Nullable;
@@ -85,6 +86,7 @@
8586
import org.junit.jupiter.api.BeforeEach;
8687
import org.junit.jupiter.api.Test;
8788
import org.mockito.ArgumentCaptor;
89+
import org.mockito.MockedStatic;
8890
import org.mockito.Mockito;
8991

9092
class InstantiatingGrpcChannelProviderTest extends AbstractMtlsTransportChannelTest {
@@ -651,7 +653,13 @@ private void createAndCloseTransportChannel(InstantiatingGrpcChannelProvider pro
651653
.setAttemptDirectPathXds()
652654
.setCertificateBasedAccess(certificateBasedAccess)
653655
.build();
654-
createAndCloseTransportChannel(provider);
656+
657+
try (MockedStatic<InstantiatingGrpcChannelProvider> mocked =
658+
Mockito.mockStatic(InstantiatingGrpcChannelProvider.class, Mockito.CALLS_REAL_METHODS)) {
659+
mocked.when(() -> InstantiatingGrpcChannelProvider.isOnComputeEngine()).thenReturn(true);
660+
createAndCloseTransportChannel(provider);
661+
}
662+
655663
assertThat(logHandler.getAllMessages())
656664
.contains(
657665
"DirectPath is misconfigured. The DirectPath XDS option was set, but the attemptDirectPath option was not. Please set both the attemptDirectPath and attemptDirectPathXds options.");
@@ -668,7 +676,13 @@ void testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSe
668676
createChannelProviderBuilderForDirectPathLogTests()
669677
.setCertificateBasedAccess(certificateBasedAccess)
670678
.build();
671-
createAndCloseTransportChannel(provider);
679+
680+
try (MockedStatic<InstantiatingGrpcChannelProvider> mocked =
681+
Mockito.mockStatic(InstantiatingGrpcChannelProvider.class, Mockito.CALLS_REAL_METHODS)) {
682+
mocked.when(() -> InstantiatingGrpcChannelProvider.isOnComputeEngine()).thenReturn(true);
683+
createAndCloseTransportChannel(provider);
684+
}
685+
672686
assertThat(logHandler.getAllMessages())
673687
.contains(
674688
"Env var GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS was found and set to TRUE, but DirectPath was not enabled for this client. If this is intended for "
@@ -679,12 +693,18 @@ void testLogDirectPathMisconfig_AttemptDirectPathNotSetAndAttemptDirectPathXdsSe
679693
@Test
680694
void testLogDirectPathMisconfig_shouldNotLogInTheBuilder() {
681695
FakeLogHandler logHandler = new FakeLogHandler();
696+
InstantiatingGrpcChannelProvider.LOG.setLevel(Level.FINE);
682697
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
683-
InstantiatingGrpcChannelProvider.newBuilder()
684-
.setAttemptDirectPathXds()
685-
.setAttemptDirectPath(true)
686-
.setCertificateBasedAccess(certificateBasedAccess)
687-
.build();
698+
699+
try (MockedStatic<InstantiatingGrpcChannelProvider> mocked =
700+
Mockito.mockStatic(InstantiatingGrpcChannelProvider.class, Mockito.CALLS_REAL_METHODS)) {
701+
mocked.when(() -> InstantiatingGrpcChannelProvider.isOnComputeEngine()).thenReturn(true);
702+
InstantiatingGrpcChannelProvider.newBuilder()
703+
.setAttemptDirectPathXds()
704+
.setAttemptDirectPath(true)
705+
.setCertificateBasedAccess(certificateBasedAccess)
706+
.build();
707+
}
688708

689709
assertThat(logHandler.getAllMessages()).isEmpty();
690710
InstantiatingGrpcChannelProvider.LOG.removeHandler(logHandler);
@@ -704,7 +724,13 @@ void testLogDirectPathMisconfigWrongCredential() throws Exception {
704724
.setCertificateBasedAccess(certificateBasedAccess)
705725
.build();
706726

707-
TransportChannel transportChannel = provider.getTransportChannel();
727+
TransportChannel transportChannel;
728+
729+
try (MockedStatic<InstantiatingGrpcChannelProvider> mocked =
730+
Mockito.mockStatic(InstantiatingGrpcChannelProvider.class, Mockito.CALLS_REAL_METHODS)) {
731+
mocked.when(() -> InstantiatingGrpcChannelProvider.isOnComputeEngine()).thenReturn(true);
732+
transportChannel = provider.getTransportChannel();
733+
}
708734

709735
assertThat(logHandler.getAllMessages())
710736
.contains(
@@ -719,6 +745,7 @@ void testLogDirectPathMisconfigWrongCredential() throws Exception {
719745
@Test
720746
void testLogDirectPathMisconfigNotOnGCE() throws Exception {
721747
FakeLogHandler logHandler = new FakeLogHandler();
748+
InstantiatingGrpcChannelProvider.LOG.setLevel(Level.FINE);
722749
InstantiatingGrpcChannelProvider.LOG.addHandler(logHandler);
723750
InstantiatingGrpcChannelProvider provider =
724751
InstantiatingGrpcChannelProvider.newBuilder()
@@ -731,7 +758,13 @@ void testLogDirectPathMisconfigNotOnGCE() throws Exception {
731758
.setCertificateBasedAccess(certificateBasedAccess)
732759
.build();
733760

734-
TransportChannel transportChannel = provider.getTransportChannel();
761+
TransportChannel transportChannel;
762+
763+
try (MockedStatic<InstantiatingGrpcChannelProvider> mocked =
764+
Mockito.mockStatic(InstantiatingGrpcChannelProvider.class, Mockito.CALLS_REAL_METHODS)) {
765+
mocked.when(() -> InstantiatingGrpcChannelProvider.isOnComputeEngine()).thenReturn(false);
766+
transportChannel = provider.getTransportChannel();
767+
}
735768

736769
if (!InstantiatingGrpcChannelProvider.isOnComputeEngine()) {
737770
assertThat(logHandler.getAllMessages())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mock-maker-inline

0 commit comments

Comments
 (0)