Skip to content

Commit 6af5223

Browse files
committed
feat: Add tracing for ReserveIds operation (#1490)
- added end-to-end test
1 parent 0abf298 commit 6af5223

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,10 @@ public List<Key> reserveIds(Key... keys) {
543543

544544
com.google.datastore.v1.ReserveIdsResponse reserveIds(
545545
final com.google.datastore.v1.ReserveIdsRequest requestPb) {
546-
Span span = traceUtil.startSpan(TraceUtil.SPAN_NAME_RESERVEIDS);
547-
try (Scope scope = traceUtil.getTracer().withSpan(span)) {
546+
com.google.cloud.datastore.telemetry.TraceUtil.Span span =
547+
otelTraceUtil.startSpan(
548+
com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RESERVE_IDS);
549+
try (com.google.cloud.datastore.telemetry.TraceUtil.Scope ignored = span.makeCurrent()) {
548550
return RetryHelper.runWithRetries(
549551
new Callable<com.google.datastore.v1.ReserveIdsResponse>() {
550552
@Override
@@ -556,10 +558,10 @@ public com.google.datastore.v1.ReserveIdsResponse call() throws DatastoreExcepti
556558
EXCEPTION_HANDLER,
557559
getOptions().getClock());
558560
} catch (RetryHelperException e) {
559-
span.setStatus(Status.UNKNOWN.withDescription(e.getMessage()));
561+
span.end(e);
560562
throw DatastoreException.translateAndThrow(e);
561563
} finally {
562-
span.end(TraceUtil.END_SPAN_OPTIONS);
564+
span.end();
563565
}
564566
}
565567

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public interface TraceUtil {
3333
static final String LIBRARY_NAME = "com.google.cloud.datastore";
3434
static final String SPAN_NAME_LOOKUP = "Lookup";
3535
static final String SPAN_NAME_ALLOCATE_IDS = "AllocateIds";
36+
static final String SPAN_NAME_RESERVE_IDS = "ReserveIds";
3637
static final String SPAN_NAME_COMMIT = "Commit";
3738
static final String SPAN_NAME_RUN_QUERY = "RunQuery";
3839
static final String SPAN_NAME_RUN_AGGREGATION_QUERY = "RunAggregationQuery";

google-cloud-datastore/src/test/java/com/google/cloud/datastore/it/ITE2ETracingTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_BEGIN_TRANSACTION;
2222
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_COMMIT;
2323
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_LOOKUP;
24+
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RESERVE_IDS;
2425
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RUN_AGGREGATION_QUERY;
2526
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RUN_QUERY;
2627
import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_COMMIT;
@@ -602,6 +603,25 @@ public void allocateIdsTraceTest() throws Exception {
602603
fetchAndValidateTrace(customSpanContext.getTraceId(), SPAN_NAME_ALLOCATE_IDS);
603604
}
604605

606+
@Test
607+
public void reserveIdsTraceTest() throws Exception {
608+
assertNotNull(customSpanContext);
609+
610+
Span rootSpan = getNewRootSpanWithContext();
611+
try (Scope ignored = rootSpan.makeCurrent()) {
612+
KeyFactory keyFactory = datastore.newKeyFactory().setKind("MyKind");
613+
Key key1 = keyFactory.newKey(10);
614+
Key key2 = keyFactory.newKey("name");
615+
List<Key> keyList = datastore.reserveIds(key1, key2);
616+
assertEquals(2, keyList.size());
617+
} finally {
618+
rootSpan.end();
619+
}
620+
waitForTracesToComplete();
621+
622+
fetchAndValidateTrace(customSpanContext.getTraceId(), SPAN_NAME_RESERVE_IDS);
623+
}
624+
605625
@Test
606626
public void commitTraceTest() throws Exception {
607627
assertNotNull(customSpanContext);

0 commit comments

Comments
 (0)