ai-perf-sdk 1.0.0 → 1.0.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/package.json +1 -1
- package/src/sdk.js +87 -8
package/package.json
CHANGED
package/src/sdk.js
CHANGED
|
@@ -1,38 +1,117 @@
|
|
|
1
|
+
// 1. Force JSON Protocol (Critical for backend compatibility)
|
|
2
|
+
// MUST happen before requiring OpenTelemetry modules
|
|
3
|
+
if (!process.env.OTEL_EXPORTER_OTLP_PROTOCOL) {
|
|
4
|
+
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = 'http/json';
|
|
5
|
+
}
|
|
6
|
+
|
|
1
7
|
const { NodeSDK } = require("@opentelemetry/sdk-node");
|
|
2
8
|
const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
|
|
3
9
|
const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http");
|
|
10
|
+
const { diag, DiagConsoleLogger, DiagLogLevel } = require("@opentelemetry/api");
|
|
4
11
|
const defaults = require("./config");
|
|
5
12
|
|
|
6
13
|
let sdkInstance = null;
|
|
7
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Initializes the AI Performance SDK.
|
|
17
|
+
* @param {Object} options Configuration options
|
|
18
|
+
* @param {string} [options.collectorEndpoint] - URL of the telemetry collector
|
|
19
|
+
* @param {Object} [options.headers] - Headers to send with telemetry (e.g., x-session-id)
|
|
20
|
+
* @param {string} [options.serviceName] - Name of the service
|
|
21
|
+
* @param {boolean} [options.debug] - Enable debug logging
|
|
22
|
+
* @param {boolean} [options.disableDbInstrumentation] - Disable DB auto-instrumentation (MongoDB/Mongoose/Redis) to prevent crashes
|
|
23
|
+
*/
|
|
8
24
|
function startSDK(options = {}) {
|
|
9
|
-
if (sdkInstance)
|
|
25
|
+
if (sdkInstance) {
|
|
26
|
+
if (options.debug) console.log("[AI-PERF] SDK already started.");
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
10
29
|
|
|
30
|
+
// 2. Merge Configuration
|
|
11
31
|
const config = {
|
|
12
32
|
...defaults,
|
|
13
33
|
...options
|
|
14
34
|
};
|
|
15
35
|
|
|
36
|
+
// 3. Enable Debug Logging if requested
|
|
37
|
+
if (config.debug) {
|
|
38
|
+
console.log("[AI-PERF] Debug Mode Enabled");
|
|
39
|
+
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// 4. Robust Header Handling
|
|
43
|
+
// Merge headers from:
|
|
44
|
+
// a. Environment Variable OTEL_EXPORTER_OTLP_HEADERS (Standard)
|
|
45
|
+
// b. options.headers (User provided)
|
|
46
|
+
// c. defaults.headers (If any)
|
|
47
|
+
|
|
48
|
+
let combinedHeaders = { ...config.headers };
|
|
49
|
+
|
|
50
|
+
// Parse OTEL_EXPORTER_OTLP_HEADERS env var manually to ensure they aren't lost
|
|
51
|
+
if (process.env.OTEL_EXPORTER_OTLP_HEADERS) {
|
|
52
|
+
try {
|
|
53
|
+
const envHeaders = process.env.OTEL_EXPORTER_OTLP_HEADERS.split(',').reduce((acc, pair) => {
|
|
54
|
+
const [k, v] = pair.split('=');
|
|
55
|
+
if (k && v) acc[k.trim()] = v.trim();
|
|
56
|
+
return acc;
|
|
57
|
+
}, {});
|
|
58
|
+
combinedHeaders = { ...combinedHeaders, ...envHeaders };
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.warn("[AI-PERF] Failed to parse OTEL_EXPORTER_OTLP_HEADERS", e);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (config.debug) {
|
|
65
|
+
console.log("[AI-PERF] Exporter Config:", {
|
|
66
|
+
url: config.collectorEndpoint,
|
|
67
|
+
headers: combinedHeaders,
|
|
68
|
+
protocol: process.env.OTEL_EXPORTER_OTLP_PROTOCOL
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
16
72
|
const exporter = new OTLPTraceExporter({
|
|
17
|
-
url: config.collectorEndpoint
|
|
73
|
+
url: config.collectorEndpoint,
|
|
74
|
+
headers: combinedHeaders
|
|
18
75
|
});
|
|
19
76
|
|
|
77
|
+
// 5. Configure Auto-Instrumentation
|
|
78
|
+
// Allow disabling DB instrumentation to prevent crashes (e.g. Mongoose 8.x conflicts)
|
|
79
|
+
const instrumentationConfig = {};
|
|
80
|
+
|
|
81
|
+
if (config.disableDbInstrumentation) {
|
|
82
|
+
if (config.debug) console.log("[AI-PERF] Disabling DB Instrumentations");
|
|
83
|
+
instrumentationConfig['@opentelemetry/instrumentation-mongodb'] = { enabled: false };
|
|
84
|
+
instrumentationConfig['@opentelemetry/instrumentation-mongoose'] = { enabled: false };
|
|
85
|
+
instrumentationConfig['@opentelemetry/instrumentation-redis'] = { enabled: false };
|
|
86
|
+
instrumentationConfig['@opentelemetry/instrumentation-ioredis'] = { enabled: false };
|
|
87
|
+
instrumentationConfig['@opentelemetry/instrumentation-pg'] = { enabled: false };
|
|
88
|
+
instrumentationConfig['@opentelemetry/instrumentation-mysql'] = { enabled: false };
|
|
89
|
+
instrumentationConfig['@opentelemetry/instrumentation-mysql2'] = { enabled: false };
|
|
90
|
+
}
|
|
91
|
+
|
|
20
92
|
sdkInstance = new NodeSDK({
|
|
21
93
|
serviceName: config.serviceName,
|
|
22
94
|
traceExporter: exporter,
|
|
23
|
-
instrumentations: [getNodeAutoInstrumentations()]
|
|
95
|
+
instrumentations: [getNodeAutoInstrumentations(instrumentationConfig)]
|
|
24
96
|
});
|
|
25
97
|
|
|
26
|
-
|
|
27
|
-
|
|
98
|
+
try {
|
|
99
|
+
sdkInstance.start();
|
|
100
|
+
console.log(`[AI-PERF] SDK started for ${config.serviceName} ` + (config.disableDbInstrumentation ? "(Safe Mode)" : ""));
|
|
101
|
+
} catch (e) {
|
|
102
|
+
console.error("[AI-PERF] Failed to start SDK:", e);
|
|
103
|
+
}
|
|
28
104
|
}
|
|
29
105
|
|
|
30
106
|
function shutdownSDK() {
|
|
31
107
|
if (!sdkInstance) return;
|
|
32
108
|
|
|
33
|
-
sdkInstance.shutdown()
|
|
34
|
-
|
|
35
|
-
|
|
109
|
+
sdkInstance.shutdown().then(() => {
|
|
110
|
+
console.log("[AI-PERF] SDK shutdown");
|
|
111
|
+
sdkInstance = null;
|
|
112
|
+
}).catch((error) => {
|
|
113
|
+
console.error("[AI-PERF] Error shutting down SDK", error);
|
|
114
|
+
});
|
|
36
115
|
}
|
|
37
116
|
|
|
38
117
|
module.exports = {
|