Skip to content

Commit e25db58

Browse files
committed
chore: Refactor code and add comments
1 parent b37ca37 commit e25db58

4 files changed

Lines changed: 44 additions & 22 deletions

File tree

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/DatastoreOpenTelemetryOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static class Builder {
128128
private Builder() {
129129
tracingEnabled = false;
130130
metricsEnabled = false;
131-
// TODO: This is disabled by default until the Firestore namespace is deployed
131+
// TODO(b/405457573): This is disabled by default until the Firestore namespace is deployed
132132
exportBuiltinMetricsToGoogleCloudMonitoring = false;
133133
openTelemetry = null;
134134
}
@@ -176,7 +176,6 @@ DatastoreOpenTelemetryOptions.Builder setMetricsEnabled(boolean enabled) {
176176
* @return this builder instance.
177177
*/
178178
@BetaApi
179-
@Nonnull
180179
public DatastoreOpenTelemetryOptions.Builder setExportBuiltinMetricsToGoogleCloudMonitoring(
181180
boolean exportBuiltinMetrics) {
182181
this.exportBuiltinMetricsToGoogleCloudMonitoring = exportBuiltinMetrics;

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/BuiltInDatastoreMetricsProvider.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.auth.Credentials;
2020
import com.google.cloud.NoCredentials;
21+
import com.google.common.base.Preconditions;
2122
import io.opentelemetry.api.OpenTelemetry;
2223
import io.opentelemetry.api.common.Attributes;
2324
import io.opentelemetry.api.common.AttributesBuilder;
@@ -47,16 +48,15 @@
4748
* automated environment detection and resource attribute configuration for the {@link
4849
* TelemetryConstants#DATASTORE_RESOURCE_TYPE} monitored resource.
4950
*/
50-
public final class BuiltInDatastoreMetricsProvider {
51+
class BuiltInDatastoreMetricsProvider {
5152

5253
public static final BuiltInDatastoreMetricsProvider INSTANCE =
5354
new BuiltInDatastoreMetricsProvider();
5455

5556
private static final Logger logger =
5657
Logger.getLogger(BuiltInDatastoreMetricsProvider.class.getName());
5758

58-
private static volatile String location;
59-
private static final String DEFAULT_LOCATION = "global";
59+
private String location;
6060

6161
// Pre-computed once per JVM; hostname lookup can block, so we pay the cost at class-init time.
6262
private static final String PID_AND_HOSTNAME = getProcessId() + "@" + getHostnameSafely();
@@ -92,6 +92,7 @@ static Map<String, String> buildClientAttributes() {
9292
@Nullable
9393
public OpenTelemetry createOpenTelemetry(
9494
@Nonnull String projectId, @Nonnull String databaseId, @Nonnull Credentials credentials) {
95+
Preconditions.checkNotNull(credentials, "Credentials cannot be null for built in metrics");
9596
SdkMeterProviderBuilder sdkMeterProviderBuilder = SdkMeterProvider.builder();
9697

9798
Map<String, String> clientAttributes = buildClientAttributes();
@@ -127,9 +128,9 @@ public OpenTelemetry createOpenTelemetry(
127128
*
128129
* @return the detected location, or "global" if detection fails.
129130
*/
130-
public static String detectClientLocation() {
131+
public String detectClientLocation() {
131132
if (location == null) {
132-
location = DEFAULT_LOCATION;
133+
location = "global";
133134
}
134135
return location;
135136
}

java-datastore/google-cloud-datastore/src/main/java/com/google/cloud/datastore/telemetry/OpenTelemetryDatastoreMetricsRecorder.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class OpenTelemetryDatastoreMetricsRecorder extends OpenTelemetryMetricsRecorder
5252
private final OpenTelemetry openTelemetry;
5353
// True when this recorder created the OpenTelemetry instance (built-in path) and therefore
5454
// owns its lifecycle. False when the instance was provided by the user.
55-
private final boolean ownsOpenTelemetry;
55+
private final boolean isBuiltInOpenTelemetryInstance;
5656

5757
// Datastore-specific transaction metrics (registered under the Datastore meter).
5858
private final DoubleHistogram transactionLatency;
@@ -66,14 +66,16 @@ class OpenTelemetryDatastoreMetricsRecorder extends OpenTelemetryMetricsRecorder
6666
/**
6767
* Creates a recorder, specifying whether this instance owns the {@link OpenTelemetry} lifecycle.
6868
*
69-
* @param ownsOpenTelemetry {@code true} if this recorder created the instance and should shut it
70-
* down on {@link #close()}; {@code false} if the user provided it.
69+
* @param isBuiltInOpenTelemetryInstance {@code true} if this recorder created the instance and
70+
* should shut it down on {@link #close()}; {@code false} if the user provided it.
7171
*/
7272
OpenTelemetryDatastoreMetricsRecorder(
73-
@Nonnull OpenTelemetry openTelemetry, String metricPrefix, boolean ownsOpenTelemetry) {
73+
@Nonnull OpenTelemetry openTelemetry,
74+
String metricPrefix,
75+
boolean isBuiltInOpenTelemetryInstance) {
7476
super(openTelemetry, metricPrefix);
7577
this.openTelemetry = openTelemetry;
76-
this.ownsOpenTelemetry = ownsOpenTelemetry;
78+
this.isBuiltInOpenTelemetryInstance = isBuiltInOpenTelemetryInstance;
7779

7880
// Note: Standard GAX RPC metrics (operation_latency, attempt_latency, etc.) are handled by the
7981
// base OpenTelemetryMetricsRecorder class. Those metrics are inherited from the parent classes.
@@ -108,7 +110,7 @@ OpenTelemetry getOpenTelemetry() {
108110
*/
109111
@Override
110112
public void close() {
111-
if (ownsOpenTelemetry && openTelemetry instanceof OpenTelemetrySdk) {
113+
if (isBuiltInOpenTelemetryInstance && openTelemetry instanceof OpenTelemetrySdk) {
112114
try {
113115
((OpenTelemetrySdk) openTelemetry).close();
114116
} catch (Exception e) {

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/telemetry/BuiltInDatastoreMetricsProviderTest.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818

1919
import static com.google.common.truth.Truth.assertThat;
2020

21+
import com.google.auth.CredentialTypeForMetrics;
22+
import com.google.auth.Credentials;
23+
import com.google.cloud.NoCredentials;
2124
import io.opentelemetry.api.OpenTelemetry;
2225
import java.util.Map;
26+
import org.easymock.EasyMock;
2327
import org.junit.Test;
2428
import org.junit.runner.RunWith;
2529
import org.junit.runners.JUnit4;
@@ -39,27 +43,43 @@ public void testBuildClientAttributes() {
3943
.isEqualTo(TelemetryConstants.SERVICE_VALUE);
4044
}
4145

46+
@Test
47+
public void testCreateOpenTelemetry_returnsNull() {
48+
OpenTelemetry otel =
49+
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(
50+
PROJECT_ID, "test-db", NoCredentials.getInstance());
51+
assertThat(otel).isNull();
52+
}
53+
4254
@Test
4355
public void testCreateOpenTelemetry_returnsNonNull() {
56+
Credentials credentials = EasyMock.mock(Credentials.class);
57+
EasyMock.expect(credentials.getMetricsCredentialType())
58+
.andReturn(CredentialTypeForMetrics.DO_NOT_SEND);
59+
EasyMock.replay(credentials);
4460
OpenTelemetry otel =
45-
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(PROJECT_ID, "test-db", null);
61+
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(
62+
PROJECT_ID, "test-db", credentials);
4663
assertThat(otel).isNotNull();
4764
}
4865

4966
@Test
5067
public void testCreateOpenTelemetry_eachCallReturnsDistinctInstance() {
68+
Credentials credentials1 = EasyMock.mock(Credentials.class);
69+
EasyMock.expect(credentials1.getMetricsCredentialType())
70+
.andReturn(CredentialTypeForMetrics.DO_NOT_SEND);
71+
Credentials credentials2 = EasyMock.mock(Credentials.class);
72+
EasyMock.expect(credentials2.getMetricsCredentialType())
73+
.andReturn(CredentialTypeForMetrics.DO_NOT_SEND);
74+
EasyMock.replay(credentials1, credentials2);
5175
OpenTelemetry otel1 =
52-
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(PROJECT_ID, "test-db", null);
76+
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(
77+
PROJECT_ID, "test-db", credentials1);
5378
OpenTelemetry otel2 =
54-
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(PROJECT_ID, "test-db", null);
79+
BuiltInDatastoreMetricsProvider.INSTANCE.createOpenTelemetry(
80+
PROJECT_ID, "test-db", credentials2);
5581
assertThat(otel1).isNotNull();
5682
assertThat(otel2).isNotNull();
5783
assertThat(otel1).isNotSameInstanceAs(otel2);
5884
}
59-
60-
@Test
61-
public void testDetectClientLocation() {
62-
String location = BuiltInDatastoreMetricsProvider.detectClientLocation();
63-
assertThat(location).isEqualTo("global");
64-
}
6585
}

0 commit comments

Comments
 (0)