ai-perf-sdk 1.0.2 → 1.0.3

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 +16 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-perf-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
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,9 +1,3 @@
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
-
7
1
  const { NodeSDK } = require("@opentelemetry/sdk-node");
8
2
  const { getNodeAutoInstrumentations } = require("@opentelemetry/auto-instrumentations-node");
9
3
  const { OTLPTraceExporter } = require("@opentelemetry/exporter-trace-otlp-http");
@@ -27,27 +21,21 @@ function startSDK(options = {}) {
27
21
  return;
28
22
  }
29
23
 
30
- // 2. Merge Configuration
24
+ // 1. Merge Configuration
31
25
  const config = {
32
26
  ...defaults,
33
27
  ...options
34
28
  };
35
29
 
36
- // 3. Enable Debug Logging if requested
30
+ // 2. Enable Debug Logging if requested
37
31
  if (config.debug) {
38
32
  console.log("[AI-PERF] Debug Mode Enabled");
39
33
  diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
40
34
  }
41
35
 
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
-
36
+ // 3. Robust Header Handling
48
37
  let combinedHeaders = { ...config.headers };
49
38
 
50
- // Parse OTEL_EXPORTER_OTLP_HEADERS env var manually to ensure they aren't lost
51
39
  if (process.env.OTEL_EXPORTER_OTLP_HEADERS) {
52
40
  try {
53
41
  const envHeaders = process.env.OTEL_EXPORTER_OTLP_HEADERS.split(',').reduce((acc, pair) => {
@@ -65,7 +53,7 @@ function startSDK(options = {}) {
65
53
  console.log("[AI-PERF] Exporter Config:", {
66
54
  url: config.collectorEndpoint,
67
55
  headers: combinedHeaders,
68
- protocol: process.env.OTEL_EXPORTER_OTLP_PROTOCOL
56
+ protocol: process.env.OTEL_EXPORTER_OTLP_PROTOCOL || 'default (protobuf)'
69
57
  });
70
58
  }
71
59
 
@@ -74,8 +62,7 @@ function startSDK(options = {}) {
74
62
  headers: combinedHeaders
75
63
  });
76
64
 
77
- // 5. Configure Auto-Instrumentation
78
- // Allow disabling DB instrumentation to prevent crashes (e.g. Mongoose 8.x conflicts)
65
+ // 4. Configure Auto-Instrumentation
79
66
  const instrumentationConfig = {};
80
67
 
81
68
  if (config.disableDbInstrumentation) {
@@ -98,6 +85,17 @@ function startSDK(options = {}) {
98
85
  try {
99
86
  sdkInstance.start();
100
87
  console.log(`[AI-PERF] SDK started for ${config.serviceName} ` + (config.disableDbInstrumentation ? "(Safe Mode)" : ""));
88
+
89
+ // 5. Automatic Graceful Shutdown
90
+ const handleShutdown = async () => {
91
+ console.log("[AI-PERF] Graceful shutdown initiated...");
92
+ await shutdownSDK();
93
+ process.exit(0);
94
+ };
95
+
96
+ process.on('SIGTERM', handleShutdown);
97
+ process.on('SIGINT', handleShutdown);
98
+
101
99
  } catch (e) {
102
100
  console.error("[AI-PERF] Failed to start SDK:", e);
103
101
  }