Skip to content

Commit

Permalink
chore: disable native metrics when there are no credentials
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
olavloite committed Dec 18, 2024
1 parent 56938e7 commit a7a061f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
Expand Down Expand Up @@ -1791,6 +1792,10 @@ public CallCredentialsProvider getCallCredentialsProvider() {
return callCredentialsProvider;
}

public boolean usesNoCredentials() {
return Objects.equals(getCredentials(), NoCredentials.getInstance());
}

public String getCompressorName() {
return compressorName;
}
Expand Down Expand Up @@ -1838,7 +1843,7 @@ private ApiTracerFactory createApiTracerFactory(

// Add Metrics Tracer factory if built in metrics are enabled and if the client is data client
// and if emulator is not enabled.
if (isEnableBuiltInMetrics() && !isAdminClient && !isEmulatorEnabled) {
if (isEnableBuiltInMetrics() && !isAdminClient && !isEmulatorEnabled && !usesNoCredentials()) {
ApiTracerFactory metricsTracerFactory = createMetricsApiTracerFactory();
if (metricsTracerFactory != null) {
apiTracerFactories.add(metricsTracerFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ public void testLeaderAwareRoutingEnablement() {

@Test
public void testEndToEndTracingEnablement() {
// Test that end to end tracing is disabled by default.
// Test that end-to-end tracing is disabled by default.
assertFalse(SpannerOptions.newBuilder().setProjectId("p").build().isEndToEndTracingEnabled());
assertTrue(
SpannerOptions.newBuilder()
Expand All @@ -755,7 +755,7 @@ public void testEndToEndTracingEnablement() {
}

@Test
public void testmonitoringHost() {
public void testMonitoringHost() {
String metricsEndpoint = "test-endpoint:443";
assertNull(SpannerOptions.newBuilder().setProjectId("p").build().getMonitoringHost());
assertThat(
Expand Down

0 comments on commit a7a061f

Please sign in to comment.