@saidsef/tracing-node 2.2.8 → 3.0.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.
@@ -34,4 +34,4 @@ spec:
34
34
  gitRepo:
35
35
  directory: "."
36
36
  repository: "https://github.com/saidsef/tracing-node.git"
37
- revision: "version-bump"
37
+ revision: "tracing-shutdown"
package/libs/index.mjs CHANGED
@@ -52,7 +52,9 @@ diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
52
52
  *
53
53
  * @returns {Tracer} - The tracer for the service.
54
54
  */
55
- export function setupTracing (options={}) {
55
+ let tracerProvider = null; // Declare provider in module scope for access in stopTracing
56
+
57
+ export function setupTracing(options = {}) {
56
58
  const {
57
59
  containerName = process.env.CONTAINER_NAME,
58
60
  deploymentEnvironment = process.env.NODE_ENV,
@@ -64,7 +66,7 @@ export function setupTracing (options={}) {
64
66
  concurrencyLimit = 10,
65
67
  } = options;
66
68
 
67
- const provider = new NodeTracerProvider({
69
+ tracerProvider = new NodeTracerProvider({
68
70
  resource: new Resource({
69
71
  [SemanticResourceAttributes.CONTAINER_NAME]: containerName || serviceName,
70
72
  [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: deploymentEnvironment,
@@ -77,11 +79,16 @@ export function setupTracing (options={}) {
77
79
  }),
78
80
  });
79
81
 
80
- // Initialize the tracer provider
81
- provider.register({
82
+ // Initialize the tracer provider with propagators
83
+ tracerProvider.register({
82
84
  propagator: new CompositePropagator({
83
- propagators: [new W3CBaggagePropagator(), new W3CTraceContextPropagator(), new B3Propagator({injectEncoding: B3InjectEncoding.MULTI_HEADER})],
84
- })});
85
+ propagators: [
86
+ new W3CBaggagePropagator(),
87
+ new W3CTraceContextPropagator(),
88
+ new B3Propagator({ injectEncoding: B3InjectEncoding.MULTI_HEADER }),
89
+ ],
90
+ }),
91
+ });
85
92
 
86
93
  // Configure exporter with the Collector endpoint - uses gRPC
87
94
  const exportOptions = {
@@ -91,7 +98,9 @@ export function setupTracing (options={}) {
91
98
  };
92
99
 
93
100
  // Register the span processor with the tracer provider
94
- provider.addSpanProcessor(new BatchSpanProcessor(new OTLPTraceExporter(exportOptions)));
101
+ const exporter = new OTLPTraceExporter(exportOptions);
102
+ const spanProcessor = new BatchSpanProcessor(exporter);
103
+ tracerProvider.addSpanProcessor(spanProcessor);
95
104
 
96
105
  // Ignore spans from static assets.
97
106
  const ignoreIncomingRequestHook = (req) => {
@@ -101,28 +110,50 @@ export function setupTracing (options={}) {
101
110
 
102
111
  // Register instrumentations
103
112
  registerInstrumentations({
104
- tracerProvider: provider,
105
- instrumentations: [
106
- new ExpressInstrumentation({
107
- ignoreIncomingRequestHook,
108
- }),
109
- new PinoInstrumentation({
110
- logHook: (span, record) => {
111
- record['resource.service.name'] = provider.resource.attributes['service.name'];
112
- },
113
- }),
114
- new HttpInstrumentation({
115
- requireParentforOutgoingSpans: false,
116
- requireParentforIncomingSpans: false,
117
- ignoreIncomingRequestHook,
118
- }),
119
- new AwsInstrumentation({
120
- sqsExtractContextPropagationFromPayload: true
121
- }),
122
- new DnsInstrumentation(),
123
- ],
124
- });
113
+ tracerProvider: tracerProvider,
114
+ instrumentations: [
115
+ new ExpressInstrumentation({
116
+ ignoreIncomingRequestHook,
117
+ }),
118
+ new PinoInstrumentation({
119
+ logHook: (span, record) => {
120
+ record['resource.service.name'] = tracerProvider.resource.attributes['service.name'];
121
+ },
122
+ }),
123
+ new HttpInstrumentation({
124
+ requireParentforOutgoingSpans: false,
125
+ requireParentforIncomingSpans: false,
126
+ ignoreIncomingRequestHook,
127
+ }),
128
+ new AwsInstrumentation({
129
+ sqsExtractContextPropagationFromPayload: true,
130
+ }),
131
+ new DnsInstrumentation(),
132
+ ],
133
+ });
125
134
 
126
135
  // Return the tracer for the service
127
- return provider.getTracer(serviceName);
136
+ return tracerProvider.getTracer(serviceName);
137
+ }
138
+
139
+ /**
140
+ * Gracefully stops the tracing by shutting down the tracer provider.
141
+ *
142
+ * This function ensures that all pending spans are exported and resources are
143
+ * cleaned up properly. It is recommended to call this function during the
144
+ * application's shutdown process.
145
+ *
146
+ * @returns {Promise<void>} - A promise that resolves when shutdown is complete.
147
+ */
148
+ export async function stopTracing() {
149
+ if (tracerProvider) {
150
+ try {
151
+ await tracerProvider.shutdown();
152
+ console.info('Tracing has been successfully shut down.');
153
+ } catch (error) {
154
+ console.error('Error during tracing shutdown:', error);
155
+ }
156
+ } else {
157
+ console.warn('Tracer provider is not initialized.');
158
+ }
128
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saidsef/tracing-node",
3
- "version": "2.2.8",
3
+ "version": "3.0.1",
4
4
  "description": "tracing NodeJS - This is a wrapper for OpenTelemetry instrumentation packages",
5
5
  "main": "libs/index.mjs",
6
6
  "scripts": {