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' ;
3
4
import { fill , isThenable , loadModule , logger } from '@sentry/utils' ;
4
5
5
6
import { DEBUG_BUILD } from '../../common/debug-build' ;
@@ -138,7 +139,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
138
139
/**
139
140
* @inheritDoc
140
141
*/
141
- public setupOnce ( _ : ( callback : EventProcessor ) => void , getCurrentHub : ( ) => Hub ) : void {
142
+ public setupOnce ( _ : ( callback : EventProcessor ) => void ) : void {
142
143
const pkg = this . loadDependency ( ) ;
143
144
144
145
if ( ! pkg ) {
@@ -147,42 +148,36 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
147
148
return ;
148
149
}
149
150
150
- this . _instrumentOperations ( pkg . Collection , this . _operations , getCurrentHub ) ;
151
+ this . _instrumentOperations ( pkg . Collection , this . _operations ) ;
151
152
}
152
153
153
154
/**
154
155
* Patches original collection methods
155
156
*/
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 ) ) ;
158
159
}
159
160
160
161
/**
161
162
* Patches original collection to utilize our tracing functionality
162
163
*/
163
- private _patchOperation ( collection : MongoCollection , operation : Operation , getCurrentHub : ( ) => Hub ) : void {
164
+ private _patchOperation ( collection : MongoCollection , operation : Operation ) : void {
164
165
if ( ! ( operation in collection . prototype ) ) return ;
165
166
166
167
const getSpanContext = this . _getSpanContextFromOperationArguments . bind ( this ) ;
167
168
168
169
fill ( collection . prototype , operation , function ( orig : ( ) => void | Promise < unknown > ) {
169
170
return function ( this : unknown , ...args : unknown [ ] ) {
170
171
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 ( ) ;
178
174
179
175
const sendDefaultPii = client ?. getOptions ( ) . sendDefaultPii ;
180
176
181
177
// Check if the operation was passed a callback. (mapReduce requires a different check, as
182
178
// its (non-callback) arguments can also be functions.)
183
179
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 ) ) ;
186
181
const maybePromiseOrCursor = orig . call ( this , ...args ) ;
187
182
188
183
if ( isThenable ( maybePromiseOrCursor ) ) {
@@ -213,8 +208,7 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
213
208
}
214
209
}
215
210
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 ) ) ) ;
218
212
219
213
return orig . call ( this , ...args . slice ( 0 , - 1 ) , function ( err : Error , result : unknown ) {
220
214
span ?. end ( ) ;
@@ -232,19 +226,19 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
232
226
operation : Operation ,
233
227
args : unknown [ ] ,
234
228
sendDefaultPii : boolean | undefined = false ,
235
- ) : SpanContext {
236
- const data : { [ key : string ] : string } = {
229
+ ) : StartSpanOptions {
230
+ const attributes : SpanAttributes = {
237
231
'db.system' : 'mongodb' ,
238
232
'db.name' : collection . dbName ,
239
233
'db.operation' : operation ,
240
234
'db.mongodb.collection' : collection . collectionName ,
235
+ [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : `${ collection . collectionName } .${ operation } ` ,
241
236
} ;
242
- const spanContext : SpanContext = {
237
+ const spanContext : StartSpanOptions = {
243
238
op : 'db' ,
244
- // TODO v8: Use `${collection.collectionName}.${operation}`
245
- origin : 'auto.db.mongo' ,
246
239
name : operation ,
247
- data,
240
+ attributes,
241
+ onlyIfParent : true ,
248
242
} ;
249
243
250
244
// If the operation takes no arguments besides `options` and `callback`, or if argument
@@ -262,11 +256,11 @@ export class Mongo implements LazyLoadedIntegration<MongoModule> {
262
256
// Special case for `mapReduce`, as the only one accepting functions as arguments.
263
257
if ( operation === 'mapReduce' ) {
264
258
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>' ;
267
261
} else {
268
262
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 ] ) ;
270
264
}
271
265
}
272
266
} catch ( _oO ) {
0 commit comments