@@ -30,7 +30,7 @@ import {
30
30
Context as OpenTelemetryContext ,
31
31
} from '@opentelemetry/api' ;
32
32
import { TraceExporter } from '@google-cloud/opentelemetry-cloud-trace-exporter' ;
33
- import { Settings } from '@google-cloud/firestore' ;
33
+ import { FirestoreOpenTelemetryOptions , Settings } from '@google-cloud/firestore' ;
34
34
import {
35
35
AlwaysOnSampler ,
36
36
BatchSpanProcessor ,
@@ -96,13 +96,6 @@ setLogFunction((msg: string) => {
96
96
console . log ( `LOG: ${ msg } ` ) ;
97
97
} ) ;
98
98
99
- // TODO(tracing): This should be moved to firestore.d.ts when we want to
100
- // release the feature.
101
- export interface FirestoreOpenTelemetryOptions {
102
- enableTracing ?: boolean ;
103
- tracerProvider ?: any ;
104
- }
105
-
106
99
interface TestConfig {
107
100
// In-Memory tests check trace correctness by inspecting traces in memory by
108
101
// utilizing InMemorySpanExporter. These tests have `e2e` set to `false`.
@@ -192,7 +185,6 @@ describe('Tracing Tests', () => {
192
185
tracerProvider : TracerProvider
193
186
) : FirestoreOpenTelemetryOptions {
194
187
const options : FirestoreOpenTelemetryOptions = {
195
- enableTracing : true ,
196
188
tracerProvider : undefined ,
197
189
} ;
198
190
@@ -285,7 +277,7 @@ describe('Tracing Tests', () => {
285
277
286
278
const settings : Settings = {
287
279
preferRest : testConfig . preferRest ,
288
- openTelemetryOptions : getOpenTelemetryOptions ( tracerProvider ) ,
280
+ openTelemetry : getOpenTelemetryOptions ( tracerProvider ) ,
289
281
} ;
290
282
291
283
// Named-database tests use an environment variable to specify the database ID. Add it to the settings.
@@ -660,7 +652,7 @@ describe('Tracing Tests', () => {
660
652
661
653
// Expect that the span exists first.
662
654
const span = getSpanByName ( spanName ) ;
663
- expect ( span ) . to . not . be . null ;
655
+ expect ( span , `Could not find the span named ${ spanName } ` ) . to . not . be . null ;
664
656
665
657
// Assert that the expected attributes are present in the span attributes.
666
658
// Note that the span attributes may be a superset of the attributes passed
@@ -672,6 +664,29 @@ describe('Tracing Tests', () => {
672
664
}
673
665
}
674
666
667
+ // Ensures that the given span exists and has the given attributes.
668
+ function expectSpanHasEvents ( spanName : string , eventNames : string [ ] ) : void {
669
+ // The Cloud Trace API does not return span attributes and events.
670
+ if ( testConfig . e2e ) {
671
+ return ;
672
+ }
673
+
674
+ // Expect that the span exists first.
675
+ const span = getSpanByName ( spanName ) ;
676
+ expect ( span , `Could not find the span named ${ spanName } ` ) . to . not . be . null ;
677
+
678
+ // Assert that the expected attributes are present in the span attributes.
679
+ // Note that the span attributes may be a superset of the attributes passed
680
+ // to this function.
681
+ if ( span ?. events ) {
682
+ const numEvents = eventNames . length ;
683
+ expect ( numEvents ) . to . equal ( span . events . length ) ;
684
+ for ( let i = 0 ; i < numEvents ; ++ i ) {
685
+ expect ( span . events [ i ] . name ) . to . equal ( eventNames [ i ] ) ;
686
+ }
687
+ }
688
+ }
689
+
675
690
describe ( IN_MEMORY_TEST_SUITE_TITLE , ( ) => {
676
691
describe ( NON_GLOBAL_OTEL_TEST_SUITE_TITLE , ( ) => {
677
692
describe ( GRPC_TEST_SUITE_TITLE , ( ) => {
@@ -753,6 +768,11 @@ describe('Tracing Tests', () => {
753
768
SPAN_NAME_DOC_REF_GET ,
754
769
SPAN_NAME_BATCH_GET_DOCUMENTS
755
770
) ;
771
+ expectSpanHasEvents ( SPAN_NAME_BATCH_GET_DOCUMENTS , [
772
+ 'Firestore.batchGetDocuments: Start' ,
773
+ 'Firestore.batchGetDocuments: First response received' ,
774
+ 'Firestore.batchGetDocuments: Completed' ,
775
+ ] ) ;
756
776
} ) ;
757
777
758
778
it ( 'document reference create()' , async ( ) => {
@@ -820,6 +840,11 @@ describe('Tracing Tests', () => {
820
840
) ;
821
841
await waitForCompletedSpans ( 2 ) ;
822
842
expectSpanHierarchy ( SPAN_NAME_TEST_ROOT , SPAN_NAME_AGGREGATION_QUERY_GET ) ;
843
+ expectSpanHasEvents ( SPAN_NAME_AGGREGATION_QUERY_GET , [
844
+ 'Firestore.runAggregationQuery: Start' ,
845
+ 'Firestore.runAggregationQuery: First response received' ,
846
+ 'Firestore.runAggregationQuery: Completed' ,
847
+ ] ) ;
823
848
} ) ;
824
849
825
850
it ( 'collection reference add()' , async ( ) => {
@@ -852,6 +877,12 @@ describe('Tracing Tests', () => {
852
877
) ;
853
878
await waitForCompletedSpans ( 2 ) ;
854
879
expectSpanHierarchy ( SPAN_NAME_TEST_ROOT , SPAN_NAME_QUERY_GET ) ;
880
+ expectSpanHasEvents ( SPAN_NAME_QUERY_GET , [
881
+ 'RunQuery' ,
882
+ 'Firestore.runQuery: Start' ,
883
+ 'Firestore.runQuery: First response received' ,
884
+ 'Firestore.runQuery: Completed' ,
885
+ ] ) ;
855
886
} ) ;
856
887
857
888
it ( 'firestore getAll()' , async ( ) => {
@@ -862,6 +893,11 @@ describe('Tracing Tests', () => {
862
893
) ;
863
894
await waitForCompletedSpans ( 2 ) ;
864
895
expectSpanHierarchy ( SPAN_NAME_TEST_ROOT , SPAN_NAME_BATCH_GET_DOCUMENTS ) ;
896
+ expectSpanHasEvents ( SPAN_NAME_BATCH_GET_DOCUMENTS , [
897
+ 'Firestore.batchGetDocuments: Start' ,
898
+ 'Firestore.batchGetDocuments: First response received' ,
899
+ 'Firestore.batchGetDocuments: Completed' ,
900
+ ] ) ;
865
901
} ) ;
866
902
867
903
it ( 'transaction' , async ( ) => {
@@ -920,6 +956,7 @@ describe('Tracing Tests', () => {
920
956
await runFirestoreOperationInRootSpan ( async ( ) => {
921
957
const query = firestore . collectionGroup ( 'foo' ) ;
922
958
let numPartitions = 0 ;
959
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
923
960
for await ( const partition of query . getPartitions ( 3 ) ) {
924
961
numPartitions ++ ;
925
962
}
0 commit comments