@@ -55,13 +55,21 @@ import { V4Connect, V4Session } from './internal-types';
5555import { PACKAGE_NAME , PACKAGE_VERSION } from './version' ;
5656import { UpDownCounter } from '@opentelemetry/api' ;
5757
58+ const DEFAULT_CONFIG : MongoDBInstrumentationConfig = {
59+ requireParentSpan : true ,
60+ } ;
61+
5862/** mongodb instrumentation plugin for OpenTelemetry */
5963export class MongoDBInstrumentation extends InstrumentationBase < MongoDBInstrumentationConfig > {
6064 private _connectionsUsage ! : UpDownCounter ;
6165 private _poolName ! : string ;
6266
6367 constructor ( config : MongoDBInstrumentationConfig = { } ) {
64- super ( PACKAGE_NAME , PACKAGE_VERSION , config ) ;
68+ super ( PACKAGE_NAME , PACKAGE_VERSION , { ...DEFAULT_CONFIG , ...config } ) ;
69+ }
70+
71+ override setConfig ( config : MongoDBInstrumentationConfig = { } ) {
72+ super . setConfig ( { ...DEFAULT_CONFIG , ...config } ) ;
6573 }
6674
6775 override _updateMetricInstruments ( ) {
@@ -438,10 +446,13 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
438446 callback ?: Function
439447 ) {
440448 const currentSpan = trace . getSpan ( context . active ( ) ) ;
449+ const skipInstrumentation =
450+ instrumentation . _checkSkipInstrumentation ( currentSpan ) ;
451+
441452 const resultHandler =
442453 typeof options === 'function' ? options : callback ;
443454 if (
444- ! currentSpan ||
455+ skipInstrumentation ||
445456 typeof resultHandler !== 'function' ||
446457 typeof ops !== 'object'
447458 ) {
@@ -451,6 +462,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
451462 return original . call ( this , server , ns , ops , options , callback ) ;
452463 }
453464 }
465+
454466 const span = instrumentation . tracer . startSpan (
455467 `mongodb.${ operationName } ` ,
456468 {
@@ -490,10 +502,14 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
490502 callback ?: Function
491503 ) {
492504 const currentSpan = trace . getSpan ( context . active ( ) ) ;
505+ const skipInstrumentation =
506+ instrumentation . _checkSkipInstrumentation ( currentSpan ) ;
507+
493508 const resultHandler =
494509 typeof options === 'function' ? options : callback ;
510+
495511 if (
496- ! currentSpan ||
512+ skipInstrumentation ||
497513 typeof resultHandler !== 'function' ||
498514 typeof cmd !== 'object'
499515 ) {
@@ -503,6 +519,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
503519 return original . call ( this , server , ns , cmd , options , callback ) ;
504520 }
505521 }
522+
506523 const commandType = MongoDBInstrumentation . _getCommandType ( cmd ) ;
507524 const type =
508525 commandType === MongodbCommandType . UNKNOWN ? 'command' : commandType ;
@@ -535,20 +552,17 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
535552 callback : any
536553 ) {
537554 const currentSpan = trace . getSpan ( context . active ( ) ) ;
555+ const skipInstrumentation =
556+ instrumentation . _checkSkipInstrumentation ( currentSpan ) ;
538557 const resultHandler = callback ;
539558 const commandType = Object . keys ( cmd ) [ 0 ] ;
540559
541- if (
542- typeof resultHandler !== 'function' ||
543- typeof cmd !== 'object' ||
544- cmd . ismaster ||
545- cmd . hello
546- ) {
560+ if ( typeof cmd !== 'object' || cmd . ismaster || cmd . hello ) {
547561 return original . call ( this , ns , cmd , options , callback ) ;
548562 }
549563
550564 let span = undefined ;
551- if ( currentSpan ) {
565+ if ( ! skipInstrumentation ) {
552566 span = instrumentation . tracer . startSpan ( `mongodb.${ commandType } ` , {
553567 kind : SpanKind . CLIENT ,
554568 } ) ;
@@ -581,6 +595,9 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
581595 ) {
582596 const [ ns , cmd ] = args ;
583597 const currentSpan = trace . getSpan ( context . active ( ) ) ;
598+ const skipInstrumentation =
599+ instrumentation . _checkSkipInstrumentation ( currentSpan ) ;
600+
584601 const commandType = Object . keys ( cmd ) [ 0 ] ;
585602 const resultHandler = ( ) => undefined ;
586603
@@ -589,7 +606,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
589606 }
590607
591608 let span = undefined ;
592- if ( currentSpan ) {
609+ if ( ! skipInstrumentation ) {
593610 span = instrumentation . tracer . startSpan ( `mongodb.${ commandType } ` , {
594611 kind : SpanKind . CLIENT ,
595612 } ) ;
@@ -634,10 +651,13 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
634651 callback ?: Function
635652 ) {
636653 const currentSpan = trace . getSpan ( context . active ( ) ) ;
654+ const skipInstrumentation =
655+ instrumentation . _checkSkipInstrumentation ( currentSpan ) ;
637656 const resultHandler =
638657 typeof options === 'function' ? options : callback ;
658+
639659 if (
640- ! currentSpan ||
660+ skipInstrumentation ||
641661 typeof resultHandler !== 'function' ||
642662 typeof cmd !== 'object'
643663 ) {
@@ -655,6 +675,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
655675 ) ;
656676 }
657677 }
678+
658679 const span = instrumentation . tracer . startSpan ( 'mongodb.find' , {
659680 kind : SpanKind . CLIENT ,
660681 } ) ;
@@ -699,9 +720,13 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
699720 callback ?: Function
700721 ) {
701722 const currentSpan = trace . getSpan ( context . active ( ) ) ;
723+ const skipInstrumentation =
724+ instrumentation . _checkSkipInstrumentation ( currentSpan ) ;
725+
702726 const resultHandler =
703727 typeof options === 'function' ? options : callback ;
704- if ( ! currentSpan || typeof resultHandler !== 'function' ) {
728+
729+ if ( skipInstrumentation || typeof resultHandler !== 'function' ) {
705730 if ( typeof options === 'function' ) {
706731 return original . call (
707732 this ,
@@ -723,6 +748,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
723748 ) ;
724749 }
725750 }
751+
726752 const span = instrumentation . tracer . startSpan ( 'mongodb.getMore' , {
727753 kind : SpanKind . CLIENT ,
728754 } ) ;
@@ -1021,4 +1047,10 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
10211047 const poolName = `mongodb://${ host } :${ port } /${ database } ` ;
10221048 this . _poolName = poolName ;
10231049 }
1050+
1051+ private _checkSkipInstrumentation ( currentSpan : Span | undefined ) {
1052+ const requireParentSpan = this . getConfig ( ) . requireParentSpan ;
1053+ const hasNoParentSpan = currentSpan === undefined ;
1054+ return requireParentSpan === true && hasNoParentSpan ;
1055+ }
10241056}
0 commit comments