(installer: PluginInstaller)
| 42 | } |
| 43 | |
| 44 | install(installer: PluginInstaller): void { |
| 45 | const plugin = this; |
| 46 | this.Collection = installer.require?.('mongodb/lib/collection') ?? require('mongodb/lib/collection'); |
| 47 | this.Cursor = installer.require?.('mongodb/lib/cursor') ?? require('mongodb/lib/cursor'); |
| 48 | this.Db = installer.require?.('mongodb/lib/db') ?? require('mongodb/lib/db'); |
| 49 | |
| 50 | const wrapCallbackWithCursorMaybe = (span: any, args: any[], idx: number): boolean => { |
| 51 | const callback = args.length > idx && typeof args[(idx = args.length - 1)] === 'function' ? args[idx] : null; |
| 52 | |
| 53 | if (!callback) return false; |
| 54 | |
| 55 | args[idx] = function (this: any) { |
| 56 | // arguments = [error: any, result: any] |
| 57 | span.mongodbInCall = false; // we do this because some operations may call callback immediately which would not create a new span for any operations in that callback (db.collection()) |
| 58 | |
| 59 | if (arguments[0]) span.error(arguments[0]); |
| 60 | |
| 61 | if (arguments[0] || !plugin.hookCursorMaybe(span, arguments[1])) span.stop(); |
| 62 | |
| 63 | return callback.apply(this, arguments); |
| 64 | }; |
| 65 | |
| 66 | return true; |
| 67 | }; |
| 68 | |
| 69 | const stringify = (params: any) => { |
| 70 | if (params === undefined) return ''; |
| 71 | else if (typeof params === 'function') return `${params}`; |
| 72 | |
| 73 | let str = JSON.stringify(params); |
| 74 | |
| 75 | if (str.length > agentConfig.mongoParametersMaxLength) |
| 76 | str = str.slice(0, agentConfig.mongoParametersMaxLength) + ' ...'; |
| 77 | |
| 78 | return str; |
| 79 | }; |
| 80 | |
| 81 | const collInsertFunc = function (this: any, operation: string, span: any, args: any[]): boolean { |
| 82 | // args = [doc(s), options, callback] |
| 83 | span.tag(Tag.dbStatement(`${this.s.namespace.collection}.${operation}()`)); |
| 84 | |
| 85 | if (agentConfig.mongoTraceParameters) span.tag(Tag.dbMongoParameters(stringify(args[0]))); |
| 86 | |
| 87 | return wrapCallbackWithCursorMaybe(span, args, 1); |
| 88 | }; |
| 89 | |
| 90 | const collDeleteFunc = function (this: any, operation: string, span: any, args: any[]): boolean { |
| 91 | // args = [filter, options, callback] |
| 92 | span.tag(Tag.dbStatement(`${this.s.namespace.collection}.${operation}(${stringify(args[0])})`)); |
| 93 | |
| 94 | return wrapCallbackWithCursorMaybe(span, args, 1); |
| 95 | }; |
| 96 | |
| 97 | const collUpdateFunc = function (this: any, operation: string, span: any, args: any[]): boolean { |
| 98 | // args = [filter, update, options, callback] |
| 99 | span.tag(Tag.dbStatement(`${this.s.namespace.collection}.${operation}(${stringify(args[0])})`)); |
| 100 | |
| 101 | if (agentConfig.mongoTraceParameters) span.tag(Tag.dbMongoParameters(stringify(args[1]))); |
nothing calls this directly
no test coverage detected