Skip to content

Commit

Permalink
test: Adding ITTracingTest to verify events and span attributes (whic… (
Browse files Browse the repository at this point in the history
#1514)

* test: Adding ITTracingTest to verify events and span attributes (which are not verified in ITE2ETracingTest) due to TraceClient API limitations.
- This test uses InMemorySpanExporter to read the generated Otel span data by the test process to verify generated span data as it were before exporting to a backend. None of the span data is exported to a durable backend.
- This test is still an E2E test as it requires a project to send RPCs to.

* fix: fixing compilation error due to missing pom dependency.

* test: Test for AllocateId and ReserveId rpcs

* test: Commit/Put/Update/Delete tests

* test: Added fixes and test for RunQuery event
  • Loading branch information
jimit-j-shah committed Aug 5, 2024
1 parent 31a6f4a commit 536c2d9
Show file tree
Hide file tree
Showing 4 changed files with 621 additions and 17 deletions.
6 changes: 6 additions & 0 deletions google-cloud-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@
<version>${opentelemetry.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<version>${opentelemetry.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-trace</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import com.google.datastore.v1.CommitResponse;
import com.google.datastore.v1.ExplainOptions;
import com.google.datastore.v1.ReadOptions;
import com.google.datastore.v1.ReserveIdsRequest;
Expand Down Expand Up @@ -290,8 +291,6 @@ com.google.datastore.v1.RunQueryResponse runQuery(
? com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_RUN_QUERY
: com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RUN_QUERY);
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
span.setAttribute("isTransactional", isTransactional);
span.setAttribute("readConsistency", readOptions.getReadConsistency().toString());

try (com.google.cloud.datastore.telemetry.TraceUtil.Scope ignored = span.makeCurrent()) {
RunQueryResponse response =
Expand All @@ -303,10 +302,17 @@ com.google.datastore.v1.RunQueryResponse runQuery(
: TRANSACTION_OPERATION_EXCEPTION_HANDLER,
getOptions().getClock());
span.addEvent(
spanName + ": Completed",
spanName,
new ImmutableMap.Builder<String, Object>()
.put("Received", response.getBatch().getEntityResultsCount())
.put("More results", response.getBatch().getMoreResults().toString())
.put("response_count", response.getBatch().getEntityResultsCount())
.put("transactional", isTransactional)
.put("read_consistency", readOptions.getReadConsistency().toString())
.put(
"transaction_id",
(isTransactional
? requestPb.getReadOptions().getTransaction().toStringUtf8()
: ""))
.put("more_results", response.getBatch().getMoreResults().toString())
.build());
return response;
} catch (RetryHelperException e) {
Expand Down Expand Up @@ -523,18 +529,18 @@ com.google.datastore.v1.LookupResponse lookup(
? com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_LOOKUP
: com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_LOOKUP);
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
span.setAttribute("isTransactional", isTransactional);

try (com.google.cloud.datastore.telemetry.TraceUtil.Scope ignored = span.makeCurrent()) {
return RetryHelper.runWithRetries(
() -> {
com.google.datastore.v1.LookupResponse response = datastoreRpc.lookup(requestPb);
span.addEvent(
spanName + ": Completed",
spanName,
new ImmutableMap.Builder<String, Object>()
.put("Received", response.getFoundCount())
.put("Missing", response.getMissingCount())
.put("Deferred", response.getDeferredCount())
.put("isTransactional", isTransactional)
.build());
return response;
},
Expand Down Expand Up @@ -690,15 +696,25 @@ com.google.datastore.v1.CommitResponse commit(
? com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_TRANSACTION_COMMIT
: com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_COMMIT;
com.google.cloud.datastore.telemetry.TraceUtil.Span span = otelTraceUtil.startSpan(spanName);
span.setAttribute("isTransactional", isTransactional);
try (com.google.cloud.datastore.telemetry.TraceUtil.Scope ignored = span.makeCurrent()) {
return RetryHelper.runWithRetries(
() -> datastoreRpc.commit(requestPb),
retrySettings,
requestPb.getTransaction().isEmpty()
? EXCEPTION_HANDLER
: TRANSACTION_OPERATION_EXCEPTION_HANDLER,
getOptions().getClock());
CommitResponse response =
RetryHelper.runWithRetries(
() -> datastoreRpc.commit(requestPb),
retrySettings,
requestPb.getTransaction().isEmpty()
? EXCEPTION_HANDLER
: TRANSACTION_OPERATION_EXCEPTION_HANDLER,
getOptions().getClock());
span.addEvent(
spanName,
new ImmutableMap.Builder<String, Object>()
.put("doc_count", response.getMutationResultsCount())
.put("transactional", isTransactional)
.put(
"transaction_id",
isTransactional ? requestPb.getTransaction().toStringUtf8() : "")
.build());
return response;
} catch (RetryHelperException e) {
span.end(e);
throw DatastoreException.translateAndThrow(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,15 @@ public void before() throws Exception {
.setNamespace(options.getNamespace())
.build();
KEY2 =
Key.newBuilder(projectId, kind1, "key3", options.getDatabaseId())
Key.newBuilder(projectId, kind1, "key2", options.getDatabaseId())
.setNamespace(options.getNamespace())
.build();
KEY3 =
Key.newBuilder(projectId, kind1, "key4", options.getDatabaseId())
.setNamespace(options.getNamespace())
.build();
KEY4 =
Key.newBuilder(projectId, kind1, "key2", options.getDatabaseId())
Key.newBuilder(projectId, kind1, "key4", options.getDatabaseId())
.setNamespace(options.getNamespace())
.build();
// Set up the tracer for custom TraceID injection
Expand Down
Loading

0 comments on commit 536c2d9

Please sign in to comment.