Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ private static String hashClientUId(String uuid) {
*/
@Nullable
Comment thread
lqiu96 marked this conversation as resolved.
Outdated
public OpenTelemetry createOpenTelemetry(@Nonnull DatastoreOptions options) {
// If built-in metrics export is disabled, return no-op OpenTelemetry to avoid instantiating
// a real SdkMeterProvider. Otherwise, the SDK-internal PeriodicMetricReader will start
// and attempt to export diagnostic metrics, leading to log spam if the exporter fails.
if (!options.getOpenTelemetryOptions().isExportBuiltinMetricsToGoogleCloudMonitoring()) {
return OpenTelemetry.noop();
Comment thread
lqiu96 marked this conversation as resolved.
}
Credentials credentials =
Preconditions.checkNotNull(
options.getCredentials(), "Credentials cannot be null for built in metrics");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.auth.CredentialTypeForMetrics;
import com.google.auth.Credentials;
import com.google.cloud.NoCredentials;
import com.google.cloud.datastore.DatastoreOpenTelemetryOptions;
import com.google.cloud.datastore.DatastoreOptions;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.OpenTelemetrySdk;
Expand Down Expand Up @@ -69,6 +70,10 @@ public void testCreateOpenTelemetry_returnsNonNull() {
.setProjectId(PROJECT_ID)
.setDatabaseId("test-db")
.setCredentials(credentials)
.setOpenTelemetryOptions(
DatastoreOpenTelemetryOptions.newBuilder()
.setExportBuiltinMetricsToGoogleCloudMonitoring(true)
.build())
.build();

OpenTelemetry otel = BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(options);
Expand Down Expand Up @@ -96,13 +101,21 @@ public void testCreateOpenTelemetry_eachCallReturnsDistinctInstance() {
.setProjectId(PROJECT_ID)
.setDatabaseId("test-db")
.setCredentials(credentials1)
.setOpenTelemetryOptions(
DatastoreOpenTelemetryOptions.newBuilder()
.setExportBuiltinMetricsToGoogleCloudMonitoring(true)
.build())
.build();

DatastoreOptions options2 =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setDatabaseId("test-db")
.setCredentials(credentials2)
.setOpenTelemetryOptions(
DatastoreOpenTelemetryOptions.newBuilder()
.setExportBuiltinMetricsToGoogleCloudMonitoring(true)
.build())
.build();

OpenTelemetry otel1 = BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(options1);
Expand Down Expand Up @@ -137,4 +150,21 @@ public void testCreateOpenTelemetry_withEmulatorHostProperty_returnsNoOp() {
System.clearProperty(DatastoreOptions.LOCAL_HOST_ENV_VAR);
}
}

@Test
public void testCreateOpenTelemetry_exportDisabled_returnsNoOp() {
Credentials credentials = EasyMock.mock(Credentials.class);
EasyMock.replay(credentials);

DatastoreOptions options =
DatastoreOptions.newBuilder()
.setProjectId(PROJECT_ID)
.setDatabaseId("test-db")
.setCredentials(credentials)
// export is disabled by default
.build();

OpenTelemetry otel = BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(options);
assertThat(otel).isInstanceOf(OpenTelemetry.noop().getClass());
Comment thread
lqiu96 marked this conversation as resolved.
Outdated
}
}
Loading