ai-perf-sdk 1.0.6 → 1.0.8
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/README.md +44 -0
- package/package.json +2 -2
- package/src/batcher.js +3 -2
- package/src/config.js +1 -1
- package/src/instrument.js +20 -21
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# ai-perf-sdk
|
|
2
|
+
|
|
3
|
+
A lightweight, plug-and-play performance monitoring SDK for Node.js.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install ai-perf-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Initialize the SDK at the very top of your application entry point:
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const { initPerformanceSDK } = require('ai-perf-sdk');
|
|
17
|
+
|
|
18
|
+
initPerformanceSDK({
|
|
19
|
+
endpoint: 'https://your-telemetry-endpoint.com/api/telemetry', // Required
|
|
20
|
+
serviceName: 'my-microservice',
|
|
21
|
+
environment: 'production',
|
|
22
|
+
batchSize: 10,
|
|
23
|
+
flushInterval: 5000
|
|
24
|
+
});
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Configuration Options
|
|
28
|
+
|
|
29
|
+
| Option | Type | Default | Description |
|
|
30
|
+
| --- | --- | --- | --- |
|
|
31
|
+
| `endpoint` | String | (Required) | The URL where telemetry data will be sent. |
|
|
32
|
+
| `serviceName` | String | `unknown-service` | The name of your service for identification. |
|
|
33
|
+
| `environment` | String | `production` | The environment name (e.g., development, staging). |
|
|
34
|
+
| `batchSize` | Number | `10` | Number of items to queue before sending a batch. |
|
|
35
|
+
| `flushInterval` | Number | `5000` | Time in ms between automatic flushes. |
|
|
36
|
+
|
|
37
|
+
## How it works
|
|
38
|
+
|
|
39
|
+
The SDK automatically instruments the `http` and `https` modules to capture:
|
|
40
|
+
- HTTP Request Latency
|
|
41
|
+
- Response Status Codes
|
|
42
|
+
- Outbound and Inbound request metrics
|
|
43
|
+
|
|
44
|
+
All data is batched and sent asynchronously to your specified endpoint.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-perf-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Plug-and-play custom performance monitoring SDK",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Akash",
|
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"node-fetch": "^2.7.0"
|
|
11
11
|
}
|
|
12
|
-
}
|
|
12
|
+
}
|
package/src/batcher.js
CHANGED
|
@@ -43,7 +43,7 @@ async function flush() {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
const payload = queue.splice(0, queue.length);
|
|
46
|
-
console.log(`[AI-PERF-SDK] Flushing ${payload.length} items...`);
|
|
46
|
+
console.log(`[AI-PERF-SDK] 🚀 Flushing ${payload.length} items to ${config.endpoint}...`);
|
|
47
47
|
|
|
48
48
|
try {
|
|
49
49
|
await sendBatch({
|
|
@@ -51,8 +51,9 @@ async function flush() {
|
|
|
51
51
|
headers: config.headers,
|
|
52
52
|
payload: payload
|
|
53
53
|
});
|
|
54
|
+
console.log(`[AI-PERF-SDK] ✅ Batch delivered successfully.`);
|
|
54
55
|
} catch (err) {
|
|
55
|
-
console.error("[AI-PERF-SDK] Flush failed. Returning items to queue.");
|
|
56
|
+
console.error("[AI-PERF-SDK] ❌ Flush failed. Returning items to queue.");
|
|
56
57
|
queue.unshift(...payload);
|
|
57
58
|
}
|
|
58
59
|
}
|
package/src/config.js
CHANGED
package/src/instrument.js
CHANGED
|
@@ -100,36 +100,35 @@ function instrumentHttp() {
|
|
|
100
100
|
return req;
|
|
101
101
|
};
|
|
102
102
|
|
|
103
|
-
// --- Inbound Request (Server) Patching ---
|
|
104
|
-
const
|
|
105
|
-
module.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
server.on('request', (req, res) => {
|
|
103
|
+
// --- Inbound Request (Server) Patching (PROTOTYPE METHOD) ---
|
|
104
|
+
const originalEmit = module.Server.prototype.emit;
|
|
105
|
+
module.Server.prototype.emit = function (event, req, res) {
|
|
106
|
+
if (event === 'request' && req && res) {
|
|
107
|
+
console.log(`[AI-PERF-SDK] Inbound Intercepted: ${req.method} ${req.url}`);
|
|
109
108
|
const start = performance.now();
|
|
110
109
|
const url = req.url;
|
|
111
110
|
const method = req.method;
|
|
112
111
|
|
|
113
112
|
// Ignore telemetry calls
|
|
114
|
-
if (url.includes('/api/telemetry'))
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
113
|
+
if (url && !url.includes('/api/telemetry')) {
|
|
114
|
+
res.on('finish', () => {
|
|
115
|
+
const duration = performance.now() - start;
|
|
116
|
+
enqueue({
|
|
117
|
+
type: "http-inbound",
|
|
118
|
+
url: url,
|
|
119
|
+
method: method,
|
|
120
|
+
duration: Math.round(duration),
|
|
121
|
+
status: res.statusCode,
|
|
122
|
+
timestamp: new Date().toISOString()
|
|
123
|
+
});
|
|
125
124
|
});
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
return server;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return originalEmit.apply(this, arguments);
|
|
130
128
|
};
|
|
131
129
|
});
|
|
132
130
|
}
|
|
133
131
|
|
|
132
|
+
|
|
134
133
|
module.exports = { instrumentHttp };
|
|
135
134
|
|