securenow 5.6.1 → 5.7.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/register.js CHANGED
@@ -1,14 +1,49 @@
1
- // securenow/preload.js
1
+ // securenow/register.js — the only preload customers need:
2
+ // node --require securenow/register app.js
3
+ //
4
+ // For ESM apps ("type": "module"), this file auto-registers the
5
+ // OpenTelemetry ESM loader hook via module.register() (Node >=20.6).
6
+ // On older Node versions it falls back to a warning.
2
7
  'use strict';
3
8
 
4
- // load .env into process.env before anything else
9
+ // 1. load .env before anything else
5
10
  try {
6
11
  require('dotenv').config();
7
12
  console.log('[securenow] dotenv loaded from', process.env.DOTENV_CONFIG_PATH || '.env');
8
13
  } catch (e) {
9
- // dotenv is optional — only warn if it’s missing
10
14
  console.warn('[securenow] dotenv not found or failed to load');
11
15
  }
12
16
 
13
- // then run the real tracer preload
17
+ // 2. Auto-register the ESM loader hook so customers never need --import
18
+ (() => {
19
+ try {
20
+ const fs = require('fs');
21
+ const path = require('path');
22
+ const pkgPath = path.resolve(process.cwd(), 'package.json');
23
+ if (!fs.existsSync(pkgPath)) return;
24
+
25
+ const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
26
+ if (pkg.type !== 'module') return;
27
+
28
+ // Already registered via --import?
29
+ const execArgv = process.execArgv.join(' ');
30
+ if (execArgv.includes('hook.mjs') || execArgv.includes('import-in-the-middle')) return;
31
+
32
+ // Node >=20.6 exposes module.register() for programmatic ESM hooks
33
+ const mod = require('node:module');
34
+ if (typeof mod.register !== 'function') {
35
+ console.warn('[securenow] ESM app detected but Node %s lacks module.register().', process.version);
36
+ console.warn('[securenow] Upgrade to Node >=20.6 or add: --import @opentelemetry/instrumentation/hook.mjs');
37
+ return;
38
+ }
39
+
40
+ const { pathToFileURL } = require('node:url');
41
+ mod.register('@opentelemetry/instrumentation/hook.mjs', pathToFileURL(__filename));
42
+ console.log('[securenow] ESM loader hook auto-registered (module.register)');
43
+ } catch (_) {
44
+ // Non-fatal — tracing.js will show its own ESM warning if the hook is missing
45
+ }
46
+ })();
47
+
48
+ // 3. Run the OTel SDK setup
14
49
  require('./tracing');
package/tracing.js CHANGED
@@ -3,7 +3,9 @@
3
3
  /**
4
4
  * Preload with: node --require securenow/register app.js
5
5
  *
6
- * For ESM apps ("type": "module" in package.json), you MUST also add the ESM loader hook:
6
+ * Works for both CJS and ESM apps. On Node >=20.6 the ESM loader hook is
7
+ * auto-registered via module.register() — no --import flag needed.
8
+ * On Node 18 with "type": "module", add the hook manually:
7
9
  * node --import @opentelemetry/instrumentation/hook.mjs --require securenow/register app.js
8
10
  *
9
11
  * Env:
@@ -29,6 +31,7 @@ const { LoggerProvider, BatchLogRecordProcessor } = require('@opentelemetry/sdk-
29
31
  const { Resource } = require('@opentelemetry/resources');
30
32
  const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
31
33
  const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
34
+ const { MongoDBInstrumentation } = require('@opentelemetry/instrumentation-mongodb');
32
35
  const { v4: uuidv4 } = require('uuid');
33
36
 
34
37
  const env = k => process.env[k] ?? process.env[k.toUpperCase()] ?? process.env[k.toLowerCase()];
@@ -101,6 +104,9 @@ function redactGraphQLQuery(query, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
101
104
  }
102
105
 
103
106
  // -------- ESM detection --------
107
+ // register.js auto-registers the hook via module.register() on Node >=20.6.
108
+ // This warning only fires if BOTH --import AND module.register() were skipped
109
+ // (e.g. Node 18, or require('securenow/tracing') called directly without register.js).
104
110
  (() => {
105
111
  try {
106
112
  const fs = require('fs');
@@ -110,11 +116,11 @@ function redactGraphQLQuery(query, sensitiveFields = DEFAULT_SENSITIVE_FIELDS) {
110
116
  const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
111
117
  if (pkg.type === 'module') {
112
118
  const execArgv = process.execArgv.join(' ');
113
- const hasEsmHook = execArgv.includes('hook.mjs') || execArgv.includes('import-in-the-middle');
114
- if (!hasEsmHook) {
115
- console.warn('[securenow] ⚠️ ESM app detected ("type": "module") but no ESM loader hook found.');
116
- console.warn('[securenow] Instrumentations will NOT work without the ESM hook.');
117
- console.warn('[securenow] Fix: node --import @opentelemetry/instrumentation/hook.mjs --require securenow/register app.js');
119
+ const hasCliHook = execArgv.includes('hook.mjs') || execArgv.includes('import-in-the-middle');
120
+ const hasModuleRegister = typeof require('node:module').register === 'function';
121
+ if (!hasCliHook && !hasModuleRegister) {
122
+ console.warn('[securenow] ⚠️ ESM app detected ("type": "module") but no ESM loader hook available.');
123
+ console.warn('[securenow] Upgrade to Node >=20.6 (recommended) or add: --import @opentelemetry/instrumentation/hook.mjs');
118
124
  }
119
125
  }
120
126
  }
@@ -387,9 +393,11 @@ const sdk = new NodeSDK({
387
393
  traceExporter,
388
394
  instrumentations: [
389
395
  httpInstrumentation,
396
+ ...(disabledMap['@opentelemetry/instrumentation-mongodb'] ? [] : [new MongoDBInstrumentation()]),
390
397
  ...getNodeAutoInstrumentations({
391
398
  ...disabledMap,
392
- '@opentelemetry/instrumentation-http': { enabled: false }, // We use our custom one above
399
+ '@opentelemetry/instrumentation-http': { enabled: false },
400
+ '@opentelemetry/instrumentation-mongodb': { enabled: false },
393
401
  }),
394
402
  ],
395
403
  resource: sharedResource,