1414 * limitations under the License.
1515 */
1616
17- // for testing locally use this command to run docker
18- // docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=localhost -p 27017:27017 --name otmongo mongo
17+ // for testing locally "npm run docker:start"
1918
2019import { context , setSpan , SpanKind } from '@opentelemetry/api' ;
2120import { BasicTracerProvider } from '@opentelemetry/tracing' ;
@@ -99,6 +98,7 @@ describe('MongoDBInstrumentation', () => {
9998
10099 afterEach ( done => {
101100 collection . deleteMany ( { } , done ) ;
101+ memoryExporter . reset ( ) ;
102102 } ) ;
103103
104104 after ( ( ) => {
@@ -109,6 +109,9 @@ describe('MongoDBInstrumentation', () => {
109109
110110 /** Should intercept query */
111111 describe ( 'Instrumenting query operations' , ( ) => {
112+ beforeEach ( ( ) => {
113+ memoryExporter . reset ( ) ;
114+ } ) ;
112115 it ( 'should create a child span for insert' , done => {
113116 const insertData = [ { a : 1 } , { a : 2 } , { a : 3 } ] ;
114117 const span = provider . getTracer ( 'default' ) . startSpan ( 'insertRootSpan' ) ;
@@ -161,6 +164,10 @@ describe('MongoDBInstrumentation', () => {
161164
162165 /** Should intercept cursor */
163166 describe ( 'Instrumenting cursor operations' , ( ) => {
167+ beforeEach ( ( ) => {
168+ memoryExporter . reset ( ) ;
169+ } ) ;
170+
164171 it ( 'should create a child span for find' , done => {
165172 const span = provider . getTracer ( 'default' ) . startSpan ( 'findRootSpan' ) ;
166173 context . with ( setSpan ( context . active ( ) , span ) , ( ) => {
@@ -212,6 +219,10 @@ describe('MongoDBInstrumentation', () => {
212219
213220 /** Should intercept command */
214221 describe ( 'Instrumenting command operations' , ( ) => {
222+ beforeEach ( ( ) => {
223+ memoryExporter . reset ( ) ;
224+ } ) ;
225+
215226 it ( 'should create a child span for create index' , done => {
216227 const span = provider . getTracer ( 'default' ) . startSpan ( 'indexRootSpan' ) ;
217228 context . with ( setSpan ( context . active ( ) , span ) , ( ) => {
@@ -229,8 +240,47 @@ describe('MongoDBInstrumentation', () => {
229240 } ) ;
230241 } ) ;
231242
243+ describe ( 'Mixed operations with callback' , ( ) => {
244+ beforeEach ( ( ) => {
245+ memoryExporter . reset ( ) ;
246+ } ) ;
247+
248+ it ( 'should create a span for find after callback insert' , done => {
249+ const insertData = [ { a : 1 } , { a : 2 } , { a : 3 } ] ;
250+ const span = provider . getTracer ( 'default' ) . startSpan ( 'insertRootSpan' ) ;
251+ context . with ( setSpan ( context . active ( ) , span ) , ( ) => {
252+ collection . insertMany ( insertData , ( err , result ) => {
253+ span . end ( ) ;
254+ assert . ifError ( err ) ;
255+ const spans = memoryExporter . getFinishedSpans ( ) ;
256+ const mainSpan = spans [ spans . length - 1 ] ;
257+ assertSpans ( spans , 'mongodb.insert' , SpanKind . CLIENT ) ;
258+ memoryExporter . reset ( ) ;
259+
260+ collection . find ( { a : 1 } ) . toArray ( ( err , result ) => {
261+ const spans2 = memoryExporter . getFinishedSpans ( ) ;
262+ spans2 . push ( mainSpan ) ;
263+
264+ assert . ifError ( err ) ;
265+ assertSpans ( spans2 , 'mongodb.find' , SpanKind . CLIENT ) ;
266+ assert . strictEqual (
267+ mainSpan . spanContext . spanId ,
268+ spans2 [ 0 ] . parentSpanId
269+ ) ;
270+ memoryExporter . reset ( ) ;
271+ done ( ) ;
272+ } ) ;
273+ } ) ;
274+ } ) ;
275+ } ) ;
276+ } ) ;
277+
232278 /** Should intercept command */
233279 describe ( 'Removing Instrumentation' , ( ) => {
280+ beforeEach ( ( ) => {
281+ memoryExporter . reset ( ) ;
282+ } ) ;
283+
234284 it ( 'should unpatch plugin' , ( ) => {
235285 assert . doesNotThrow ( ( ) => {
236286 instrumentation . disable ( ) ;
0 commit comments