securenow 4.0.11 → 4.0.12

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.
Files changed (2) hide show
  1. package/nextjs.js +78 -27
  2. package/package.json +1 -1
package/nextjs.js CHANGED
@@ -23,6 +23,16 @@ const { v4: uuidv4 } = require('uuid');
23
23
 
24
24
  const env = k => process.env[k] ?? process.env[k.toUpperCase()] ?? process.env[k.toLowerCase()];
25
25
 
26
+ const parseHeaders = str => {
27
+ const out = {}; if (!str) return out;
28
+ for (const raw of String(str).split(',')) {
29
+ const s = raw.trim(); if (!s) continue;
30
+ const i = s.indexOf('='); if (i === -1) continue;
31
+ out[s.slice(0, i).trim().toLowerCase()] = s.slice(i + 1).trim();
32
+ }
33
+ return out;
34
+ };
35
+
26
36
  let isRegistered = false;
27
37
 
28
38
  // Default sensitive fields to redact from request bodies
@@ -215,6 +225,9 @@ function registerSecureNow(options = {}) {
215
225
  return;
216
226
  }
217
227
 
228
+ // Detect environment outside try block for error handling
229
+ const isVercel = !!(env('VERCEL') || env('VERCEL_ENV') || env('VERCEL_URL'));
230
+
218
231
  try {
219
232
  console.log('[securenow] Next.js integration loading (pid=%d)', process.pid);
220
233
 
@@ -264,8 +277,12 @@ function registerSecureNow(options = {}) {
264
277
  const customSensitiveFields = (env('SECURENOW_SENSITIVE_FIELDS') || '').split(',').map(s => s.trim()).filter(Boolean);
265
278
  const allSensitiveFields = [...DEFAULT_SENSITIVE_FIELDS, ...customSensitiveFields];
266
279
 
267
- // -------- Use @vercel/otel with enhanced configuration --------
268
- const { registerOTel } = require('@vercel/otel');
280
+ // -------- Log environment detection --------
281
+ if (!isVercel) {
282
+ console.log('[securenow] 🖥️ Self-hosted environment detected (EC2/PM2) - using vanilla SDK');
283
+ }
284
+
285
+ // -------- Use different initialization based on environment --------
269
286
  const { HttpInstrumentation } = require('@opentelemetry/instrumentation-http');
270
287
 
271
288
  // Configure HTTP instrumentation with comprehensive header capture
@@ -433,31 +450,61 @@ function registerSecureNow(options = {}) {
433
450
  },
434
451
  });
435
452
 
436
- registerOTel({
437
- serviceName: serviceName,
438
- attributes: {
439
- 'deployment.environment': env('NODE_ENV') || env('VERCEL_ENV') || 'development',
440
- 'service.version': process.env.npm_package_version || process.env.VERCEL_GIT_COMMIT_SHA || undefined,
441
- 'vercel.region': process.env.VERCEL_REGION || undefined,
442
- },
443
- instrumentations: [httpInstrumentation],
444
- instrumentationConfig: {
445
- fetch: {
446
- // Propagate context to your backend APIs
447
- propagateContextUrls: [
448
- /^https?:\/\/localhost/,
449
- /^https?:\/\/.*\.vercel\.app/,
450
- // Add your backend domains here
451
- ],
452
- // Optionally ignore certain URLs
453
- ignoreUrls: [
454
- /_next\/static/,
455
- /_next\/image/,
456
- /\.map$/,
457
- ],
453
+ if (isVercel) {
454
+ // -------- Vercel Environment: Use @vercel/otel --------
455
+ const { registerOTel } = require('@vercel/otel');
456
+
457
+ registerOTel({
458
+ serviceName: serviceName,
459
+ attributes: {
460
+ 'deployment.environment': env('NODE_ENV') || env('VERCEL_ENV') || 'development',
461
+ 'service.version': process.env.npm_package_version || process.env.VERCEL_GIT_COMMIT_SHA || undefined,
462
+ 'vercel.region': process.env.VERCEL_REGION || undefined,
458
463
  },
459
- },
460
- });
464
+ instrumentations: [httpInstrumentation],
465
+ instrumentationConfig: {
466
+ fetch: {
467
+ // Propagate context to your backend APIs
468
+ propagateContextUrls: [
469
+ /^https?:\/\/localhost/,
470
+ /^https?:\/\/.*\.vercel\.app/,
471
+ // Add your backend domains here
472
+ ],
473
+ // Optionally ignore certain URLs
474
+ ignoreUrls: [
475
+ /_next\/static/,
476
+ /_next\/image/,
477
+ /\.map$/,
478
+ ],
479
+ },
480
+ },
481
+ });
482
+ } else {
483
+ // -------- Self-Hosted (EC2/PM2): Use Vanilla OpenTelemetry SDK --------
484
+ const { NodeSDK } = require('@opentelemetry/sdk-node');
485
+ const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
486
+ const { Resource } = require('@opentelemetry/resources');
487
+ const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventions');
488
+
489
+ const traceExporter = new OTLPTraceExporter({
490
+ url: tracesUrl,
491
+ headers: parseHeaders(env('OTEL_EXPORTER_OTLP_HEADERS'))
492
+ });
493
+
494
+ const sdk = new NodeSDK({
495
+ serviceName: serviceName,
496
+ traceExporter: traceExporter,
497
+ instrumentations: [httpInstrumentation],
498
+ resource: new Resource({
499
+ [SemanticResourceAttributes.SERVICE_NAME]: serviceName,
500
+ [SemanticResourceAttributes.DEPLOYMENT_ENVIRONMENT]: env('NODE_ENV') || env('VERCEL_ENV') || 'production',
501
+ [SemanticResourceAttributes.SERVICE_VERSION]: process.env.npm_package_version || undefined,
502
+ }),
503
+ });
504
+
505
+ sdk.start();
506
+ console.log('[securenow] 🎯 Vanilla SDK initialized for self-hosted environment');
507
+ }
461
508
 
462
509
  isRegistered = true;
463
510
  console.log('[securenow] ✅ OpenTelemetry started for Next.js → %s', tracesUrl);
@@ -485,7 +532,11 @@ function registerSecureNow(options = {}) {
485
532
 
486
533
  } catch (error) {
487
534
  console.error('[securenow] Failed to initialize OpenTelemetry:', error);
488
- console.error('[securenow] Make sure you have @vercel/otel installed: npm install @vercel/otel');
535
+ if (isVercel) {
536
+ console.error('[securenow] Make sure you have @vercel/otel installed: npm install @vercel/otel');
537
+ } else {
538
+ console.error('[securenow] Make sure OpenTelemetry dependencies are installed');
539
+ }
489
540
  }
490
541
  }
491
542
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "securenow",
3
- "version": "4.0.11",
3
+ "version": "4.0.12",
4
4
  "description": "OpenTelemetry instrumentation for Node.js and Next.js - Send traces to SigNoz or any OTLP backend",
5
5
  "type": "commonjs",
6
6
  "main": "register.js",