@saidsef/tracing-node 3.22.0 → 3.22.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/libs/index.mjs CHANGED
@@ -1,5 +1,3 @@
1
- 'use strict';
2
-
3
1
  /*
4
2
  * Copyright Said Sef
5
3
  *
@@ -36,6 +34,15 @@ import {ATTR_CONTAINER_NAME} from '@opentelemetry/semantic-conventions/incubatin
36
34
 
37
35
  diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO);
38
36
 
37
+ // Set a non-negative integer span attribute from a header value; ignore invalid input.
38
+ const setIntAttribute = (span, name, value) => {
39
+ if (!value) return;
40
+ const parsed = parseInt(value, 10);
41
+ if (!Number.isNaN(parsed) && parsed >= 0) {
42
+ span.setAttribute(name, parsed);
43
+ }
44
+ };
45
+
39
46
  /**
40
47
  * Sets up tracing for the application using OpenTelemetry.
41
48
  *
@@ -120,11 +127,6 @@ export function setupTracing(options = {}) {
120
127
  // explicit config, with the recommended context manager.
121
128
  tracerProvider.register();
122
129
 
123
- // Ignore spans from static assets.
124
- const ignoreIncomingRequestHook = (req) => {
125
- return req.url.startsWith('/metrics') || req.url.startsWith('/healthz');
126
- };
127
-
128
130
  // Hook to set peer service name for outgoing requests
129
131
  const applyCustomAttributesOnSpan = (span, request) => {
130
132
  const url = request?.url || request?.uri || '';
@@ -149,7 +151,8 @@ export function setupTracing(options = {}) {
149
151
  const instrumentations = [
150
152
  new HttpInstrumentation({
151
153
  serverName: serviceName,
152
- ignoreIncomingRequestHook,
154
+ // Ignore spans from static assets (metrics/health probes).
155
+ ignoreIncomingRequestHook: (req) => req.url.startsWith('/metrics') || req.url.startsWith('/healthz'),
153
156
  applyCustomAttributesOnSpan,
154
157
  requestHook: (span, request) => {
155
158
  // Enrich spans with additional HTTP request attributes
@@ -168,13 +171,7 @@ export function setupTracing(options = {}) {
168
171
  if (userAgent) span.setAttribute('http.user_agent', userAgent);
169
172
  if (contentType) span.setAttribute('http.request.content_type', contentType);
170
173
 
171
- // Safe integer parsing with validation
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
- }
174
+ setIntAttribute(span, 'http.request.content_length', contentLength);
178
175
 
179
176
  // Correlation headers for distributed tracing
180
177
  if (requestId) span.setAttribute('http.request_id', requestId);
@@ -191,12 +188,7 @@ export function setupTracing(options = {}) {
191
188
 
192
189
  if (contentType) span.setAttribute('http.response.content_type', contentType);
193
190
 
194
- if (contentLength) {
195
- const length = parseInt(contentLength, 10);
196
- if (!Number.isNaN(length) && length >= 0) {
197
- span.setAttribute('http.response.content_length', length);
198
- }
199
- }
191
+ setIntAttribute(span, 'http.response.content_length', contentLength);
200
192
 
201
193
  if (requestId) span.setAttribute('http.request_id', requestId);
202
194
  },
@@ -282,20 +274,11 @@ export function setupTracing(options = {}) {
282
274
  }
283
275
  },
284
276
  responseHook: (span, cmdName, cmdArgs, response) => {
285
- // Ensure peer.service persists through response
286
- span.setAttribute('peer.service', 'redis');
287
- span.setAttribute('db.system', 'redis');
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
277
+ // peer.service, db.system and db.operation are already set on this span
278
+ // by requestHook and persist for the span's lifetime, so they are not
279
+ // re-set here. Record only the response shape for observability.
295
280
  if (response !== undefined && response !== null) {
296
- const responseType = typeof response;
297
- span.setAttribute('db.response.type', responseType);
298
-
281
+ span.setAttribute('db.response.type', typeof response);
299
282
  if (Array.isArray(response)) {
300
283
  span.setAttribute('db.response.count', response.length);
301
284
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saidsef/tracing-node",
3
- "version": "3.22.0",
3
+ "version": "3.22.2",
4
4
  "description": "tracing NodeJS - Wrapper for OpenTelemetry instrumentation packages",
5
5
  "main": "libs/index.mjs",
6
6
  "scripts": {
package/.eslintrc.yml DELETED
@@ -1,26 +0,0 @@
1
- env:
2
- node: true
3
- commonjs: false
4
- es2021: true
5
- extends: eslint:recommended
6
- parserOptions:
7
- ecmaVersion: latest
8
- sourceType: module
9
- rules:
10
- "prefer-const": ["error", { "ignoreReadBeforeAssign": true }]
11
- arrow-spacing: ["error", { "before": true, "after": true }]
12
- computed-property-spacing: ["error", "never"]
13
- eol-last: ["error", "always"]
14
- no-compare-neg-zero: "error"
15
- no-tabs: ["error", { allowIndentationTabs: true }]
16
- no-trailing-spaces: "error"
17
- no-unused-vars: "error"
18
- no-whitespace-before-property: "error"
19
- object-curly-spacing: ["error", "never"]
20
- quotes: ["warn", "single"]
21
- rest-spread-spacing: ["error", "always"]
22
- semi: ["warn", "always"]
23
- space-before-blocks: "error"
24
- template-curly-spacing: "error"
25
- overrides:
26
- - files: [ "libs/index.mjs"]