From 2879c0cf6337ff44142e8615cb48e2478e2f0ad9 Mon Sep 17 00:00:00 2001 From: Said Sef Date: Sat, 24 Feb 2024 11:46:06 +0000 Subject: [PATCH 1/3] feat: add test to tracing-node libs --- libs/index.js | 11 +++-- libs/index.test.js | 44 +++++++++++++++++ package-lock.json | 114 ++++++++++++++++++++++++++++++++++++++++++++- package.json | 3 +- 4 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 libs/index.test.js diff --git a/libs/index.js b/libs/index.js index 66f6d69..717e21a 100644 --- a/libs/index.js +++ b/libs/index.js @@ -33,7 +33,14 @@ const { B3Propagator, B3InjectEncoding } = require('@opentelemetry/propagator-b3 diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); -function setupTracing(serviceName, appName="application", endpoint=null) { +/** +* Sets up tracing for the application with OpenTelemetry. +* @param {string} serviceName - The name of the service to trace. +* @param {string} [appName="application"] - The name of the application. +* @param {string|null} [endpoint=null] - The endpoint for the tracing collector. +* @returns {Tracer} - The tracer instance for the service. +*/ +module.exports.setupTracing = (serviceName, appName="application", endpoint=null) => { const provider = new NodeTracerProvider({ resource: new Resource({ [SemanticResourceAttributes.SERVICE_NAME]: serviceName, @@ -92,5 +99,3 @@ function setupTracing(serviceName, appName="application", endpoint=null) { // Return the tracer for the service return provider.getTracer(serviceName); } - -module.exports.setupTracing = setupTracing; diff --git a/libs/index.test.js b/libs/index.test.js new file mode 100644 index 0000000..83746ac --- /dev/null +++ b/libs/index.test.js @@ -0,0 +1,44 @@ +const assert = require('assert'); +const sinon = require('sinon'); + +// Mocking OpenTelemetry and its dependencies +const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node'); +const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = require('@opentelemetry/core'); + +const { setupTracing } = require('./index'); + +// Mocking OpenTelemetry and its dependencies +sinon.stub(NodeTracerProvider.prototype, 'addSpanProcessor'); +sinon.stub(NodeTracerProvider.prototype, 'register'); +sinon.stub(CompositePropagator.prototype, 'constructor'); +sinon.stub(W3CBaggagePropagator.prototype, 'constructor'); +sinon.stub(W3CTraceContextPropagator.prototype, 'constructor'); + +// Test case for setupTracing function +function testSetupTracing() { + const serviceName = 'test-service'; + const tracer = setupTracing(serviceName); + + // Ensure that setupTracing returns a valid tracer + assert(tracer, 'Tracer should exist'); + + console.log('Setup Tracing test passed successfully'); +} + +// Test case for setupTracing function with optional parameters +function testSetupTracingWithOptionalParams() { + const serviceName = 'test-service'; + const appName = 'test-app'; + const endpoint = 'https://your-collector-endpoint'; // Provide your collector endpoint here + + const tracer = setupTracing(serviceName, appName, endpoint); + + // Ensure that setupTracing with optional parameters returns a valid tracer + assert(tracer, 'Tracer should exist'); + + console.log('Setup Tracing with optional params test passed successfully'); +} + +// Run the tests +testSetupTracing(); +testSetupTracingWithOptionalParams(); diff --git a/package-lock.json b/package-lock.json index 9da6df7..3f295d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,8 @@ "@opentelemetry/semantic-conventions": "^1.21.0" }, "devDependencies": { - "eslint": "8.56.0" + "eslint": "8.56.0", + "sinon": "^17.0.1" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -583,6 +584,50 @@ "resolved": "https://registry.npmjs.org/@protobufjs/utf8/-/utf8-1.1.0.tgz", "integrity": "sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw==" }, + "node_modules/@sinonjs/commons": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-3.0.1.tgz", + "integrity": "sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, + "node_modules/@sinonjs/fake-timers": { + "version": "11.2.2", + "resolved": "https://registry.npmjs.org/@sinonjs/fake-timers/-/fake-timers-11.2.2.tgz", + "integrity": "sha512-G2piCSxQ7oWOxwGSAyFHfPIsyeJGXYtc6mFbnFA+kRXkiEnTl8c/8jul2S329iFBnDI9HGoeWWAZvuvOkZccgw==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^3.0.0" + } + }, + "node_modules/@sinonjs/samsam": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/samsam/-/samsam-8.0.0.tgz", + "integrity": "sha512-Bp8KUVlLp8ibJZrnvq2foVhP0IVX2CIprMJPK0vqGqgrDa0OHVKeZyBykqskkrdxV6yKBPmGasO8LVjAKR3Gew==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^2.0.0", + "lodash.get": "^4.4.2", + "type-detect": "^4.0.8" + } + }, + "node_modules/@sinonjs/samsam/node_modules/@sinonjs/commons": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@sinonjs/commons/-/commons-2.0.0.tgz", + "integrity": "sha512-uLa0j859mMrg2slwQYdO/AkrOfmH+X6LTVmNTS9CqexuE2IvVORIkSpJLqePAbEnKJ77aMmCwr1NUZ57120Xcg==", + "dev": true, + "dependencies": { + "type-detect": "4.0.8" + } + }, + "node_modules/@sinonjs/text-encoding": { + "version": "0.7.2", + "resolved": "https://registry.npmjs.org/@sinonjs/text-encoding/-/text-encoding-0.7.2.tgz", + "integrity": "sha512-sXXKG+uL9IrKqViTtao2Ws6dy0znu9sOaP1di/jKGW1M6VssO8vlpXCQcpZ+jisQ1tTFAC5Jo/EOzFbggBagFQ==", + "dev": true + }, "node_modules/@types/node": { "version": "20.11.16", "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.16.tgz", @@ -791,6 +836,15 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, + "node_modules/diff": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/diff/-/diff-5.2.0.tgz", + "integrity": "sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==", + "dev": true, + "engines": { + "node": ">=0.3.1" + } + }, "node_modules/doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -1286,6 +1340,12 @@ "integrity": "sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==", "dev": true }, + "node_modules/just-extend": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/just-extend/-/just-extend-6.2.0.tgz", + "integrity": "sha512-cYofQu2Xpom82S6qD778jBDpwvvy39s1l/hrYij2u9AMdQcGRpaBu6kY4mVhuno5kJVi1DAz4aiphA2WI1/OAw==", + "dev": true + }, "node_modules/keyv": { "version": "4.5.4", "resolved": "https://registry.npmjs.org/keyv/-/keyv-4.5.4.tgz", @@ -1328,6 +1388,12 @@ "resolved": "https://registry.npmjs.org/lodash.camelcase/-/lodash.camelcase-4.3.0.tgz", "integrity": "sha512-TwuEnCnxbc3rAvhf/LbG7tJUDzhqXyFnv3dtzLOPgCG/hODL7WFnsbwktkD7yUV0RrreP/l1PALq/YSg6VvjlA==" }, + "node_modules/lodash.get": { + "version": "4.4.2", + "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", + "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==", + "dev": true + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", @@ -1377,6 +1443,19 @@ "integrity": "sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==", "dev": true }, + "node_modules/nise": { + "version": "5.1.9", + "resolved": "https://registry.npmjs.org/nise/-/nise-5.1.9.tgz", + "integrity": "sha512-qOnoujW4SV6e40dYxJOb3uvuoPHtmLzIk4TFo+j0jPJoC+5Z9xja5qH5JZobEPsa8+YYphMrOSwnrshEhG2qww==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^3.0.0", + "@sinonjs/fake-timers": "^11.2.2", + "@sinonjs/text-encoding": "^0.7.2", + "just-extend": "^6.2.0", + "path-to-regexp": "^6.2.1" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -1477,6 +1556,12 @@ "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==" }, + "node_modules/path-to-regexp": { + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.1.tgz", + "integrity": "sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==", + "dev": true + }, "node_modules/prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", @@ -1672,6 +1757,24 @@ "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.2.1.tgz", "integrity": "sha512-sQTKC1Re/rM6XyFM6fIAGHRPVGvyXfgzIDvzoq608vM+jeyVD0Tu1E6Np0Kc2zAIFWIj963V2800iF/9LPieQw==" }, + "node_modules/sinon": { + "version": "17.0.1", + "resolved": "https://registry.npmjs.org/sinon/-/sinon-17.0.1.tgz", + "integrity": "sha512-wmwE19Lie0MLT+ZYNpDymasPHUKTaZHUH/pKEubRXIzySv9Atnlw+BUMGCzWgV7b7wO+Hw6f1TEOr0IUnmU8/g==", + "dev": true, + "dependencies": { + "@sinonjs/commons": "^3.0.0", + "@sinonjs/fake-timers": "^11.2.2", + "@sinonjs/samsam": "^8.0.0", + "diff": "^5.1.0", + "nise": "^5.1.5", + "supports-color": "^7.2.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/sinon" + } + }, "node_modules/string-width": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", @@ -1749,6 +1852,15 @@ "node": ">= 0.8.0" } }, + "node_modules/type-detect": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/type-detect/-/type-detect-4.0.8.tgz", + "integrity": "sha512-0fr/mIH1dlO+x7TlcMy+bIDqKPsw/70tVyeHW787goQjhmqaZe10uwLujubK9q9Lg6Fiho1KUKDYz0Z7k7g5/g==", + "dev": true, + "engines": { + "node": ">=4" + } + }, "node_modules/type-fest": { "version": "0.20.2", "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.20.2.tgz", diff --git a/package.json b/package.json index aca67d4..7c51611 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "@opentelemetry/semantic-conventions": "^1.21.0" }, "devDependencies": { - "eslint": "8.56.0" + "eslint": "8.56.0", + "sinon": "^17.0.1" } } From 32735f56ca0a050edc800791f03fdc8b2c9506ce Mon Sep 17 00:00:00 2001 From: Said Sef Date: Sat, 24 Feb 2024 11:50:58 +0000 Subject: [PATCH 2/3] chore: application version bump --- package-lock.json | 28 ++++++++++++++-------------- package.json | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3f295d9..f102299 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@saidsef/tracing-node", - "version": "1.8.27", + "version": "1.8.28", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@saidsef/tracing-node", - "version": "1.8.27", + "version": "1.8.28", "license": "Apache-2.0", "dependencies": { "@opentelemetry/api": "^1.7.0", @@ -94,9 +94,9 @@ } }, "node_modules/@grpc/grpc-js": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.10.0.tgz", - "integrity": "sha512-tx+eoEsqkMkLCHR4OOplwNIaJ7SVZWzeVKzEMBz8VR+TbssgBYOP4a0P+KQiQ6LaTG4SGaIEu7YTS8xOmkOWLA==", + "version": "1.10.1", + "resolved": "https://registry.npmjs.org/@grpc/grpc-js/-/grpc-js-1.10.1.tgz", + "integrity": "sha512-55ONqFytZExfOIjF1RjXPcVmT/jJqFzbbDqxK9jmRV4nxiYWtL9hENSW1Jfx0SdZfrvoqd44YJ/GJTqfRrawSQ==", "dependencies": { "@grpc/proto-loader": "^0.7.8", "@types/node": ">=12.12.47" @@ -629,9 +629,9 @@ "dev": true }, "node_modules/@types/node": { - "version": "20.11.16", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.16.tgz", - "integrity": "sha512-gKb0enTmRCzXSSUJDq6/sPcqrfCv2mkkG6Jt/clpn5eiCbKTY+SgZUxo+p8ZKMof5dCp9vHQUAB7wOUTod22wQ==", + "version": "20.11.20", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.11.20.tgz", + "integrity": "sha512-7/rR21OS+fq8IyHTgtLkDK949uzsa6n8BkziAKtPVpugIkO6D+/ooXMvzXxDnZrmtXVfjb1bKQafYpb8s89LOg==", "dependencies": { "undici-types": "~5.26.4" } @@ -1094,9 +1094,9 @@ } }, "node_modules/flatted": { - "version": "3.2.9", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.9.tgz", - "integrity": "sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==", + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.1.tgz", + "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, "node_modules/fs.realpath": { @@ -1184,9 +1184,9 @@ } }, "node_modules/hasown": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", - "integrity": "sha512-vUptKVTpIJhcczKBbgnS+RtcuYMB8+oNzPK2/Hp3hanz8JmpATdmmgLgSaadVREkDm+e2giHwY3ZRkyjSIDDFA==", + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.1.tgz", + "integrity": "sha512-1/th4MHjnwncwXsIW6QMzlvYL9kG5e/CpVvLRZe4XPa8TOUNbCELqmvhDmnkNsAjwaG4+I8gJJL0JBvTTLO9qA==", "dependencies": { "function-bind": "^1.1.2" }, diff --git a/package.json b/package.json index 7c51611..02eb9f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@saidsef/tracing-node", - "version": "1.8.27", + "version": "1.8.28", "description": "tracing NodeJS - This is a wrapper for OpenTelemetry instrumentation packages", "main": "libs/index.js", "scripts": { From 7964980a62fc19c5a5592da3cf95b394116d9201 Mon Sep 17 00:00:00 2001 From: Said Sef Date: Sat, 24 Feb 2024 11:59:16 +0000 Subject: [PATCH 3/3] chore: updated JSDoc returns --- libs/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/index.js b/libs/index.js index 717e21a..094e973 100644 --- a/libs/index.js +++ b/libs/index.js @@ -38,7 +38,7 @@ diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO); * @param {string} serviceName - The name of the service to trace. * @param {string} [appName="application"] - The name of the application. * @param {string|null} [endpoint=null] - The endpoint for the tracing collector. -* @returns {Tracer} - The tracer instance for the service. +* @returns {NodeTracerProvider} - The NodeTracerProvider instance for the service. */ module.exports.setupTracing = (serviceName, appName="application", endpoint=null) => { const provider = new NodeTracerProvider({