1
1
'use strict'
2
2
3
+ const sinon = require ( 'sinon' )
3
4
const semver = require ( 'semver' )
4
5
const agent = require ( '../../dd-trace/test/plugins/agent' )
5
6
const { ERROR_MESSAGE , ERROR_TYPE , ERROR_STACK } = require ( '../../dd-trace/src/constants' )
6
7
const { expectedSchema, rawExpectedSchema } = require ( './naming' )
7
8
9
+ const MongodbCorePlugin = require ( '../../datadog-plugin-mongodb-core/src/index' )
10
+ const ddpv = require ( 'mocha/package.json' ) . version
11
+
8
12
const withTopologies = fn => {
9
13
withVersions ( 'mongodb-core' , [ 'mongodb-core' , 'mongodb' ] , '<4' , ( version , moduleName ) => {
10
14
describe ( 'using the server topology' , ( ) => {
@@ -29,6 +33,7 @@ describe('Plugin', () => {
29
33
let id
30
34
let tracer
31
35
let collection
36
+ let injectDbmCommandSpy
32
37
33
38
describe ( 'mongodb-core (core)' , ( ) => {
34
39
withTopologies ( getServer => {
@@ -397,6 +402,181 @@ describe('Plugin', () => {
397
402
}
398
403
)
399
404
} )
405
+
406
+ describe ( 'with dbmPropagationMode service' , ( ) => {
407
+ before ( ( ) => {
408
+ return agent . load ( 'mongodb-core' , { dbmPropagationMode : 'service' } )
409
+ } )
410
+
411
+ after ( ( ) => {
412
+ return agent . close ( { ritmReset : false } )
413
+ } )
414
+
415
+ beforeEach ( done => {
416
+ const Server = getServer ( )
417
+
418
+ server = new Server ( {
419
+ host : '127.0.0.1' ,
420
+ port : 27017 ,
421
+ reconnect : false
422
+ } )
423
+
424
+ server . on ( 'connect' , ( ) => done ( ) )
425
+ server . on ( 'error' , done )
426
+
427
+ server . connect ( )
428
+
429
+ injectDbmCommandSpy = sinon . spy ( MongodbCorePlugin . prototype , 'injectDbmCommand' )
430
+ } )
431
+
432
+ afterEach ( ( ) => {
433
+ injectDbmCommandSpy ?. restore ( )
434
+ } )
435
+
436
+ it ( 'DBM propagation should inject service mode as comment' , done => {
437
+ agent
438
+ . use ( traces => {
439
+ const span = traces [ 0 ] [ 0 ]
440
+
441
+ expect ( injectDbmCommandSpy . called ) . to . be . true
442
+ const instrumentedCommand = injectDbmCommandSpy . getCall ( 0 ) . returnValue
443
+ expect ( instrumentedCommand ) . to . have . property ( 'comment' )
444
+ expect ( instrumentedCommand . comment ) . to . equal (
445
+ `dddb='${ encodeURIComponent ( span . meta [ 'db.name' ] ) } ',` +
446
+ 'dddbs=\'test-mongodb\',' +
447
+ 'dde=\'tester\',' +
448
+ `ddh='${ encodeURIComponent ( span . meta [ 'out.host' ] ) } ',` +
449
+ `ddps='${ encodeURIComponent ( span . meta . service ) } ',` +
450
+ `ddpv='${ ddpv } ',` +
451
+ `ddprs='${ encodeURIComponent ( span . meta [ 'peer.service' ] ) } '`
452
+ )
453
+ } )
454
+ . then ( done )
455
+ . catch ( done )
456
+
457
+ server . insert ( `test.${ collection } ` , [ { a : 1 } ] , ( ) => { } )
458
+ } )
459
+
460
+ it ( 'DBM propagation should inject service mode after eixsting str comment' , done => {
461
+ agent
462
+ . use ( traces => {
463
+ const span = traces [ 0 ] [ 0 ]
464
+
465
+ expect ( injectDbmCommandSpy . called ) . to . be . true
466
+ const instrumentedCommand = injectDbmCommandSpy . getCall ( 0 ) . returnValue
467
+ expect ( instrumentedCommand ) . to . have . property ( 'comment' )
468
+ expect ( instrumentedCommand . comment ) . to . equal (
469
+ 'test comment,' +
470
+ `dddb='${ encodeURIComponent ( span . meta [ 'db.name' ] ) } ',` +
471
+ 'dddbs=\'test-mongodb\',' +
472
+ 'dde=\'tester\',' +
473
+ `ddh='${ encodeURIComponent ( span . meta [ 'out.host' ] ) } ',` +
474
+ `ddps='${ encodeURIComponent ( span . meta . service ) } ',` +
475
+ `ddpv='${ ddpv } ',` +
476
+ `ddprs='${ encodeURIComponent ( span . meta [ 'peer.service' ] ) } '`
477
+ )
478
+ } )
479
+ . then ( done )
480
+ . catch ( done )
481
+
482
+ server . command ( `test.${ collection } ` , {
483
+ find : `test.${ collection } ` ,
484
+ query : {
485
+ _id : Buffer . from ( '1234' )
486
+ } ,
487
+ comment : 'test comment'
488
+ } , ( ) => { } )
489
+ } )
490
+
491
+ it ( 'DBM propagation should inject service mode after eixsting array comment' , done => {
492
+ agent
493
+ . use ( traces => {
494
+ const span = traces [ 0 ] [ 0 ]
495
+
496
+ expect ( injectDbmCommandSpy . called ) . to . be . true
497
+ const instrumentedCommand = injectDbmCommandSpy . getCall ( 0 ) . returnValue
498
+ expect ( instrumentedCommand ) . to . have . property ( 'comment' )
499
+ expect ( instrumentedCommand . comment ) . to . deep . equal ( [
500
+ 'test comment' ,
501
+ `dddb='${ encodeURIComponent ( span . meta [ 'db.name' ] ) } ',` +
502
+ 'dddbs=\'test-mongodb\',' +
503
+ 'dde=\'tester\',' +
504
+ `ddh='${ encodeURIComponent ( span . meta [ 'out.host' ] ) } ',` +
505
+ `ddps='${ encodeURIComponent ( span . meta . service ) } ',` +
506
+ `ddpv='${ ddpv } ',` +
507
+ `ddprs='${ encodeURIComponent ( span . meta [ 'peer.service' ] ) } '`
508
+ ] )
509
+ } )
510
+ . then ( done )
511
+ . catch ( done )
512
+
513
+ server . command ( `test.${ collection } ` , {
514
+ find : `test.${ collection } ` ,
515
+ query : {
516
+ _id : Buffer . from ( '1234' )
517
+ } ,
518
+ comment : [ 'test comment' ]
519
+ } , ( ) => { } )
520
+ } )
521
+ } )
522
+
523
+ describe ( 'with dbmPropagationMode full' , ( ) => {
524
+ before ( ( ) => {
525
+ return agent . load ( 'mongodb-core' , { dbmPropagationMode : 'full' } )
526
+ } )
527
+
528
+ after ( ( ) => {
529
+ return agent . close ( { ritmReset : false } )
530
+ } )
531
+
532
+ beforeEach ( done => {
533
+ const Server = getServer ( )
534
+
535
+ server = new Server ( {
536
+ host : '127.0.0.1' ,
537
+ port : 27017 ,
538
+ reconnect : false
539
+ } )
540
+
541
+ server . on ( 'connect' , ( ) => done ( ) )
542
+ server . on ( 'error' , done )
543
+
544
+ server . connect ( )
545
+
546
+ injectDbmCommandSpy = sinon . spy ( MongodbCorePlugin . prototype , 'injectDbmCommand' )
547
+ } )
548
+
549
+ afterEach ( ( ) => {
550
+ injectDbmCommandSpy ?. restore ( )
551
+ } )
552
+
553
+ it ( 'DBM propagation should inject full mode with traceparent as comment' , done => {
554
+ agent
555
+ . use ( traces => {
556
+ const span = traces [ 0 ] [ 0 ]
557
+ const traceId = span . meta [ '_dd.p.tid' ] + span . trace_id . toString ( 16 ) . padStart ( 16 , '0' )
558
+ const spanId = span . span_id . toString ( 16 ) . padStart ( 16 , '0' )
559
+
560
+ expect ( injectDbmCommandSpy . called ) . to . be . true
561
+ const instrumentedCommand = injectDbmCommandSpy . getCall ( 0 ) . returnValue
562
+ expect ( instrumentedCommand ) . to . have . property ( 'comment' )
563
+ expect ( instrumentedCommand . comment ) . to . equal (
564
+ `dddb='${ encodeURIComponent ( span . meta [ 'db.name' ] ) } ',` +
565
+ 'dddbs=\'test-mongodb\',' +
566
+ 'dde=\'tester\',' +
567
+ `ddh='${ encodeURIComponent ( span . meta [ 'out.host' ] ) } ',` +
568
+ `ddps='${ encodeURIComponent ( span . meta . service ) } ',` +
569
+ `ddpv='${ ddpv } ',` +
570
+ `ddprs='${ encodeURIComponent ( span . meta [ 'peer.service' ] ) } ',` +
571
+ `traceparent='00-${ traceId } -${ spanId } -00'`
572
+ )
573
+ } )
574
+ . then ( done )
575
+ . catch ( done )
576
+
577
+ server . insert ( `test.${ collection } ` , [ { a : 1 } ] , ( ) => { } )
578
+ } )
579
+ } )
400
580
} )
401
581
} )
402
582
} )
0 commit comments