Skip to content

Commit

Permalink
feat: add test to tracing-node libs (#100)
Browse files Browse the repository at this point in the history
* feat: add test to tracing-node libs

* chore: application version bump

* chore: updated JSDoc returns
  • Loading branch information
saidsef authored Feb 24, 2024
1 parent 91b0ba7 commit 627e901
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 20 deletions.
11 changes: 8 additions & 3 deletions libs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {NodeTracerProvider} - The NodeTracerProvider instance for the service.
*/
module.exports.setupTracing = (serviceName, appName="application", endpoint=null) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
Expand Down Expand Up @@ -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;
44 changes: 44 additions & 0 deletions libs/index.test.js
Original file line number Diff line number Diff line change
@@ -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();
142 changes: 127 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -49,6 +49,7 @@
"@opentelemetry/semantic-conventions": "^1.21.0"
},
"devDependencies": {
"eslint": "8.56.0"
"eslint": "8.56.0",
"sinon": "^17.0.1"
}
}

0 comments on commit 627e901

Please sign in to comment.