Skip to content

Commit

Permalink
chore: update module to es12 (#112)
Browse files Browse the repository at this point in the history
* chore: update module to ES12

* chore: app versio bump and test pod update

* chore: update eslint to es12 module

* chore: update test in package to point to mjs

* chore: updates pod test file name
  • Loading branch information
saidsef authored Mar 15, 2024
1 parent acb0215 commit 51f47bc
Show file tree
Hide file tree
Showing 7 changed files with 3,961 additions and 1,006 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ env:
browser: true
commonjs: true
es2021: true
jest: true
extends: eslint:recommended
parserOptions:
ecmaVersion: latest
sourceType: module
rules: {}
4 changes: 2 additions & 2 deletions deployment/base/job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spec:
- /bin/sh
args:
- -c
- "npm install && node libs/index.js"
- "npm install && node libs/index.mjs"
resources:
limits:
memory: "1Gi"
Expand All @@ -31,4 +31,4 @@ spec:
gitRepo:
directory: "."
repository: "https://github.com/saidsef/tracing-node.git"
revision: "ins-dns-express-otlp-sdk"
revision: "es12-module"
55 changes: 35 additions & 20 deletions libs/index.js → libs/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,46 @@
* limitations under the License.
*/

const { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } = require('@opentelemetry/core');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { BatchSpanProcessor } = require('@opentelemetry/sdk-trace-base');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-grpc');
const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
const { Resource } = require('@opentelemetry/resources');
const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
const { AwsInstrumentation } = require('@opentelemetry/instrumentation-aws-sdk');
const { PinoInstrumentation } = require('@opentelemetry/instrumentation-pino');
const { DnsInstrumentation } = require('@opentelemetry/instrumentation-dns');
const { B3Propagator, B3InjectEncoding } = require('@opentelemetry/propagator-b3');
import { CompositePropagator, W3CBaggagePropagator, W3CTraceContextPropagator } from '@opentelemetry/core';
import { registerInstrumentations } from '@opentelemetry/instrumentation';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
import { Resource } from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { AwsInstrumentation } from '@opentelemetry/instrumentation-aws-sdk';
import { PinoInstrumentation } from '@opentelemetry/instrumentation-pino';
import { DnsInstrumentation } from '@opentelemetry/instrumentation-dns';
import { B3Propagator, B3InjectEncoding } from '@opentelemetry/propagator-b3';

diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);

/**
* 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.
* Sets up tracing for a given service, allowing for the collection and export of trace data.
* This function configures a NodeTracerProvider with specific resource attributes, including
* service name, namespace, and host. It also configures an exporter using the OTLP protocol
* over gRPC, with the option to specify an endpoint. Instrumentations for HTTP, Express,
* AWS, Pino, and DNS are registered to capture relevant spans. The function finally returns
* a tracer specific to the service for initiating and exporting spans.
*
* @param {string} serviceName - The name of the service to be traced. This will be used to
* label traces and is essential for identifying traces belonging
* to this service.
* @param {string} [appName="application"] - The namespace or application name the service
* belongs to. This helps in grouping services under
* a common namespace for better trace organization.
* @param {string|null} [endpoint=null] - The endpoint URL for the OTLP gRPC exporter. If not
* provided, the exporter will default to its standard
* configuration, which might not be suitable for all
* deployments.
* @returns {Tracer} - Returns a Tracer instance configured for the service. This tracer can
* be used to create and export spans for tracing various operations within
* the service.
*/
module.exports.setupTracing = (serviceName, appName="application", endpoint=null) => {
export const setupTracing = (serviceName, appName="application", endpoint=null) => {
const provider = new NodeTracerProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
Expand Down
44 changes: 0 additions & 44 deletions libs/index.test.js

This file was deleted.

62 changes: 62 additions & 0 deletions libs/index.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// tracing.test.mjs
import { setupTracing } from './index.mjs';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import { BatchSpanProcessor } from '@opentelemetry/sdk-trace-base';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';

// Mocking external dependencies
jest.mock('@opentelemetry/sdk-trace-node', () => {
const originalModule = jest.requireActual('@opentelemetry/sdk-trace-node');
return {
...originalModule,
NodeTracerProvider: jest.fn().mockImplementation(() => ({
addSpanProcessor: jest.fn(),
register: jest.fn(),
getTracer: jest.fn().mockReturnValue({}),
resource: {
attributes: {},
},
})),
};
});

jest.mock('@opentelemetry/sdk-trace-base', () => ({
BatchSpanProcessor: jest.fn(),
SpanProcessor: jest.fn(),
}));

jest.mock('@opentelemetry/exporter-trace-otlp-grpc', () => ({
OTLPTraceExporter: jest.fn(),
}));

describe('setupTracing', () => {
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
NodeTracerProvider.mockClear();
BatchSpanProcessor.mockClear();
OTLPTraceExporter.mockClear();
});

it('should create a tracer with default parameters', () => {
const tracer = setupTracing('test-service');
expect(NodeTracerProvider).toHaveBeenCalledTimes(1);
expect(BatchSpanProcessor).toHaveBeenCalledTimes(1);
expect(OTLPTraceExporter).toHaveBeenCalledWith({
serviceName: 'test-service',
url: null,
});
expect(tracer).toBeDefined();
});

it('should create a tracer with custom application name and endpoint', () => {
const tracer = setupTracing('test-service', 'custom-app', 'custom-endpoint');
expect(NodeTracerProvider).toHaveBeenCalledTimes(1);
expect(BatchSpanProcessor).toHaveBeenCalledTimes(1);
expect(OTLPTraceExporter).toHaveBeenCalledWith({
serviceName: 'test-service',
url: 'custom-endpoint',
});
expect(tracer).toBeDefined();
});

});
Loading

0 comments on commit 51f47bc

Please sign in to comment.