From fa1235e3c2beebb035b41d23eefd67eeb240ae26 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 7 Mar 2024 10:34:21 +0100 Subject: [PATCH 01/15] fix(instrumentation-mongodb): fix for changes in v6.4.0 --- .../package.json | 2 +- .../src/instrumentation.ts | 29 ++++++++++++++----- .../src/internal-types.ts | 12 ++++++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/package.json b/plugins/node/opentelemetry-instrumentation-mongodb/package.json index 184bf1b499..b57e545e34 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/package.json +++ b/plugins/node/opentelemetry-instrumentation-mongodb/package.json @@ -58,7 +58,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "3.6.11", + "mongodb": "^6.4.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.0.0", diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index 02af814bf2..485d192810 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -353,7 +353,12 @@ export class MongoDBInstrumentation extends InstrumentationBase { instrumentation.setPoolName(options); callback(err, conn); }; - return original.call(this, options, patchedCallback); + + const result = original.call(this, options, patchedCallback); + if (result && typeof result.then === 'function') { + result.then(() => instrumentation.setPoolName(options)); + } + return result; }; }; } @@ -493,7 +498,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { callback: any ) { const currentSpan = trace.getSpan(context.active()); - const resultHandler = callback; + const resultHandler = callback || (() => undefined); // from v6.4.0 commnad method does not have a callback param const commandType = Object.keys(cmd)[0]; if ( @@ -504,15 +509,15 @@ export class MongoDBInstrumentation extends InstrumentationBase { ) { return original.call(this, ns, cmd, options, callback); } + + let patchedCallback: Function; if (!currentSpan) { - const patchedCallback = instrumentation._patchEnd( + patchedCallback = instrumentation._patchEnd( undefined, resultHandler, this.id, commandType ); - - return original.call(this, ns, cmd, options, patchedCallback); } else { const span = instrumentation.tracer.startSpan( `mongodb.${commandType}`, @@ -527,15 +532,23 @@ export class MongoDBInstrumentation extends InstrumentationBase { cmd, commandType ); - const patchedCallback = instrumentation._patchEnd( + patchedCallback = instrumentation._patchEnd( span, resultHandler, this.id, commandType ); - - return original.call(this, ns, cmd, options, patchedCallback); } + + const result = original.call(this, ns, cmd, options, patchedCallback); + if (result && typeof result.then === 'function') { + // Call patched callback in each scenario with the proper params + result.then( + (res: any) => patchedCallback(null, res), + (err: any) => patchedCallback(err), + ); + } + return result; }; }; } diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts index 5cb4119de5..b0677f2f72 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts @@ -175,13 +175,17 @@ export type Document = { }; // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection.ts +// From version 6.4.0 the method does not expect a callback and returns a promise +// this signature compacts both method signatures so it's not necessay to do extra +// type assertions in the instrumentation code +// https://github.com/mongodb/node-mongodb-native/blob/v6.4.2/src/cmap/connection.ts export type V4Connection = { command( ns: any, cmd: Document, options: undefined | unknown, callback: any - ): void; + ): (void | Promise); }; // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection_pool.ts @@ -192,8 +196,12 @@ export type V4ConnectionPool = { }; // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connect.ts +// From version 6.4.0 the method does not expect a callback and returns a promise +// this signature compacts both method signatures so it's not necessay to do extra +// type assertions in the instrumentation code +// https://github.com/mongodb/node-mongodb-native/blob/v6.4.0/src/cmap/connect.ts export type V4Connect = { - connect: (options: any, callback: any) => void; + connect: (options: any, callback: any) => (void | Promise); }; // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/sessions.ts From 5093dc7864458d26718f32142e001e1eee12ffe1 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 7 Mar 2024 10:37:39 +0100 Subject: [PATCH 02/15] chore(instrumentation-mongodb): update package-lock.json --- package-lock.json | 364 +++++++++++++++++++++++++--------------------- 1 file changed, 200 insertions(+), 164 deletions(-) diff --git a/package-lock.json b/package-lock.json index 0b5e7c49ca..57ed41e0aa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6324,7 +6324,6 @@ "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.4.tgz", "integrity": "sha512-8zJ8N1x51xo9hwPh6AWnKdLGEC5N3lDa6kms1YHmFBoRhTpJR6HG8wWk0td1MVCu9cD4YBrvjZEtd5Obw0Fbnw==", "dev": true, - "optional": true, "dependencies": { "sparse-bitfield": "^3.0.3" } @@ -21727,8 +21726,7 @@ "version": "1.5.0", "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", - "dev": true, - "optional": true + "dev": true }, "node_modules/meow": { "version": "8.1.2", @@ -25326,18 +25324,6 @@ "node": ">=0.10" } }, - "node_modules/optional-require": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/optional-require/-/optional-require-1.1.8.tgz", - "integrity": "sha512-jq83qaUb0wNg9Krv1c5OQ+58EK+vHde6aBPzLvPPqJm89UQWsvSuFy9X/OSNJnFeSOKo7btE0n8Nl2+nE+z5nA==", - "dev": true, - "dependencies": { - "require-at": "^1.0.6" - }, - "engines": { - "node": ">=4" - } - }, "node_modules/optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", @@ -27600,15 +27586,6 @@ "node": ">=0.10" } }, - "node_modules/require-at": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/require-at/-/require-at-1.0.6.tgz", - "integrity": "sha512-7i1auJbMUrXEAZCOQ0VNJgmcT2VOKPRl2YGJwgpHpC9CE91Mv4/4UYIUm4chGJaI381ZDq1JUicFii64Hapd8g==", - "dev": true, - "engines": { - "node": ">=4" - } - }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -28302,19 +28279,6 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "devOptional": true }, - "node_modules/saslprep": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", - "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", - "dev": true, - "optional": true, - "dependencies": { - "sparse-bitfield": "^3.0.3" - }, - "engines": { - "node": ">=6" - } - }, "node_modules/sax": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", @@ -29171,7 +29135,6 @@ "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", "dev": true, - "optional": true, "dependencies": { "memory-pager": "^1.0.2" } @@ -35432,7 +35395,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "3.6.11", + "mongodb": "^6.4.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.0.0", @@ -35446,57 +35409,115 @@ "@opentelemetry/api": "^1.3.0" } }, - "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/bl": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz", - "integrity": "sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g==", + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/@types/whatwg-url": { + "version": "11.0.4", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.4.tgz", + "integrity": "sha512-lXCmTWSHJvf0TRSO58nm978b8HJ/EdsSsEKLd3ODHFjo+3VGAyyTp4v50nWvwtzBxSMQrVOK7tcuN0zGPLICMw==", "dev": true, "dependencies": { - "readable-stream": "^2.3.5", - "safe-buffer": "^5.1.1" + "@types/webidl-conversions": "*" + } + }, + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/agent-base": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", + "dev": true, + "optional": true, + "peer": true, + "dependencies": { + "debug": "4" + }, + "engines": { + "node": ">= 6.0.0" } }, "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/bson": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.6.tgz", - "integrity": "sha512-EvVNVeGo4tHxwi8L6bPj3y3itEvStdwvvlojVxxbyYfoaxJ6keLgrTuKdyfEAszFK+H3olzBuafE0yoh0D1gdg==", + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.4.0.tgz", + "integrity": "sha512-6/gSSEdbkuFlSb+ufj5jUSU4+wo8xQOwm2bDSqwmxiPE17JTpsP63eAwoN8iF8Oy4gJYj+PAL3zdRCTdaw5Y1g==", "dev": true, "engines": { - "node": ">=0.6.19" + "node": ">=16.20.1" } }, - "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/denque": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz", - "integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==", + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/gaxios": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-5.1.3.tgz", + "integrity": "sha512-95hVgBRgEIRQQQHIbnxBXeHbW4TqFk4ZDJW7wmVtvYar72FdhRIo1UGOLS2eRAKCPEdPBWu+M7+A33D9CdX9rA==", "dev": true, + "optional": true, + "peer": true, + "dependencies": { + "extend": "^3.0.2", + "https-proxy-agent": "^5.0.0", + "is-stream": "^2.0.0", + "node-fetch": "^2.6.9" + }, "engines": { - "node": ">=0.10" + "node": ">=12" + } + }, + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/gcp-metadata": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-5.3.0.tgz", + "integrity": "sha512-FNTkdNEnBdlqF2oatizolQqNANMrcqJt6AAYt99B3y1aLLC8Hc5IOBb+ZnnzllodEEf6xMBp6wRcBbc16fa65w==", + "dev": true, + "optional": true, + "peer": true, + "dependencies": { + "gaxios": "^5.0.0", + "json-bigint": "^1.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/https-proxy-agent": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz", + "integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==", + "dev": true, + "optional": true, + "peer": true, + "dependencies": { + "agent-base": "6", + "debug": "4" + }, + "engines": { + "node": ">= 6" } }, "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb": { - "version": "3.6.11", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.11.tgz", - "integrity": "sha512-4Y4lTFHDHZZdgMaHmojtNAlqkvddX2QQBEN0K//GzxhGwlI9tZ9R0vhbjr1Decw+TF7qK0ZLjQT292XgHRRQgw==", + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.4.0.tgz", + "integrity": "sha512-MdFHsyb1a/Ee0H3NmzWTSLqchacDV/APF0H6BNQvraWrOiIocys2EmTFZPgHxWhcfO94c1F34I9MACU7x0hHKA==", "dev": true, "dependencies": { - "bl": "^2.2.1", - "bson": "^1.1.4", - "denque": "^1.4.1", - "optional-require": "^1.0.3", - "safe-buffer": "^5.1.2" + "@mongodb-js/saslprep": "^1.1.0", + "bson": "^6.4.0", + "mongodb-connection-string-url": "^3.0.0" }, "engines": { - "node": ">=4" + "node": ">=16.20.1" }, - "optionalDependencies": { - "saslprep": "^1.0.0" + "peerDependencies": { + "@aws-sdk/credential-providers": "^3.188.0", + "@mongodb-js/zstd": "^1.1.0", + "gcp-metadata": "^5.2.0", + "kerberos": "^2.0.1", + "mongodb-client-encryption": ">=6.0.0 <7", + "snappy": "^7.2.2", + "socks": "^2.7.1" }, "peerDependenciesMeta": { - "aws4": { + "@aws-sdk/credential-providers": { + "optional": true + }, + "@mongodb-js/zstd": { "optional": true }, - "bson-ext": { + "gcp-metadata": { "optional": true }, "kerberos": { @@ -35505,42 +35526,47 @@ "mongodb-client-encryption": { "optional": true }, - "mongodb-extjson": { + "snappy": { "optional": true }, - "snappy": { + "socks": { "optional": true } } }, - "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/readable-stream": { - "version": "2.3.8", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", - "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb-connection-string-url": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.0.tgz", + "integrity": "sha512-t1Vf+m1I5hC2M5RJx/7AtxgABy1cZmIPQRMXw+gEIPn/cZNF3Oiy+l0UIypUwVB5trcWHq3crg2g3uAR9aAwsQ==", "dev": true, "dependencies": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.3", - "isarray": "~1.0.0", - "process-nextick-args": "~2.0.0", - "safe-buffer": "~5.1.1", - "string_decoder": "~1.1.1", - "util-deprecate": "~1.0.1" + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^13.0.0" } }, - "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/tr46": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", + "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "dev": true, + "dependencies": { + "punycode": "^2.3.0" + }, + "engines": { + "node": ">=14" + } }, - "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/string_decoder": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", - "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==", "dev": true, "dependencies": { - "safe-buffer": "~5.1.0" + "tr46": "^4.1.1", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=16" } }, "plugins/node/opentelemetry-instrumentation-mysql": { @@ -41589,7 +41615,6 @@ "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.4.tgz", "integrity": "sha512-8zJ8N1x51xo9hwPh6AWnKdLGEC5N3lDa6kms1YHmFBoRhTpJR6HG8wWk0td1MVCu9cD4YBrvjZEtd5Obw0Fbnw==", "dev": true, - "optional": true, "requires": { "sparse-bitfield": "^3.0.3" } @@ -43750,7 +43775,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "3.6.11", + "mongodb": "^6.4.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.0.0", @@ -43758,70 +43783,108 @@ "typescript": "4.4.4" }, "dependencies": { - "bl": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.1.tgz", - "integrity": "sha512-6Pesp1w0DEX1N550i/uGV/TqucVL4AM/pgThFSN/Qq9si1/DF9aIHs1BxD8V/QU0HoeHO6cQRTAuYnLPKq1e4g==", + "@types/whatwg-url": { + "version": "11.0.4", + "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.4.tgz", + "integrity": "sha512-lXCmTWSHJvf0TRSO58nm978b8HJ/EdsSsEKLd3ODHFjo+3VGAyyTp4v50nWvwtzBxSMQrVOK7tcuN0zGPLICMw==", "dev": true, "requires": { - "readable-stream": "^2.3.5", - "safe-buffer": "^5.1.1" + "@types/webidl-conversions": "*" + } + }, + "agent-base": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-6.0.2.tgz", + "integrity": "sha512-RZNwNclF7+MS/8bDg70amg32dyeZGZxiDuQmZxKLAlQjr3jGyLx+4Kkk58UO7D2QdgFIQCovuSuZESne6RG6XQ==", + "dev": true, + "optional": true, + "peer": true, + "requires": { + "debug": "4" } }, "bson": { - "version": "1.1.6", - "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.6.tgz", - "integrity": "sha512-EvVNVeGo4tHxwi8L6bPj3y3itEvStdwvvlojVxxbyYfoaxJ6keLgrTuKdyfEAszFK+H3olzBuafE0yoh0D1gdg==", + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/bson/-/bson-6.4.0.tgz", + "integrity": "sha512-6/gSSEdbkuFlSb+ufj5jUSU4+wo8xQOwm2bDSqwmxiPE17JTpsP63eAwoN8iF8Oy4gJYj+PAL3zdRCTdaw5Y1g==", "dev": true }, - "denque": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz", - "integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw==", - "dev": true + "gaxios": { + "version": "5.1.3", + "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-5.1.3.tgz", + "integrity": "sha512-95hVgBRgEIRQQQHIbnxBXeHbW4TqFk4ZDJW7wmVtvYar72FdhRIo1UGOLS2eRAKCPEdPBWu+M7+A33D9CdX9rA==", + "dev": true, + "optional": true, + "peer": true, + "requires": { + "extend": "^3.0.2", + "https-proxy-agent": "^5.0.0", + "is-stream": "^2.0.0", + "node-fetch": "^2.6.9" + } + }, + "gcp-metadata": { + "version": "5.3.0", + "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-5.3.0.tgz", + "integrity": "sha512-FNTkdNEnBdlqF2oatizolQqNANMrcqJt6AAYt99B3y1aLLC8Hc5IOBb+ZnnzllodEEf6xMBp6wRcBbc16fa65w==", + "dev": true, + "optional": true, + "peer": true, + "requires": { + "gaxios": "^5.0.0", + "json-bigint": "^1.0.0" + } + }, + "https-proxy-agent": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-5.0.1.tgz", + "integrity": "sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==", + "dev": true, + "optional": true, + "peer": true, + "requires": { + "agent-base": "6", + "debug": "4" + } }, "mongodb": { - "version": "3.6.11", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.11.tgz", - "integrity": "sha512-4Y4lTFHDHZZdgMaHmojtNAlqkvddX2QQBEN0K//GzxhGwlI9tZ9R0vhbjr1Decw+TF7qK0ZLjQT292XgHRRQgw==", + "version": "6.4.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.4.0.tgz", + "integrity": "sha512-MdFHsyb1a/Ee0H3NmzWTSLqchacDV/APF0H6BNQvraWrOiIocys2EmTFZPgHxWhcfO94c1F34I9MACU7x0hHKA==", "dev": true, "requires": { - "bl": "^2.2.1", - "bson": "^1.1.4", - "denque": "^1.4.1", - "optional-require": "^1.0.3", - "safe-buffer": "^5.1.2", - "saslprep": "^1.0.0" + "@mongodb-js/saslprep": "^1.1.0", + "bson": "^6.4.0", + "mongodb-connection-string-url": "^3.0.0" } }, - "readable-stream": { - "version": "2.3.8", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", - "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "mongodb-connection-string-url": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.0.tgz", + "integrity": "sha512-t1Vf+m1I5hC2M5RJx/7AtxgABy1cZmIPQRMXw+gEIPn/cZNF3Oiy+l0UIypUwVB5trcWHq3crg2g3uAR9aAwsQ==", "dev": true, "requires": { - "core-util-is": "~1.0.0", - "inherits": "~2.0.3", - "isarray": "~1.0.0", - "process-nextick-args": "~2.0.0", - "safe-buffer": "~5.1.1", - "string_decoder": "~1.1.1", - "util-deprecate": "~1.0.1" + "@types/whatwg-url": "^11.0.2", + "whatwg-url": "^13.0.0" } }, - "safe-buffer": { - "version": "5.1.2", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", - "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", - "dev": true + "tr46": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz", + "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==", + "dev": true, + "requires": { + "punycode": "^2.3.0" + } }, - "string_decoder": { - "version": "1.1.1", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", - "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "whatwg-url": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-13.0.0.tgz", + "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==", "dev": true, "requires": { - "safe-buffer": "~5.1.0" + "tr46": "^4.1.1", + "webidl-conversions": "^7.0.0" } } } @@ -55865,8 +55928,7 @@ "version": "1.5.0", "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", - "dev": true, - "optional": true + "dev": true }, "meow": { "version": "8.1.2", @@ -58779,15 +58841,6 @@ "resolved": "https://registry.npmjs.org/opentracing/-/opentracing-0.14.7.tgz", "integrity": "sha512-vz9iS7MJ5+Bp1URw8Khvdyw1H/hGvzHWlKQ7eRrQojSCDL1/SrWfrY9QebLw97n2deyRtzHRC3MkQfVNUCo91Q==" }, - "optional-require": { - "version": "1.1.8", - "resolved": "https://registry.npmjs.org/optional-require/-/optional-require-1.1.8.tgz", - "integrity": "sha512-jq83qaUb0wNg9Krv1c5OQ+58EK+vHde6aBPzLvPPqJm89UQWsvSuFy9X/OSNJnFeSOKo7btE0n8Nl2+nE+z5nA==", - "dev": true, - "requires": { - "require-at": "^1.0.6" - } - }, "optionator": { "version": "0.9.3", "resolved": "https://registry.npmjs.org/optionator/-/optionator-0.9.3.tgz", @@ -60524,12 +60577,6 @@ "integrity": "sha512-PV0dzCYDNfRi1jCDbJzpW7jNNDRuCOG/jI5ctQcGKt/clZD+YcPS3yIlWuTJMmESC8aevCFmWJy5wjAFgNqN6w==", "dev": true }, - "require-at": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/require-at/-/require-at-1.0.6.tgz", - "integrity": "sha512-7i1auJbMUrXEAZCOQ0VNJgmcT2VOKPRl2YGJwgpHpC9CE91Mv4/4UYIUm4chGJaI381ZDq1JUicFii64Hapd8g==", - "dev": true - }, "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -61041,16 +61088,6 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "devOptional": true }, - "saslprep": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", - "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", - "dev": true, - "optional": true, - "requires": { - "sparse-bitfield": "^3.0.3" - } - }, "sax": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.1.tgz", @@ -61766,7 +61803,6 @@ "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==", "dev": true, - "optional": true, "requires": { "memory-pager": "^1.0.2" } From b00181b666abe7515515fe8fed90acb71fa06686 Mon Sep 17 00:00:00 2001 From: David Luna Date: Tue, 12 Mar 2024 14:48:12 +0100 Subject: [PATCH 03/15] fix(instrumentation-mongodb): split command patch --- .../package.json | 2 +- .../src/instrumentation.ts | 144 +++++++++++++----- .../src/internal-types.ts | 33 ++-- .../test/mongodb-v3.test.ts | 5 +- .../test/mongodb-v4.test.ts | 4 +- 5 files changed, 134 insertions(+), 54 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/package.json b/plugins/node/opentelemetry-instrumentation-mongodb/package.json index 58be4735a3..6194c51bae 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/package.json +++ b/plugins/node/opentelemetry-instrumentation-mongodb/package.json @@ -7,7 +7,7 @@ "repository": "open-telemetry/opentelemetry-js-contrib", "scripts": { "docker:start": "docker run -e MONGODB_DB=opentelemetry-tests -e MONGODB_PORT=27017 -e MONGODB_HOST=127.0.0.1 -p 27017:27017 --rm mongo", - "test": "npm run test-v3", + "test": "npm run test-v5-v6", "test-v3": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/**/mongodb-v3.test.ts'", "test-v4": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongodb-v4-v5-v6.metrics.test.ts' 'test/**/mongodb-v4.test.ts'", "test-v5-v6": "nyc ts-mocha -p tsconfig.json --require '@opentelemetry/contrib-test-utils' 'test/mongodb-v4-v5-v6.metrics.test.ts' 'test/**/mongodb-v5-v6.test.ts'", diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index eaf8ee65d5..85aba5bb41 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -74,7 +74,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { } = this._getV3ConnectionPatches(); const { v4PatchConnect, v4UnpatchConnect } = this._getV4ConnectPatches(); - const { v4PatchConnection, v4UnpatchConnection } = + const { v4PatchConnectionCallback, v4PatchConnectionPromise, v4UnpatchConnection } = this._getV4ConnectionPatches(); const { v4PatchConnectionPool, v4UnpatchConnectionPool } = this._getV4ConnectionPoolPatches(); @@ -97,14 +97,20 @@ export class MongoDBInstrumentation extends InstrumentationBase { ), new InstrumentationNodeModuleDefinition( 'mongodb', - ['4.*', '5.*', '>=6 <6.4'], + ['4.*', '5.*', '6.*'], undefined, undefined, [ new InstrumentationNodeModuleFile( 'mongodb/lib/cmap/connection.js', ['4.*', '5.*', '>=6 <6.4'], - v4PatchConnection, + v4PatchConnectionCallback, + v4UnpatchConnection + ), + new InstrumentationNodeModuleFile( + 'mongodb/lib/cmap/connection.js', + ['>=6.4'], + v4PatchConnectionPromise, v4UnpatchConnection ), new InstrumentationNodeModuleFile( @@ -115,13 +121,13 @@ export class MongoDBInstrumentation extends InstrumentationBase { ), new InstrumentationNodeModuleFile( 'mongodb/lib/cmap/connect.js', - ['4.*', '5.*', '>=6 <6.4'], + ['4.*', '5.*', '6.*'], v4PatchConnect, v4UnpatchConnect ), new InstrumentationNodeModuleFile( 'mongodb/lib/sessions.js', - ['4.*', '5.*', '>=6 <6.4'], + ['4.*', '5.*', '6.*'], v4PatchSessions, v4UnpatchSessions ), @@ -339,12 +345,23 @@ export class MongoDBInstrumentation extends InstrumentationBase { private _getV4ConnectCommand() { const instrumentation = this; - return (original: V4Connect['connect']) => { + return (original: V4Connect['connectCallback'] | V4Connect['connectPromise']) => { return function patchedConnect( this: unknown, options: any, callback: any ) { + // from v6.4 `connect` method only accepts an options param and returns a promise + // with the connection + if (original.length === 1) { + const result = (original as V4Connect['connectPromise']).call(this, options); + if (result && typeof result.then === 'function') { + result.then(() => instrumentation.setPoolName(options)); + } + return result; + } + + // Earlier versions expects a callback param and return void const patchedCallback = function (err: any, conn: any) { if (err || !conn) { callback(err, conn); @@ -354,11 +371,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { callback(err, conn); }; - const result = original.call(this, options, patchedCallback); - if (result && typeof result.then === 'function') { - result.then(() => instrumentation.setPoolName(options)); - } - return result; + return (original as V4Connect['connectCallback']).call(this, options, patchedCallback); }; }; } @@ -366,7 +379,21 @@ export class MongoDBInstrumentation extends InstrumentationBase { // eslint-disable-next-line @typescript-eslint/no-unused-vars private _getV4ConnectionPatches() { return { - v4PatchConnection: (moduleExports: any, moduleVersion?: string) => { + v4PatchConnectionCallback: (moduleExports: any, moduleVersion?: string) => { + diag.debug(`Applying patch for mongodb@${moduleVersion}`); + // patch insert operation + if (isWrapped(moduleExports.Connection.prototype.command)) { + this._unwrap(moduleExports.Connection.prototype, 'command'); + } + + this._wrap( + moduleExports.Connection.prototype, + 'command', + this._getV4PatchCommandCallback() + ); + return moduleExports; + }, + v4PatchConnectionPromise: (moduleExports: any, moduleVersion?: string) => { diag.debug(`Applying patch for mongodb@${moduleVersion}`); // patch insert operation if (isWrapped(moduleExports.Connection.prototype.command)) { @@ -376,7 +403,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { this._wrap( moduleExports.Connection.prototype, 'command', - this._getV4PatchCommand() + this._getV4PatchCommandPromise() ); return moduleExports; }, @@ -487,9 +514,9 @@ export class MongoDBInstrumentation extends InstrumentationBase { } /** Creates spans for command operation */ - private _getV4PatchCommand() { + private _getV4PatchCommandCallback() { const instrumentation = this; - return (original: V4Connection['command']) => { + return (original: V4Connection['commandCallback']) => { return function patchedV4ServerCommand( this: any, ns: any, @@ -498,7 +525,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { callback: any ) { const currentSpan = trace.getSpan(context.active()); - const resultHandler = callback || (() => undefined); // from v6.4.0 commnad method does not have a callback param + const resultHandler = callback; const commandType = Object.keys(cmd)[0]; if ( @@ -510,16 +537,9 @@ export class MongoDBInstrumentation extends InstrumentationBase { return original.call(this, ns, cmd, options, callback); } - let patchedCallback: Function; - if (!currentSpan) { - patchedCallback = instrumentation._patchEnd( - undefined, - resultHandler, - this.id, - commandType - ); - } else { - const span = instrumentation.tracer.startSpan( + let span = undefined + if (currentSpan) { + span = instrumentation.tracer.startSpan( `mongodb.${commandType}`, { kind: SpanKind.CLIENT, @@ -532,23 +552,71 @@ export class MongoDBInstrumentation extends InstrumentationBase { cmd, commandType ); - patchedCallback = instrumentation._patchEnd( + } + const patchedCallback = instrumentation._patchEnd( + span, + resultHandler, + this.id, + commandType + ); + + return original.call(this, ns, cmd, options, patchedCallback); + }; + }; + } + + private _getV4PatchCommandPromise() { + const instrumentation = this; + return (original: V4Connection['commandPromise']) => { + return function patchedV4ServerCommand( + this: any, + ns: any, + cmd: any, + options: undefined | unknown, + ) { + const currentSpan = trace.getSpan(context.active()); + const commandType = Object.keys(cmd)[0]; + const resultHandler = () => undefined; + + if ( + typeof cmd !== 'object' || + cmd.ismaster || + cmd.hello + ) { + return original.call(this, ns, cmd, options); + } + + let span = undefined; + if (currentSpan) { + span = instrumentation.tracer.startSpan( + `mongodb.${commandType}`, + { + kind: SpanKind.CLIENT, + } + ); + instrumentation._populateV4Attributes( span, - resultHandler, - this.id, + this, + ns, + cmd, commandType ); } - const result = original.call(this, ns, cmd, options, patchedCallback); - if (result && typeof result.then === 'function') { - // Call patched callback in each scenario with the proper params - result.then( - (res: any) => patchedCallback(null, res), - (err: any) => patchedCallback(err), - ); - } - return result; + const patchedCallback = instrumentation._patchEnd( + span, + resultHandler, + this.id, + commandType + ); + + const result = original.call(this, ns, cmd, options); + result.then( + (res: any) => patchedCallback(null, res), + (err: any) => patchedCallback(err), + ); + + return result; }; }; } diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts index b0677f2f72..a0aacead26 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts @@ -174,18 +174,24 @@ export type Document = { [key: string]: any; }; -// https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection.ts -// From version 6.4.0 the method does not expect a callback and returns a promise -// this signature compacts both method signatures so it's not necessay to do extra -// type assertions in the instrumentation code -// https://github.com/mongodb/node-mongodb-native/blob/v6.4.2/src/cmap/connection.ts + export type V4Connection = { - command( + command: Function; + // From version 6.4.0 the method does not expect a callback and returns a promise + // https://github.com/mongodb/node-mongodb-native/blob/v6.4.2/src/cmap/connection.ts + commandPromise( + ns: any, + cmd: Document, + options: undefined | unknown, + ): Promise; + // Earlier versions expect a callback param and return void + // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection.ts + commandCallback( ns: any, cmd: Document, options: undefined | unknown, callback: any - ): (void | Promise); + ): void; }; // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection_pool.ts @@ -195,13 +201,14 @@ export type V4ConnectionPool = { checkOut: (callback: (error: any, connection: any) => void) => void; }; -// https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connect.ts -// From version 6.4.0 the method does not expect a callback and returns a promise -// this signature compacts both method signatures so it's not necessay to do extra -// type assertions in the instrumentation code -// https://github.com/mongodb/node-mongodb-native/blob/v6.4.0/src/cmap/connect.ts export type V4Connect = { - connect: (options: any, callback: any) => (void | Promise); + connect: Function; + // From version 6.4.0 the method does not expect a callback and returns a promise + // https://github.com/mongodb/node-mongodb-native/blob/v6.4.0/src/cmap/connect.ts + connectPromise: (options: any) => Promise; + // Earlier versions expect a callback param and return void + // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connect.ts + connectCallback: (options: any, callback: any) => void; }; // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/sessions.ts diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index 938c00875a..84346aa087 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -83,6 +83,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { } // Non traced insertion of basic data to perform tests const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; + // @ts-expect-error -- v5 removed callback support collection.insertMany(insertData, (err: any, result: any) => { resetMemoryExporter(); done(); @@ -91,7 +92,8 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { afterEach(done => { if (shouldTest) { - return collection.deleteMany({}, done); + // @ts-expect-error -- v5 removed callback support + collection.deleteMany({}, done); } done(); }); @@ -571,6 +573,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { it('should generate correct span attributes', done => { const span = trace.getTracer('default').startSpan('findRootSpan'); context.with(trace.setSpan(context.active(), span), () => { + // @ts-expect-error -- v5 removed callback support collection.find({ a: 1 }).toArray((err, results) => { span.end(); const [mongoSpan] = getTestSpans(); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index 7fb8dc4fca..f4e7659b59 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -84,6 +84,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { } // Non traced insertion of basic data to perform tests const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; + // @ts-expect-error -- v5 removed callback support collection.insertMany(insertData, (err: any, result: any) => { resetMemoryExporter(); done(); @@ -92,7 +93,8 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { afterEach(done => { if (shouldTest) { - return collection.deleteMany({}, done); + // @ts-expect-error -- v5 removed callback support + collection.deleteMany({}, done); } done(); }); From 5ab8eb2d88d40aa34aae38420d07bac7bc210f77 Mon Sep 17 00:00:00 2001 From: David Luna Date: Tue, 12 Mar 2024 15:18:26 +0100 Subject: [PATCH 04/15] fix(instrumentation-mongodb): fix tests for v3 & v4 --- .../test/mongodb-v3.test.ts | 3 ++- .../test/mongodb-v4.test.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index 84346aa087..bb8898dc8e 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -94,8 +94,9 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { if (shouldTest) { // @ts-expect-error -- v5 removed callback support collection.deleteMany({}, done); + } else { + done(); } - done(); }); after(() => { diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index f4e7659b59..612dcf2eeb 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -95,8 +95,9 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { if (shouldTest) { // @ts-expect-error -- v5 removed callback support collection.deleteMany({}, done); + } else { + done(); } - done(); }); after(async () => { From 9a81b73dadd47c70fe46bcceaf4fb9b28016d52f Mon Sep 17 00:00:00 2001 From: David Luna Date: Tue, 12 Mar 2024 15:49:01 +0100 Subject: [PATCH 05/15] fix(instrumentation-mongodb): fix lint issues --- .../src/instrumentation.ts | 62 +++++++++++-------- .../src/internal-types.ts | 3 +- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index 85aba5bb41..9a592ea899 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -74,8 +74,11 @@ export class MongoDBInstrumentation extends InstrumentationBase { } = this._getV3ConnectionPatches(); const { v4PatchConnect, v4UnpatchConnect } = this._getV4ConnectPatches(); - const { v4PatchConnectionCallback, v4PatchConnectionPromise, v4UnpatchConnection } = - this._getV4ConnectionPatches(); + const { + v4PatchConnectionCallback, + v4PatchConnectionPromise, + v4UnpatchConnection, + } = this._getV4ConnectionPatches(); const { v4PatchConnectionPool, v4UnpatchConnectionPool } = this._getV4ConnectionPoolPatches(); const { v4PatchSessions, v4UnpatchSessions } = this._getV4SessionsPatches(); @@ -345,7 +348,9 @@ export class MongoDBInstrumentation extends InstrumentationBase { private _getV4ConnectCommand() { const instrumentation = this; - return (original: V4Connect['connectCallback'] | V4Connect['connectPromise']) => { + return ( + original: V4Connect['connectCallback'] | V4Connect['connectPromise'] + ) => { return function patchedConnect( this: unknown, options: any, @@ -354,7 +359,10 @@ export class MongoDBInstrumentation extends InstrumentationBase { // from v6.4 `connect` method only accepts an options param and returns a promise // with the connection if (original.length === 1) { - const result = (original as V4Connect['connectPromise']).call(this, options); + const result = (original as V4Connect['connectPromise']).call( + this, + options + ); if (result && typeof result.then === 'function') { result.then(() => instrumentation.setPoolName(options)); } @@ -371,7 +379,11 @@ export class MongoDBInstrumentation extends InstrumentationBase { callback(err, conn); }; - return (original as V4Connect['connectCallback']).call(this, options, patchedCallback); + return (original as V4Connect['connectCallback']).call( + this, + options, + patchedCallback + ); }; }; } @@ -379,7 +391,10 @@ export class MongoDBInstrumentation extends InstrumentationBase { // eslint-disable-next-line @typescript-eslint/no-unused-vars private _getV4ConnectionPatches() { return { - v4PatchConnectionCallback: (moduleExports: any, moduleVersion?: string) => { + v4PatchConnectionCallback: ( + moduleExports: any, + moduleVersion?: string + ) => { diag.debug(`Applying patch for mongodb@${moduleVersion}`); // patch insert operation if (isWrapped(moduleExports.Connection.prototype.command)) { @@ -393,7 +408,10 @@ export class MongoDBInstrumentation extends InstrumentationBase { ); return moduleExports; }, - v4PatchConnectionPromise: (moduleExports: any, moduleVersion?: string) => { + v4PatchConnectionPromise: ( + moduleExports: any, + moduleVersion?: string + ) => { diag.debug(`Applying patch for mongodb@${moduleVersion}`); // patch insert operation if (isWrapped(moduleExports.Connection.prototype.command)) { @@ -537,14 +555,11 @@ export class MongoDBInstrumentation extends InstrumentationBase { return original.call(this, ns, cmd, options, callback); } - let span = undefined + let span = undefined; if (currentSpan) { - span = instrumentation.tracer.startSpan( - `mongodb.${commandType}`, - { - kind: SpanKind.CLIENT, - } - ); + span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, { + kind: SpanKind.CLIENT, + }); instrumentation._populateV4Attributes( span, this, @@ -572,28 +587,21 @@ export class MongoDBInstrumentation extends InstrumentationBase { this: any, ns: any, cmd: any, - options: undefined | unknown, + options: undefined | unknown ) { const currentSpan = trace.getSpan(context.active()); const commandType = Object.keys(cmd)[0]; const resultHandler = () => undefined; - if ( - typeof cmd !== 'object' || - cmd.ismaster || - cmd.hello - ) { + if (typeof cmd !== 'object' || cmd.ismaster || cmd.hello) { return original.call(this, ns, cmd, options); } let span = undefined; if (currentSpan) { - span = instrumentation.tracer.startSpan( - `mongodb.${commandType}`, - { - kind: SpanKind.CLIENT, - } - ); + span = instrumentation.tracer.startSpan(`mongodb.${commandType}`, { + kind: SpanKind.CLIENT, + }); instrumentation._populateV4Attributes( span, this, @@ -613,7 +621,7 @@ export class MongoDBInstrumentation extends InstrumentationBase { const result = original.call(this, ns, cmd, options); result.then( (res: any) => patchedCallback(null, res), - (err: any) => patchedCallback(err), + (err: any) => patchedCallback(err) ); return result; diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts index a0aacead26..38b357077b 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/internal-types.ts @@ -174,7 +174,6 @@ export type Document = { [key: string]: any; }; - export type V4Connection = { command: Function; // From version 6.4.0 the method does not expect a callback and returns a promise @@ -182,7 +181,7 @@ export type V4Connection = { commandPromise( ns: any, cmd: Document, - options: undefined | unknown, + options: undefined | unknown ): Promise; // Earlier versions expect a callback param and return void // https://github.com/mongodb/node-mongodb-native/blob/v4.2.2/src/cmap/connection.ts From 3c9c71a0ff6e1ef057331420bde7bd2bb8c049da Mon Sep 17 00:00:00 2001 From: David Luna Date: Tue, 12 Mar 2024 16:38:56 +0100 Subject: [PATCH 06/15] fix(instrumentation-mongodb): fix lint issues --- .../src/instrumentation.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts index 9a592ea899..0a1dad9af9 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/src/instrumentation.ts @@ -364,7 +364,11 @@ export class MongoDBInstrumentation extends InstrumentationBase { options ); if (result && typeof result.then === 'function') { - result.then(() => instrumentation.setPoolName(options)); + result.then( + () => instrumentation.setPoolName(options), + // this handler is set to pass the lint rules + () => undefined + ); } return result; } From 30475170b5f8e1ba5ab6e18028b3cb34777b32e2 Mon Sep 17 00:00:00 2001 From: David Luna Date: Tue, 12 Mar 2024 22:41:56 +0100 Subject: [PATCH 07/15] fix(instrumentation-mongodb): skip v4.17.1 in TAV tests --- package-lock.json | 32 +++++++++---------- .../.tav.yml | 26 +++++++++------ .../package.json | 2 +- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/package-lock.json b/package-lock.json index f9d0e466cc..96992c084e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6716,9 +6716,9 @@ "dev": true }, "node_modules/@mongodb-js/saslprep": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.4.tgz", - "integrity": "sha512-8zJ8N1x51xo9hwPh6AWnKdLGEC5N3lDa6kms1YHmFBoRhTpJR6HG8wWk0td1MVCu9cD4YBrvjZEtd5Obw0Fbnw==", + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.5.tgz", + "integrity": "sha512-XLNOMH66KhJzUJNwT/qlMnS4WsNDWD5ASdyaSH3EtK+F4r/CFGa3jT4GNi4mfOitGvWXtdLgQJkQjxSVrio+jA==", "dev": true, "dependencies": { "sparse-bitfield": "^3.0.3" @@ -37982,7 +37982,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "^6.4.0", + "mongodb": "^6.5.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.1.0", @@ -38076,12 +38076,12 @@ } }, "plugins/node/opentelemetry-instrumentation-mongodb/node_modules/mongodb": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.4.0.tgz", - "integrity": "sha512-MdFHsyb1a/Ee0H3NmzWTSLqchacDV/APF0H6BNQvraWrOiIocys2EmTFZPgHxWhcfO94c1F34I9MACU7x0hHKA==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.5.0.tgz", + "integrity": "sha512-Fozq68InT+JKABGLqctgtb8P56pRrJFkbhW0ux+x1mdHeyinor8oNzJqwLjV/t5X5nJGfTlluxfyMnOXNggIUA==", "dev": true, "dependencies": { - "@mongodb-js/saslprep": "^1.1.0", + "@mongodb-js/saslprep": "^1.1.5", "bson": "^6.4.0", "mongodb-connection-string-url": "^3.0.0" }, @@ -44373,9 +44373,9 @@ "dev": true }, "@mongodb-js/saslprep": { - "version": "1.1.4", - "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.4.tgz", - "integrity": "sha512-8zJ8N1x51xo9hwPh6AWnKdLGEC5N3lDa6kms1YHmFBoRhTpJR6HG8wWk0td1MVCu9cD4YBrvjZEtd5Obw0Fbnw==", + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.5.tgz", + "integrity": "sha512-XLNOMH66KhJzUJNwT/qlMnS4WsNDWD5ASdyaSH3EtK+F4r/CFGa3jT4GNi4mfOitGvWXtdLgQJkQjxSVrio+jA==", "dev": true, "requires": { "sparse-bitfield": "^3.0.3" @@ -46527,7 +46527,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "^6.4.0", + "mongodb": "^6.5.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.1.0", @@ -46600,12 +46600,12 @@ } }, "mongodb": { - "version": "6.4.0", - "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.4.0.tgz", - "integrity": "sha512-MdFHsyb1a/Ee0H3NmzWTSLqchacDV/APF0H6BNQvraWrOiIocys2EmTFZPgHxWhcfO94c1F34I9MACU7x0hHKA==", + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.5.0.tgz", + "integrity": "sha512-Fozq68InT+JKABGLqctgtb8P56pRrJFkbhW0ux+x1mdHeyinor8oNzJqwLjV/t5X5nJGfTlluxfyMnOXNggIUA==", "dev": true, "requires": { - "@mongodb-js/saslprep": "^1.1.0", + "@mongodb-js/saslprep": "^1.1.5", "bson": "^6.4.0", "mongodb-connection-string-url": "^3.0.0" } diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml b/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml index 403959c458..1a803f026a 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml +++ b/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml @@ -1,11 +1,17 @@ mongodb: - jobs: - - versions: ">=3.3 <4" - commands: npm run test-v3 - - versions: ">=4 <5" - commands: npm run test-v4 - - versions: ">=5 <6" - commands: npm run test-v5-v6 - - versions: ">=6 <6.4" - node: '>=15.0.0' - commands: npm run test-v5-v6 + - versions: ">=3.3 <4" + commands: npm run test-v3 + - versions: + include: ">=4 <5" + # This version has been excluded because `mongoose` instrumentation has it + # pined as a transitive dependency. + # When `test-all-versions` tries to install it npm actually does nothing and + # we end up running tests for the wrong version (v6.5.0) + # Ref: https://github.com/open-telemetry/opentelemetry-js-contrib/pull/2001#issuecomment-1992202942 + exclude: "4.17.1" + commands: npm run test-v4 + - versions: ">=5 <6" + commands: npm run test-v5-v6 + - versions: ">=6 <6.4" + node: '>=15.0.0' + commands: npm run test-v5-v6 diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/package.json b/plugins/node/opentelemetry-instrumentation-mongodb/package.json index 6194c51bae..51b491ef48 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/package.json +++ b/plugins/node/opentelemetry-instrumentation-mongodb/package.json @@ -58,7 +58,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "^6.4.0", + "mongodb": "^6.5.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.1.0", From eac3c2915f8d086ac26d79a9f190b1c8895d1392 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 14 Mar 2024 15:42:31 +0100 Subject: [PATCH 08/15] fix(instrumentation-mongodb): lazy load mongodb on tests --- .../test/mongodb-v3.test.ts | 8 +++--- .../test/mongodb-v4-v5-v6.metrics.test.ts | 27 ++++++++++++++----- .../test/mongodb-v4.test.ts | 8 +++--- .../test/mongodb-v5-v6.test.ts | 8 +++--- .../test/utils.ts | 21 ++++++++++----- 5 files changed, 47 insertions(+), 25 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index bb8898dc8e..d96b72d717 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -31,7 +31,7 @@ const instrumentation = registerInstrumentationTesting( new MongoDBInstrumentation() ); -import * as mongodb from 'mongodb'; +import type {MongoClient, Collection} from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; @@ -54,8 +54,8 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests'; const COLLECTION_NAME = 'test'; - let client: mongodb.MongoClient; - let collection: mongodb.Collection; + let client: MongoClient; + let collection: Collection; before(done => { shouldTest = true; @@ -67,7 +67,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { }) .catch((err: Error) => { console.log( - 'Skipping test-mongodb. Could not connect. Run MongoDB to test' + 'Skipping test-mongodb. ' + err.message ); shouldTest = false; done(); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts index eb8d3b3946..06e3e4d198 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts @@ -44,7 +44,7 @@ const instrumentation = registerInstrumentationTesting( ); import { accessCollection, DEFAULT_MONGO_HOST } from './utils'; -import * as mongodb from 'mongodb'; +import type { MongoClient } from 'mongodb'; import * as assert from 'assert'; async function waitForNumberOfExports( @@ -80,13 +80,31 @@ describe('MongoDBInstrumentation-Metrics', () => { const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-metrics'; const COLLECTION_NAME = 'test-metrics'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; - let client: mongodb.MongoClient; + + let client: MongoClient; + let collection: Collection; - before(() => { + before(done => { otelTestingMeterProvider.addMetricReader(metricReader); instrumentation?.setMeterProvider(otelTestingMeterProvider); + + shouldTest = true; + accessCollection(URL, DB_NAME, COLLECTION_NAME) + .then(result => { + client = result.client; + collection = result.collection; + done(); + }) + .catch((err: Error) => { + console.log( + 'Skipping test-mongodb. ' + err.message + ); + shouldTest = false; + done(); + }); }); + beforeEach(function mongoBeforeEach(done) { // Skipping all tests in beforeEach() is a workaround. Mocha does not work // properly when skipping tests in before() on nested describe() calls. @@ -100,9 +118,6 @@ describe('MongoDBInstrumentation-Metrics', () => { }); it('Should add connection usage metrics', async () => { - const result = await accessCollection(URL, DB_NAME, COLLECTION_NAME); - client = result.client; - const collection = result.collection; const insertData = [{ a: 1 }, { a: 2 }, { a: 3 }]; await collection.insertMany(insertData); await collection.deleteMany({}); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index 612dcf2eeb..82ec9c3d95 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -33,7 +33,7 @@ const instrumentation = registerInstrumentationTesting( new MongoDBInstrumentation() ); -import * as mongodb from 'mongodb'; +import type {MongoClient, Collection} from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; @@ -56,8 +56,8 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { const COLLECTION_NAME = 'test-traces'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; - let client: mongodb.MongoClient; - let collection: mongodb.Collection; + let client: MongoClient; + let collection: Collection; before(done => { accessCollection(URL, DB_NAME, COLLECTION_NAME) @@ -68,7 +68,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { }) .catch((err: Error) => { console.log( - 'Skipping test-mongodb. Could not connect. Run MongoDB to test' + 'Skipping test-mongodb. ' + err.message ); shouldTest = false; done(); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts index b264a44efe..dd74a84137 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts @@ -42,7 +42,7 @@ let instrumentation: MongoDBInstrumentation; } } -import * as mongodb from 'mongodb'; +import type {MongoClient, Collection} from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; @@ -65,8 +65,8 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { const COLLECTION_NAME = 'test-traces'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; - let client: mongodb.MongoClient; - let collection: mongodb.Collection; + let client: MongoClient; + let collection: Collection; before(done => { accessCollection(URL, DB_NAME, COLLECTION_NAME) @@ -77,7 +77,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { }) .catch((err: Error) => { console.log( - 'Skipping test-mongodb. Could not connect. Run MongoDB to test' + 'Skipping test-mongodb. ' + err.message ); shouldTest = false; done(); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts index 02b26705f6..dc8fb304ad 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts @@ -18,13 +18,13 @@ import { SpanKind, SpanStatusCode } from '@opentelemetry/api'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { ReadableSpan } from '@opentelemetry/sdk-trace-base'; import * as assert from 'assert'; -import * as mongodb from 'mongodb'; +import type { MongoClient, MongoClientOptions, Collection } from 'mongodb'; export const DEFAULT_MONGO_HOST = '127.0.0.1'; export interface MongoDBAccess { - client: mongodb.MongoClient; - collection: mongodb.Collection; + client: MongoClient; + collection: Collection; } /** @@ -38,19 +38,26 @@ export function accessCollection( url: string, dbName: string, collectionName: string, - options: mongodb.MongoClientOptions = {} + options: MongoClientOptions = {} ): Promise { return new Promise((resolve, reject) => { + let mongodb; + try { + mongodb = require('mongodb'); + } catch (err: any) { + reject(new Error('Could not load mongodb. ' + err.message)) + return; + } mongodb.MongoClient.connect(url, { serverSelectionTimeoutMS: 1000, }) - .then(client => { + .then((client: MongoClient) => { const db = client.db(dbName); const collection = db.collection(collectionName); resolve({ client, collection }); }) - .catch(reason => { - reject(reason); + .catch((reason: any) => { + reject(new Error('Could not connect. Run MongoDB to test. ' + reason.message)); }); }); } From 8dfbded258332ee0702a85fb2b783a4cab4e3230 Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 14 Mar 2024 17:47:00 +0100 Subject: [PATCH 09/15] chore(instrumentation-mongodb): fix lint issues --- .../test/mongodb-v3.test.ts | 6 ++---- .../test/mongodb-v4-v5-v6.metrics.test.ts | 9 +++------ .../test/mongodb-v4.test.ts | 6 ++---- .../test/mongodb-v5-v6.test.ts | 6 ++---- .../opentelemetry-instrumentation-mongodb/test/utils.ts | 8 +++++--- 5 files changed, 14 insertions(+), 21 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index d96b72d717..f0c4837256 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -31,7 +31,7 @@ const instrumentation = registerInstrumentationTesting( new MongoDBInstrumentation() ); -import type {MongoClient, Collection} from 'mongodb'; +import type { MongoClient, Collection } from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; @@ -66,9 +66,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { done(); }) .catch((err: Error) => { - console.log( - 'Skipping test-mongodb. ' + err.message - ); + console.log('Skipping test-mongodb. ' + err.message); shouldTest = false; done(); }); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts index 06e3e4d198..9bb49cb45b 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4-v5-v6.metrics.test.ts @@ -44,7 +44,7 @@ const instrumentation = registerInstrumentationTesting( ); import { accessCollection, DEFAULT_MONGO_HOST } from './utils'; -import type { MongoClient } from 'mongodb'; +import type { MongoClient, Collection } from 'mongodb'; import * as assert from 'assert'; async function waitForNumberOfExports( @@ -80,7 +80,7 @@ describe('MongoDBInstrumentation-Metrics', () => { const DB_NAME = process.env.MONGODB_DB || 'opentelemetry-tests-metrics'; const COLLECTION_NAME = 'test-metrics'; const URL = `mongodb://${HOST}:${PORT}/${DB_NAME}`; - + let client: MongoClient; let collection: Collection; @@ -96,15 +96,12 @@ describe('MongoDBInstrumentation-Metrics', () => { done(); }) .catch((err: Error) => { - console.log( - 'Skipping test-mongodb. ' + err.message - ); + console.log('Skipping test-mongodb. ' + err.message); shouldTest = false; done(); }); }); - beforeEach(function mongoBeforeEach(done) { // Skipping all tests in beforeEach() is a workaround. Mocha does not work // properly when skipping tests in before() on nested describe() calls. diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts index 82ec9c3d95..1a8af5f081 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v4.test.ts @@ -33,7 +33,7 @@ const instrumentation = registerInstrumentationTesting( new MongoDBInstrumentation() ); -import type {MongoClient, Collection} from 'mongodb'; +import type { MongoClient, Collection } from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; @@ -67,9 +67,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => { done(); }) .catch((err: Error) => { - console.log( - 'Skipping test-mongodb. ' + err.message - ); + console.log('Skipping test-mongodb. ' + err.message); shouldTest = false; done(); }); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts index dd74a84137..be6ac80ee0 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v5-v6.test.ts @@ -42,7 +42,7 @@ let instrumentation: MongoDBInstrumentation; } } -import type {MongoClient, Collection} from 'mongodb'; +import type { MongoClient, Collection } from 'mongodb'; import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; @@ -76,9 +76,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => { done(); }) .catch((err: Error) => { - console.log( - 'Skipping test-mongodb. ' + err.message - ); + console.log('Skipping test-mongodb. ' + err.message); shouldTest = false; done(); }); diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts index dc8fb304ad..a158cc9b16 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/utils.ts @@ -45,9 +45,9 @@ export function accessCollection( try { mongodb = require('mongodb'); } catch (err: any) { - reject(new Error('Could not load mongodb. ' + err.message)) + reject(new Error('Could not load mongodb. ' + err.message)); return; - } + } mongodb.MongoClient.connect(url, { serverSelectionTimeoutMS: 1000, }) @@ -57,7 +57,9 @@ export function accessCollection( resolve({ client, collection }); }) .catch((reason: any) => { - reject(new Error('Could not connect. Run MongoDB to test. ' + reason.message)); + reject( + new Error('Could not connect. Run MongoDB to test. ' + reason.message) + ); }); }); } From b34189d36ebb00ee1e201b8972085222b697219f Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 14 Mar 2024 17:52:39 +0100 Subject: [PATCH 10/15] chore(instrumentation-mongodb): fix compile issues --- .../test/mongodb-v3.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts index f0c4837256..144c375c68 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts +++ b/plugins/node/opentelemetry-instrumentation-mongodb/test/mongodb-v3.test.ts @@ -543,8 +543,8 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { }); describe('MongoDb useUnifiedTopology enabled', () => { - let client: mongodb.MongoClient; - let collection: mongodb.Collection; + let client: MongoClient; + let collection: Collection; before(done => { accessCollection(URL, DB_NAME, COLLECTION_NAME, { // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -557,9 +557,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => { done(); }) .catch((err: Error) => { - console.log( - 'Skipping test-mongodb. Could not connect. Run MongoDB to test' - ); + console.log('Skipping test-mongodb. ' + err.message); shouldTest = false; done(); }); From f904e5e529c5e0ae3e6793770a25ec2ec3123ead Mon Sep 17 00:00:00 2001 From: David Luna Date: Thu, 14 Mar 2024 17:58:58 +0100 Subject: [PATCH 11/15] fix(instrumentation-mongodb): change node version for unit tests --- .github/workflows/unit-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index af0bd5cdc5..49a298650c 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -11,7 +11,7 @@ jobs: matrix: node: ["14", "16", "18.18.2"] include: - - node: 14 + - node: 16 code-coverage: true runs-on: ubuntu-latest services: From f0a4afe6487b312da5937ff0838d0468c6bc88a7 Mon Sep 17 00:00:00 2001 From: David Luna Date: Tue, 19 Mar 2024 14:39:30 +0100 Subject: [PATCH 12/15] Update plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml Co-authored-by: Trent Mick --- plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml b/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml index 1a803f026a..64af522512 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml +++ b/plugins/node/opentelemetry-instrumentation-mongodb/.tav.yml @@ -12,6 +12,6 @@ mongodb: commands: npm run test-v4 - versions: ">=5 <6" commands: npm run test-v5-v6 - - versions: ">=6 <6.4" + - versions: ">=6 <7" node: '>=15.0.0' commands: npm run test-v5-v6 From 6451531fafce354dc98bed1263dbf95579081137 Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 25 Mar 2024 15:37:38 +0100 Subject: [PATCH 13/15] Update plugins/node/opentelemetry-instrumentation-mongodb/package.json Co-authored-by: Marc Pichler --- plugins/node/opentelemetry-instrumentation-mongodb/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/node/opentelemetry-instrumentation-mongodb/package.json b/plugins/node/opentelemetry-instrumentation-mongodb/package.json index 51b491ef48..c0ad347f68 100644 --- a/plugins/node/opentelemetry-instrumentation-mongodb/package.json +++ b/plugins/node/opentelemetry-instrumentation-mongodb/package.json @@ -58,7 +58,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "^6.5.0", + "mongodb": "6.5.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.1.0", From 5cdc148a7e706a184146f99bd67596c197ecab9d Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 25 Mar 2024 18:18:39 +0100 Subject: [PATCH 14/15] fix(instrumentation-mongodb): update pacakge.json and coverage config --- .github/workflows/unit-test.yml | 2 +- package-lock.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 49a298650c..4331a3c192 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -11,7 +11,7 @@ jobs: matrix: node: ["14", "16", "18.18.2"] include: - - node: 16 + - node: 18.8.2 code-coverage: true runs-on: ubuntu-latest services: diff --git a/package-lock.json b/package-lock.json index 93a08d89e2..65bcc6f9f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -38049,7 +38049,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "^6.5.0", + "mongodb": "6.5.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.1.0", @@ -46598,7 +46598,7 @@ "@types/mongodb": "3.6.20", "@types/node": "18.6.5", "mocha": "7.2.0", - "mongodb": "^6.5.0", + "mongodb": "6.5.0", "nyc": "15.1.0", "rimraf": "5.0.5", "test-all-versions": "6.1.0", From 4c1aac9c153ccb04d7dd1bf9e97394d994a3229a Mon Sep 17 00:00:00 2001 From: David Luna Date: Mon, 25 Mar 2024 18:41:13 +0100 Subject: [PATCH 15/15] fix(instrumentation-mongodb): update coverage config --- .github/workflows/unit-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 4331a3c192..025b727e9b 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -11,7 +11,7 @@ jobs: matrix: node: ["14", "16", "18.18.2"] include: - - node: 18.8.2 + - node: 18.18.2 code-coverage: true runs-on: ubuntu-latest services: