1- import type { Hub , SentrySpan } from '@sentry/core' ;
2- import type { EventProcessor , SpanContext } from '@sentry/types' ;
1+ import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN , startInactiveSpan } from '@sentry/core' ;
2+ import { getClient } from '@sentry/core' ;
3+ import type { EventProcessor , SpanAttributes , StartSpanOptions } from '@sentry/types' ;
34import { fill , isThenable , loadModule , logger } from '@sentry/utils' ;
45
56import { DEBUG_BUILD } from '../../common/debug-build' ;
@@ -138,7 +139,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
138139 /**
139140 * @inheritDoc
140141 */
141- public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
142+ public setupOnce ( _ : ( callback : EventProcessor ) => void ) : void {
142143 const pkg = this . loadDependency ( ) ;
143144
144145 if ( ! pkg ) {
@@ -147,42 +148,36 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
147148 return ;
148149 }
149150
150- this . _instrumentOperations ( pkg . Collection , this . _operations , getCurrentHub ) ;
151+ this . _instrumentOperations ( pkg . Collection , this . _operations ) ;
151152 }
152153
153154 /**
154155 * Patches original collection methods
155156 */
156- private _instrumentOperations ( collection : MongoCollection , operations : Operation [ ] , getCurrentHub : ( ) => Hub ) : void {
157- operations . forEach ( ( operation : Operation ) => this . _patchOperation ( collection , operation , getCurrentHub ) ) ;
157+ private _instrumentOperations ( collection : MongoCollection , operations : Operation [ ] ) : void {
158+ operations . forEach ( ( operation : Operation ) => this . _patchOperation ( collection , operation ) ) ;
158159 }
159160
160161 /**
161162 * Patches original collection to utilize our tracing functionality
162163 */
163- private _patchOperation ( collection : MongoCollection , operation : Operation , getCurrentHub : ( ) => Hub ) : void {
164+ private _patchOperation ( collection : MongoCollection , operation : Operation ) : void {
164165 if ( ! ( operation in collection . prototype ) ) return ;
165166
166167 const getSpanContext = this . _getSpanContextFromOperationArguments . bind ( this ) ;
167168
168169 fill ( collection . prototype , operation , function ( orig : ( ) => void | Promise < unknown > ) {
169170 return function ( this : unknown , ...args : unknown [ ] ) {
170171 const lastArg = args [ args . length - 1 ] ;
171- const hub = getCurrentHub ( ) ;
172- // eslint-disable-next-line deprecation/deprecation
173- const scope = hub . getScope ( ) ;
174- // eslint-disable-next-line deprecation/deprecation
175- const client = hub . getClient ( ) ;
176- // eslint-disable-next-line deprecation/deprecation
177- const parentSpan = scope . getSpan ( ) as SentrySpan | undefined ;
172+
173+ const client = getClient ( ) ;
178174
179175 const sendDefaultPii = client ?. getOptions ( ) . sendDefaultPii ;
180176
181177 // Check if the operation was passed a callback. (mapReduce requires a different check, as
182178 // its (non-callback) arguments can also be functions.)
183179 if ( typeof lastArg !== 'function' || ( operation === 'mapReduce' && args . length === 2 ) ) {
184- // eslint-disable-next-line deprecation/deprecation
185- const span = parentSpan ?. startChild ( getSpanContext ( this , operation , args , sendDefaultPii ) ) ;
180+ const span = startInactiveSpan ( getSpanContext ( this , operation , args , sendDefaultPii ) ) ;
186181 const maybePromiseOrCursor = orig . call ( this , ...args ) ;
187182
188183 if ( isThenable ( maybePromiseOrCursor ) ) {
@@ -213,8 +208,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
213208 }
214209 }
215210
216- // eslint-disable-next-line deprecation/deprecation
217- const span = parentSpan ?. startChild ( getSpanContext ( this , operation , args . slice ( 0 , - 1 ) ) ) ;
211+ const span = startInactiveSpan ( getSpanContext ( this , operation , args . slice ( 0 , - 1 ) ) ) ;
218212
219213 return orig . call ( this , ...args . slice ( 0 , - 1 ) , function ( err : Error , result : unknown ) {
220214 span ?. end ( ) ;
@@ -232,19 +226,19 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
232226 operation : Operation ,
233227 args : unknown [ ] ,
234228 sendDefaultPii : boolean | undefined = false ,
235- ) : SpanContext {
236- const data : { [ key : string ] : string } = {
229+ ) : StartSpanOptions {
230+ const attributes : SpanAttributes = {
237231 'db.system' : 'mongodb' ,
238232 'db.name' : collection . dbName ,
239233 'db.operation' : operation ,
240234 'db.mongodb.collection' : collection . collectionName ,
235+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : `${ collection . collectionName } .${ operation } ` ,
241236 } ;
242- const spanContext : SpanContext = {
237+ const spanContext : StartSpanOptions = {
243238 op : 'db' ,
244- // TODO v8: Use `${collection.collectionName}.${operation}`
245- origin : 'auto.db.mongo' ,
246239 name : operation ,
247- data,
240+ attributes,
241+ onlyIfParent : true ,
248242 } ;
249243
250244 // If the operation takes no arguments besides `options` and `callback`, or if argument
@@ -262,11 +256,11 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
262256 // Special case for `mapReduce`, as the only one accepting functions as arguments.
263257 if ( operation === 'mapReduce' ) {
264258 const [ map , reduce ] = args as { name ?: string } [ ] ;
265- data [ signature [ 0 ] ] = typeof map === 'string' ? map : map . name || '<anonymous>' ;
266- data [ signature [ 1 ] ] = typeof reduce === 'string' ? reduce : reduce . name || '<anonymous>' ;
259+ attributes [ signature [ 0 ] ] = typeof map === 'string' ? map : map . name || '<anonymous>' ;
260+ attributes [ signature [ 1 ] ] = typeof reduce === 'string' ? reduce : reduce . name || '<anonymous>' ;
267261 } else {
268262 for ( let i = 0 ; i < signature . length ; i ++ ) {
269- data [ `db.mongodb.${ signature [ i ] } ` ] = JSON . stringify ( args [ i ] ) ;
263+ attributes [ `db.mongodb.${ signature [ i ] } ` ] = JSON . stringify ( args [ i ] ) ;
270264 }
271265 }
272266 } catch ( _oO ) {
0 commit comments