Skip to content

Commit 8fbc1c6

Browse files
committed
feat(datastore): add database_id to metric attributes and fix IT tests
1 parent b9f269f commit 8fbc1c6

4 files changed

Lines changed: 19 additions & 13 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public T call() throws DatastoreException {
254254
private void recordAttempt(String status) {
255255
Map<String, String> attributes =
256256
TelemetryUtils.buildMetricAttributes(
257-
TelemetryConstants.METHOD_TRANSACTION_COMMIT, status);
257+
TelemetryConstants.METHOD_TRANSACTION_COMMIT, status, datastore.getOptions().getDatabaseId());
258258
metricsRecorder.recordTransactionAttemptCount(1, attributes);
259259
}
260260
}
@@ -292,7 +292,7 @@ public <T> T runInTransaction(
292292
} finally {
293293
long latencyMs = stopwatch.elapsed(TimeUnit.MILLISECONDS);
294294
Map<String, String> attributes =
295-
TelemetryUtils.buildMetricAttributes(TelemetryConstants.METHOD_TRANSACTION_RUN, status);
295+
TelemetryUtils.buildMetricAttributes(TelemetryConstants.METHOD_TRANSACTION_RUN, status, getOptions().getDatabaseId());
296296
metricsRecorder.recordTransactionLatency(latencyMs, attributes);
297297
span.end();
298298
}
@@ -801,7 +801,7 @@ private <T> T runWithObservability(
801801

802802
DatastoreOptions options = getOptions();
803803
Callable<T> attemptCallable =
804-
TelemetryUtils.attemptMetricsCallable(callable, metricsRecorder, methodName);
804+
TelemetryUtils.attemptMetricsCallable(callable, metricsRecorder, methodName, options.getDatabaseId());
805805
try (TraceUtil.Scope ignored = span.makeCurrent()) {
806806
return RetryHelper.runWithRetries(
807807
attemptCallable, retrySettings, exceptionHandler, options.getClock());
@@ -811,7 +811,7 @@ private <T> T runWithObservability(
811811
throw DatastoreException.translateAndThrow(e);
812812
} finally {
813813
TelemetryUtils.recordOperationMetrics(
814-
metricsRecorder, operationStopwatch, methodName, operationStatus);
814+
metricsRecorder, operationStopwatch, methodName, operationStatus, options.getDatabaseId());
815815
span.end();
816816
}
817817
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ <O> O invokeRpc(Callable<O> block, String startSpan, String methodName) {
205205
String operationStatus = StatusCode.Code.UNKNOWN.toString();
206206
try (TraceUtil.Scope ignored = span.makeCurrent()) {
207207
Callable<O> callable =
208-
TelemetryUtils.attemptMetricsCallable(block, metricsRecorder, methodName);
208+
TelemetryUtils.attemptMetricsCallable(block, metricsRecorder, methodName, datastoreOptions.getDatabaseId());
209209
O result =
210210
RetryHelper.runWithRetries(
211211
callable, this.retrySettings, EXCEPTION_HANDLER, this.datastoreOptions.getClock());
@@ -217,7 +217,7 @@ <O> O invokeRpc(Callable<O> block, String startSpan, String methodName) {
217217
throw DatastoreException.translateAndThrow(e);
218218
} finally {
219219
TelemetryUtils.recordOperationMetrics(
220-
metricsRecorder, stopwatch, methodName, operationStatus);
220+
metricsRecorder, stopwatch, methodName, operationStatus, datastoreOptions.getDatabaseId());
221221
span.end();
222222
}
223223
}

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ private TelemetryUtils() {}
4242
* @param status The status of the operation or attempt.
4343
* @return The map of attributes.
4444
*/
45-
public static Map<String, String> buildMetricAttributes(String methodName, String status) {
45+
public static Map<String, String> buildMetricAttributes(String methodName, String status, String databaseId) {
4646
Map<String, String> attributes = new HashMap<>();
4747
attributes.put(TelemetryConstants.ATTRIBUTES_KEY_METHOD, methodName);
4848
attributes.put(TelemetryConstants.ATTRIBUTES_KEY_STATUS, status);
4949
attributes.put(TelemetryConstants.ATTRIBUTES_KEY_SERVICE, TelemetryConstants.SERVICE_VALUE);
50+
if (databaseId != null) {
51+
attributes.put(TelemetryConstants.ATTRIBUTES_KEY_DATABASE_ID, databaseId);
52+
}
5053
return attributes;
5154
}
5255

@@ -65,9 +68,10 @@ public static void recordOperationMetrics(
6568
DatastoreMetricsRecorder metricsRecorder,
6669
Stopwatch operationStopwatch,
6770
String methodName,
68-
String status) {
71+
String status,
72+
String databaseId) {
6973
if (methodName != null) {
70-
Map<String, String> attributes = buildMetricAttributes(methodName, status);
74+
Map<String, String> attributes = buildMetricAttributes(methodName, status, databaseId);
7175
metricsRecorder.recordOperationLatency(
7276
operationStopwatch.elapsed(TimeUnit.MILLISECONDS), attributes);
7377
metricsRecorder.recordOperationCount(1, attributes);
@@ -87,7 +91,7 @@ public static void recordOperationMetrics(
8791
* @return A wrapped callable that includes attempt-level metrics recording.
8892
*/
8993
public static <T> Callable<T> attemptMetricsCallable(
90-
Callable<T> callable, DatastoreMetricsRecorder metricsRecorder, String methodName) {
94+
Callable<T> callable, DatastoreMetricsRecorder metricsRecorder, String methodName, String databaseId) {
9195
return () -> {
9296
Stopwatch stopwatch = Stopwatch.createStarted();
9397
String status = StatusCode.Code.UNKNOWN.toString();
@@ -99,7 +103,7 @@ public static <T> Callable<T> attemptMetricsCallable(
99103
status = DatastoreException.extractStatusCode(e);
100104
throw e;
101105
} finally {
102-
Map<String, String> attributes = buildMetricAttributes(methodName, status);
106+
Map<String, String> attributes = buildMetricAttributes(methodName, status, databaseId);
103107
metricsRecorder.recordAttemptLatency(stopwatch.elapsed(TimeUnit.MILLISECONDS), attributes);
104108
metricsRecorder.recordAttemptCount(1, attributes);
105109
}

java-datastore/google-cloud-datastore/src/test/java/com/google/cloud/datastore/ITDatastoreBuiltInAndCustomMetrics.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,11 @@ public void bothBackendsActive_recorderIsComposite() {
182182
*/
183183
@Test
184184
public void builtInMetricsExport_isDisabledByDefault() {
185+
DatastoreOptions defaultOptions = DatastoreOptions.newBuilder()
186+
.setProjectId(PROJECT_ID)
187+
.build();
185188
assertThat(
186-
datastore
187-
.getOptions()
189+
defaultOptions
188190
.getOpenTelemetryOptions()
189191
.isExportBuiltinMetricsToGoogleCloudMonitoring())
190192
.isFalse();

0 commit comments

Comments
 (0)