@oas-tools/oas-telemetry 0.2.0 → 0.2.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.
package/package.json CHANGED
@@ -1,63 +1,63 @@
1
- {
2
- "name": "@oas-tools/oas-telemetry",
3
- "version": "0.2.0",
4
- "description": "This package exports an express midelware that allows to trace the requests and responses of an express application using OpenTelemetry",
5
- "author": "Manuel Otero",
6
- "contributors": [
7
- "Alejandro Santisteban","Pablo Fernandez"
8
- ],
9
- "license": "Apache-2.0",
10
- "type": "module",
11
- "scripts": {
12
- "build": "babel src -d dist --out-file-extension .cjs",
13
- "publish": "npm run build && npm publish",
14
- "testPerformance": "npm i && cd test/performance/ && ./test.sh",
15
- "pretest": "npm run build",
16
- "test": "npm run testCJS && npm run testMJS",
17
- "testCJS": "npm run preTestCJS && npm run launchTestCJS && npm run postTestCJS",
18
- "preTestCJS": "node test/functional/otTestServer.cjs &",
19
- "launchTestCJS": "npx -y wait-on http://localhost:3000/api/v1/pets && npm run sendRequests && sleep 4 && npm run checkRequestsLog",
20
- "postTestCJS": "kill `ps -uax | grep \"node test/functional/otTestServer\" | grep -v \"grep\" | grep -v \"sh\" | awk '{print $2}'`",
21
- "testMJS": "npm run preTestMJS && npm run launchTestMJS && npm run postTestMJS",
22
- "preTestMJS": "node test/functional/otTestServer.mjs &",
23
- "launchTestMJS": "npx -y wait-on http://localhost:3000/api/v1/pets && npm run sendRequests && sleep 4 && npm run checkRequestsLog",
24
- "postTestMJS": "kill `ps -uax | grep \"node test/functional/otTestServer\" | grep -v \"grep\" | grep -v \"sh\" | awk '{print $2}'`",
25
- "sendRequests": "npx -y newman run test/functional/request-collection.json -e test/functional/request-environment.json",
26
- "checkRequestsLog": "npx -y newman run test/functional/request-collection-check.json -e test/functional/request-environment.json"
27
- },
28
- "files": [
29
- "dist",
30
- "src"
31
- ],
32
- "main": "./dist/index.cjs",
33
- "module": "./src/index.js",
34
- "exports": {
35
- "require": "./dist/index.cjs",
36
- "import": "./src/index.js",
37
- "default": "./src/index.js"
38
- },
39
- "dependencies": {
40
- "@opentelemetry/instrumentation-http": "^0.51.0",
41
- "@opentelemetry/resources": "^1.24.0",
42
- "@opentelemetry/sdk-node": "^0.49.1",
43
- "axios": "^1.6.8",
44
- "express": "^4.19.2",
45
- "js-yaml": "^4.1.0",
46
- "nedb": "^1.8.0",
47
- "readline": "^1.3.0",
48
- "v8": "^0.1.0"
49
- },
50
- "devDependencies": {
51
- "@babel/cli": "^7.24.1",
52
- "@babel/core": "^7.24.4",
53
- "@babel/plugin-proposal-class-properties": "^7.18.6",
54
- "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
55
- "@babel/preset-env": "^7.24.4",
56
- "@opentelemetry/api": "^1.8.0",
57
- "@opentelemetry/auto-instrumentations-node": "^0.43.0",
58
- "apipecker": "^1.3.1",
59
- "babel-plugin-add-module-exports": "^1.0.4",
60
- "babel-plugin-module-extension": "^0.1.3",
61
- "nodemon": "^3.1.0"
62
- }
63
- }
1
+ {
2
+ "name": "@oas-tools/oas-telemetry",
3
+ "version": "0.2.1",
4
+ "description": "This package exports an express midelware that allows to trace the requests and responses of an express application using OpenTelemetry",
5
+ "author": "Manuel Otero",
6
+ "contributors": [
7
+ "Alejandro Santisteban","Pablo Fernandez"
8
+ ],
9
+ "license": "Apache-2.0",
10
+ "type": "module",
11
+ "scripts": {
12
+ "build": "babel src -d dist --out-file-extension .cjs",
13
+ "publish": "npm run build && npm publish",
14
+ "testPerformance": "npm i && cd test/performance/ && ./test.sh",
15
+ "pretest": "npm run build",
16
+ "test": "npm run testCJS && npm run testMJS",
17
+ "testCJS": "npm run preTestCJS && npm run launchTestCJS && npm run postTestCJS",
18
+ "preTestCJS": "node test/functional/otTestServer.cjs &",
19
+ "launchTestCJS": "npx -y wait-on http://localhost:3000/api/v1/pets && npm run sendRequests && sleep 4 && npm run checkRequestsLog",
20
+ "postTestCJS": "kill `ps -uax | grep \"node test/functional/otTestServer\" | grep -v \"grep\" | grep -v \"sh\" | awk '{print $2}'`",
21
+ "testMJS": "npm run preTestMJS && npm run launchTestMJS && npm run postTestMJS",
22
+ "preTestMJS": "node test/functional/otTestServer.mjs &",
23
+ "launchTestMJS": "npx -y wait-on http://localhost:3000/api/v1/pets && npm run sendRequests && sleep 4 && npm run checkRequestsLog",
24
+ "postTestMJS": "kill `ps -uax | grep \"node test/functional/otTestServer\" | grep -v \"grep\" | grep -v \"sh\" | awk '{print $2}'`",
25
+ "sendRequests": "npx -y newman run test/functional/request-collection.json -e test/functional/request-environment.json",
26
+ "checkRequestsLog": "npx -y newman run test/functional/request-collection-check.json -e test/functional/request-environment.json"
27
+ },
28
+ "files": [
29
+ "dist",
30
+ "src"
31
+ ],
32
+ "main": "./dist/index.cjs",
33
+ "module": "./src/index.js",
34
+ "exports": {
35
+ "require": "./dist/index.cjs",
36
+ "import": "./src/index.js",
37
+ "default": "./src/index.js"
38
+ },
39
+ "dependencies": {
40
+ "@opentelemetry/instrumentation-http": "^0.51.0",
41
+ "@opentelemetry/resources": "^1.24.0",
42
+ "@opentelemetry/sdk-node": "^0.49.1",
43
+ "axios": "^1.6.8",
44
+ "express": "^4.19.2",
45
+ "js-yaml": "^4.1.0",
46
+ "nedb": "^1.8.0",
47
+ "readline": "^1.3.0",
48
+ "v8": "^0.1.0"
49
+ },
50
+ "devDependencies": {
51
+ "@babel/cli": "^7.24.1",
52
+ "@babel/core": "^7.24.4",
53
+ "@babel/plugin-proposal-class-properties": "^7.18.6",
54
+ "@babel/plugin-proposal-object-rest-spread": "^7.20.7",
55
+ "@babel/preset-env": "^7.24.4",
56
+ "@opentelemetry/api": "^1.8.0",
57
+ "@opentelemetry/auto-instrumentations-node": "^0.43.0",
58
+ "apipecker": "^1.3.1",
59
+ "babel-plugin-add-module-exports": "^1.0.4",
60
+ "babel-plugin-module-extension": "^0.1.3",
61
+ "nodemon": "^3.1.0"
62
+ }
63
+ }
@@ -1,90 +1,155 @@
1
- import { ExportResultCode } from '@opentelemetry/core';
2
-
3
- //import in memory database
4
- import dataStore from 'nedb'
5
-
6
- export class InMemoryExporter {
7
- constructor() {
8
- this._spans = new dataStore();
9
- this._stopped = false;
10
- }
11
- export(readableSpans, resultCallback) {
12
- try {
13
- if (!this._stopped) {
14
- // Remove circular references
15
- const cleanSpans = readableSpans.map(span => removeCircular(span));
16
-
17
- // Insert spans into the in-memory database
18
- this._spans.insert(cleanSpans, (err, newDoc) => {
19
- if (err) {
20
- console.error(err);
21
- return;
22
- }
23
- });
24
-
25
- }
26
- setTimeout(() => resultCallback({ code: ExportResultCode.SUCCESS }), 0);
27
- } catch (error) {
28
- console.error('Error exporting spans\n' + error.message + '\n' + error.stack);
29
- return resultCallback({
30
- code: ExportResultCode.FAILED,
31
- error: new Error('Error exporting spans\n' + error.message + '\n' + error.stack),
32
- })
33
- }
34
- }
35
- start() {
36
- this._stopped = false;
37
- }
38
- stop() {
39
- this._stopped = true;
40
- }
41
- shutdown() {
42
- this._stopped = true;
43
- this._spans = new dataStore();
44
- return this.forceFlush();
45
- }
46
- /**
47
- * Exports any pending spans in the exporter
48
- */
49
- forceFlush() {
50
- return Promise.resolve();
51
- }
52
- reset() {
53
- this._spans = new dataStore();
54
- }
55
- getFinishedSpans() {
56
- return this._spans;
57
- }
58
- }
59
-
60
- function removeCircular(obj) {
61
- const seen = new WeakMap(); // Used to keep track of visited objects
62
-
63
-
64
- // Replacer function to handle circular references
65
- function replacer(key, value) {
66
- if (key === "_spanProcessor") {
67
- return "oas-telemetry skips this field to avoid circular reference";
68
- }
69
- // GENERIC CIRCULAR REFERENCE HANDLING
70
- // if (typeof value === "object" && value !== null) {
71
- // // If the object has been visited before, return the name prefixed with "CIRCULAR+"
72
- // if (seen.has(value)) {
73
- // return `CIRCULAR${key}`;
74
- // }
75
- // seen.set(value, key); // Mark the object as visited with its name
76
- // }
77
- return value;
78
- }
79
-
80
- // Convert the object to a string and then parse it back
81
- // This will trigger the replacer function to handle circular references
82
- const jsonString = JSON.stringify(obj, replacer);
83
- const spanNoDotsInKeys =jsonString.replace(/[^"]*":/g, (match) => {
84
- // Replace all dots in the key with underscores (e.g. "http.method" -> "http_method")
85
- const newMatch = match.replace(/\./g,"_dot_")
86
-
87
- return newMatch
88
- })
89
- return JSON.parse(spanNoDotsInKeys);
1
+ import { ExportResultCode } from '@opentelemetry/core';
2
+
3
+ //import in memory database
4
+ import dataStore from 'nedb'
5
+
6
+ export class InMemoryExporter {
7
+ constructor() {
8
+ this._spans = new dataStore();
9
+ this._stopped = true;
10
+ }
11
+ export(readableSpans, resultCallback) {
12
+ try {
13
+ if (!this._stopped) {
14
+ // Prepare spans to be inserted into the in-memory database (remove circular references and convert to nested objects)
15
+ const cleanSpans = readableSpans
16
+ .map(nestedSpan => removeCircularRefs(nestedSpan))// to avoid JSON parsing error
17
+ .map(span => applyNesting(span))// to avoid dot notation in keys (neDB does not support dot notation in keys)
18
+ .filter(span => !span.attributes?.http?.target?.includes("/telemetry"));// to avoid telemetry spans
19
+
20
+ // Insert spans into the in-memory database
21
+ this._spans.insert(cleanSpans, (err, newDoc) => {
22
+ if (err) {
23
+ console.error(err);
24
+ return;
25
+ }
26
+ });
27
+
28
+ }
29
+ setTimeout(() => resultCallback({ code: ExportResultCode.SUCCESS }), 0);
30
+ } catch (error) {
31
+ console.error('Error exporting spans\n' + error.message + '\n' + error.stack);
32
+ return resultCallback({
33
+ code: ExportResultCode.FAILED,
34
+ error: new Error('Error exporting spans\n' + error.message + '\n' + error.stack),
35
+ })
36
+ }
37
+ }
38
+ start() {
39
+ this._stopped = false;
40
+ }
41
+ stop() {
42
+ this._stopped = true;
43
+ }
44
+ shutdown() {
45
+ this._stopped = true;
46
+ this._spans = new dataStore();
47
+ return this.forceFlush();
48
+ }
49
+ /**
50
+ * Exports any pending spans in the exporter
51
+ */
52
+ forceFlush() {
53
+ return Promise.resolve();
54
+ }
55
+ reset() {
56
+ this._spans = new dataStore();
57
+ }
58
+ getFinishedSpans() {
59
+ return this._spans;
60
+ }
61
+ }
62
+
63
+ function removeCircularRefs(obj) {
64
+ const seen = new WeakMap(); // Used to keep track of visited objects
65
+
66
+
67
+ // Replacer function to handle circular references
68
+ function replacer(key, value) {
69
+ if (key === "_spanProcessor") {
70
+ return "oas-telemetry skips this field to avoid circular reference";
71
+ }
72
+ // GENERIC CIRCULAR REFERENCE HANDLING
73
+ // if (typeof value === "object" && value !== null) {
74
+ // // If the object has been visited before, return the name prefixed with "CIRCULAR+"
75
+ // if (seen.has(value)) {
76
+ // return `CIRCULAR${key}`;
77
+ // }
78
+ // seen.set(value, key); // Mark the object as visited with its name
79
+ // }
80
+ return value;
81
+ }
82
+
83
+ // Convert the object to a string and then parse it back
84
+ // This will trigger the replacer function to handle circular references
85
+ const jsonString = JSON.stringify(obj, replacer);
86
+ return JSON.parse(jsonString);
87
+ }
88
+
89
+ /**
90
+ * Recursively converts dot-separated keys in an object to nested objects.
91
+ *
92
+ * @param {Object} obj - The object to process.
93
+ * @returns {Object} - The object with all dot-separated keys converted to nested objects.
94
+ * @example
95
+ * // Input:
96
+ * // {
97
+ * // "http.method": "GET",
98
+ * // "http.url": "http://example.com",
99
+ * // "nested.obj.key": "value"
100
+ * // }
101
+ * // Output:
102
+ * // {
103
+ * // "http": {
104
+ * // "method": "GET",
105
+ * // "url": "http://example.com"
106
+ * // },
107
+ * // "nested": {
108
+ * // "obj": {
109
+ * // "key": "value"
110
+ * // }
111
+ * // }
112
+ * // }
113
+ */
114
+ function convertToNestedObject(obj) {
115
+ const result = {};
116
+
117
+ for (const key in obj) {
118
+ const keys = key.split('.');
119
+ let temp = result;
120
+
121
+ for (let i = 0; i < keys.length; i++) {
122
+ const currentKey = keys[i];
123
+
124
+ if (i === keys.length - 1) {
125
+ // Last key, set the value
126
+ temp[currentKey] = obj[key];
127
+ } else {
128
+ // Intermediate key, ensure the object exists
129
+ if (!temp[currentKey]) {
130
+ temp[currentKey] = {};
131
+ }
132
+ temp = temp[currentKey];
133
+ }
134
+ }
135
+ }
136
+
137
+ return result;
138
+ }
139
+
140
+ /**
141
+ * Applies nesting to all dot-separated keys within an object.
142
+ *
143
+ * @param {Object} obj - The object to apply nesting to.
144
+ * @returns {Object} - The transformed object with nested structures.
145
+ */
146
+ function applyNesting(obj) {
147
+ // Recursively apply convertToNestedObject to each level of the object
148
+ for (const key in obj) {
149
+ if (typeof obj[key] === 'object' && obj[key] !== null) {
150
+ obj[key] = applyNesting(obj[key]);
151
+ }
152
+ }
153
+
154
+ return convertToNestedObject(obj);
90
155
  }