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