@oas-tools/oas-telemetry 0.5.1 → 0.6.0
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 +334 -43
- package/dist/config.cjs +6 -0
- package/dist/controllers/metricsController.cjs +48 -0
- package/dist/controllers/telemetryController.cjs +15 -15
- package/dist/exporters/InMemoryDBMetricsExporter.cjs +101 -0
- package/dist/exporters/InMemoryDbExporter.cjs +1 -1
- package/dist/index.cjs +33 -3
- package/dist/openTelemetry.cjs +39 -1
- package/dist/routes/metricsRoutes.cjs +15 -0
- package/dist/systemMetrics.cjs +97 -0
- package/package.json +4 -2
- package/src/config.js +5 -0
- package/src/controllers/metricsController.js +30 -0
- package/src/controllers/telemetryController.js +2 -1
- package/src/exporters/InMemoryDBMetricsExporter.js +111 -0
- package/src/exporters/InMemoryDbExporter.js +1 -1
- package/src/index.js +34 -5
- package/src/openTelemetry.js +49 -13
- package/src/routes/metricsRoutes.js +15 -0
- package/src/services/uiService.js +21 -21
- package/src/systemMetrics.js +102 -0
- package/dist/client.cjs +0 -14
- package/dist/middleware/auth.cjs +0 -17
- package/dist/telemetry.cjs +0 -23
- package/dist/types/exporters/InMemoryDbExporter.d.ts +0 -16
- package/dist/types/index.d.ts +0 -1
- package/dist/types/telemetry.d.ts +0 -2
- package/dist/types/ui.d.ts +0 -4
- package/dist/ui.cjs +0 -1200
package/README.md
CHANGED
|
@@ -1,53 +1,137 @@
|
|
|
1
1
|
# OAS TELEMETRY
|
|
2
2
|
|
|
3
|
-
OAS Telemetry offers an express middleware designed for collecting telemetry data using Open Telemetry in applications built using the OpenAPI Specification (OAS)
|
|
3
|
+
**OAS Telemetry** offers an express middleware designed for collecting telemetry data using **Open Telemetry** in applications built using the **OpenAPI Specification (OAS)**. This middleware allows developers to easily incorporate telemetry functionality into their APIs.
|
|
4
4
|
|
|
5
|
-
OAS Telemetry provides a set of endpoints that can be accessed to perform various actions related to telemetry data, such as starting and stopping data collection, resetting telemetry data, listing collected data, and searching for specific telemetry records. These endpoints can be easily integrated into an Express.js application, providing developers with a convenient way to manage and analyze telemetry data.
|
|
5
|
+
**OAS Telemetry** provides a set of endpoints that can be accessed to perform various actions related to telemetry data, such as starting and stopping data collection, resetting telemetry data, listing collected data, and searching for specific telemetry records. These endpoints can be easily integrated into an **Express.js** application, providing developers with a convenient way to manage and analyze telemetry data.
|
|
6
6
|
|
|
7
|
-
Additionally, OAS Telemetry offers customization options, allowing developers to configure the telemetry middleware according to their specific requirements.
|
|
7
|
+
Additionally, **OAS Telemetry** offers customization options, allowing developers to configure the telemetry middleware according to their specific requirements.
|
|
8
8
|
|
|
9
|
-
Overall, OAS Telemetry will serve as a valuable tool for developers looking to gain insights into the operation and performance of their OAS-based APIs
|
|
9
|
+
Overall, **OAS Telemetry** will serve as a valuable tool for developers looking to gain insights into the operation and performance of their **OAS-based APIs**, enabling them to monitor, debug, and optimize their applications effectively.
|
|
10
10
|
|
|
11
|
-
The package now supports both ES Module (ESM) and CommonJS (CJS) formats, making it compatible with a wide range of applications. Furthermore, OAS Telemetry provides a range of plugins to extend its functionality, enabling developers to tailor telemetry data collection, alerting, and reporting to meet specific requirements. See the [Telemetry Plugins](#telemetry-plugins) section for more information.
|
|
11
|
+
The package now supports both **ES Module (ESM)** and **CommonJS (CJS)** formats, making it compatible with a wide range of applications. Furthermore, **OAS Telemetry** provides a range of plugins to extend its functionality, enabling developers to tailor telemetry data collection, alerting, and reporting to meet specific requirements. See the [Telemetry Plugins](#telemetry-plugins) section for more information.
|
|
12
12
|
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
This section provides an overview of how to install and integrate **OAS Telemetry** into your EXISTING **Express.js** application. If you want to create a new example express application with **OAS Telemetry** integrated, refer to the [Full Examples](#full-examples) section at the end of this document.
|
|
13
16
|
|
|
17
|
+
First, install the package using npm:
|
|
14
18
|
|
|
19
|
+
```sh
|
|
20
|
+
npm install @oas-tools/oas-telemetry
|
|
21
|
+
```
|
|
15
22
|
|
|
23
|
+
You can integrate the middleware into your Express application. The `spec` option is the OpenAPI Specification (OAS) content in JSON or YAML format. While this configuration is optional, it is recommended for the UI to function correctly.
|
|
24
|
+
|
|
25
|
+
### Using ES Modules (ESM)
|
|
26
|
+
|
|
27
|
+
Add the following lines to your `index.js` file:
|
|
16
28
|
|
|
17
|
-
## Usage
|
|
18
|
-
To use the middelware add this two lines in your index.js (ESM):
|
|
19
29
|
```js
|
|
20
|
-
import
|
|
21
|
-
import
|
|
30
|
+
// This import MUST be at the top of the file
|
|
31
|
+
import oasTelemetry from '@oas-tools/oas-telemetry';
|
|
32
|
+
import { readFileSync } from 'fs';
|
|
33
|
+
|
|
34
|
+
// ...rest of your code here creating an express app
|
|
22
35
|
|
|
23
36
|
app.use(oasTelemetry({
|
|
24
|
-
spec
|
|
25
|
-
}))
|
|
37
|
+
spec: readFileSync('./spec/oas.yaml', { encoding: 'utf8', flag: 'r' })
|
|
38
|
+
}));
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Using CommonJS
|
|
42
|
+
|
|
43
|
+
Add the following lines to your `index.js` file:
|
|
26
44
|
|
|
45
|
+
```js
|
|
46
|
+
// This require MUST be at the top of the file
|
|
47
|
+
const oasTelemetry = require('@oas-tools/oas-telemetry');
|
|
48
|
+
const { readFileSync } = require('fs');
|
|
49
|
+
|
|
50
|
+
// ...rest of your code here creating an express app
|
|
51
|
+
|
|
52
|
+
app.use(oasTelemetry({
|
|
53
|
+
spec: readFileSync('./spec/oas.yaml', { encoding: 'utf8', flag: 'r' })
|
|
54
|
+
}));
|
|
27
55
|
```
|
|
28
56
|
|
|
57
|
+
For complete examples of a working API with OAS Telemetry enabled, refer to the [Full Examples](#full-examples) section at the end of this document.
|
|
58
|
+
|
|
29
59
|
## Custom Configuration
|
|
30
60
|
|
|
31
61
|
You can also customize the telemetry configuration by passing options to the middleware function. For example:
|
|
62
|
+
|
|
32
63
|
```js
|
|
33
64
|
const customTelemetryConfig = {
|
|
34
|
-
|
|
35
|
-
|
|
65
|
+
spec: /* OAS content in json or yaml */, // Highly recommended
|
|
66
|
+
baseURL: "/custom-telemetry", //default is "/telemetry"
|
|
67
|
+
autoActivate: false, //default is true, whether to start telemetry data collection automatically
|
|
68
|
+
authEnabled: true, //default is false
|
|
69
|
+
apiKeyMaxAge: 1000 * 60 * 30, // 30 minutes
|
|
70
|
+
password: "custom-password", //default is "oas-telemetry-password"
|
|
71
|
+
jwtSecret: "custom-secret", //default is "oas-telemetry-secret"
|
|
72
|
+
exporter: myCustomExporter, // Experimental, just for devs
|
|
36
73
|
};
|
|
37
74
|
|
|
38
75
|
app.use(oasTelemetry(customTelemetryConfig));
|
|
39
76
|
```
|
|
40
77
|
|
|
78
|
+
**Note:** To disable the module, set the environment variable `OASTLM_MODULE_DISABLED` to `'true'`.
|
|
79
|
+
|
|
41
80
|
## Telemetry UI
|
|
42
81
|
|
|
43
|
-
You can access the telemetry UI in the endpoint
|
|
82
|
+
You can access the telemetry UI in the endpoint `/telemetry` (or `/custom-telemetry` if you set the `baseURL` option). This UI provides a user-friendly interface to interact with the telemetry data collected by the middleware.
|
|
83
|
+
|
|
84
|
+
## Metrics Development (Temporary)
|
|
85
|
+
|
|
86
|
+
This feature is currently in development. The following endpoints are available under <baseURL>/metrics (e.g., /telemetry/metrics):
|
|
87
|
+
|
|
88
|
+
- GET /
|
|
89
|
+
- POST /find
|
|
90
|
+
- GET /reset
|
|
91
|
+
|
|
92
|
+
Expect an array of metrics objects. Each object includes data like a timestamp, cpuUsageData, processCpuUsageData, memoryData, and processMemoryData. Example snippet for one CPU core (followed by others in the array):
|
|
93
|
+
|
|
94
|
+
```json
|
|
95
|
+
{
|
|
96
|
+
"timestamp": 1741717005911,
|
|
97
|
+
"cpuUsageData": [
|
|
98
|
+
{
|
|
99
|
+
"cpuNumber": "0",
|
|
100
|
+
"idle": 60486.234000000004,
|
|
101
|
+
"user": 1364.515,
|
|
102
|
+
"system": 1246.796,
|
|
103
|
+
"interrupt": 167,
|
|
104
|
+
"nice": 0,
|
|
105
|
+
"userP": 0.009375623379214043,
|
|
106
|
+
"systemP": 0.002992220227408737,
|
|
107
|
+
"idleP": 0.9850388988629563,
|
|
108
|
+
"interruptP": 0,
|
|
109
|
+
"niceP": 0
|
|
110
|
+
}
|
|
111
|
+
],
|
|
112
|
+
"processCpuUsageData": {
|
|
113
|
+
"user": 0.968,
|
|
114
|
+
"system": 0.32799999999999996,
|
|
115
|
+
"userP": 0,
|
|
116
|
+
"systemP": 0
|
|
117
|
+
},
|
|
118
|
+
"memoryData": {
|
|
119
|
+
"used": 15726522368,
|
|
120
|
+
"free": 18437427200,
|
|
121
|
+
"usedP": 0.4603250668280579,
|
|
122
|
+
"freeP": 0.539674933171942
|
|
123
|
+
},
|
|
124
|
+
"processMemoryData": 75988992,
|
|
125
|
+
"_id": "6mXKM8uK7xSOqJVT"
|
|
126
|
+
}
|
|
127
|
+
```
|
|
44
128
|
|
|
129
|
+
The shape of these objects may change as development continues.
|
|
45
130
|
|
|
46
131
|
## API Telemetry Endpoints
|
|
47
132
|
|
|
48
133
|
OAS Telemetry middleware adds the following endpoints to your Express application:
|
|
49
134
|
|
|
50
|
-
|
|
51
135
|
- /telemetry/start: Start telemetry data collection.
|
|
52
136
|
- /telemetry/stop: Stop telemetry data collection.
|
|
53
137
|
- /telemetry/status: Get status of telemetry.
|
|
@@ -55,12 +139,96 @@ OAS Telemetry middleware adds the following endpoints to your Express applicatio
|
|
|
55
139
|
- /telemetry/list: List all telemetry data.
|
|
56
140
|
- /telemetry/find (POST): Search telemetry data.
|
|
57
141
|
- /telemetry/heapStats: Shows v8 heapStats.
|
|
58
|
-
|
|
59
142
|
- /telemetry/plugins: List all plugins.
|
|
60
143
|
- /telemetry/plugins (POST): Add a plugin.
|
|
61
144
|
|
|
145
|
+
## Telemetry Plugins
|
|
146
|
+
|
|
147
|
+
OAS Telemetry supports a range of plugins to extend its functionality, allowing developers to tailor telemetry data collection, alerting, and reporting to meet specific requirements. Plugins enable additional features, such as integration with alerting systems, custom data exporters, and data visualization tools.
|
|
148
|
+
|
|
149
|
+
One example plugin is the **Outlier Alert Over Messaging** plugin, which can be configured to send anomaly alerts to messaging platforms like Telegram. This plugin is especially useful for monitoring abnormal response times in your API, notifying selected channels to allow rapid responses to potential issues. For setup details, refer to its [README documentation](https://github.com/oas-tools/oas-telemetry-plugin-outlier-messaging/blob/main/README.md).
|
|
150
|
+
|
|
151
|
+
OAS Telemetry plugins are flexible and support both ES Modules (ESM) and CommonJS (CJS) formats, regardless of whether your application is using ESM or CJS. This compatibility ensures that plugins work seamlessly in all configurations:
|
|
152
|
+
|
|
153
|
+
- ESM applications can use plugins in either ESM or CJS format.
|
|
154
|
+
- CJS applications can use plugins in either CJS or ESM format.
|
|
155
|
+
|
|
156
|
+
This flexibility makes it easy to incorporate a wide variety of plugins in your preferred module system.
|
|
157
|
+
|
|
158
|
+
## Accessing Telemetry Data
|
|
159
|
+
|
|
160
|
+
Using OAS Telemetry, you can access telemetry data through the UI, the `/telemetry/list` endpoint, or the `/telemetry/find` endpoint with a POST request using a MongoDB search syntax.
|
|
161
|
+
|
|
162
|
+
Note: if authentication is enabled, you must provide the correct credentials to access the telemetry data.
|
|
163
|
+
|
|
164
|
+
### Simple Search Example
|
|
165
|
+
|
|
166
|
+
To perform a simple search, send a POST request to the `/telemetry/find` endpoint with the following JSON payload:
|
|
167
|
+
|
|
168
|
+
```json
|
|
169
|
+
{
|
|
170
|
+
"search": {
|
|
171
|
+
"attributes.http.target": "/api/v1/pets",
|
|
172
|
+
"attributes.http.method": "GET",
|
|
173
|
+
"$or": [
|
|
174
|
+
{"attributes.http.status_code": 200},
|
|
175
|
+
{"attributes.http.status_code": 304}
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Complex Search Example
|
|
182
|
+
|
|
183
|
+
For more complex searches using regex, additional parsing on the server and extra attributes in the POST request are required. Send a POST request to the `/telemetry/find` endpoint with the following JSON payload:
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
{
|
|
187
|
+
"flags": {
|
|
188
|
+
"containsRegex": true
|
|
189
|
+
},
|
|
190
|
+
"config": {
|
|
191
|
+
"regexIds": ["attributes.http.target"]
|
|
192
|
+
},
|
|
193
|
+
"search": {
|
|
194
|
+
"attributes.http.target": "^/api/v1/pets.*$",
|
|
195
|
+
"attributes.http.method": "GET",
|
|
196
|
+
"$or": [
|
|
197
|
+
{"attributes.http.status_code": 200},
|
|
198
|
+
{"attributes.http.status_code": 304}
|
|
199
|
+
]
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
## Full Examples
|
|
205
|
+
|
|
206
|
+
To run these examples, follow these steps:
|
|
207
|
+
|
|
208
|
+
1. Create a new folder for your project.
|
|
209
|
+
2. Navigate to the folder and initialize a new Node.js project:
|
|
210
|
+
|
|
211
|
+
```sh
|
|
212
|
+
npm init -y
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
3. Install the **OAS Telemetry** package:
|
|
216
|
+
|
|
217
|
+
```sh
|
|
218
|
+
npm install @oas-tools/oas-telemetry
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
4. Save the example code as `index.js` in the project folder.
|
|
222
|
+
5. Run the application:
|
|
223
|
+
|
|
224
|
+
```sh
|
|
225
|
+
node index.js
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Your project folder should now contain the necessary files to run the example with **OAS Telemetry** integrated.
|
|
229
|
+
|
|
230
|
+
### Simple Example [ES Module](https://nodejs.org/docs/latest/api/esm.html) (*.mjs)
|
|
62
231
|
|
|
63
|
-
## Simple Example [ES Module](https://nodejs.org/docs/latest/api/esm.html) (*.mjs)
|
|
64
232
|
```js index.mjs
|
|
65
233
|
import oasTelemetry from '@oas-tools/oas-telemetry';
|
|
66
234
|
import express from 'express';
|
|
@@ -70,6 +238,51 @@ const port = 3000;
|
|
|
70
238
|
|
|
71
239
|
const spec = { "paths": {
|
|
72
240
|
"/api/v1/pets": {
|
|
241
|
+
"get": {
|
|
242
|
+
"summary": "Get pets",
|
|
243
|
+
"responses":{
|
|
244
|
+
"200": {
|
|
245
|
+
"description": "Success"
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
},
|
|
249
|
+
"post": {
|
|
250
|
+
"summary": "Insert a pet",
|
|
251
|
+
"responses":{
|
|
252
|
+
"201": {
|
|
253
|
+
"description": "Pet Created"
|
|
254
|
+
},
|
|
255
|
+
"400": {
|
|
256
|
+
"description": "Bad Request"
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
"/api/v1/pets/{petName}": {
|
|
262
|
+
"get": {
|
|
263
|
+
"summary": "Get a pet",
|
|
264
|
+
"parameters": [
|
|
265
|
+
{
|
|
266
|
+
"name": "petName",
|
|
267
|
+
"in": "path",
|
|
268
|
+
"required": true,
|
|
269
|
+
"description": "The name of the pet to retrieve",
|
|
270
|
+
"schema": {
|
|
271
|
+
"type": "string"
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
],
|
|
275
|
+
"responses":{
|
|
276
|
+
"200": {
|
|
277
|
+
"description": "Success"
|
|
278
|
+
},
|
|
279
|
+
"404": {
|
|
280
|
+
"description": "Not Found"
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
"/api/v1/clinics": {
|
|
73
286
|
"get": {
|
|
74
287
|
"summary": "Get pets",
|
|
75
288
|
"responses":{
|
|
@@ -82,23 +295,48 @@ const spec = { "paths": {
|
|
|
82
295
|
}
|
|
83
296
|
}
|
|
84
297
|
|
|
85
|
-
|
|
86
|
-
spec : JSON.stringify(spec)
|
|
87
|
-
|
|
298
|
+
const oasTlmConfig = {
|
|
299
|
+
spec : JSON.stringify(spec),
|
|
300
|
+
baseURL: "/telemetry",
|
|
301
|
+
}
|
|
302
|
+
app.use(oasTelemetry(oasTlmConfig));
|
|
88
303
|
|
|
89
304
|
app.use(express.json());
|
|
90
305
|
|
|
91
|
-
app.get("/api/v1/pets", (req, res) => {
|
|
92
|
-
res.send([{ name: "rocky"},{ name: "pikachu"}]);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
306
|
app.listen(port, () => {
|
|
96
307
|
console.log(`Example app listening at http://localhost:${port}`);
|
|
97
|
-
console.log(`Telemetry portal available at http://localhost:${port}
|
|
308
|
+
console.log(`Telemetry portal available at http://localhost:${port}${oasTlmConfig.baseURL}`);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
let pets =[{ name: "rocky"},{ name: "pikachu"}];
|
|
312
|
+
let clinics =[{ name: "Pet Heaven"},{ name: "Pet Care"}];
|
|
313
|
+
|
|
314
|
+
app.get("/api/v1/pets", (req, res) => {
|
|
315
|
+
res.send(pets);
|
|
316
|
+
});
|
|
317
|
+
app.post("/api/v1/pets", (req, res) => {
|
|
318
|
+
if(req.body && req.body.name){
|
|
319
|
+
pets.push(req.body);
|
|
320
|
+
res.sendStatus(201);
|
|
321
|
+
}else{
|
|
322
|
+
res.sendStatus(400);
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
app.get("/api/v1/pets/:name", (req, res) => {
|
|
326
|
+
let name = req.params.name;
|
|
327
|
+
let filterdPets = pets.filter((p)=>(p.name==name));
|
|
328
|
+
if(filterdPets.length > 0)
|
|
329
|
+
return res.send(filterdPets[0]);
|
|
330
|
+
else
|
|
331
|
+
return res.sendStatus(404);
|
|
332
|
+
});
|
|
333
|
+
app.get("/api/v1/clinics", (req, res) => {
|
|
334
|
+
res.send(clinics);
|
|
98
335
|
});
|
|
99
336
|
```
|
|
100
337
|
|
|
101
|
-
|
|
338
|
+
### Simple Example [Common.js Module](https://nodejs.org/docs/latest/api/modules.html) (*.cjs)
|
|
339
|
+
|
|
102
340
|
```js index.cjs
|
|
103
341
|
let oasTelemetry = require('@oas-tools/oas-telemetry');
|
|
104
342
|
let express = require('express');
|
|
@@ -108,6 +346,51 @@ const port = 3000;
|
|
|
108
346
|
|
|
109
347
|
const spec = { "paths": {
|
|
110
348
|
"/api/v1/pets": {
|
|
349
|
+
"get": {
|
|
350
|
+
"summary": "Get pets",
|
|
351
|
+
"responses":{
|
|
352
|
+
"200": {
|
|
353
|
+
"description": "Success"
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
},
|
|
357
|
+
"post": {
|
|
358
|
+
"summary": "Insert a pet",
|
|
359
|
+
"responses":{
|
|
360
|
+
"201": {
|
|
361
|
+
"description": "Pet Created"
|
|
362
|
+
},
|
|
363
|
+
"400": {
|
|
364
|
+
"description": "Bad Request"
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
},
|
|
369
|
+
"/api/v1/pets/{petName}": {
|
|
370
|
+
"get": {
|
|
371
|
+
"summary": "Get a pet",
|
|
372
|
+
"parameters": [
|
|
373
|
+
{
|
|
374
|
+
"name": "petName",
|
|
375
|
+
"in": "path",
|
|
376
|
+
"required": true,
|
|
377
|
+
"description": "The name of the pet to retrieve",
|
|
378
|
+
"schema": {
|
|
379
|
+
"type": "string"
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
],
|
|
383
|
+
"responses":{
|
|
384
|
+
"200": {
|
|
385
|
+
"description": "Success"
|
|
386
|
+
},
|
|
387
|
+
"404": {
|
|
388
|
+
"description": "Not Found"
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
},
|
|
393
|
+
"/api/v1/clinics": {
|
|
111
394
|
"get": {
|
|
112
395
|
"summary": "Get pets",
|
|
113
396
|
"responses":{
|
|
@@ -126,26 +409,34 @@ app.use(oasTelemetry({
|
|
|
126
409
|
|
|
127
410
|
app.use(express.json());
|
|
128
411
|
|
|
129
|
-
app.get("/api/v1/pets", (req, res) => {
|
|
130
|
-
res.send([{ name: "rocky"},{ name: "pikachu"}]);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
412
|
app.listen(port, () => {
|
|
134
413
|
console.log(`Example app listening at http://localhost:${port}`);
|
|
135
414
|
console.log(`Telemetry portal available at http://localhost:${port}/telemetry`);
|
|
136
415
|
});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Telemetry Plugins
|
|
140
|
-
|
|
141
|
-
OAS Telemetry supports a range of plugins to extend its functionality, allowing developers to tailor telemetry data collection, alerting, and reporting to meet specific requirements. Plugins enable additional features, such as integration with alerting systems, custom data exporters, and data visualization tools.
|
|
142
|
-
|
|
143
|
-
One example plugin is the **Outlier Alert Over Messaging** plugin, which can be configured to send anomaly alerts to messaging platforms like Telegram. This plugin is especially useful for monitoring abnormal response times in your API, notifying selected channels to allow rapid responses to potential issues. For setup details, refer to its [README documentation](https://github.com/oas-tools/oas-telemetry-plugin-outlier-messaging/blob/main/README.md).
|
|
144
|
-
|
|
145
|
-
OAS Telemetry plugins are flexible and support both ES Modules (ESM) and CommonJS (CJS) formats, regardless of whether your application is using ESM or CJS. This compatibility ensures that plugins work seamlessly in all configurations:
|
|
146
|
-
- ESM applications can use plugins in either ESM or CJS format.
|
|
147
|
-
- CJS applications can use plugins in either CJS or ESM format.
|
|
148
|
-
|
|
149
|
-
This flexibility makes it easy to incorporate a wide variety of plugins in your preferred module system.
|
|
150
416
|
|
|
417
|
+
let pets =[{ name: "rocky"},{ name: "pikachu"}];
|
|
418
|
+
let clinics =[{ name: "Pet Heaven"},{ name: "Pet Care"}];
|
|
151
419
|
|
|
420
|
+
app.get("/api/v1/pets", (req, res) => {
|
|
421
|
+
res.send(pets);
|
|
422
|
+
});
|
|
423
|
+
app.post("/api/v1/pets", (req, res) => {
|
|
424
|
+
if(req.body && req.body.name){
|
|
425
|
+
pets.push(req.body);
|
|
426
|
+
res.sendStatus(201);
|
|
427
|
+
}else{
|
|
428
|
+
res.sendStatus(400);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
app.get("/api/v1/pets/:name", (req, res) => {
|
|
432
|
+
let name = req.params.name;
|
|
433
|
+
let filterdPets = pets.filter((p)=>(p.name==name));
|
|
434
|
+
if(filterdPets.length > 0)
|
|
435
|
+
return res.send(filterdPets[0]);
|
|
436
|
+
else
|
|
437
|
+
return res.sendStatus(404);
|
|
438
|
+
});
|
|
439
|
+
app.get("/api/v1/clinics", (req, res) => {
|
|
440
|
+
res.send(clinics);
|
|
441
|
+
});
|
|
442
|
+
```
|
package/dist/config.cjs
CHANGED
|
@@ -4,17 +4,23 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.globalOasTlmConfig = exports.default = void 0;
|
|
7
|
+
var _api = require("@opentelemetry/api");
|
|
7
8
|
var _dynamicExporter = _interopRequireDefault(require("./exporters/dynamicExporter.cjs"));
|
|
9
|
+
var _InMemoryDBMetricsExporter = require("./exporters/InMemoryDBMetricsExporter.cjs");
|
|
8
10
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
11
|
//Environment variables
|
|
10
12
|
//OASTLM_MODULE_DISABLED = 'true' //Disables the module (empty middleware and no tracing)
|
|
11
13
|
|
|
12
14
|
const globalOasTlmConfig = exports.globalOasTlmConfig = {
|
|
13
15
|
dynamicExporter: new _dynamicExporter.default(),
|
|
16
|
+
metricsExporter: new _InMemoryDBMetricsExporter.InMemoryDBMetricsExporter(),
|
|
17
|
+
systemMetricsInterval: 1000 * 5,
|
|
18
|
+
// 5 seconds
|
|
14
19
|
baseURL: "/telemetry",
|
|
15
20
|
spec: null,
|
|
16
21
|
specFileName: "",
|
|
17
22
|
autoActivate: true,
|
|
23
|
+
authEnabled: false,
|
|
18
24
|
apiKeyMaxAge: 1000 * 60 * 60,
|
|
19
25
|
// 1 hour
|
|
20
26
|
password: "oas-telemetry-password",
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.resetMetrics = exports.listMetrics = exports.findMetrics = void 0;
|
|
7
|
+
var _config = require("../config.cjs");
|
|
8
|
+
const listMetrics = async (req, res) => {
|
|
9
|
+
try {
|
|
10
|
+
const metrics = await _config.globalOasTlmConfig.metricsExporter.getFinishedMetrics();
|
|
11
|
+
res.send({
|
|
12
|
+
metricsCount: metrics.length,
|
|
13
|
+
metrics: metrics
|
|
14
|
+
});
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error(err);
|
|
17
|
+
res.status(500).send({
|
|
18
|
+
error: 'Failed to list metrics data'
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.listMetrics = listMetrics;
|
|
23
|
+
const findMetrics = (req, res) => {
|
|
24
|
+
const body = req.body;
|
|
25
|
+
const search = body?.search ? body.search : {};
|
|
26
|
+
_config.globalOasTlmConfig.metricsExporter.find(search, (err, docs) => {
|
|
27
|
+
if (err) {
|
|
28
|
+
console.error(err);
|
|
29
|
+
res.status(404).send({
|
|
30
|
+
metricsCount: 0,
|
|
31
|
+
metrics: [],
|
|
32
|
+
error: err
|
|
33
|
+
});
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const metrics = docs;
|
|
37
|
+
res.send({
|
|
38
|
+
metricsCount: metrics.length,
|
|
39
|
+
metrics: metrics
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
};
|
|
43
|
+
exports.findMetrics = findMetrics;
|
|
44
|
+
const resetMetrics = (req, res) => {
|
|
45
|
+
_config.globalOasTlmConfig.metricsExporter.reset();
|
|
46
|
+
res.send('Metrics reset');
|
|
47
|
+
};
|
|
48
|
+
exports.resetMetrics = resetMetrics;
|
|
@@ -71,22 +71,22 @@ const findTelemetry = (req, res) => {
|
|
|
71
71
|
});
|
|
72
72
|
return;
|
|
73
73
|
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const spans = docs;
|
|
85
|
-
res.send({
|
|
86
|
-
spansCount: spans.length,
|
|
87
|
-
spans: spans
|
|
74
|
+
}
|
|
75
|
+
_config.globalOasTlmConfig.dynamicExporter.exporter.find(search, (err, docs) => {
|
|
76
|
+
if (err) {
|
|
77
|
+
console.error(err);
|
|
78
|
+
res.status(404).send({
|
|
79
|
+
spansCount: 0,
|
|
80
|
+
spans: [],
|
|
81
|
+
error: err
|
|
88
82
|
});
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const spans = docs;
|
|
86
|
+
res.send({
|
|
87
|
+
spansCount: spans.length,
|
|
88
|
+
spans: spans
|
|
89
89
|
});
|
|
90
|
-
}
|
|
90
|
+
});
|
|
91
91
|
};
|
|
92
92
|
exports.findTelemetry = findTelemetry;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.InMemoryDBMetricsExporter = void 0;
|
|
7
|
+
var _core = require("@opentelemetry/core");
|
|
8
|
+
var _nedb = _interopRequireDefault(require("@seald-io/nedb"));
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
class InMemoryDBMetricsExporter {
|
|
11
|
+
constructor() {
|
|
12
|
+
this._metrics = new _nedb.default();
|
|
13
|
+
this._stopped = false;
|
|
14
|
+
}
|
|
15
|
+
export(metrics, resultCallback) {
|
|
16
|
+
try {
|
|
17
|
+
if (!this._stopped) {
|
|
18
|
+
// metrics = metrics?.scopeMetrics;
|
|
19
|
+
// const cleanMetrics = metrics.map(metric => applyNesting(metric));
|
|
20
|
+
this._metrics.insert(metrics, (err, newDoc) => {
|
|
21
|
+
if (err) {
|
|
22
|
+
console.error('Insertion Error:', err);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
setTimeout(() => resultCallback({
|
|
28
|
+
code: _core.ExportResultCode.SUCCESS
|
|
29
|
+
}), 0);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('Error exporting metrics\n' + error.message + '\n' + error.stack);
|
|
32
|
+
return resultCallback({
|
|
33
|
+
code: _core.ExportResultCode.FAILED,
|
|
34
|
+
error: new Error('Error exporting metrics\n' + error.message + '\n' + error.stack)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
start() {
|
|
39
|
+
this._stopped = false;
|
|
40
|
+
}
|
|
41
|
+
stop() {
|
|
42
|
+
this._stopped = true;
|
|
43
|
+
}
|
|
44
|
+
isRunning() {
|
|
45
|
+
return !this._stopped;
|
|
46
|
+
}
|
|
47
|
+
shutdown() {
|
|
48
|
+
this._stopped = true;
|
|
49
|
+
this._metrics = new _nedb.default();
|
|
50
|
+
return this.forceFlush();
|
|
51
|
+
}
|
|
52
|
+
forceFlush() {
|
|
53
|
+
return Promise.resolve();
|
|
54
|
+
}
|
|
55
|
+
find(search, callback) {
|
|
56
|
+
this._metrics.find(search, callback);
|
|
57
|
+
}
|
|
58
|
+
reset() {
|
|
59
|
+
this._metrics = new _nedb.default();
|
|
60
|
+
}
|
|
61
|
+
getFinishedMetrics() {
|
|
62
|
+
return this._metrics.getAllData();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.InMemoryDBMetricsExporter = InMemoryDBMetricsExporter;
|
|
66
|
+
function convertToNestedObject(obj) {
|
|
67
|
+
const result = {};
|
|
68
|
+
for (const key in obj) {
|
|
69
|
+
const keys = key.split('.');
|
|
70
|
+
let temp = result;
|
|
71
|
+
for (let i = 0; i < keys.length; i++) {
|
|
72
|
+
const currentKey = keys[i];
|
|
73
|
+
if (i === keys.length - 1) {
|
|
74
|
+
// Last key, set the value
|
|
75
|
+
temp[currentKey] = obj[key];
|
|
76
|
+
} else {
|
|
77
|
+
// Intermediate key, ensure the object exists
|
|
78
|
+
if (!temp[currentKey]) {
|
|
79
|
+
temp[currentKey] = {};
|
|
80
|
+
}
|
|
81
|
+
temp = temp[currentKey];
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Applies nesting to all dot-separated keys within an object.
|
|
90
|
+
*
|
|
91
|
+
* @param {Object} obj - The object to apply nesting to.
|
|
92
|
+
* @returns {Object} - The transformed object with nested structures.
|
|
93
|
+
*/
|
|
94
|
+
function applyNesting(obj) {
|
|
95
|
+
for (const key in obj) {
|
|
96
|
+
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
97
|
+
obj[key] = applyNesting(obj[key]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return obj;
|
|
101
|
+
}
|
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.InMemoryExporter = void 0;
|
|
7
7
|
var _core = require("@opentelemetry/core");
|
|
8
|
-
var _nedb = _interopRequireDefault(require("nedb"));
|
|
8
|
+
var _nedb = _interopRequireDefault(require("@seald-io/nedb"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
10
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
11
11
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|