@@ -30,7 +30,7 @@ import {
3030 Context as OpenTelemetryContext ,
3131} from '@opentelemetry/api' ;
3232import { TraceExporter } from '@google-cloud/opentelemetry-cloud-trace-exporter' ;
33- import { Settings } from '@google-cloud/firestore' ;
33+ import { FirestoreOpenTelemetryOptions , Settings } from '@google-cloud/firestore' ;
3434import {
3535 AlwaysOnSampler ,
3636 BatchSpanProcessor ,
@@ -96,13 +96,6 @@ setLogFunction((msg: string) => {
9696 console . log ( `LOG: ${ msg } ` ) ;
9797} ) ;
9898
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-
10699interface TestConfig {
107100 // In-Memory tests check trace correctness by inspecting traces in memory by
108101 // utilizing InMemorySpanExporter. These tests have `e2e` set to `false`.
@@ -192,7 +185,6 @@ describe('Tracing Tests', () => {
192185 tracerProvider : TracerProvider
193186 ) : FirestoreOpenTelemetryOptions {
194187 const options : FirestoreOpenTelemetryOptions = {
195- enableTracing : true ,
196188 tracerProvider : undefined ,
197189 } ;
198190
@@ -285,7 +277,7 @@ describe('Tracing Tests', () => {
285277
286278 const settings : Settings = {
287279 preferRest : testConfig . preferRest ,
288- openTelemetryOptions : getOpenTelemetryOptions ( tracerProvider ) ,
280+ openTelemetry : getOpenTelemetryOptions ( tracerProvider ) ,
289281 } ;
290282
291283 // Named-database tests use an environment variable to specify the database ID. Add it to the settings.
@@ -660,7 +652,7 @@ describe('Tracing Tests', () => {
660652
661653 // Expect that the span exists first.
662654 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 ;
664656
665657 // Assert that the expected attributes are present in the span attributes.
666658 // Note that the span attributes may be a superset of the attributes passed
@@ -672,6 +664,29 @@ describe('Tracing Tests', () => {
672664 }
673665 }
674666
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+
675690 describe ( IN_MEMORY_TEST_SUITE_TITLE , ( ) => {
676691 describe ( NON_GLOBAL_OTEL_TEST_SUITE_TITLE , ( ) => {
677692 describe ( GRPC_TEST_SUITE_TITLE , ( ) => {
@@ -753,6 +768,11 @@ describe('Tracing Tests', () => {
753768 SPAN_NAME_DOC_REF_GET ,
754769 SPAN_NAME_BATCH_GET_DOCUMENTS
755770 ) ;
771+ expectSpanHasEvents ( SPAN_NAME_BATCH_GET_DOCUMENTS , [
772+ 'Firestore.batchGetDocuments: Start' ,
773+ 'Firestore.batchGetDocuments: First response received' ,
774+ 'Firestore.batchGetDocuments: Completed' ,
775+ ] ) ;
756776 } ) ;
757777
758778 it ( 'document reference create()' , async ( ) => {
@@ -820,6 +840,11 @@ describe('Tracing Tests', () => {
820840 ) ;
821841 await waitForCompletedSpans ( 2 ) ;
822842 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+ ] ) ;
823848 } ) ;
824849
825850 it ( 'collection reference add()' , async ( ) => {
@@ -852,6 +877,12 @@ describe('Tracing Tests', () => {
852877 ) ;
853878 await waitForCompletedSpans ( 2 ) ;
854879 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+ ] ) ;
855886 } ) ;
856887
857888 it ( 'firestore getAll()' , async ( ) => {
@@ -862,6 +893,11 @@ describe('Tracing Tests', () => {
862893 ) ;
863894 await waitForCompletedSpans ( 2 ) ;
864895 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+ ] ) ;
865901 } ) ;
866902
867903 it ( 'transaction' , async ( ) => {
@@ -920,6 +956,7 @@ describe('Tracing Tests', () => {
920956 await runFirestoreOperationInRootSpan ( async ( ) => {
921957 const query = firestore . collectionGroup ( 'foo' ) ;
922958 let numPartitions = 0 ;
959+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
923960 for await ( const partition of query . getPartitions ( 3 ) ) {
924961 numPartitions ++ ;
925962 }
0 commit comments