securenow 3.0.6 → 3.0.7
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/package.json +1 -1
- package/tracing.js +42 -57
package/package.json
CHANGED
package/tracing.js
CHANGED
|
@@ -2,18 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Preload with: NODE_OPTIONS="-r securenow/register"
|
|
5
|
-
*
|
|
6
5
|
* Env:
|
|
7
6
|
* SECURENOW_INSTANCE=http://host:4318
|
|
8
7
|
* OTEL_EXPORTER_OTLP_ENDPOINT=...
|
|
9
8
|
* OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=...
|
|
10
9
|
* OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer abc123,foo=bar"
|
|
11
|
-
* OTEL_SERVICE_NAME=logical-name
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* SECURENOW_DISABLE_INSTRUMENTATIONS="@opentelemetry/instrumentation-mongodb,@opentelemetry/instrumentation-redis"
|
|
10
|
+
* OTEL_SERVICE_NAME=logical-name | SECURENOW_APPID=logical-name
|
|
11
|
+
* SECURENOW_NO_UUID=1 # one service.name across all workers
|
|
12
|
+
* SECURENOW_DISABLE_INSTRUMENTATIONS="pkg1,pkg2"
|
|
15
13
|
* OTEL_LOG_LEVEL=info|debug
|
|
16
|
-
* SECURENOW_TEST_SPAN=1
|
|
14
|
+
* SECURENOW_TEST_SPAN=1
|
|
17
15
|
*/
|
|
18
16
|
|
|
19
17
|
const { diag, DiagConsoleLogger, DiagLogLevel } = require('@opentelemetry/api');
|
|
@@ -24,71 +22,67 @@ const { SemanticResourceAttributes } = require('@opentelemetry/semantic-conventi
|
|
|
24
22
|
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
|
|
25
23
|
const { v4: uuidv4 } = require('uuid');
|
|
26
24
|
|
|
27
|
-
// -------------------- helpers --------------------
|
|
28
25
|
const env = k => process.env[k] ?? process.env[k.toUpperCase()] ?? process.env[k.toLowerCase()];
|
|
29
|
-
const parseHeaders =
|
|
30
|
-
const out = {};
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
if (!s) continue;
|
|
35
|
-
const i = s.indexOf('=');
|
|
36
|
-
if (i === -1) continue;
|
|
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;
|
|
37
31
|
out[s.slice(0, i).trim().toLowerCase()] = s.slice(i + 1).trim();
|
|
38
32
|
}
|
|
39
33
|
return out;
|
|
40
34
|
};
|
|
41
35
|
|
|
42
|
-
//
|
|
36
|
+
// Diagnostics
|
|
43
37
|
(() => {
|
|
44
38
|
const L = (env('OTEL_LOG_LEVEL') || '').toLowerCase();
|
|
45
|
-
const level =
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
L === 'error' ? DiagLogLevel.ERROR : DiagLogLevel.NONE;
|
|
39
|
+
const level = L === 'debug' ? DiagLogLevel.DEBUG :
|
|
40
|
+
L === 'info' ? DiagLogLevel.INFO :
|
|
41
|
+
L === 'warn' ? DiagLogLevel.WARN :
|
|
42
|
+
L === 'error' ? DiagLogLevel.ERROR : DiagLogLevel.NONE;
|
|
50
43
|
diag.setLogger(new DiagConsoleLogger(), level);
|
|
51
|
-
console.log('[securenow] preload loaded pid
|
|
44
|
+
console.log('[securenow] preload loaded pid=%d', process.pid);
|
|
52
45
|
})();
|
|
53
46
|
|
|
54
|
-
//
|
|
47
|
+
// Endpoint config
|
|
55
48
|
const endpointBase = (env('SECURENOW_INSTANCE') || env('OTEL_EXPORTER_OTLP_ENDPOINT') || 'http://46.62.173.237:4318').replace(/\/$/, '');
|
|
56
49
|
const tracesUrl = env('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT') || `${endpointBase}/v1/traces`;
|
|
57
50
|
const headers = parseHeaders(env('OTEL_EXPORTER_OTLP_HEADERS'));
|
|
58
51
|
|
|
59
|
-
//
|
|
60
|
-
const rawBase =
|
|
61
|
-
(env('OTEL_SERVICE_NAME') || env('SECURENOW_APPID') || '').trim().replace(/^['"]|['"]$/g, '');
|
|
52
|
+
// Naming rules
|
|
53
|
+
const rawBase = (env('OTEL_SERVICE_NAME') || env('SECURENOW_APPID') || '').trim().replace(/^['"]|['"]$/g, '');
|
|
62
54
|
const baseName = rawBase || null;
|
|
63
|
-
|
|
64
|
-
// SECURENOW_NO_UUID=1 -> keep one service.name for all workers (baseName), but unique instance ids
|
|
65
55
|
const noUuid = String(env('SECURENOW_NO_UUID')) === '1' || String(env('SECURENOW_NO_UUID')).toLowerCase() === 'true';
|
|
66
56
|
|
|
57
|
+
// service.name
|
|
67
58
|
let serviceName;
|
|
68
59
|
if (baseName) {
|
|
69
60
|
serviceName = noUuid ? baseName : `${baseName}-${uuidv4()}`;
|
|
70
61
|
} else {
|
|
71
|
-
// No provided name: safe fallback, unique per process
|
|
72
62
|
serviceName = `securenow-free-${uuidv4()}`;
|
|
73
63
|
}
|
|
74
64
|
|
|
75
|
-
//
|
|
65
|
+
// service.instance.id (always appID+uuid if appID provided)
|
|
76
66
|
const instancePrefix = baseName || 'securenow';
|
|
77
67
|
const serviceInstanceId = `${instancePrefix}-${uuidv4()}`;
|
|
78
68
|
|
|
79
|
-
|
|
69
|
+
// One loud line per worker
|
|
70
|
+
console.log('[securenow] pid=%d SECURENOW_APPID=%s OTEL_SERVICE_NAME=%s → service.name=%s instance.id=%s',
|
|
71
|
+
process.pid,
|
|
72
|
+
JSON.stringify(env('SECURENOW_APPID')),
|
|
73
|
+
JSON.stringify(env('OTEL_SERVICE_NAME')),
|
|
74
|
+
serviceName,
|
|
75
|
+
serviceInstanceId
|
|
76
|
+
);
|
|
80
77
|
|
|
81
|
-
//
|
|
82
|
-
const disabledList = (env('SECURENOW_DISABLE_INSTRUMENTATIONS') || '')
|
|
83
|
-
.split(',')
|
|
84
|
-
.map(s => s.trim())
|
|
85
|
-
.filter(Boolean);
|
|
78
|
+
// Instrumentations (allow selective disables)
|
|
86
79
|
const disabledMap = {};
|
|
87
|
-
for (const
|
|
80
|
+
for (const n of (env('SECURENOW_DISABLE_INSTRUMENTATIONS') || '').split(',').map(s => s.trim()).filter(Boolean)) {
|
|
81
|
+
disabledMap[n] = { enabled: false };
|
|
82
|
+
}
|
|
88
83
|
|
|
89
|
-
//
|
|
84
|
+
// SDK
|
|
90
85
|
const traceExporter = new OTLPTraceExporter({ url: tracesUrl, headers });
|
|
91
|
-
|
|
92
86
|
const sdk = new NodeSDK({
|
|
93
87
|
traceExporter,
|
|
94
88
|
instrumentations: getNodeAutoInstrumentations({ ...disabledMap }),
|
|
@@ -100,35 +94,26 @@ const sdk = new NodeSDK({
|
|
|
100
94
|
}),
|
|
101
95
|
});
|
|
102
96
|
|
|
103
|
-
//
|
|
97
|
+
// Start (works if start() is sync or async)
|
|
104
98
|
(async () => {
|
|
105
99
|
try {
|
|
106
100
|
await Promise.resolve(sdk.start?.());
|
|
107
|
-
console.log('[securenow] OTel SDK started →', tracesUrl);
|
|
108
|
-
|
|
101
|
+
console.log('[securenow] OTel SDK started → %s', tracesUrl);
|
|
109
102
|
if (String(env('SECURENOW_TEST_SPAN')) === '1') {
|
|
110
103
|
const api = require('@opentelemetry/api');
|
|
111
104
|
const tracer = api.trace.getTracer('securenow-smoke');
|
|
112
|
-
const span = tracer.startSpan('securenow.startup.smoke');
|
|
113
|
-
span.setAttribute('securenow.test', true);
|
|
114
|
-
span.end();
|
|
115
|
-
console.log('[securenow] emitted startup smoke span');
|
|
105
|
+
const span = tracer.startSpan('securenow.startup.smoke'); span.end();
|
|
116
106
|
}
|
|
117
|
-
} catch (
|
|
118
|
-
console.error('[securenow] OTel start failed:',
|
|
107
|
+
} catch (e) {
|
|
108
|
+
console.error('[securenow] OTel start failed:', e && e.stack || e);
|
|
119
109
|
}
|
|
120
110
|
})();
|
|
121
111
|
|
|
122
|
-
//
|
|
112
|
+
// Shutdown
|
|
123
113
|
async function safeShutdown(sig) {
|
|
124
|
-
try {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
} catch (err) {
|
|
128
|
-
console.error('[securenow] Tracing shutdown error:', err);
|
|
129
|
-
} finally {
|
|
130
|
-
process.exit(0);
|
|
131
|
-
}
|
|
114
|
+
try { await Promise.resolve(sdk.shutdown?.()); console.log(`[securenow] Tracing terminated on ${sig}`); }
|
|
115
|
+
catch (e) { console.error('[securenow] Tracing shutdown error:', e); }
|
|
116
|
+
finally { process.exit(0); }
|
|
132
117
|
}
|
|
133
118
|
process.on('SIGINT', () => safeShutdown('SIGINT'));
|
|
134
119
|
process.on('SIGTERM', () => safeShutdown('SIGTERM'));
|