Skip to content

Commit dd59c36

Browse files
committed
chore: Refactor DatastoreImplMetricsTest to check the attributes
1 parent 1697208 commit dd59c36

File tree

5 files changed

+315
-143
lines changed

5 files changed

+315
-143
lines changed

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

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,10 @@ public Transaction newTransaction() {
131131
}
132132

133133
/**
134-
* A wrapper around {@link ReadWriteTransactionCallable} that adds OpenTelemetry tracing context
135-
* propagation. All transaction logic (begin, run, commit, rollback, metrics recording) is
136-
* delegated to the underlying {@link ReadWriteTransactionCallable}.
134+
* A Tracing callable that adds OpenTelemetry tracing context. Intended to be used for
135+
* transactions and wraps {@link ReadWriteTransactionCallable} as the delegate. All transaction
136+
* logic (begin, run, commit, rollback, metrics recording) is handled in the delegate (this solely
137+
* handles tracing).
137138
*/
138139
static class TracedReadWriteTransactionCallable<T> implements Callable<T> {
139140
private final ReadWriteTransactionCallable<T> delegate;
@@ -146,10 +147,6 @@ static class TracedReadWriteTransactionCallable<T> implements Callable<T> {
146147
this.parentSpan = parentSpan;
147148
}
148149

149-
ReadWriteTransactionCallable<T> getDelegate() {
150-
return delegate;
151-
}
152-
153150
@Override
154151
public T call() throws DatastoreException {
155152
try (io.opentelemetry.context.Scope ignored =
@@ -192,18 +189,6 @@ static class ReadWriteTransactionCallable<T> implements Callable<T> {
192189
this.transaction = null;
193190
}
194191

195-
Datastore getDatastore() {
196-
return datastore;
197-
}
198-
199-
TransactionOptions getOptions() {
200-
return options;
201-
}
202-
203-
Transaction getTransaction() {
204-
return transaction;
205-
}
206-
207192
void setPrevTransactionId(ByteString transactionId) {
208193
TransactionOptions.ReadWrite readWrite =
209194
TransactionOptions.ReadWrite.newBuilder().setPreviousTransaction(transactionId).build();
@@ -221,19 +206,19 @@ public T call() throws DatastoreException {
221206
return value;
222207
} catch (Exception ex) {
223208
attemptStatus = DatastoreException.extractStatusCode(ex);
224-
// An exception here can originate from either callable.run() (before commit was attempted)
225-
// or from transaction.commit() itself. In both cases the transaction is still active.
226-
// isActive() returns false if the transaction was already committed or rolled back, so
227-
// it is safe to use as the sole guard here without tracking a separate committed flag.
209+
// An exception here can stem from either `callable.run()` (before commit was attempted)
210+
// or from `transaction.commit()`. If there is an exception thrown from either call site,
211+
// then the transaction is still active. Check if it is still active (e.g. not commited)
212+
// and roll back the transaction.
228213
if (transaction.isActive()) {
229214
transaction.rollback();
230215
}
231216
throw DatastoreException.propagateUserException(ex);
232217
} finally {
233218
recordAttempt(attemptStatus);
234-
// transaction.isActive() returns false after both a successful commit or a completed
235-
// rollback, so it already guards against rolling back a committed transaction or
236-
// rolling back a transaction that has already been rolled back.
219+
// If the transaction is active, then commit the rollback. If it was already successfully
220+
// rolled back, the transaction is inactive (prevents rolling back an already rolled back
221+
// transaction).
237222
if (transaction.isActive()) {
238223
transaction.rollback();
239224
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
/**
2727
* Interface to record specific metric operations.
2828
*
29-
* <p><b>Warning:</b> This is an internal API and is not intended for external use. Do not implement
30-
* or extend this interface.
29+
* <p><b>Warning:</b> This is intended to be an internal API and is not intended for external use.
30+
* This is public solely for implementation purposes and does not promise any backwards
31+
* compatibility.
3132
*/
3233
@InternalExtensionOnly
3334
public interface MetricsRecorder {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ class OpenTelemetryMetricsRecorder implements MetricsRecorder {
4141

4242
this.transactionLatency =
4343
meter
44-
.histogramBuilder(TelemetryConstants.SERVICE_NAME + "/transaction_latency")
44+
.histogramBuilder(TelemetryConstants.METRIC_NAME_TRANSACTION_LATENCY)
4545
.setDescription("Total latency of transaction operations")
4646
.setUnit("ms")
4747
.build();
4848

4949
this.transactionAttemptCount =
5050
meter
51-
.counterBuilder(TelemetryConstants.SERVICE_NAME + "/transaction_attempt_count")
51+
.counterBuilder(TelemetryConstants.METRIC_NAME_TRANSACTION_ATTEMPT_COUNT)
5252
.setDescription("Number of attempts to commit a transaction")
5353
.build();
5454
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ public class TelemetryConstants {
4545
/** Attribute key for the Datastore database ID. */
4646
public static final String ATTRIBUTES_KEY_DATABASE_ID = "database_id";
4747

48+
/** Metric name for the total latency of a transaction. */
49+
public static final String METRIC_NAME_TRANSACTION_LATENCY =
50+
SERVICE_NAME + "/transaction_latency";
51+
52+
/** Metric name for the number of attempts a transaction took. */
53+
public static final String METRIC_NAME_TRANSACTION_ATTEMPT_COUNT =
54+
SERVICE_NAME + "/transaction_attempt_count";
55+
4856
/* TODO(lawrenceqiu): For now, these are a duplicate of method names in TraceUtil. Those will use these eventually */
4957
// Format is not SnakeCase to keep backward compatibility with the existing values TraceUtil spans
5058
public static final String METHOD_ALLOCATE_IDS = "AllocateIds";

0 commit comments

Comments
 (0)