Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test to tracing-node libs #100

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {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,
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();
114 changes: 113 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
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"
}
}