|
16 | 16 |
|
17 | 17 | package com.google.cloud.datastore.it;
|
18 | 18 |
|
| 19 | +import static com.google.cloud.datastore.aggregation.Aggregation.count; |
19 | 20 | import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_COMMIT;
|
20 | 21 | import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_LOOKUP;
|
| 22 | +import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RUN_AGGREGATION_QUERY; |
21 | 23 | import static com.google.cloud.datastore.telemetry.TraceUtil.SPAN_NAME_RUN_QUERY;
|
| 24 | +import static com.google.common.truth.Truth.assertThat; |
22 | 25 | import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME;
|
23 | 26 | import static org.junit.Assert.assertEquals;
|
24 | 27 | import static org.junit.Assert.assertFalse;
|
|
27 | 30 | import static org.junit.Assert.assertTrue;
|
28 | 31 |
|
29 | 32 | import com.google.api.gax.rpc.NotFoundException;
|
| 33 | +import com.google.cloud.datastore.AggregationQuery; |
| 34 | +import com.google.cloud.datastore.AggregationResult; |
| 35 | +import com.google.cloud.datastore.AggregationResults; |
30 | 36 | import com.google.cloud.datastore.Datastore;
|
31 | 37 | import com.google.cloud.datastore.DatastoreOptions;
|
32 | 38 | import com.google.cloud.datastore.Entity;
|
33 | 39 | import com.google.cloud.datastore.Key;
|
34 | 40 | import com.google.cloud.datastore.Query;
|
35 | 41 | import com.google.cloud.datastore.QueryResults;
|
| 42 | +import com.google.cloud.datastore.StructuredQuery; |
36 | 43 | import com.google.cloud.datastore.StructuredQuery.PropertyFilter;
|
37 | 44 | import com.google.cloud.datastore.testing.RemoteDatastoreHelper;
|
38 | 45 | import com.google.cloud.opentelemetry.trace.TraceConfiguration;
|
|
95 | 102 | // 5. Traces are read-back using TraceServiceClient and verified against expected Call Stacks.
|
96 | 103 | @RunWith(TestParameterInjector.class)
|
97 | 104 | public class ITE2ETracingTest {
|
| 105 | + |
98 | 106 | protected boolean isUsingGlobalOpenTelemetrySDK() {
|
99 | 107 | return useGlobalOpenTelemetrySDK;
|
100 | 108 | }
|
@@ -214,6 +222,10 @@ private boolean dfsContainsCallStack(long spanId, List<String> expectedCallStack
|
214 | 222 |
|
215 | 223 | private static Key KEY2;
|
216 | 224 |
|
| 225 | + private static Key KEY3; |
| 226 | + |
| 227 | + private static Key KEY4; |
| 228 | + |
217 | 229 | // Random int generator for trace ID and span ID
|
218 | 230 | private static Random random;
|
219 | 231 |
|
@@ -309,10 +321,17 @@ public void before() throws Exception {
|
309 | 321 | .setNamespace(options.getNamespace())
|
310 | 322 | .build();
|
311 | 323 | KEY2 =
|
| 324 | + Key.newBuilder(projectId, kind1, "key3", options.getDatabaseId()) |
| 325 | + .setNamespace(options.getNamespace()) |
| 326 | + .build(); |
| 327 | + KEY3 = |
| 328 | + Key.newBuilder(projectId, kind1, "key4", options.getDatabaseId()) |
| 329 | + .setNamespace(options.getNamespace()) |
| 330 | + .build(); |
| 331 | + KEY4 = |
312 | 332 | Key.newBuilder(projectId, kind1, "key2", options.getDatabaseId())
|
313 | 333 | .setNamespace(options.getNamespace())
|
314 | 334 | .build();
|
315 |
| - |
316 | 335 | // Set up the tracer for custom TraceID injection
|
317 | 336 | rootSpanName =
|
318 | 337 | String.format("%s%d", this.getClass().getSimpleName(), System.currentTimeMillis());
|
@@ -658,4 +677,62 @@ public void runQueryTraceTest() throws Exception {
|
658 | 677 |
|
659 | 678 | fetchAndValidateTrace(customSpanContext.getTraceId(), SPAN_NAME_RUN_QUERY);
|
660 | 679 | }
|
| 680 | + |
| 681 | + @Test |
| 682 | + public void runAggregationQueryTraceTest() throws Exception { |
| 683 | + Entity entity1 = |
| 684 | + Entity.newBuilder(KEY1) |
| 685 | + .set("pepper_name", "jalapeno") |
| 686 | + .set("max_scoville_level", 10000) |
| 687 | + .build(); |
| 688 | + Entity entity2 = |
| 689 | + Entity.newBuilder(KEY2) |
| 690 | + .set("pepper_name", "serrano") |
| 691 | + .set("max_scoville_level", 25000) |
| 692 | + .build(); |
| 693 | + Entity entity3 = |
| 694 | + Entity.newBuilder(KEY3) |
| 695 | + .set("pepper_name", "habanero") |
| 696 | + .set("max_scoville_level", 350000) |
| 697 | + .build(); |
| 698 | + Entity entity4 = |
| 699 | + Entity.newBuilder(KEY4) |
| 700 | + .set("pepper_name", "ghost") |
| 701 | + .set("max_scoville_level", 1500000) |
| 702 | + .build(); |
| 703 | + |
| 704 | + List<Entity> entityList = new ArrayList<>(); |
| 705 | + entityList.add(entity1); |
| 706 | + entityList.add(entity2); |
| 707 | + entityList.add(entity3); |
| 708 | + entityList.add(entity4); |
| 709 | + |
| 710 | + List<Entity> response = datastore.add(entity1, entity2, entity3, entity4); |
| 711 | + assertEquals(entityList, response); |
| 712 | + |
| 713 | + Span rootSpan = getNewRootSpanWithContext(); |
| 714 | + try (Scope ignored = rootSpan.makeCurrent()) { |
| 715 | + PropertyFilter mediumSpicyFilters = PropertyFilter.lt("max_scoville_level", 100000); |
| 716 | + StructuredQuery<Entity> mediumSpicyQuery = |
| 717 | + Query.newEntityQueryBuilder() |
| 718 | + .setKind(KEY1.getKind()) |
| 719 | + .setFilter(mediumSpicyFilters) |
| 720 | + .build(); |
| 721 | + AggregationQuery countSpicyPeppers = |
| 722 | + Query.newAggregationQueryBuilder() |
| 723 | + .addAggregation(count().as("count")) |
| 724 | + .over(mediumSpicyQuery) |
| 725 | + .build(); |
| 726 | + AggregationResults results = datastore.runAggregation(countSpicyPeppers); |
| 727 | + assertThat(results.size()).isEqualTo(1); |
| 728 | + AggregationResult result = results.get(0); |
| 729 | + assertThat(result.getLong("count")).isEqualTo(2L); |
| 730 | + } finally { |
| 731 | + rootSpan.end(); |
| 732 | + } |
| 733 | + |
| 734 | + waitForTracesToComplete(); |
| 735 | + |
| 736 | + fetchAndValidateTrace(customSpanContext.getTraceId(), SPAN_NAME_RUN_AGGREGATION_QUERY); |
| 737 | + } |
661 | 738 | }
|
0 commit comments