Skip to content

Commit adbe8b0

Browse files
committed
chore: Always return CompositeMetricsRecorder
1 parent 6d749ad commit adbe8b0

5 files changed

Lines changed: 40 additions & 94 deletions

File tree

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import com.google.cloud.RetryHelper;
2525
import com.google.cloud.RetryHelper.RetryHelperException;
2626
import com.google.cloud.datastore.spi.v1.DatastoreRpc;
27+
import com.google.cloud.datastore.telemetry.CompositeDatastoreMetricsRecorder;
2728
import com.google.cloud.datastore.telemetry.DatastoreMetricsRecorder;
28-
import com.google.cloud.datastore.telemetry.NoOpDatastoreMetricsRecorder;
2929
import com.google.cloud.datastore.telemetry.TelemetryConstants;
3030
import com.google.cloud.datastore.telemetry.TelemetryUtils;
3131
import com.google.cloud.datastore.telemetry.TraceUtil;
@@ -48,7 +48,9 @@
4848
import com.google.datastore.v1.RunAggregationQueryResponse;
4949
import com.google.datastore.v1.RunQueryRequest;
5050
import com.google.datastore.v1.RunQueryResponse;
51+
import java.util.ArrayList;
5152
import java.util.concurrent.Callable;
53+
import javax.annotation.Nonnull;
5254

5355
/**
5456
* An implementation of {@link DatastoreRpc} which acts as a Decorator and decorates the underlying
@@ -58,7 +60,7 @@
5860
public class RetryAndTraceDatastoreRpcDecorator implements DatastoreRpc {
5961

6062
private final DatastoreRpc datastoreRpc;
61-
private final com.google.cloud.datastore.telemetry.TraceUtil otelTraceUtil;
63+
private final TraceUtil otelTraceUtil;
6264
private final RetrySettings retrySettings;
6365
private final DatastoreOptions datastoreOptions;
6466
private final DatastoreMetricsRecorder metricsRecorder;
@@ -73,7 +75,7 @@ public RetryAndTraceDatastoreRpcDecorator(
7375
this.retrySettings = retrySettings;
7476
this.datastoreOptions = datastoreOptions;
7577
this.otelTraceUtil = otelTraceUtil;
76-
this.metricsRecorder = new NoOpDatastoreMetricsRecorder();
78+
this.metricsRecorder = new CompositeDatastoreMetricsRecorder(new ArrayList<>());
7779
}
7880

7981
private RetryAndTraceDatastoreRpcDecorator(Builder builder) {
@@ -94,10 +96,8 @@ public static class Builder {
9496
private RetrySettings retrySettings;
9597
private DatastoreOptions datastoreOptions;
9698

97-
// Defaults configured for this class
98-
private DatastoreMetricsRecorder metricsRecorder = new NoOpDatastoreMetricsRecorder();
99-
100-
private Builder() {}
99+
private DatastoreMetricsRecorder metricsRecorder =
100+
new CompositeDatastoreMetricsRecorder(new ArrayList<>());
101101

102102
public Builder setDatastoreRpc(DatastoreRpc datastoreRpc) {
103103
this.datastoreRpc = datastoreRpc;
@@ -119,8 +119,8 @@ public Builder setDatastoreOptions(DatastoreOptions datastoreOptions) {
119119
return this;
120120
}
121121

122-
public Builder setMetricsRecorder(DatastoreMetricsRecorder metricsRecorder) {
123-
Preconditions.checkNotNull(metricsRecorder, "metricsRecorder can not be null");
122+
@InternalApi
123+
public Builder setMetricsRecorder(@Nonnull DatastoreMetricsRecorder metricsRecorder) {
124124
this.metricsRecorder = metricsRecorder;
125125
return this;
126126
}
@@ -130,6 +130,7 @@ public RetryAndTraceDatastoreRpcDecorator build() {
130130
Preconditions.checkNotNull(otelTraceUtil, "otelTraceUtil is required");
131131
Preconditions.checkNotNull(retrySettings, "retrySettings is required");
132132
Preconditions.checkNotNull(datastoreOptions, "datastoreOptions is required");
133+
Preconditions.checkNotNull(metricsRecorder, "metricsRecorder is required");
133134
return new RetryAndTraceDatastoreRpcDecorator(this);
134135
}
135136
}
@@ -176,9 +177,8 @@ public RunAggregationQueryResponse runAggregationQuery(RunAggregationQueryReques
176177
boolean isTransactional = readOptions.hasTransaction() || readOptions.hasNewTransaction();
177178
String spanName =
178179
(isTransactional
179-
? com.google.cloud.datastore.telemetry.TraceUtil
180-
.SPAN_NAME_TRANSACTION_RUN_AGGREGATION_QUERY
181-
: com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RUN_AGGREGATION_QUERY);
180+
? TraceUtil.SPAN_NAME_TRANSACTION_RUN_AGGREGATION_QUERY
181+
: TraceUtil.SPAN_NAME_RUN_AGGREGATION_QUERY);
182182
return invokeRpc(
183183
() -> datastoreRpc.runAggregationQuery(request),
184184
spanName,

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.cloud.datastore.telemetry;
1818

19+
import com.google.api.core.InternalApi;
20+
import com.google.common.annotations.VisibleForTesting;
1921
import java.util.List;
2022
import java.util.Map;
2123
import java.util.logging.Level;
@@ -28,17 +30,23 @@
2830
* <p>This allows simultaneous recording to both built-in (Cloud Monitoring) and custom
2931
* (user-provided) OpenTelemetry backends.
3032
*/
31-
class CompositeDatastoreMetricsRecorder implements DatastoreMetricsRecorder {
33+
@InternalApi
34+
public class CompositeDatastoreMetricsRecorder implements DatastoreMetricsRecorder {
3235

3336
private static final Logger logger =
3437
Logger.getLogger(CompositeDatastoreMetricsRecorder.class.getName());
3538

3639
private final List<DatastoreMetricsRecorder> recorders;
3740

38-
CompositeDatastoreMetricsRecorder(List<DatastoreMetricsRecorder> recorders) {
41+
public CompositeDatastoreMetricsRecorder(List<DatastoreMetricsRecorder> recorders) {
3942
this.recorders = recorders;
4043
}
4144

45+
@VisibleForTesting
46+
List<DatastoreMetricsRecorder> getMetricRecorders() {
47+
return recorders;
48+
}
49+
4250
/** {@inheritDoc} */
4351
@Override
4452
public void recordTransactionLatency(double latencyMs, Map<String, String> attributes) {

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,6 @@ static DatastoreMetricsRecorder getInstance(@Nonnull DatastoreOptions datastoreO
125125
new OpenTelemetryDatastoreMetricsRecorder(customOtel, TelemetryConstants.METRIC_PREFIX));
126126
}
127127

128-
// Default metrics are disabled and user has no custom Otel instance
129-
if (recorders.isEmpty()) {
130-
return new NoOpDatastoreMetricsRecorder();
131-
}
132-
// CompositeMetricsRecorder is not needed for one recorder
133-
if (recorders.size() == 1) {
134-
return recorders.get(0);
135-
}
136128
return new CompositeDatastoreMetricsRecorder(recorders);
137129
}
138130
}

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ private DatastoreOptions.Builder baseOptions() {
4141
}
4242

4343
@Test
44-
public void defaultOptionsWithBuiltInMetricsDisabled_returnsNoOp() {
45-
// When both custom metrics and built-in metrics export are disabled, should return NoOp
44+
public void defaultOptionsWithBuiltInMetricsDisabled_returnsNoRecorders() {
45+
// When both custom metrics and built-in metrics export are disabled,
46+
// there should be no recorders
4647
DatastoreOptions options =
4748
baseOptions()
4849
.setOpenTelemetryOptions(
@@ -51,11 +52,14 @@ public void defaultOptionsWithBuiltInMetricsDisabled_returnsNoOp() {
5152
.build())
5253
.build();
5354
DatastoreMetricsRecorder recorder = DatastoreMetricsRecorder.getInstance(options);
54-
assertThat(recorder).isInstanceOf(NoOpDatastoreMetricsRecorder.class);
55+
assertThat(recorder).isInstanceOf(CompositeDatastoreMetricsRecorder.class);
56+
CompositeDatastoreMetricsRecorder compositeRecorder =
57+
(CompositeDatastoreMetricsRecorder) recorder;
58+
assertThat(compositeRecorder.getMetricRecorders().size()).isEqualTo(0);
5559
}
5660

5761
@Test
58-
public void tracingEnabledButMetricsDisabledAndBuiltInDisabled_returnsNoOp() {
62+
public void tracingEnabledButMetricsDisabledAndBuiltInDisabled_returnsNoRecorders() {
5963
// Enabling tracing alone should not enable metrics
6064
DatastoreOptions options =
6165
baseOptions()
@@ -66,11 +70,14 @@ public void tracingEnabledButMetricsDisabledAndBuiltInDisabled_returnsNoOp() {
6670
.build())
6771
.build();
6872
DatastoreMetricsRecorder recorder = DatastoreMetricsRecorder.getInstance(options);
69-
assertThat(recorder).isInstanceOf(NoOpDatastoreMetricsRecorder.class);
73+
assertThat(recorder).isInstanceOf(CompositeDatastoreMetricsRecorder.class);
74+
CompositeDatastoreMetricsRecorder compositeRecorder =
75+
(CompositeDatastoreMetricsRecorder) recorder;
76+
assertThat(compositeRecorder.getMetricRecorders().size()).isEqualTo(0);
7077
}
7178

7279
@Test
73-
public void defaultOptionsWithBuiltInMetricsEnabled_butNoCredentials_returnsNoOpRecorder() {
80+
public void defaultOptionsWithBuiltInMetricsEnabled_butNoCredentials_returnsNoRecorders() {
7481
// Explicitly enable built-in metrics export
7582
DatastoreOptions options =
7683
baseOptions() // Uses NoCredentials by default
@@ -81,9 +88,12 @@ public void defaultOptionsWithBuiltInMetricsEnabled_butNoCredentials_returnsNoOp
8188
.build();
8289
DatastoreMetricsRecorder recorder = DatastoreMetricsRecorder.getInstance(options);
8390

84-
// Since baseOptions() uses NoCredentials, it should return NoOpDatastoreMetricsRecorder
91+
// Since baseOptions() uses NoCredentials, it should not have any recorders
8592
// as we don't want to send metrics for local emulator logic.
86-
assertThat(recorder).isInstanceOf(NoOpDatastoreMetricsRecorder.class);
93+
assertThat(recorder).isInstanceOf(CompositeDatastoreMetricsRecorder.class);
94+
CompositeDatastoreMetricsRecorder compositeRecorder =
95+
(CompositeDatastoreMetricsRecorder) recorder;
96+
assertThat(compositeRecorder.getMetricRecorders().size()).isEqualTo(0);
8797
}
8898

8999
@Test

0 commit comments

Comments
 (0)