@oas-tools/oas-telemetry 0.5.0 → 0.5.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/README.md +277 -41
- package/dist/config.cjs +2 -2
- package/dist/controllers/telemetryController.cjs +15 -15
- package/dist/exporters/InMemoryDbExporter.cjs +1 -1
- package/dist/index.cjs +31 -3
- package/dist/openTelemetry.cjs +16 -1
- package/package.json +3 -2
- package/src/config.js +2 -2
- package/src/controllers/telemetryController.js +2 -1
- package/src/exporters/InMemoryDbExporter.js +1 -1
- package/src/index.js +32 -5
- package/src/openTelemetry.js +16 -2
- package/src/services/uiService.js +21 -21
- 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,92 @@
|
|
|
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
|
|
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.
|
|
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.
|
|
12
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.
|
|
13
12
|
|
|
14
13
|
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
## Usage
|
|
18
|
-
|
|
17
|
+
|
|
18
|
+
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.
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
First, install the package using npm:
|
|
22
|
+
```sh
|
|
23
|
+
npm install @oas-tools/oas-telemetry
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
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.
|
|
28
|
+
|
|
29
|
+
### Using ES Modules (ESM)
|
|
30
|
+
|
|
31
|
+
Add the following lines to your `index.js` file:
|
|
19
32
|
```js
|
|
20
|
-
import
|
|
21
|
-
import
|
|
33
|
+
// This import MUST be at the top of the file
|
|
34
|
+
import oasTelemetry from '@oas-tools/oas-telemetry';
|
|
35
|
+
import { readFileSync } from 'fs';
|
|
36
|
+
|
|
37
|
+
// ...rest of your code here creating an express app
|
|
22
38
|
|
|
23
39
|
app.use(oasTelemetry({
|
|
24
|
-
spec
|
|
25
|
-
}))
|
|
40
|
+
spec: readFileSync('./spec/oas.yaml', { encoding: 'utf8', flag: 'r' })
|
|
41
|
+
}));
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Using CommonJS
|
|
26
45
|
|
|
46
|
+
Add the following lines to your `index.js` file:
|
|
47
|
+
```js
|
|
48
|
+
// This require MUST be at the top of the file
|
|
49
|
+
const oasTelemetry = require('@oas-tools/oas-telemetry');
|
|
50
|
+
const { readFileSync } = require('fs');
|
|
51
|
+
|
|
52
|
+
// ...rest of your code here creating an express app
|
|
53
|
+
|
|
54
|
+
app.use(oasTelemetry({
|
|
55
|
+
spec: readFileSync('./spec/oas.yaml', { encoding: 'utf8', flag: 'r' })
|
|
56
|
+
}));
|
|
27
57
|
```
|
|
28
58
|
|
|
59
|
+
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.
|
|
60
|
+
|
|
29
61
|
## Custom Configuration
|
|
30
62
|
|
|
31
63
|
You can also customize the telemetry configuration by passing options to the middleware function. For example:
|
|
32
64
|
```js
|
|
33
65
|
const customTelemetryConfig = {
|
|
34
|
-
|
|
35
|
-
|
|
66
|
+
spec: /* OAS content in json or yaml */, // Highly recommended
|
|
67
|
+
baseURL: "/custom-telemetry", //default is "/telemetry"
|
|
68
|
+
autoActivate: false, //default is true, whether to start telemetry data collection automatically
|
|
69
|
+
authEnabled: true, //default is false
|
|
70
|
+
apiKeyMaxAge: 1000 * 60 * 30, // 30 minutes
|
|
71
|
+
password: "custom-password", //default is "oas-telemetry-password"
|
|
72
|
+
jwtSecret: "custom-secret", //default is "oas-telemetry-secret"
|
|
73
|
+
exporter: myCustomExporter, // Experimental, just for devs
|
|
36
74
|
};
|
|
37
75
|
|
|
38
76
|
app.use(oasTelemetry(customTelemetryConfig));
|
|
39
77
|
```
|
|
40
78
|
|
|
79
|
+
**Note:** To disable the module, set the environment variable `OASTLM_MODULE_DISABLED` to `'true'`.
|
|
80
|
+
|
|
41
81
|
## Telemetry UI
|
|
42
82
|
|
|
43
|
-
You can access the telemetry UI in the endpoint
|
|
83
|
+
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.
|
|
44
84
|
|
|
45
85
|
|
|
46
86
|
## API Telemetry Endpoints
|
|
47
87
|
|
|
48
88
|
OAS Telemetry middleware adds the following endpoints to your Express application:
|
|
49
89
|
|
|
50
|
-
|
|
51
90
|
- /telemetry/start: Start telemetry data collection.
|
|
52
91
|
- /telemetry/stop: Stop telemetry data collection.
|
|
53
92
|
- /telemetry/status: Get status of telemetry.
|
|
@@ -55,12 +94,85 @@ OAS Telemetry middleware adds the following endpoints to your Express applicatio
|
|
|
55
94
|
- /telemetry/list: List all telemetry data.
|
|
56
95
|
- /telemetry/find (POST): Search telemetry data.
|
|
57
96
|
- /telemetry/heapStats: Shows v8 heapStats.
|
|
58
|
-
|
|
59
97
|
- /telemetry/plugins: List all plugins.
|
|
60
98
|
- /telemetry/plugins (POST): Add a plugin.
|
|
61
99
|
|
|
100
|
+
## Telemetry Plugins
|
|
101
|
+
|
|
102
|
+
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.
|
|
62
103
|
|
|
63
|
-
|
|
104
|
+
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).
|
|
105
|
+
|
|
106
|
+
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:
|
|
107
|
+
- ESM applications can use plugins in either ESM or CJS format.
|
|
108
|
+
- CJS applications can use plugins in either CJS or ESM format.
|
|
109
|
+
|
|
110
|
+
This flexibility makes it easy to incorporate a wide variety of plugins in your preferred module system.
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
## Accessing Telemetry Data
|
|
114
|
+
|
|
115
|
+
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.
|
|
116
|
+
|
|
117
|
+
Note: if authentication is enabled, you must provide the correct credentials to access the telemetry data.
|
|
118
|
+
|
|
119
|
+
### Simple Search Example
|
|
120
|
+
To perform a simple search, send a POST request to the `/telemetry/find` endpoint with the following JSON payload:
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"search": {
|
|
124
|
+
"attributes.http.target": "/api/v1/pets",
|
|
125
|
+
"attributes.http.method": "GET",
|
|
126
|
+
"$or": [
|
|
127
|
+
{"attributes.http.status_code": 200},
|
|
128
|
+
{"attributes.http.status_code": 304}
|
|
129
|
+
]
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Complex Search Example
|
|
135
|
+
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:
|
|
136
|
+
```json
|
|
137
|
+
{
|
|
138
|
+
"flags": {
|
|
139
|
+
"containsRegex": true
|
|
140
|
+
},
|
|
141
|
+
"config": {
|
|
142
|
+
"regexIds": ["attributes.http.target"]
|
|
143
|
+
},
|
|
144
|
+
"search": {
|
|
145
|
+
"attributes.http.target": "^/api/v1/pets.*$",
|
|
146
|
+
"attributes.http.method": "GET",
|
|
147
|
+
"$or": [
|
|
148
|
+
{"attributes.http.status_code": 200},
|
|
149
|
+
{"attributes.http.status_code": 304}
|
|
150
|
+
]
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Full Examples
|
|
156
|
+
To run these examples, follow these steps:
|
|
157
|
+
|
|
158
|
+
1. Create a new folder for your project.
|
|
159
|
+
2. Navigate to the folder and initialize a new Node.js project:
|
|
160
|
+
```sh
|
|
161
|
+
npm init -y
|
|
162
|
+
```
|
|
163
|
+
3. Install the **OAS Telemetry** package:
|
|
164
|
+
```sh
|
|
165
|
+
npm install @oas-tools/oas-telemetry
|
|
166
|
+
```
|
|
167
|
+
4. Save the example code as `index.js` in the project folder.
|
|
168
|
+
5. Run the application:
|
|
169
|
+
```sh
|
|
170
|
+
node index.js
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Your project folder should now contain the necessary files to run the example with **OAS Telemetry** integrated.
|
|
174
|
+
|
|
175
|
+
### Simple Example [ES Module](https://nodejs.org/docs/latest/api/esm.html) (*.mjs)
|
|
64
176
|
```js index.mjs
|
|
65
177
|
import oasTelemetry from '@oas-tools/oas-telemetry';
|
|
66
178
|
import express from 'express';
|
|
@@ -70,6 +182,51 @@ const port = 3000;
|
|
|
70
182
|
|
|
71
183
|
const spec = { "paths": {
|
|
72
184
|
"/api/v1/pets": {
|
|
185
|
+
"get": {
|
|
186
|
+
"summary": "Get pets",
|
|
187
|
+
"responses":{
|
|
188
|
+
"200": {
|
|
189
|
+
"description": "Success"
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
"post": {
|
|
194
|
+
"summary": "Insert a pet",
|
|
195
|
+
"responses":{
|
|
196
|
+
"201": {
|
|
197
|
+
"description": "Pet Created"
|
|
198
|
+
},
|
|
199
|
+
"400": {
|
|
200
|
+
"description": "Bad Request"
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
"/api/v1/pets/{petName}": {
|
|
206
|
+
"get": {
|
|
207
|
+
"summary": "Get a pet",
|
|
208
|
+
"parameters": [
|
|
209
|
+
{
|
|
210
|
+
"name": "petName",
|
|
211
|
+
"in": "path",
|
|
212
|
+
"required": true,
|
|
213
|
+
"description": "The name of the pet to retrieve",
|
|
214
|
+
"schema": {
|
|
215
|
+
"type": "string"
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
],
|
|
219
|
+
"responses":{
|
|
220
|
+
"200": {
|
|
221
|
+
"description": "Success"
|
|
222
|
+
},
|
|
223
|
+
"404": {
|
|
224
|
+
"description": "Not Found"
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
"/api/v1/clinics": {
|
|
73
230
|
"get": {
|
|
74
231
|
"summary": "Get pets",
|
|
75
232
|
"responses":{
|
|
@@ -82,23 +239,47 @@ const spec = { "paths": {
|
|
|
82
239
|
}
|
|
83
240
|
}
|
|
84
241
|
|
|
85
|
-
|
|
86
|
-
spec : JSON.stringify(spec)
|
|
87
|
-
|
|
242
|
+
const oasTlmConfig = {
|
|
243
|
+
spec : JSON.stringify(spec),
|
|
244
|
+
baseURL: "/telemetry",
|
|
245
|
+
}
|
|
246
|
+
app.use(oasTelemetry(oasTlmConfig));
|
|
88
247
|
|
|
89
248
|
app.use(express.json());
|
|
90
249
|
|
|
91
|
-
app.get("/api/v1/pets", (req, res) => {
|
|
92
|
-
res.send([{ name: "rocky"},{ name: "pikachu"}]);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
250
|
app.listen(port, () => {
|
|
96
251
|
console.log(`Example app listening at http://localhost:${port}`);
|
|
97
|
-
console.log(`Telemetry portal available at http://localhost:${port}
|
|
252
|
+
console.log(`Telemetry portal available at http://localhost:${port}${oasTlmConfig.baseURL}`);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
let pets =[{ name: "rocky"},{ name: "pikachu"}];
|
|
256
|
+
let clinics =[{ name: "Pet Heaven"},{ name: "Pet Care"}];
|
|
257
|
+
|
|
258
|
+
app.get("/api/v1/pets", (req, res) => {
|
|
259
|
+
res.send(pets);
|
|
260
|
+
});
|
|
261
|
+
app.post("/api/v1/pets", (req, res) => {
|
|
262
|
+
if(req.body && req.body.name){
|
|
263
|
+
pets.push(req.body);
|
|
264
|
+
res.sendStatus(201);
|
|
265
|
+
}else{
|
|
266
|
+
res.sendStatus(400);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
app.get("/api/v1/pets/:name", (req, res) => {
|
|
270
|
+
let name = req.params.name;
|
|
271
|
+
let filterdPets = pets.filter((p)=>(p.name==name));
|
|
272
|
+
if(filterdPets.length > 0)
|
|
273
|
+
return res.send(filterdPets[0]);
|
|
274
|
+
else
|
|
275
|
+
return res.sendStatus(404);
|
|
276
|
+
});
|
|
277
|
+
app.get("/api/v1/clinics", (req, res) => {
|
|
278
|
+
res.send(clinics);
|
|
98
279
|
});
|
|
99
280
|
```
|
|
100
281
|
|
|
101
|
-
|
|
282
|
+
### Simple Example [Common.js Module](https://nodejs.org/docs/latest/api/modules.html) (*.cjs)
|
|
102
283
|
```js index.cjs
|
|
103
284
|
let oasTelemetry = require('@oas-tools/oas-telemetry');
|
|
104
285
|
let express = require('express');
|
|
@@ -108,6 +289,51 @@ const port = 3000;
|
|
|
108
289
|
|
|
109
290
|
const spec = { "paths": {
|
|
110
291
|
"/api/v1/pets": {
|
|
292
|
+
"get": {
|
|
293
|
+
"summary": "Get pets",
|
|
294
|
+
"responses":{
|
|
295
|
+
"200": {
|
|
296
|
+
"description": "Success"
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
"post": {
|
|
301
|
+
"summary": "Insert a pet",
|
|
302
|
+
"responses":{
|
|
303
|
+
"201": {
|
|
304
|
+
"description": "Pet Created"
|
|
305
|
+
},
|
|
306
|
+
"400": {
|
|
307
|
+
"description": "Bad Request"
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
},
|
|
312
|
+
"/api/v1/pets/{petName}": {
|
|
313
|
+
"get": {
|
|
314
|
+
"summary": "Get a pet",
|
|
315
|
+
"parameters": [
|
|
316
|
+
{
|
|
317
|
+
"name": "petName",
|
|
318
|
+
"in": "path",
|
|
319
|
+
"required": true,
|
|
320
|
+
"description": "The name of the pet to retrieve",
|
|
321
|
+
"schema": {
|
|
322
|
+
"type": "string"
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
],
|
|
326
|
+
"responses":{
|
|
327
|
+
"200": {
|
|
328
|
+
"description": "Success"
|
|
329
|
+
},
|
|
330
|
+
"404": {
|
|
331
|
+
"description": "Not Found"
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
"/api/v1/clinics": {
|
|
111
337
|
"get": {
|
|
112
338
|
"summary": "Get pets",
|
|
113
339
|
"responses":{
|
|
@@ -126,26 +352,36 @@ app.use(oasTelemetry({
|
|
|
126
352
|
|
|
127
353
|
app.use(express.json());
|
|
128
354
|
|
|
129
|
-
app.get("/api/v1/pets", (req, res) => {
|
|
130
|
-
res.send([{ name: "rocky"},{ name: "pikachu"}]);
|
|
131
|
-
});
|
|
132
|
-
|
|
133
355
|
app.listen(port, () => {
|
|
134
356
|
console.log(`Example app listening at http://localhost:${port}`);
|
|
135
357
|
console.log(`Telemetry portal available at http://localhost:${port}/telemetry`);
|
|
136
358
|
});
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
## Telemetry Plugins
|
|
140
359
|
|
|
141
|
-
|
|
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.
|
|
360
|
+
let pets =[{ name: "rocky"},{ name: "pikachu"}];
|
|
361
|
+
let clinics =[{ name: "Pet Heaven"},{ name: "Pet Care"}];
|
|
148
362
|
|
|
149
|
-
|
|
363
|
+
app.get("/api/v1/pets", (req, res) => {
|
|
364
|
+
res.send(pets);
|
|
365
|
+
});
|
|
366
|
+
app.post("/api/v1/pets", (req, res) => {
|
|
367
|
+
if(req.body && req.body.name){
|
|
368
|
+
pets.push(req.body);
|
|
369
|
+
res.sendStatus(201);
|
|
370
|
+
}else{
|
|
371
|
+
res.sendStatus(400);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
app.get("/api/v1/pets/:name", (req, res) => {
|
|
375
|
+
let name = req.params.name;
|
|
376
|
+
let filterdPets = pets.filter((p)=>(p.name==name));
|
|
377
|
+
if(filterdPets.length > 0)
|
|
378
|
+
return res.send(filterdPets[0]);
|
|
379
|
+
else
|
|
380
|
+
return res.sendStatus(404);
|
|
381
|
+
});
|
|
382
|
+
app.get("/api/v1/clinics", (req, res) => {
|
|
383
|
+
res.send(clinics);
|
|
384
|
+
});
|
|
385
|
+
```
|
|
150
386
|
|
|
151
387
|
|
package/dist/config.cjs
CHANGED
|
@@ -7,15 +7,15 @@ exports.globalOasTlmConfig = exports.default = void 0;
|
|
|
7
7
|
var _dynamicExporter = _interopRequireDefault(require("./exporters/dynamicExporter.cjs"));
|
|
8
8
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
9
|
//Environment variables
|
|
10
|
-
//OASTLM_API_KEY = 'oas-telemetry-api-key' //Cookie value for the API key
|
|
11
10
|
//OASTLM_MODULE_DISABLED = 'true' //Disables the module (empty middleware and no tracing)
|
|
12
11
|
|
|
13
12
|
const globalOasTlmConfig = exports.globalOasTlmConfig = {
|
|
14
13
|
dynamicExporter: new _dynamicExporter.default(),
|
|
15
|
-
baseURL: "/
|
|
14
|
+
baseURL: "/telemetry",
|
|
16
15
|
spec: null,
|
|
17
16
|
specFileName: "",
|
|
18
17
|
autoActivate: true,
|
|
18
|
+
authEnabled: false,
|
|
19
19
|
apiKeyMaxAge: 1000 * 60 * 60,
|
|
20
20
|
// 1 hour
|
|
21
21
|
password: "oas-telemetry-password",
|
|
@@ -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;
|
|
@@ -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 + ""; }
|
package/dist/index.cjs
CHANGED
|
@@ -25,6 +25,7 @@ if (process.env.OTDEBUG == "true") dbglog = console.log;
|
|
|
25
25
|
* @param {Object} [OasTlmConfig.spec] The OpenAPI spec object.
|
|
26
26
|
* @param {string} [OasTlmConfig.specFileName] Alternative to `spec`: the path to the OpenAPI spec file.
|
|
27
27
|
* @param {boolean} [OasTlmConfig.autoActivate=true] Whether to start telemetry automatically on load.
|
|
28
|
+
* @param {boolean} [OasTlmConfig.authEnabled=true] Whether to enable authentication middleware.
|
|
28
29
|
* @param {number} [OasTlmConfig.apiKeyMaxAge=1800000] The maximum age of the API key in milliseconds.
|
|
29
30
|
* @param {string} [OasTlmConfig.defaultApiKey] The default API key to use.
|
|
30
31
|
* @param {OasTlmExporter} [OasTlmConfig.exporter=InMemoryExporter] The exporter to use. Must implement the `OasTlmExporter` interface.
|
|
@@ -50,11 +51,10 @@ function oasTelemetry(OasTlmConfig) {
|
|
|
50
51
|
console.error("No spec available !");
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
|
-
|
|
54
|
+
let allAuthMiddlewares = getWrappedMiddlewares(() => _config.globalOasTlmConfig.authEnabled, [(0, _cookieParser.default)(), _authRoutes.default, _authMiddleware.authMiddleware]);
|
|
54
55
|
const baseURL = _config.globalOasTlmConfig.baseURL;
|
|
55
56
|
router.use((0, _express.json)());
|
|
56
|
-
router.use(baseURL,
|
|
57
|
-
router.use(baseURL, _authMiddleware.authMiddleware); // Add the auth middleware
|
|
57
|
+
router.use(baseURL, allAuthMiddlewares);
|
|
58
58
|
router.use(baseURL, _telemetryRoutes.telemetryRoutes);
|
|
59
59
|
if (_config.globalOasTlmConfig.autoActivate) {
|
|
60
60
|
_config.globalOasTlmConfig.dynamicExporter.exporter?.start();
|
|
@@ -77,4 +77,32 @@ function oasTelemetry(OasTlmConfig) {
|
|
|
77
77
|
* @method {Promise<void>} forceFlush() Exports any pending data that has not yet been processed.
|
|
78
78
|
* @property {Array} plugins An array of plugins that can be activated by the exporter.
|
|
79
79
|
*/
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* This function wraps the provided middleware functions with a condition callback.
|
|
83
|
+
* If the condition callback returns true, the middleware/router will be executed.
|
|
84
|
+
* If the condition callback returns false, the middleware/router will be skipped.
|
|
85
|
+
*
|
|
86
|
+
* @callback {function} conditionCallback A callback function that returns a boolean to determine if the middleware should be used.
|
|
87
|
+
* @param {Array} middlewares An array of middleware or routers to be wrapped.
|
|
88
|
+
* @returns {Array} An array of wrapped middleware functions.
|
|
89
|
+
*/
|
|
90
|
+
function getWrappedMiddlewares(conditionCallback, middlewares) {
|
|
91
|
+
return middlewares.map(middleware => {
|
|
92
|
+
return function (req, res, next) {
|
|
93
|
+
if (conditionCallback()) {
|
|
94
|
+
if (typeof middleware === 'function') {
|
|
95
|
+
// look for handle property, if it exists, it's a router. If not call middleware
|
|
96
|
+
if (middleware.handle) {
|
|
97
|
+
middleware.handle(req, res, next);
|
|
98
|
+
} else {
|
|
99
|
+
middleware(req, res, next);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
next();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
});
|
|
107
|
+
}
|
|
80
108
|
module.exports = exports.default;
|
package/dist/openTelemetry.cjs
CHANGED
|
@@ -8,6 +8,7 @@ var _config = require("./config.cjs");
|
|
|
8
8
|
|
|
9
9
|
// DynamicExporter allows changing to any exporter at runtime;
|
|
10
10
|
const traceExporter = _config.globalOasTlmConfig.dynamicExporter;
|
|
11
|
+
// Alternative 1: Using NodeSDK
|
|
11
12
|
const sdk = new _sdkNode.NodeSDK({
|
|
12
13
|
resource: new _resources.Resource({
|
|
13
14
|
service: 'oas-telemetry-service'
|
|
@@ -17,4 +18,18 @@ const sdk = new _sdkNode.NodeSDK({
|
|
|
17
18
|
});
|
|
18
19
|
if (process.env.OASTLM_MODULE_DISABLED !== 'true') {
|
|
19
20
|
sdk.start();
|
|
20
|
-
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Alternative 2:
|
|
24
|
+
// const provider = new NodeTracerProvider();
|
|
25
|
+
// provider.addSpanProcessor(new SimpleSpanProcessor(traceExporter));
|
|
26
|
+
|
|
27
|
+
// if (process.env.OASTLM_MODULE_DISABLED !== 'true') {
|
|
28
|
+
// provider.register();
|
|
29
|
+
// registerInstrumentations({
|
|
30
|
+
// instrumentations: [
|
|
31
|
+
// new HttpInstrumentation(),
|
|
32
|
+
// new ExpressInstrumentation(),
|
|
33
|
+
// ],
|
|
34
|
+
// });
|
|
35
|
+
// }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oas-tools/oas-telemetry",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "This package exports an Express middleware that traces requests and responses of an Express application using OpenTelemetry.",
|
|
5
5
|
"author": "Manuel Otero",
|
|
6
6
|
"contributors": [
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"testPerformance": "npm i && cd test/performance/ && ./test.sh",
|
|
20
20
|
"pretest": "npm run build",
|
|
21
21
|
"test": "npm run testCJS && npm run testMJS",
|
|
22
|
+
"build:dockerTest": "npm run build && docker build -t oastlm-test-ks-api -f ./test/performance/ks-api/Dockerfile .",
|
|
22
23
|
"testCJS": "npm run preTestCJS && npm run launchTestCJS && npm run postTestCJS",
|
|
23
24
|
"preTestCJS": "node test/functional/otTestServer.cjs &",
|
|
24
25
|
"launchTestCJS": "npx -y wait-on http://localhost:3000/api/v1/pets && npm run sendRequests && sleep 4 && npm run checkRequestsLog",
|
|
@@ -47,6 +48,7 @@
|
|
|
47
48
|
"@opentelemetry/instrumentation-http": "^0.51.0",
|
|
48
49
|
"@opentelemetry/resources": "^1.24.0",
|
|
49
50
|
"@opentelemetry/sdk-node": "^0.49.1",
|
|
51
|
+
"@seald-io/nedb": "^4.0.4",
|
|
50
52
|
"axios": "^1.6.8",
|
|
51
53
|
"cookie-parser": "^1.4.7",
|
|
52
54
|
"dynamic-installer": "^1.1.1",
|
|
@@ -55,7 +57,6 @@
|
|
|
55
57
|
"import-from-string": "^0.0.4",
|
|
56
58
|
"js-yaml": "^4.1.0",
|
|
57
59
|
"jsonwebtoken": "^9.0.2",
|
|
58
|
-
"nedb": "^1.8.0",
|
|
59
60
|
"readline": "^1.3.0",
|
|
60
61
|
"v8": "^0.1.0"
|
|
61
62
|
},
|
package/src/config.js
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import DynamicExporter from "./exporters/dynamicExporter.js";
|
|
2
2
|
|
|
3
3
|
//Environment variables
|
|
4
|
-
//OASTLM_API_KEY = 'oas-telemetry-api-key' //Cookie value for the API key
|
|
5
4
|
//OASTLM_MODULE_DISABLED = 'true' //Disables the module (empty middleware and no tracing)
|
|
6
5
|
|
|
7
6
|
export const globalOasTlmConfig = {
|
|
8
7
|
dynamicExporter: new DynamicExporter(),
|
|
9
|
-
baseURL: "/
|
|
8
|
+
baseURL: "/telemetry",
|
|
10
9
|
spec: null,
|
|
11
10
|
specFileName: "",
|
|
12
11
|
autoActivate: true,
|
|
12
|
+
authEnabled: false,
|
|
13
13
|
apiKeyMaxAge: 1000 * 60 * 60, // 1 hour
|
|
14
14
|
password: "oas-telemetry-password",
|
|
15
15
|
jwtSecret: "oas-telemetry-secret",
|
|
@@ -55,6 +55,7 @@ export const findTelemetry = (req, res) => {
|
|
|
55
55
|
res.status(404).send({ spansCount: 0, spans: [], error: e });
|
|
56
56
|
return;
|
|
57
57
|
}
|
|
58
|
+
}
|
|
58
59
|
globalOasTlmConfig.dynamicExporter.exporter.find(search,(err, docs) => {
|
|
59
60
|
if (err) {
|
|
60
61
|
console.error(err);
|
|
@@ -64,5 +65,5 @@ export const findTelemetry = (req, res) => {
|
|
|
64
65
|
const spans = docs;
|
|
65
66
|
res.send({ spansCount: spans.length, spans: spans });
|
|
66
67
|
});
|
|
67
|
-
|
|
68
|
+
|
|
68
69
|
}
|