@saidsef/tracing-node 3.22.0 → 3.22.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/libs/index.mjs +17 -32
- package/package.json +1 -1
package/libs/index.mjs
CHANGED
|
@@ -36,6 +36,15 @@ import {ATTR_CONTAINER_NAME} from '@opentelemetry/semantic-conventions/incubatin
|
|
|
36
36
|
|
|
37
37
|
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
|
|
38
38
|
|
|
39
|
+
// Set a non-negative integer span attribute from a header value; ignore invalid input.
|
|
40
|
+
const setIntAttribute = (span, name, value) => {
|
|
41
|
+
if (!value) return;
|
|
42
|
+
const parsed = parseInt(value, 10);
|
|
43
|
+
if (!Number.isNaN(parsed) && parsed >= 0) {
|
|
44
|
+
span.setAttribute(name, parsed);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
39
48
|
/**
|
|
40
49
|
* Sets up tracing for the application using OpenTelemetry.
|
|
41
50
|
*
|
|
@@ -120,11 +129,6 @@ export function setupTracing(options = {}) {
|
|
|
120
129
|
// explicit config, with the recommended context manager.
|
|
121
130
|
tracerProvider.register();
|
|
122
131
|
|
|
123
|
-
// Ignore spans from static assets.
|
|
124
|
-
const ignoreIncomingRequestHook = (req) => {
|
|
125
|
-
return req.url.startsWith('/metrics') || req.url.startsWith('/healthz');
|
|
126
|
-
};
|
|
127
|
-
|
|
128
132
|
// Hook to set peer service name for outgoing requests
|
|
129
133
|
const applyCustomAttributesOnSpan = (span, request) => {
|
|
130
134
|
const url = request?.url || request?.uri || '';
|
|
@@ -149,7 +153,8 @@ export function setupTracing(options = {}) {
|
|
|
149
153
|
const instrumentations = [
|
|
150
154
|
new HttpInstrumentation({
|
|
151
155
|
serverName: serviceName,
|
|
152
|
-
|
|
156
|
+
// Ignore spans from static assets (metrics/health probes).
|
|
157
|
+
ignoreIncomingRequestHook: (req) => req.url.startsWith('/metrics') || req.url.startsWith('/healthz'),
|
|
153
158
|
applyCustomAttributesOnSpan,
|
|
154
159
|
requestHook: (span, request) => {
|
|
155
160
|
// Enrich spans with additional HTTP request attributes
|
|
@@ -168,13 +173,7 @@ export function setupTracing(options = {}) {
|
|
|
168
173
|
if (userAgent) span.setAttribute('http.user_agent', userAgent);
|
|
169
174
|
if (contentType) span.setAttribute('http.request.content_type', contentType);
|
|
170
175
|
|
|
171
|
-
|
|
172
|
-
if (contentLength) {
|
|
173
|
-
const length = parseInt(contentLength, 10);
|
|
174
|
-
if (!Number.isNaN(length) && length >= 0) {
|
|
175
|
-
span.setAttribute('http.request.content_length', length);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
176
|
+
setIntAttribute(span, 'http.request.content_length', contentLength);
|
|
178
177
|
|
|
179
178
|
// Correlation headers for distributed tracing
|
|
180
179
|
if (requestId) span.setAttribute('http.request_id', requestId);
|
|
@@ -191,12 +190,7 @@ export function setupTracing(options = {}) {
|
|
|
191
190
|
|
|
192
191
|
if (contentType) span.setAttribute('http.response.content_type', contentType);
|
|
193
192
|
|
|
194
|
-
|
|
195
|
-
const length = parseInt(contentLength, 10);
|
|
196
|
-
if (!Number.isNaN(length) && length >= 0) {
|
|
197
|
-
span.setAttribute('http.response.content_length', length);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
193
|
+
setIntAttribute(span, 'http.response.content_length', contentLength);
|
|
200
194
|
|
|
201
195
|
if (requestId) span.setAttribute('http.request_id', requestId);
|
|
202
196
|
},
|
|
@@ -282,20 +276,11 @@ export function setupTracing(options = {}) {
|
|
|
282
276
|
}
|
|
283
277
|
},
|
|
284
278
|
responseHook: (span, cmdName, cmdArgs, response) => {
|
|
285
|
-
//
|
|
286
|
-
span
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
// Add command details for better observability
|
|
290
|
-
if (cmdName) {
|
|
291
|
-
span.setAttribute('db.operation', cmdName.toUpperCase());
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// Log response size if available
|
|
279
|
+
// peer.service, db.system and db.operation are already set on this span
|
|
280
|
+
// by requestHook and persist for the span's lifetime, so they are not
|
|
281
|
+
// re-set here. Record only the response shape for observability.
|
|
295
282
|
if (response !== undefined && response !== null) {
|
|
296
|
-
|
|
297
|
-
span.setAttribute('db.response.type', responseType);
|
|
298
|
-
|
|
283
|
+
span.setAttribute('db.response.type', typeof response);
|
|
299
284
|
if (Array.isArray(response)) {
|
|
300
285
|
span.setAttribute('db.response.count', response.length);
|
|
301
286
|
}
|