Skip to content

Commit dfc154e

Browse files
authored
chore: disable native metrics when there are no credentials (#3567)
* chore: disable native metrics when there are no credentials Native metrics were automatically disabled when the emulator is used, but some clients (e.g. PGAdapter) set up the connection to the emulator manually instead of setting the environment variable. Also, when using in-mem mock servers, native metrics should be disabled, as they cannot be exported. This PR therefore adds an additional check that disables native metrics when the client uses a NoCredentials instance. * chore: make method private
1 parent b9ce1a6 commit dfc154e

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/SpannerOptions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import java.util.List;
8484
import java.util.Map;
8585
import java.util.Map.Entry;
86+
import java.util.Objects;
8687
import java.util.Set;
8788
import java.util.concurrent.ScheduledExecutorService;
8889
import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -1791,6 +1792,10 @@ public CallCredentialsProvider getCallCredentialsProvider() {
17911792
return callCredentialsProvider;
17921793
}
17931794

1795+
private boolean usesNoCredentials() {
1796+
return Objects.equals(getCredentials(), NoCredentials.getInstance());
1797+
}
1798+
17941799
public String getCompressorName() {
17951800
return compressorName;
17961801
}
@@ -1838,7 +1843,7 @@ private ApiTracerFactory createApiTracerFactory(
18381843

18391844
// Add Metrics Tracer factory if built in metrics are enabled and if the client is data client
18401845
// and if emulator is not enabled.
1841-
if (isEnableBuiltInMetrics() && !isAdminClient && !isEmulatorEnabled) {
1846+
if (isEnableBuiltInMetrics() && !isAdminClient && !isEmulatorEnabled && !usesNoCredentials()) {
18421847
ApiTracerFactory metricsTracerFactory = createMetricsApiTracerFactory();
18431848
if (metricsTracerFactory != null) {
18441849
apiTracerFactories.add(metricsTracerFactory);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpannerOptionsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ public void testLeaderAwareRoutingEnablement() {
738738

739739
@Test
740740
public void testEndToEndTracingEnablement() {
741-
// Test that end to end tracing is disabled by default.
741+
// Test that end-to-end tracing is disabled by default.
742742
assertFalse(SpannerOptions.newBuilder().setProjectId("p").build().isEndToEndTracingEnabled());
743743
assertTrue(
744744
SpannerOptions.newBuilder()
@@ -755,7 +755,7 @@ public void testEndToEndTracingEnablement() {
755755
}
756756

757757
@Test
758-
public void testmonitoringHost() {
758+
public void testMonitoringHost() {
759759
String metricsEndpoint = "test-endpoint:443";
760760
assertNull(SpannerOptions.newBuilder().setProjectId("p").build().getMonitoringHost());
761761
assertThat(

0 commit comments

Comments
 (0)