@@ -459,6 +459,95 @@ describe('Plugin', () => {
459459 } ) . toArray ( )
460460 } )
461461 } )
462+
463+ describe ( 'with heartbeatEnabled configuration' , ( ) => {
464+ describe ( 'when heartbeat tracing is disabled' , ( ) => {
465+ before ( ( ) => {
466+ return agent . load ( 'mongodb-core' , {
467+ heartbeatEnabled : false
468+ } )
469+ } )
470+
471+ after ( ( ) => {
472+ return agent . close ( { ritmReset : false } )
473+ } )
474+
475+ beforeEach ( async ( ) => {
476+ client = await createClient ( )
477+ db = client . db ( 'test' )
478+ } )
479+
480+ it ( 'should NOT create a span for heartbeat commands' , ( done ) => {
481+ const parentSpan = tracer . startSpan ( 'test.parent' )
482+
483+ agent
484+ . use ( traces => {
485+ // Should only receive the trace for the parent span
486+ expect ( traces [ 0 ] ) . to . have . length ( 1 )
487+ const span = traces [ 0 ] [ 0 ]
488+ expect ( span . name ) . to . equal ( 'test.parent' )
489+ } )
490+ . then ( done )
491+
492+ // Activate parent span scope and trigger heartbeat command
493+ tracer . scope ( ) . activate ( parentSpan , async ( ) => {
494+ // Admin connect should be all that is needed to trigger heartbeat command for newer versions of mongo
495+ client = await createClient ( )
496+ db = client . db ( 'test' )
497+
498+ // but we should send a test heartbeat command since older versions of mongo don't auto-send heartbeats
499+ db . command ( { hello : 1 } )
500+ setTimeout ( ( ) => parentSpan . finish ( ) , 50 )
501+ } )
502+ } )
503+ } )
504+
505+ describe ( 'when heartbeat tracing is enabled (default)' , ( ) => {
506+ before ( ( ) => {
507+ return agent . load ( 'mongodb-core' , {
508+ heartbeatEnabled : true
509+ } )
510+ } )
511+
512+ after ( ( ) => {
513+ return agent . close ( { ritmReset : false } )
514+ } )
515+
516+ beforeEach ( async ( ) => {
517+ } )
518+
519+ it ( 'should create a child span for heartbeat commands' , ( done ) => {
520+ const parentSpan = tracer . startSpan ( 'test.parent' )
521+
522+ agent
523+ . use ( traces => {
524+ expect ( traces [ 0 ] ) . to . have . length . at . least ( 2 )
525+ const rootSpan = traces [ 0 ] [ 0 ]
526+
527+ expect ( rootSpan . name ) . to . equal ( 'test.parent' )
528+
529+ // assert that some child spans were created, these are the heartbeat spans
530+ // don't assert on exact number of spans because it's dynamic
531+ for ( const childSpan of traces [ 0 ] . slice ( 1 ) ) {
532+ expect ( childSpan . name ) . to . equal ( expectedSchema . outbound . opName )
533+ expect ( childSpan . parent_id . toString ( ) ) . to . equal ( rootSpan . span_id . toString ( ) ) // Verify parent-child
534+ }
535+ } )
536+ . then ( done )
537+
538+ // Activate parent span scope and trigger heartbeat command
539+ tracer . scope ( ) . activate ( parentSpan , async ( ) => {
540+ // Admin connect should be all that is needed to trigger heartbeat command for newer versions of mongo
541+ client = await createClient ( )
542+ db = client . db ( 'test' )
543+
544+ // but we should send a test heartbeat command since older versions of mongo don't auto-send heartbeats
545+ db . command ( { hello : 1 } )
546+ setTimeout ( ( ) => parentSpan . finish ( ) , 200 )
547+ } )
548+ } )
549+ } )
550+ } )
462551 } )
463552 } )
464553} )
0 commit comments