Skip to content

Commit 3b0a404

Browse files
committed
chore(Datastore): Enable default client-side metrics to be set to Cloud Monitoring
1 parent d537100 commit 3b0a404

11 files changed

Lines changed: 289 additions & 138 deletions

File tree

java-datastore/google-cloud-datastore/pom.xml

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@
140140
<groupId>io.opentelemetry.instrumentation</groupId>
141141
<artifactId>opentelemetry-grpc-1.6</artifactId>
142142
</dependency>
143+
<dependency>
144+
<groupId>io.opentelemetry</groupId>
145+
<artifactId>opentelemetry-sdk</artifactId>
146+
</dependency>
147+
<dependency>
148+
<groupId>io.opentelemetry</groupId>
149+
<artifactId>opentelemetry-sdk-common</artifactId>
150+
</dependency>
151+
<dependency>
152+
<groupId>io.opentelemetry</groupId>
153+
<artifactId>opentelemetry-sdk-metrics</artifactId>
154+
</dependency>
155+
<dependency>
156+
<groupId>com.google.cloud</groupId>
157+
<artifactId>google-cloud-monitoring</artifactId>
158+
<version>3.90.0-SNAPSHOT</version><!-- {x-version-update:google-cloud-monitoring:current} -->
159+
</dependency>
160+
<dependency>
161+
<groupId>com.google.api.grpc</groupId>
162+
<artifactId>proto-google-cloud-monitoring-v3</artifactId>
163+
<version>3.90.0-SNAPSHOT</version><!-- {x-version-update:google-cloud-monitoring:current} -->
164+
</dependency>
143165

144166
<dependency>
145167
<groupId>${project.groupId}</groupId>
@@ -193,16 +215,6 @@
193215
<version>1.17</version>
194216
<scope>test</scope>
195217
</dependency>
196-
<dependency>
197-
<groupId>io.opentelemetry</groupId>
198-
<artifactId>opentelemetry-sdk</artifactId>
199-
<scope>test</scope>
200-
</dependency>
201-
<dependency>
202-
<groupId>io.opentelemetry</groupId>
203-
<artifactId>opentelemetry-sdk-common</artifactId>
204-
<scope>test</scope>
205-
</dependency>
206218
<dependency>
207219
<groupId>io.opentelemetry</groupId>
208220
<artifactId>opentelemetry-sdk-testing</artifactId>
@@ -213,11 +225,6 @@
213225
<artifactId>opentelemetry-sdk-trace</artifactId>
214226
<scope>test</scope>
215227
</dependency>
216-
<dependency>
217-
<groupId>io.opentelemetry</groupId>
218-
<artifactId>opentelemetry-sdk-metrics</artifactId>
219-
<scope>test</scope>
220-
</dependency>
221228
<dependency>
222229
<groupId>io.opentelemetry</groupId>
223230
<artifactId>opentelemetry-semconv</artifactId>

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
import com.google.cloud.datastore.telemetry.TelemetryUtils;
5454
import com.google.cloud.datastore.telemetry.TraceUtil;
5555
import com.google.cloud.datastore.telemetry.TraceUtil.Scope;
56-
import com.google.cloud.http.HttpTransportOptions;
5756
import com.google.common.base.MoreObjects;
5857
import com.google.common.base.Preconditions;
5958
import com.google.common.base.Stopwatch;
@@ -102,15 +101,12 @@ final class DatastoreImpl extends BaseService<DatastoreOptions> implements Datas
102101
private final com.google.cloud.datastore.telemetry.TraceUtil otelTraceUtil =
103102
getOptions().getTraceUtil();
104103
private final MetricsRecorder metricsRecorder = getOptions().getMetricsRecorder();
105-
private final boolean isHttpTransport;
106-
107104
private final ReadOptionProtoPreparer readOptionProtoPreparer;
108105
private final AggregationQueryExecutor aggregationQueryExecutor;
109106

110107
DatastoreImpl(DatastoreOptions options) {
111108
super(options);
112109
this.datastoreRpc = options.getDatastoreRpcV1();
113-
this.isHttpTransport = options.getTransportOptions() instanceof HttpTransportOptions;
114110
retrySettings =
115111
MoreObjects.firstNonNull(options.getRetrySettings(), ServiceOptions.getNoRetrySettings());
116112

@@ -805,15 +801,12 @@ private <T> T runWithObservability(
805801
ResultRetryAlgorithm<?> exceptionHandler) {
806802
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
807803

808-
// Gax already records operation and attempt metrics. Since Datastore HttpJson does not
809-
// integrate with Gax, manually instrument these metrics when using HttpJson for parity
810-
Stopwatch operationStopwatch = isHttpTransport ? Stopwatch.createStarted() : null;
804+
Stopwatch operationStopwatch = Stopwatch.createStarted();
811805
String operationStatus = StatusCode.Code.OK.toString();
812806

813807
DatastoreOptions options = getOptions();
814808
Callable<T> attemptCallable =
815-
TelemetryUtils.attemptMetricsCallable(
816-
callable, metricsRecorder, options, isHttpTransport, methodName);
809+
TelemetryUtils.attemptMetricsCallable(callable, metricsRecorder, options, methodName);
817810
try (TraceUtil.Scope ignored = span.makeCurrent()) {
818811
return RetryHelper.runWithRetries(
819812
attemptCallable, retrySettings, exceptionHandler, options.getClock());
@@ -823,12 +816,7 @@ private <T> T runWithObservability(
823816
throw DatastoreException.translateAndThrow(e);
824817
} finally {
825818
TelemetryUtils.recordOperationMetrics(
826-
metricsRecorder,
827-
options,
828-
isHttpTransport,
829-
operationStopwatch,
830-
methodName,
831-
operationStatus);
819+
metricsRecorder, options, operationStopwatch, methodName, operationStatus);
832820
span.end();
833821
}
834822
}

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

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@
2020
import javax.annotation.Nonnull;
2121
import javax.annotation.Nullable;
2222

23+
/**
24+
* Represents the options that are used to configure the use of OpenTelemetry for telemetry
25+
* collection in the Datastore SDK.
26+
*/
2327
public class DatastoreOpenTelemetryOptions {
2428
private final boolean tracingEnabled;
2529
private final boolean metricsEnabled;
30+
private final boolean exportBuiltinMetricsToGoogleCloudMonitoring;
2631
private final @Nullable OpenTelemetry openTelemetry;
2732

2833
DatastoreOpenTelemetryOptions(Builder builder) {
2934
this.tracingEnabled = builder.tracingEnabled;
3035
this.metricsEnabled = builder.metricsEnabled;
36+
this.exportBuiltinMetricsToGoogleCloudMonitoring =
37+
builder.exportBuiltinMetricsToGoogleCloudMonitoring;
3138
this.openTelemetry = builder.openTelemetry;
3239
}
3340

@@ -50,24 +57,53 @@ public boolean isTracingEnabled() {
5057
}
5158

5259
/**
53-
* Returns whether metrics are enabled.
60+
* Returns whether metrics are enabled for the custom (user-provided) OpenTelemetry backend.
5461
*
5562
* @return {@code true} if metrics are enabled, {@code false} otherwise.
5663
*/
5764
public boolean isMetricsEnabled() {
5865
return metricsEnabled;
5966
}
6067

68+
/**
69+
* Returns whether built-in metrics should be exported to Google Cloud Monitoring.
70+
*
71+
* <p>When enabled, client-side metrics are automatically exported to Google Cloud Monitoring using
72+
* the Cloud Monitoring API. This is independent of the custom OpenTelemetry backend configured via
73+
* {@link #getOpenTelemetry()}.
74+
*
75+
* @return {@code true} if built-in metrics export to Cloud Monitoring is enabled, {@code false}
76+
* otherwise.
77+
*/
78+
public boolean isExportBuiltinMetricsToGoogleCloudMonitoring() {
79+
return exportBuiltinMetricsToGoogleCloudMonitoring;
80+
}
81+
82+
/**
83+
* Returns the custom {@link OpenTelemetry} instance, if one was provided.
84+
*
85+
* @return the custom {@link OpenTelemetry} instance, or {@code null} if none was provided.
86+
*/
6187
@Nullable
6288
public OpenTelemetry getOpenTelemetry() {
6389
return openTelemetry;
6490
}
6591

92+
/**
93+
* Returns a new {@link Builder} initialized with the values from this options instance.
94+
*
95+
* @return a new {@link Builder}.
96+
*/
6697
@Nonnull
6798
public DatastoreOpenTelemetryOptions.Builder toBuilder() {
6899
return new DatastoreOpenTelemetryOptions.Builder(this);
69100
}
70101

102+
/**
103+
* Returns a new default {@link Builder}.
104+
*
105+
* @return a new {@link Builder}.
106+
*/
71107
@Nonnull
72108
public static DatastoreOpenTelemetryOptions.Builder newBuilder() {
73109
return new DatastoreOpenTelemetryOptions.Builder();
@@ -77,25 +113,30 @@ public static class Builder {
77113

78114
private boolean tracingEnabled;
79115
private boolean metricsEnabled;
116+
private boolean exportBuiltinMetricsToGoogleCloudMonitoring;
80117

81118
@Nullable private OpenTelemetry openTelemetry;
82119

83120
private Builder() {
84121
tracingEnabled = false;
85122
metricsEnabled = false;
123+
exportBuiltinMetricsToGoogleCloudMonitoring = true;
86124
openTelemetry = null;
87125
}
88126

89127
private Builder(DatastoreOpenTelemetryOptions options) {
90128
this.tracingEnabled = options.tracingEnabled;
91129
this.metricsEnabled = options.metricsEnabled;
130+
this.exportBuiltinMetricsToGoogleCloudMonitoring =
131+
options.exportBuiltinMetricsToGoogleCloudMonitoring;
92132
this.openTelemetry = options.openTelemetry;
93133
}
94134

95135
/**
96136
* Sets whether tracing should be enabled.
97137
*
98138
* @param enabled Whether tracing should be enabled.
139+
* @return this builder instance.
99140
*/
100141
@Nonnull
101142
public DatastoreOpenTelemetryOptions.Builder setTracingEnabled(boolean enabled) {
@@ -104,23 +145,42 @@ public DatastoreOpenTelemetryOptions.Builder setTracingEnabled(boolean enabled)
104145
}
105146

106147
/**
107-
* Sets whether metrics should be enabled.
148+
* Sets whether metrics should be enabled for the custom (user-provided) OpenTelemetry backend.
108149
*
109150
* @param enabled Whether metrics should be enabled.
110-
* @return the builder instance.
151+
* @return this builder instance.
111152
*/
112153
@Nonnull
113154
DatastoreOpenTelemetryOptions.Builder setMetricsEnabled(boolean enabled) {
114155
this.metricsEnabled = enabled;
115156
return this;
116157
}
117158

159+
/**
160+
* Sets whether built-in metrics should be exported to Google Cloud Monitoring.
161+
*
162+
* <p>When enabled (the default), client-side metrics are automatically exported to Google Cloud
163+
* Monitoring using the Cloud Monitoring API. This can be disabled to prevent metrics from being
164+
* sent to Cloud Monitoring while still allowing metrics to flow to a custom OpenTelemetry
165+
* backend.
166+
*
167+
* @param exportBuiltinMetrics Whether built-in metrics should be exported to Cloud Monitoring.
168+
* @return this builder instance.
169+
*/
170+
@Nonnull
171+
public DatastoreOpenTelemetryOptions.Builder setExportBuiltinMetricsToGoogleCloudMonitoring(
172+
boolean exportBuiltinMetrics) {
173+
this.exportBuiltinMetricsToGoogleCloudMonitoring = exportBuiltinMetrics;
174+
return this;
175+
}
176+
118177
/**
119178
* Sets the {@link OpenTelemetry} to use with this Datastore instance. If telemetry collection
120-
* is enabled, but an `OpenTelemetry` is not provided, the Datastore SDK will attempt to use the
121-
* `GlobalOpenTelemetry`.
179+
* is enabled, but an {@code OpenTelemetry} is not provided, the Datastore SDK will attempt to
180+
* use the {@code GlobalOpenTelemetry}.
122181
*
123182
* @param openTelemetry The OpenTelemetry that should be used by this Datastore instance.
183+
* @return this builder instance.
124184
*/
125185
@Nonnull
126186
public DatastoreOpenTelemetryOptions.Builder setOpenTelemetry(
@@ -129,6 +189,11 @@ public DatastoreOpenTelemetryOptions.Builder setOpenTelemetry(
129189
return this;
130190
}
131191

192+
/**
193+
* Builds a new {@link DatastoreOpenTelemetryOptions} instance from this builder.
194+
*
195+
* @return a new {@link DatastoreOpenTelemetryOptions}.
196+
*/
132197
@Nonnull
133198
public DatastoreOpenTelemetryOptions build() {
134199
return new DatastoreOpenTelemetryOptions(this);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private DatastoreOptions(Builder builder) {
223223
? builder.openTelemetryOptions
224224
: DatastoreOpenTelemetryOptions.newBuilder().build();
225225
this.traceUtil = com.google.cloud.datastore.telemetry.TraceUtil.getInstance(this);
226-
this.metricsRecorder = MetricsRecorder.getInstance(openTelemetryOptions);
226+
this.metricsRecorder = MetricsRecorder.getInstance(this);
227227

228228
namespace = MoreObjects.firstNonNull(builder.namespace, defaultNamespace());
229229
databaseId = MoreObjects.firstNonNull(builder.databaseId, DEFAULT_DATABASE_ID);

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.google.cloud.datastore.telemetry.TelemetryConstants;
3030
import com.google.cloud.datastore.telemetry.TelemetryUtils;
3131
import com.google.cloud.datastore.telemetry.TraceUtil;
32-
import com.google.cloud.http.HttpTransportOptions;
3332
import com.google.common.base.Preconditions;
3433
import com.google.common.base.Stopwatch;
3534
import com.google.datastore.v1.AllocateIdsRequest;
@@ -63,7 +62,6 @@ public class RetryAndTraceDatastoreRpcDecorator implements DatastoreRpc {
6362
private final RetrySettings retrySettings;
6463
private final DatastoreOptions datastoreOptions;
6564
private final MetricsRecorder metricsRecorder;
66-
private final boolean isHttpTransport;
6765

6866
@ObsoleteApi("Prefer to create RetryAndTraceDatastoreRpcDecorator via the Builder")
6967
public RetryAndTraceDatastoreRpcDecorator(
@@ -76,7 +74,6 @@ public RetryAndTraceDatastoreRpcDecorator(
7674
this.datastoreOptions = datastoreOptions;
7775
this.otelTraceUtil = otelTraceUtil;
7876
this.metricsRecorder = new NoOpMetricsRecorder();
79-
this.isHttpTransport = datastoreOptions.getTransportOptions() instanceof HttpTransportOptions;
8077
}
8178

8279
private RetryAndTraceDatastoreRpcDecorator(Builder builder) {
@@ -85,7 +82,6 @@ private RetryAndTraceDatastoreRpcDecorator(Builder builder) {
8582
this.retrySettings = builder.retrySettings;
8683
this.datastoreOptions = builder.datastoreOptions;
8784
this.metricsRecorder = builder.metricsRecorder;
88-
this.isHttpTransport = builder.isHttpTransport;
8985
}
9086

9187
public static Builder newBuilder() {
@@ -100,7 +96,6 @@ public static class Builder {
10096

10197
// Defaults configured for this class
10298
private MetricsRecorder metricsRecorder = new NoOpMetricsRecorder();
103-
private boolean isHttpTransport = false;
10499

105100
private Builder() {}
106101

@@ -135,7 +130,6 @@ public RetryAndTraceDatastoreRpcDecorator build() {
135130
Preconditions.checkNotNull(otelTraceUtil, "otelTraceUtil is required");
136131
Preconditions.checkNotNull(retrySettings, "retrySettings is required");
137132
Preconditions.checkNotNull(datastoreOptions, "datastoreOptions is required");
138-
this.isHttpTransport = datastoreOptions.getTransportOptions() instanceof HttpTransportOptions;
139133
return new RetryAndTraceDatastoreRpcDecorator(this);
140134
}
141135
}
@@ -207,12 +201,12 @@ public <O> O invokeRpc(Callable<O> block, String startSpan) {
207201

208202
<O> O invokeRpc(Callable<O> block, String startSpan, String methodName) {
209203
TraceUtil.Span span = otelTraceUtil.startSpan(startSpan);
210-
Stopwatch stopwatch = isHttpTransport ? Stopwatch.createStarted() : null;
204+
Stopwatch stopwatch = Stopwatch.createStarted();
211205
String operationStatus = StatusCode.Code.UNKNOWN.toString();
212206
try (TraceUtil.Scope ignored = span.makeCurrent()) {
213207
Callable<O> callable =
214208
TelemetryUtils.attemptMetricsCallable(
215-
block, metricsRecorder, datastoreOptions, isHttpTransport, methodName);
209+
block, metricsRecorder, datastoreOptions, methodName);
216210
O result =
217211
RetryHelper.runWithRetries(
218212
callable, this.retrySettings, EXCEPTION_HANDLER, this.datastoreOptions.getClock());
@@ -224,12 +218,7 @@ <O> O invokeRpc(Callable<O> block, String startSpan, String methodName) {
224218
throw DatastoreException.translateAndThrow(e);
225219
} finally {
226220
TelemetryUtils.recordOperationMetrics(
227-
metricsRecorder,
228-
datastoreOptions,
229-
isHttpTransport,
230-
stopwatch,
231-
methodName,
232-
operationStatus);
221+
metricsRecorder, datastoreOptions, stopwatch, methodName, operationStatus);
233222
span.end();
234223
}
235224
}

0 commit comments

Comments
 (0)