@@ -10,6 +10,7 @@ import { PromisifyCallbacks } from '@sofie-automation/shared-lib/dist/lib/types'
1010import type { AnyBulkWriteOperation } from 'mongodb'
1111import { MinimalMongoCursor , WrappedMongoCollectionBase } from './base'
1212import { AsyncOnlyMongoCollection } from '../collection'
13+ import { profiler } from '../../api/profiler'
1314
1415export class WrappedAsyncMongoCollection < DBInterface extends { _id : ProtectedString < any > } >
1516 extends WrappedMongoCollectionBase < DBInterface >
@@ -23,38 +24,111 @@ export class WrappedAsyncMongoCollection<DBInterface extends { _id: ProtectedStr
2324 selector : MongoQuery < DBInterface > | DBInterface [ '_id' ] ,
2425 options ?: FindOptions < DBInterface >
2526 ) : Promise < Array < DBInterface > > {
26- return this . find ( selector as any , options ) . fetchAsync ( )
27+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .findFetch` )
28+ if ( span ) {
29+ span . addLabels ( {
30+ collection : this . name ,
31+ query : JSON . stringify ( selector ) ,
32+ } )
33+ }
34+ try {
35+ const res = await this . _collection . find ( ( selector ?? { } ) as any , options as any ) . fetchAsync ( )
36+ if ( span ) span . end ( )
37+ return res
38+ } catch ( e ) {
39+ if ( span ) span . end ( )
40+ this . wrapMongoError ( e )
41+ }
2742 }
2843
2944 async findOneAsync (
3045 selector : MongoQuery < DBInterface > | DBInterface [ '_id' ] ,
3146 options ?: FindOptions < DBInterface >
3247 ) : Promise < DBInterface | undefined > {
33- const arr = await this . findFetchAsync ( selector , { ...options , limit : 1 } )
34- return arr [ 0 ]
48+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .findOne` )
49+ if ( span ) {
50+ span . addLabels ( {
51+ collection : this . name ,
52+ query : JSON . stringify ( selector ) ,
53+ } )
54+ }
55+ try {
56+ const arr = await this . _collection
57+ . find ( ( selector ?? { } ) as any , { ...( options as any ) , limit : 1 } )
58+ . fetchAsync ( )
59+ if ( span ) span . end ( )
60+ return arr [ 0 ]
61+ } catch ( e ) {
62+ if ( span ) span . end ( )
63+ this . wrapMongoError ( e )
64+ }
3565 }
3666
3767 async findWithCursor (
3868 selector ?: MongoQuery < DBInterface > | DBInterface [ '_id' ] ,
3969 options ?: FindOptions < DBInterface >
4070 ) : Promise < MinimalMongoCursor < DBInterface > > {
41- return this . find ( selector as any , options )
71+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .findCursor` )
72+ if ( span ) {
73+ span . addLabels ( {
74+ collection : this . name ,
75+ query : JSON . stringify ( selector ) ,
76+ } )
77+ }
78+ try {
79+ const res = this . _collection . find ( ( selector ?? { } ) as any , options as any )
80+ if ( span ) span . end ( )
81+ return res
82+ } catch ( e ) {
83+ if ( span ) span . end ( )
84+ this . wrapMongoError ( e )
85+ }
4286 }
4387
4488 async observeChanges (
4589 selector : MongoQuery < DBInterface > | DBInterface [ '_id' ] ,
4690 callbacks : PromisifyCallbacks < ObserveChangesCallbacks < DBInterface > > ,
4791 options ?: FindOptions < DBInterface >
4892 ) : Promise < Meteor . LiveQueryHandle > {
49- return this . find ( selector as any , options ) . observeChangesAsync ( callbacks )
93+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .observeChanges` )
94+ if ( span ) {
95+ span . addLabels ( {
96+ collection : this . name ,
97+ query : JSON . stringify ( selector ) ,
98+ } )
99+ }
100+ try {
101+ const res = await this . _collection
102+ . find ( ( selector ?? { } ) as any , options as any )
103+ . observeChangesAsync ( callbacks )
104+ if ( span ) span . end ( )
105+ return res
106+ } catch ( e ) {
107+ if ( span ) span . end ( )
108+ this . wrapMongoError ( e )
109+ }
50110 }
51111
52112 async observe (
53113 selector : MongoQuery < DBInterface > | DBInterface [ '_id' ] ,
54114 callbacks : PromisifyCallbacks < ObserveCallbacks < DBInterface > > ,
55115 options ?: FindOptions < DBInterface >
56116 ) : Promise < Meteor . LiveQueryHandle > {
57- return this . find ( selector as any , options ) . observeAsync ( callbacks )
117+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .observe` )
118+ if ( span ) {
119+ span . addLabels ( {
120+ collection : this . name ,
121+ query : JSON . stringify ( selector ) ,
122+ } )
123+ }
124+ try {
125+ const res = await this . _collection . find ( ( selector ?? { } ) as any , options as any ) . observeAsync ( callbacks )
126+ if ( span ) span . end ( )
127+ return res
128+ } catch ( e ) {
129+ if ( span ) span . end ( )
130+ this . wrapMongoError ( e )
131+ }
58132 }
59133
60134 async insertManyAsync ( docs : DBInterface [ ] ) : Promise < Array < DBInterface [ '_id' ] > > {
@@ -81,6 +155,14 @@ export class WrappedAsyncMongoCollection<DBInterface extends { _id: ProtectedStr
81155 }
82156
83157 async bulkWriteAsync ( ops : Array < AnyBulkWriteOperation < DBInterface > > ) : Promise < void > {
158+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .bulkWrite` )
159+ if ( span ) {
160+ span . addLabels ( {
161+ collection : this . name ,
162+ opCount : ops . length ,
163+ } )
164+ }
165+
84166 if ( ops . length > 0 ) {
85167 const rawCollection = this . rawCollection ( )
86168 const bulkWriteResult = await rawCollection . bulkWrite ( ops , {
@@ -92,12 +174,24 @@ export class WrappedAsyncMongoCollection<DBInterface extends { _id: ProtectedStr
92174 throw new Meteor . Error ( 500 , `Errors in rawCollection.bulkWrite: ${ writeErrors . join ( ',' ) } ` )
93175 }
94176 }
177+
178+ if ( span ) span . end ( )
95179 }
96180
97181 async countDocuments ( selector ?: MongoQuery < DBInterface > , options ?: FindOptions < DBInterface > ) : Promise < number > {
182+ const span = profiler . startSpan ( `MongoCollection.${ this . name } .countDocuments` )
183+ if ( span ) {
184+ span . addLabels ( {
185+ collection : this . name ,
186+ query : JSON . stringify ( selector ) ,
187+ } )
188+ }
98189 try {
99- return this . _collection . find ( ( selector ?? { } ) as any , options as any ) . countAsync ( )
190+ const res = await this . _collection . find ( ( selector ?? { } ) as any , options as any ) . countAsync ( )
191+ if ( span ) span . end ( )
192+ return res
100193 } catch ( e ) {
194+ if ( span ) span . end ( )
101195 this . wrapMongoError ( e )
102196 }
103197 }
0 commit comments