@saidsef/tracing-node 2.2.8 → 3.0.2
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/deployment/base/job.yml +1 -1
- package/libs/index.mjs +60 -29
- package/package.json +13 -13
package/deployment/base/job.yml
CHANGED
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
82
|
+
// Initialize the tracer provider with propagators
|
|
83
|
+
tracerProvider.register({
|
|
82
84
|
propagator: new CompositePropagator({
|
|
83
|
-
|
|
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
|
-
|
|
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
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
|
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": "
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "tracing NodeJS - This is a wrapper for OpenTelemetry instrumentation packages",
|
|
5
5
|
"main": "libs/index.mjs",
|
|
6
6
|
"scripts": {
|
|
@@ -39,18 +39,18 @@
|
|
|
39
39
|
"homepage": "https://github.com/saidsef/tracing-nodejs#readme",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@opentelemetry/api": "^1.9.0",
|
|
42
|
-
"@opentelemetry/exporter-trace-otlp-grpc": "^0.
|
|
43
|
-
"@opentelemetry/instrumentation": "^0.
|
|
44
|
-
"@opentelemetry/instrumentation-aws-sdk": "^0.
|
|
45
|
-
"@opentelemetry/instrumentation-dns": "^0.
|
|
46
|
-
"@opentelemetry/instrumentation-express": "^0.
|
|
47
|
-
"@opentelemetry/instrumentation-http": "^0.
|
|
48
|
-
"@opentelemetry/instrumentation-pino": "^0.
|
|
49
|
-
"@opentelemetry/propagator-b3": "^1.
|
|
50
|
-
"@opentelemetry/resources": "^1.
|
|
51
|
-
"@opentelemetry/sdk-trace-base": "^1.
|
|
52
|
-
"@opentelemetry/sdk-trace-node": "^1.
|
|
53
|
-
"@opentelemetry/semantic-conventions": "^1.
|
|
42
|
+
"@opentelemetry/exporter-trace-otlp-grpc": "^0.55.0",
|
|
43
|
+
"@opentelemetry/instrumentation": "^0.55.0",
|
|
44
|
+
"@opentelemetry/instrumentation-aws-sdk": "^0.47.0",
|
|
45
|
+
"@opentelemetry/instrumentation-dns": "^0.41.0",
|
|
46
|
+
"@opentelemetry/instrumentation-express": "^0.45.0",
|
|
47
|
+
"@opentelemetry/instrumentation-http": "^0.55.0",
|
|
48
|
+
"@opentelemetry/instrumentation-pino": "^0.44.0",
|
|
49
|
+
"@opentelemetry/propagator-b3": "^1.28.0",
|
|
50
|
+
"@opentelemetry/resources": "^1.28.0",
|
|
51
|
+
"@opentelemetry/sdk-trace-base": "^1.28.0",
|
|
52
|
+
"@opentelemetry/sdk-trace-node": "^1.28.0",
|
|
53
|
+
"@opentelemetry/semantic-conventions": "^1.28.0"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"eslint": "^9.8.0",
|