@saidsef/tracing-node 1.8.30 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintrc.yml CHANGED
@@ -2,7 +2,9 @@ env:
2
2
  browser: true
3
3
  commonjs: true
4
4
  es2021: true
5
+ jest: true
5
6
  extends: eslint:recommended
6
7
  parserOptions:
7
8
  ecmaVersion: latest
9
+ sourceType: module
8
10
  rules: {}
@@ -18,7 +18,7 @@ spec:
18
18
  - /bin/sh
19
19
  args:
20
20
  - -c
21
- - "npm install && node libs/index.js"
21
+ - "npm install && node libs/index.mjs"
22
22
  resources:
23
23
  limits:
24
24
  memory: "1Gi"
@@ -31,4 +31,4 @@ spec:
31
31
  gitRepo:
32
32
  directory: "."
33
33
  repository: "https://github.com/saidsef/tracing-node.git"
34
- revision: "ins-dns-express-otlp-sdk"
34
+ revision: "es12-module"
@@ -16,31 +16,46 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
- const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = require('@opentelemetry/core');
20
- const { registerInstrumentations } = require('@opentelemetry/instrumentation');
21
- const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
22
- const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
23
- const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
24
- const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
25
- const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
26
- const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
27
- const { Resource } = require('@opentelemetry/resources');
28
- const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
29
- const { AwsInstrumentation } = require('@opentelemetry/instrumentation-aws-sdk');
30
- const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino');
31
- const { DnsInstrumentation } = require('@opentelemetry/instrumentation-dns');
32
- const { B3Propagator, B3InjectEncoding } = require('@opentelemetry/propagator-b3');
19
+ import { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } from '@opentelemetry/core';
20
+ import { registerInstrumentations } from '@opentelemetry/instrumentation';
21
+ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
22
+ import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
23
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
24
+ import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
25
+ import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
26
+ import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
27
+ import { Resource } from '@opentelemetry/resources';
28
+ import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
29
+ import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk';
30
+ import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
31
+ import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';
32
+ import { B3Propagator, B3InjectEncoding } from '@opentelemetry/propagator-b3';
33
33
 
34
34
  diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
35
35
 
36
36
  /**
37
- * Sets up tracing for the application with OpenTelemetry.
38
- * @param {string} serviceName - The name of the service to trace.
39
- * @param {string} [appName="application"] - The name of the application.
40
- * @param {string|null} [endpoint=null] - The endpoint for the tracing collector.
41
- * @returns {NodeTracerProvider} - The NodeTracerProvider instance for the service.
37
+ * Sets up tracing for a given service, allowing for the collection and export of trace data.
38
+ * This function configures a NodeTracerProvider with specific resource attributes, including
39
+ * service name, namespace, and host. It also configures an exporter using the OTLP protocol
40
+ * over gRPC, with the option to specify an endpoint. Instrumentations for HTTP, Express,
41
+ * AWS, Pino, and DNS are registered to capture relevant spans. The function finally returns
42
+ * a tracer specific to the service for initiating and exporting spans.
43
+ *
44
+ * @param {string} serviceName - The name of the service to be traced. This will be used to
45
+ * label traces and is essential for identifying traces belonging
46
+ * to this service.
47
+ * @param {string} [appName="application"] - The namespace or application name the service
48
+ * belongs to. This helps in grouping services under
49
+ * a common namespace for better trace organization.
50
+ * @param {string|null} [endpoint=null] - The endpoint URL for the OTLP gRPC exporter. If not
51
+ * provided, the exporter will default to its standard
52
+ * configuration, which might not be suitable for all
53
+ * deployments.
54
+ * @returns {Tracer} - Returns a Tracer instance configured for the service. This tracer can
55
+ * be used to create and export spans for tracing various operations within
56
+ * the service.
42
57
  */
43
- module.exports.setupTracing = (serviceName, appName="application", endpoint=null) => {
58
+ export const setupTracing = (serviceName, appName="application", endpoint=null) => {
44
59
  const provider = new NodeTracerProvider({
45
60
  resource: new Resource({
46
61
  [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
@@ -0,0 +1,62 @@
1
+ // tracing.test.mjs
2
+ import { setupTracing } from './index.mjs';
3
+ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
4
+ import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
5
+ import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
6
+
7
+ // Mocking external dependencies
8
+ jest.mock('@opentelemetry/sdk-trace-node', () => {
9
+ const originalModule = jest.requireActual('@opentelemetry/sdk-trace-node');
10
+ return {
11
+ ...originalModule,
12
+ NodeTracerProvider: jest.fn().mockImplementation(() => ({
13
+ addSpanProcessor: jest.fn(),
14
+ register: jest.fn(),
15
+ getTracer: jest.fn().mockReturnValue({}),
16
+ resource: {
17
+ attributes: {},
18
+ },
19
+ })),
20
+ };
21
+ });
22
+
23
+ jest.mock('@opentelemetry/sdk-trace-base', () => ({
24
+ BatchSpanProcessor: jest.fn(),
25
+ SpanProcessor: jest.fn(),
26
+ }));
27
+
28
+ jest.mock('@opentelemetry/exporter-trace-otlp-grpc', () => ({
29
+ OTLPTraceExporter: jest.fn(),
30
+ }));
31
+
32
+ describe('setupTracing', () => {
33
+ beforeEach(() => {
34
+ // Clear all instances and calls to constructor and all methods:
35
+ NodeTracerProvider.mockClear();
36
+ BatchSpanProcessor.mockClear();
37
+ OTLPTraceExporter.mockClear();
38
+ });
39
+
40
+ it('should create a tracer with default parameters', () => {
41
+ const tracer = setupTracing('test-service');
42
+ expect(NodeTracerProvider).toHaveBeenCalledTimes(1);
43
+ expect(BatchSpanProcessor).toHaveBeenCalledTimes(1);
44
+ expect(OTLPTraceExporter).toHaveBeenCalledWith({
45
+ serviceName: 'test-service',
46
+ url: null,
47
+ });
48
+ expect(tracer).toBeDefined();
49
+ });
50
+
51
+ it('should create a tracer with custom application name and endpoint', () => {
52
+ const tracer = setupTracing('test-service', 'custom-app', 'custom-endpoint');
53
+ expect(NodeTracerProvider).toHaveBeenCalledTimes(1);
54
+ expect(BatchSpanProcessor).toHaveBeenCalledTimes(1);
55
+ expect(OTLPTraceExporter).toHaveBeenCalledWith({
56
+ serviceName: 'test-service',
57
+ url: 'custom-endpoint',
58
+ });
59
+ expect(tracer).toBeDefined();
60
+ });
61
+
62
+ });
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@saidsef/tracing-node",
3
- "version": "1.8.30",
3
+ "version": "1.9.1",
4
4
  "description": "tracing NodeJS - This is a wrapper for OpenTelemetry instrumentation packages",
5
- "main": "libs/index.js",
5
+ "main": "libs/index.mjs",
6
6
  "scripts": {
7
- "test": "node --trace-warnings --test --report-uncaught-exception --heap-prof --cpu-prof --track-heap-objects --report-dir=test/ --diagnostic-dir=test/ --heap-prof-dir=test/ libs/index.js",
8
- "lint": "eslint libs",
7
+ "test": "node --trace-warnings --test --report-uncaught-exception --heap-prof --cpu-prof --track-heap-objects --report-dir=test/ --diagnostic-dir=test/ --heap-prof-dir=test/ libs/index.mjs",
8
+ "lint": "eslint --ext js,mjs libs",
9
9
  "rebuild": "rm -rfv node_modules/ package-lock.json && NODE_ENV=production npm install"
10
10
  },
11
+ "type": "module",
11
12
  "private": false,
12
13
  "repository": {
13
14
  "type": "git",
@@ -29,6 +30,9 @@
29
30
  ],
30
31
  "author": "Said Sef <saidsef@gmail.com>",
31
32
  "license": "Apache-2.0",
33
+ "engines": {
34
+ "node": ">= 20"
35
+ },
32
36
  "bugs": {
33
37
  "url": "https://github.com/saidsef/tracing-nodejs/issues"
34
38
  },
@@ -41,7 +45,7 @@
41
45
  "@opentelemetry/instrumentation-dns": "^0.33.0",
42
46
  "@opentelemetry/instrumentation-express": "^0.35.0",
43
47
  "@opentelemetry/instrumentation-http": "^0.49.1",
44
- "@opentelemetry/instrumentation-pino": "^0.35.0",
48
+ "@opentelemetry/instrumentation-pino": "^0.36.0",
45
49
  "@opentelemetry/propagator-b3": "^1.22.0",
46
50
  "@opentelemetry/resources": "^1.21.0",
47
51
  "@opentelemetry/sdk-trace-base": "^1.22.0",
@@ -50,6 +54,6 @@
50
54
  },
51
55
  "devDependencies": {
52
56
  "eslint": "8.57.0",
53
- "sinon": "^17.0.1"
57
+ "jest": "^29.7.0"
54
58
  }
55
59
  }
@@ -1,44 +0,0 @@
1
- const assert = require('assert');
2
- const sinon = require('sinon');
3
-
4
- // Mocking OpenTelemetry and its dependencies
5
- const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
6
- const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = require('@opentelemetry/core');
7
-
8
- const { setupTracing } = require('./index');
9
-
10
- // Mocking OpenTelemetry and its dependencies
11
- sinon.stub(NodeTracerProvider.prototype, 'addSpanProcessor');
12
- sinon.stub(NodeTracerProvider.prototype, 'register');
13
- sinon.stub(CompositePropagator.prototype, 'constructor');
14
- sinon.stub(W3CBaggagePropagator.prototype, 'constructor');
15
- sinon.stub(W3CTraceContextPropagator.prototype, 'constructor');
16
-
17
- // Test case for setupTracing function
18
- function testSetupTracing() {
19
- const serviceName = 'test-service';
20
- const tracer = setupTracing(serviceName);
21
-
22
- // Ensure that setupTracing returns a valid tracer
23
- assert(tracer, 'Tracer should exist');
24
-
25
- console.log('Setup Tracing test passed successfully');
26
- }
27
-
28
- // Test case for setupTracing function with optional parameters
29
- function testSetupTracingWithOptionalParams() {
30
- const serviceName = 'test-service';
31
- const appName = 'test-app';
32
- const endpoint = 'https://your-collector-endpoint'; // Provide your collector endpoint here
33
-
34
- const tracer = setupTracing(serviceName, appName, endpoint);
35
-
36
- // Ensure that setupTracing with optional parameters returns a valid tracer
37
- assert(tracer, 'Tracer should exist');
38
-
39
- console.log('Setup Tracing with optional params test passed successfully');
40
- }
41
-
42
- // Run the tests
43
- testSetupTracing();
44
- testSetupTracingWithOptionalParams();