flow-debugger 1.0.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/PORTFOLIO_README_SECTION.md +177 -0
- package/README.md +251 -0
- package/dashboard/app.js +339 -0
- package/dashboard/index.html +168 -0
- package/dashboard/style.css +846 -0
- package/dist/cjs/core/Analytics.js +174 -0
- package/dist/cjs/core/Analytics.js.map +1 -0
- package/dist/cjs/core/Classifier.js +66 -0
- package/dist/cjs/core/Classifier.js.map +1 -0
- package/dist/cjs/core/HealthMonitor.js +79 -0
- package/dist/cjs/core/HealthMonitor.js.map +1 -0
- package/dist/cjs/core/RootCause.js +89 -0
- package/dist/cjs/core/RootCause.js.map +1 -0
- package/dist/cjs/core/Sampler.js +34 -0
- package/dist/cjs/core/Sampler.js.map +1 -0
- package/dist/cjs/core/Timeline.js +90 -0
- package/dist/cjs/core/Timeline.js.map +1 -0
- package/dist/cjs/core/TraceEngine.js +222 -0
- package/dist/cjs/core/TraceEngine.js.map +1 -0
- package/dist/cjs/core/types.js +21 -0
- package/dist/cjs/core/types.js.map +1 -0
- package/dist/cjs/index.js +46 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/integrations/axios.js +136 -0
- package/dist/cjs/integrations/axios.js.map +1 -0
- package/dist/cjs/integrations/fetch.js +153 -0
- package/dist/cjs/integrations/fetch.js.map +1 -0
- package/dist/cjs/integrations/mongo.js +111 -0
- package/dist/cjs/integrations/mongo.js.map +1 -0
- package/dist/cjs/integrations/mysql.js +212 -0
- package/dist/cjs/integrations/mysql.js.map +1 -0
- package/dist/cjs/integrations/postgres.js +182 -0
- package/dist/cjs/integrations/postgres.js.map +1 -0
- package/dist/cjs/integrations/redis.js +105 -0
- package/dist/cjs/integrations/redis.js.map +1 -0
- package/dist/cjs/middleware/express.js +255 -0
- package/dist/cjs/middleware/express.js.map +1 -0
- package/dist/esm/core/Analytics.js +170 -0
- package/dist/esm/core/Analytics.js.map +1 -0
- package/dist/esm/core/Classifier.js +61 -0
- package/dist/esm/core/Classifier.js.map +1 -0
- package/dist/esm/core/HealthMonitor.js +75 -0
- package/dist/esm/core/HealthMonitor.js.map +1 -0
- package/dist/esm/core/RootCause.js +86 -0
- package/dist/esm/core/RootCause.js.map +1 -0
- package/dist/esm/core/Sampler.js +30 -0
- package/dist/esm/core/Sampler.js.map +1 -0
- package/dist/esm/core/Timeline.js +86 -0
- package/dist/esm/core/Timeline.js.map +1 -0
- package/dist/esm/core/TraceEngine.js +217 -0
- package/dist/esm/core/TraceEngine.js.map +1 -0
- package/dist/esm/core/types.js +18 -0
- package/dist/esm/core/types.js.map +1 -0
- package/dist/esm/index.js +22 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/integrations/axios.js +133 -0
- package/dist/esm/integrations/axios.js.map +1 -0
- package/dist/esm/integrations/fetch.js +149 -0
- package/dist/esm/integrations/fetch.js.map +1 -0
- package/dist/esm/integrations/mongo.js +107 -0
- package/dist/esm/integrations/mongo.js.map +1 -0
- package/dist/esm/integrations/mysql.js +209 -0
- package/dist/esm/integrations/mysql.js.map +1 -0
- package/dist/esm/integrations/postgres.js +179 -0
- package/dist/esm/integrations/postgres.js.map +1 -0
- package/dist/esm/integrations/redis.js +102 -0
- package/dist/esm/integrations/redis.js.map +1 -0
- package/dist/esm/middleware/express.js +219 -0
- package/dist/esm/middleware/express.js.map +1 -0
- package/dist/types/core/Analytics.d.ts +35 -0
- package/dist/types/core/Analytics.d.ts.map +1 -0
- package/dist/types/core/Classifier.d.ts +21 -0
- package/dist/types/core/Classifier.d.ts.map +1 -0
- package/dist/types/core/HealthMonitor.d.ts +14 -0
- package/dist/types/core/HealthMonitor.d.ts.map +1 -0
- package/dist/types/core/RootCause.d.ts +12 -0
- package/dist/types/core/RootCause.d.ts.map +1 -0
- package/dist/types/core/Sampler.d.ts +13 -0
- package/dist/types/core/Sampler.d.ts.map +1 -0
- package/dist/types/core/Timeline.d.ts +22 -0
- package/dist/types/core/Timeline.d.ts.map +1 -0
- package/dist/types/core/TraceEngine.d.ts +47 -0
- package/dist/types/core/TraceEngine.d.ts.map +1 -0
- package/dist/types/core/types.d.ts +118 -0
- package/dist/types/core/types.d.ts.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/integrations/axios.d.ts +22 -0
- package/dist/types/integrations/axios.d.ts.map +1 -0
- package/dist/types/integrations/fetch.d.ts +25 -0
- package/dist/types/integrations/fetch.d.ts.map +1 -0
- package/dist/types/integrations/mongo.d.ts +26 -0
- package/dist/types/integrations/mongo.d.ts.map +1 -0
- package/dist/types/integrations/mysql.d.ts +20 -0
- package/dist/types/integrations/mysql.d.ts.map +1 -0
- package/dist/types/integrations/postgres.d.ts +20 -0
- package/dist/types/integrations/postgres.d.ts.map +1 -0
- package/dist/types/integrations/redis.d.ts +20 -0
- package/dist/types/integrations/redis.d.ts.map +1 -0
- package/dist/types/middleware/express.d.ts +39 -0
- package/dist/types/middleware/express.d.ts.map +1 -0
- package/example/server.ts +234 -0
- package/jest.config.js +8 -0
- package/package.json +110 -0
- package/portfolio-repo/APIRESPONSE DASH.png +0 -0
- package/portfolio-repo/PAYLOAD.png +0 -0
- package/portfolio-repo/README.md +182 -0
- package/src/core/Analytics.ts +209 -0
- package/src/core/Classifier.ts +82 -0
- package/src/core/HealthMonitor.ts +92 -0
- package/src/core/RootCause.ts +105 -0
- package/src/core/Sampler.ts +35 -0
- package/src/core/Timeline.ts +108 -0
- package/src/core/TraceEngine.ts +266 -0
- package/src/core/types.ts +170 -0
- package/src/index.ts +42 -0
- package/src/integrations/axios.ts +164 -0
- package/src/integrations/fetch.ts +172 -0
- package/src/integrations/mongo.ts +130 -0
- package/src/integrations/mysql.ts +239 -0
- package/src/integrations/postgres.ts +217 -0
- package/src/integrations/redis.ts +122 -0
- package/src/middleware/express.ts +264 -0
- package/tests/Analytics.test.ts +136 -0
- package/tests/Classifier.test.ts +57 -0
- package/tests/RootCause.test.ts +69 -0
- package/tests/TraceEngine.test.ts +110 -0
- package/tsconfig.cjs.json +9 -0
- package/tsconfig.esm.json +9 -0
- package/tsconfig.json +31 -0
- package/tsconfig.types.json +8 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ─────────────────────────────────────────────────────────────
|
|
3
|
+
// flow-debugger — MongoDB/Mongoose Auto-Instrument
|
|
4
|
+
// Patches Mongoose to automatically trace all DB queries
|
|
5
|
+
// ─────────────────────────────────────────────────────────────
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.mongoTracer = mongoTracer;
|
|
8
|
+
exports.removeMongoTracer = removeMongoTracer;
|
|
9
|
+
const types_1 = require("../core/types");
|
|
10
|
+
const Classifier_1 = require("../core/Classifier");
|
|
11
|
+
/**
|
|
12
|
+
* Auto-instrument Mongoose to trace all queries.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* mongoTracer(mongoose, { getTracer: () => currentTracer })
|
|
16
|
+
*
|
|
17
|
+
* Output:
|
|
18
|
+
* Mongo users.findOne → 22ms ✔
|
|
19
|
+
* Mongo payments.insertMany → error ❌
|
|
20
|
+
*/
|
|
21
|
+
function mongoTracer(mongoose, options) {
|
|
22
|
+
try {
|
|
23
|
+
const config = { ...types_1.DEFAULT_CONFIG, ...options?.config };
|
|
24
|
+
const queryProto = mongoose.Query?.prototype;
|
|
25
|
+
if (!queryProto || queryProto.__flowDebuggerPatched)
|
|
26
|
+
return;
|
|
27
|
+
queryProto.__flowDebuggerPatched = true;
|
|
28
|
+
const originalExec = queryProto.exec;
|
|
29
|
+
queryProto.exec = async function (...args) {
|
|
30
|
+
const tracer = options?.getTracer?.();
|
|
31
|
+
if (!tracer) {
|
|
32
|
+
return originalExec.apply(this, args);
|
|
33
|
+
}
|
|
34
|
+
const op = this.op || 'query';
|
|
35
|
+
const collection = this.mongooseCollection?.name || this.model?.collection?.name || 'unknown';
|
|
36
|
+
const stepName = `Mongo ${collection}.${op}`;
|
|
37
|
+
const startTime = performance.now();
|
|
38
|
+
let status = 'success';
|
|
39
|
+
let error;
|
|
40
|
+
let stackTrace;
|
|
41
|
+
let result;
|
|
42
|
+
try {
|
|
43
|
+
result = await originalExec.apply(this, args);
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
status = 'error';
|
|
47
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
48
|
+
error = e.message;
|
|
49
|
+
stackTrace = e.stack;
|
|
50
|
+
// Record step before re-throwing
|
|
51
|
+
const endTime = performance.now();
|
|
52
|
+
const duration = endTime - startTime;
|
|
53
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
54
|
+
const step = {
|
|
55
|
+
name: stepName,
|
|
56
|
+
service: 'mongo',
|
|
57
|
+
status,
|
|
58
|
+
classification,
|
|
59
|
+
startTime: startTime,
|
|
60
|
+
endTime,
|
|
61
|
+
duration,
|
|
62
|
+
error,
|
|
63
|
+
stackTrace,
|
|
64
|
+
metadata: { operation: op, collection },
|
|
65
|
+
};
|
|
66
|
+
tracer.addStep(step);
|
|
67
|
+
throw err;
|
|
68
|
+
}
|
|
69
|
+
const endTime = performance.now();
|
|
70
|
+
const duration = endTime - startTime;
|
|
71
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
72
|
+
const step = {
|
|
73
|
+
name: stepName,
|
|
74
|
+
service: 'mongo',
|
|
75
|
+
status,
|
|
76
|
+
classification,
|
|
77
|
+
startTime: startTime,
|
|
78
|
+
endTime,
|
|
79
|
+
duration,
|
|
80
|
+
error,
|
|
81
|
+
stackTrace,
|
|
82
|
+
metadata: { operation: op, collection },
|
|
83
|
+
};
|
|
84
|
+
tracer.addStep(step);
|
|
85
|
+
// Log slow query warning
|
|
86
|
+
if (duration > (config.slowQueryThreshold ?? 300)) {
|
|
87
|
+
try {
|
|
88
|
+
(config.logger || console.log)(`\x1b[33m⚠ Slow MongoDB query: ${stepName} (${Math.round(duration)}ms)\x1b[0m`);
|
|
89
|
+
}
|
|
90
|
+
catch (_) { }
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
catch (_) {
|
|
96
|
+
// Production-safe: never crash the app
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Remove the monkey-patch from Mongoose (for testing/cleanup).
|
|
101
|
+
*/
|
|
102
|
+
function removeMongoTracer(mongoose) {
|
|
103
|
+
try {
|
|
104
|
+
const queryProto = mongoose.Query?.prototype;
|
|
105
|
+
if (queryProto) {
|
|
106
|
+
delete queryProto.__flowDebuggerPatched;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (_) { }
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=mongo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mongo.js","sourceRoot":"","sources":["../../../src/integrations/mongo.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,mDAAmD;AACnD,yDAAyD;AACzD,gEAAgE;;AAyBhE,kCAyFC;AAKD,8CAOC;AA3HD,yCAAsF;AACtF,mDAAmD;AAWnD;;;;;;;;;GASG;AACH,SAAgB,WAAW,CAAC,QAAqB,EAAE,OAA4B;IAC3E,IAAI,CAAC;QACD,MAAM,MAAM,GAAG,EAAE,GAAG,sBAAc,EAAE,GAAG,OAAO,EAAE,MAAM,EAAE,CAAC;QACzD,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;QAE7C,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC,qBAAqB;YAAE,OAAO;QAC5D,UAAU,CAAC,qBAAqB,GAAG,IAAI,CAAC;QAExC,MAAM,YAAY,GAAG,UAAU,CAAC,IAAI,CAAC;QAErC,UAAU,CAAC,IAAI,GAAG,KAAK,WAAsB,GAAG,IAAW;YACvD,MAAM,MAAM,GAAG,OAAO,EAAE,SAAS,EAAE,EAAE,CA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IAAI,CAAC;QACD,MAAM,UAAU,GAAG,QAAQ,CAAC,KAAK,EAAE,SAAS,CAAC;QAC7C,IAAI,UAAU,EAAE,CAAC;YACb,OAAO,UAAU,CAAC,qBAAqB,CAAC;QAC5C,CAAC;IACL,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC,CAAC,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ─────────────────────────────────────────────────────────────
|
|
3
|
+
// flow-debugger — MySQL Auto-Instrument (mysql2)
|
|
4
|
+
// Patches mysql2 pool to trace all queries
|
|
5
|
+
// ─────────────────────────────────────────────────────────────
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.mysqlTracer = mysqlTracer;
|
|
8
|
+
const types_1 = require("../core/types");
|
|
9
|
+
const Classifier_1 = require("../core/Classifier");
|
|
10
|
+
/**
|
|
11
|
+
* Auto-instrument mysql2 pool to trace all queries.
|
|
12
|
+
*
|
|
13
|
+
* Usage:
|
|
14
|
+
* mysqlTracer(pool, { getTracer: () => currentTracer })
|
|
15
|
+
*
|
|
16
|
+
* Output:
|
|
17
|
+
* MySQL SELECT users → 12ms ✔
|
|
18
|
+
* MySQL INSERT payments → error ❌
|
|
19
|
+
*/
|
|
20
|
+
function mysqlTracer(pool, options) {
|
|
21
|
+
try {
|
|
22
|
+
if (pool.__flowDebuggerPatched)
|
|
23
|
+
return;
|
|
24
|
+
pool.__flowDebuggerPatched = true;
|
|
25
|
+
const config = { ...types_1.DEFAULT_CONFIG, ...options?.config };
|
|
26
|
+
// Patch pool.query
|
|
27
|
+
const originalQuery = pool.query.bind(pool);
|
|
28
|
+
pool.query = function tracedQuery(...args) {
|
|
29
|
+
const tracer = options?.getTracer?.();
|
|
30
|
+
if (!tracer) {
|
|
31
|
+
return originalQuery(...args);
|
|
32
|
+
}
|
|
33
|
+
const sql = typeof args[0] === 'string' ? args[0] : args[0]?.sql || 'unknown';
|
|
34
|
+
const operation = extractSqlOperation(sql);
|
|
35
|
+
const stepName = `MySQL ${operation}`;
|
|
36
|
+
const startTime = performance.now();
|
|
37
|
+
// Check if using callback or promise style
|
|
38
|
+
const lastArg = args[args.length - 1];
|
|
39
|
+
if (typeof lastArg === 'function') {
|
|
40
|
+
// Callback style
|
|
41
|
+
const callback = lastArg;
|
|
42
|
+
args[args.length - 1] = function (err, results, fields) {
|
|
43
|
+
const endTime = performance.now();
|
|
44
|
+
const duration = endTime - startTime;
|
|
45
|
+
const status = err ? 'error' : 'success';
|
|
46
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
47
|
+
const step = {
|
|
48
|
+
name: stepName,
|
|
49
|
+
service: 'mysql',
|
|
50
|
+
status,
|
|
51
|
+
classification,
|
|
52
|
+
startTime,
|
|
53
|
+
endTime,
|
|
54
|
+
duration,
|
|
55
|
+
error: err?.message,
|
|
56
|
+
stackTrace: err?.stack,
|
|
57
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
58
|
+
};
|
|
59
|
+
tracer.addStep(step);
|
|
60
|
+
if (duration > (config.slowQueryThreshold ?? 300)) {
|
|
61
|
+
try {
|
|
62
|
+
(config.logger || console.log)(`\x1b[33m⚠ Slow MySQL query: ${stepName} (${Math.round(duration)}ms)\n ${sql.substring(0, 100)}\x1b[0m`);
|
|
63
|
+
}
|
|
64
|
+
catch (_) { }
|
|
65
|
+
}
|
|
66
|
+
callback(err, results, fields);
|
|
67
|
+
};
|
|
68
|
+
return originalQuery(...args);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Promise style
|
|
72
|
+
const promise = originalQuery(...args);
|
|
73
|
+
if (promise && typeof promise.then === 'function') {
|
|
74
|
+
return promise.then((result) => {
|
|
75
|
+
const endTime = performance.now();
|
|
76
|
+
const duration = endTime - startTime;
|
|
77
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, 'success', config);
|
|
78
|
+
const step = {
|
|
79
|
+
name: stepName,
|
|
80
|
+
service: 'mysql',
|
|
81
|
+
status: 'success',
|
|
82
|
+
classification,
|
|
83
|
+
startTime,
|
|
84
|
+
endTime,
|
|
85
|
+
duration,
|
|
86
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
87
|
+
};
|
|
88
|
+
tracer.addStep(step);
|
|
89
|
+
if (duration > (config.slowQueryThreshold ?? 300)) {
|
|
90
|
+
try {
|
|
91
|
+
(config.logger || console.log)(`\x1b[33m⚠ Slow MySQL query: ${stepName} (${Math.round(duration)}ms)\n ${sql.substring(0, 100)}\x1b[0m`);
|
|
92
|
+
}
|
|
93
|
+
catch (_) { }
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
}, (err) => {
|
|
97
|
+
const endTime = performance.now();
|
|
98
|
+
const duration = endTime - startTime;
|
|
99
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, 'error', config);
|
|
100
|
+
const step = {
|
|
101
|
+
name: stepName,
|
|
102
|
+
service: 'mysql',
|
|
103
|
+
status: 'error',
|
|
104
|
+
classification,
|
|
105
|
+
startTime,
|
|
106
|
+
endTime,
|
|
107
|
+
duration,
|
|
108
|
+
error: err?.message,
|
|
109
|
+
stackTrace: err?.stack,
|
|
110
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
111
|
+
};
|
|
112
|
+
tracer.addStep(step);
|
|
113
|
+
throw err;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
return promise;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
// Patch pool.execute (mysql2 specific)
|
|
120
|
+
if (pool.execute) {
|
|
121
|
+
const originalExecute = pool.execute.bind(pool);
|
|
122
|
+
pool.execute = function tracedExecute(...args) {
|
|
123
|
+
const tracer = options?.getTracer?.();
|
|
124
|
+
if (!tracer) {
|
|
125
|
+
return originalExecute(...args);
|
|
126
|
+
}
|
|
127
|
+
const sql = typeof args[0] === 'string' ? args[0] : args[0]?.sql || 'unknown';
|
|
128
|
+
const operation = extractSqlOperation(sql);
|
|
129
|
+
const stepName = `MySQL ${operation}`;
|
|
130
|
+
const startTime = performance.now();
|
|
131
|
+
const lastArg = args[args.length - 1];
|
|
132
|
+
if (typeof lastArg === 'function') {
|
|
133
|
+
const callback = lastArg;
|
|
134
|
+
args[args.length - 1] = function (err, results, fields) {
|
|
135
|
+
const endTime = performance.now();
|
|
136
|
+
const duration = endTime - startTime;
|
|
137
|
+
const status = err ? 'error' : 'success';
|
|
138
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
139
|
+
tracer.addStep({
|
|
140
|
+
name: stepName,
|
|
141
|
+
service: 'mysql',
|
|
142
|
+
status,
|
|
143
|
+
classification,
|
|
144
|
+
startTime,
|
|
145
|
+
endTime,
|
|
146
|
+
duration,
|
|
147
|
+
error: err?.message,
|
|
148
|
+
stackTrace: err?.stack,
|
|
149
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
150
|
+
});
|
|
151
|
+
callback(err, results, fields);
|
|
152
|
+
};
|
|
153
|
+
return originalExecute(...args);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const promise = originalExecute(...args);
|
|
157
|
+
if (promise && typeof promise.then === 'function') {
|
|
158
|
+
return promise.then((result) => {
|
|
159
|
+
const endTime = performance.now();
|
|
160
|
+
const duration = endTime - startTime;
|
|
161
|
+
tracer.addStep({
|
|
162
|
+
name: stepName,
|
|
163
|
+
service: 'mysql',
|
|
164
|
+
status: 'success',
|
|
165
|
+
classification: (0, Classifier_1.classifyQuery)(duration, 'success', config),
|
|
166
|
+
startTime,
|
|
167
|
+
endTime,
|
|
168
|
+
duration,
|
|
169
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
170
|
+
});
|
|
171
|
+
return result;
|
|
172
|
+
}, (err) => {
|
|
173
|
+
const endTime = performance.now();
|
|
174
|
+
const duration = endTime - startTime;
|
|
175
|
+
tracer.addStep({
|
|
176
|
+
name: stepName,
|
|
177
|
+
service: 'mysql',
|
|
178
|
+
status: 'error',
|
|
179
|
+
classification: (0, Classifier_1.classifyQuery)(duration, 'error', config),
|
|
180
|
+
startTime,
|
|
181
|
+
endTime,
|
|
182
|
+
duration,
|
|
183
|
+
error: err?.message,
|
|
184
|
+
stackTrace: err?.stack,
|
|
185
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
186
|
+
});
|
|
187
|
+
throw err;
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return promise;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (_) {
|
|
196
|
+
// Production-safe: never crash
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** Extract the SQL operation type from a query string */
|
|
200
|
+
function extractSqlOperation(sql) {
|
|
201
|
+
const trimmed = sql.trim().toUpperCase();
|
|
202
|
+
const match = trimmed.match(/^(SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|TRUNCATE|REPLACE|CALL)\b/);
|
|
203
|
+
if (match) {
|
|
204
|
+
const op = match[1];
|
|
205
|
+
// Try to extract table name
|
|
206
|
+
const tableMatch = sql.match(/(?:FROM|INTO|UPDATE|TABLE|JOIN)\s+[`"]?(\w+)[`"]?/i);
|
|
207
|
+
const table = tableMatch ? tableMatch[1] : '';
|
|
208
|
+
return table ? `${op} ${table}` : op;
|
|
209
|
+
}
|
|
210
|
+
return sql.substring(0, 30);
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=mysql.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mysql.js","sourceRoot":"","sources":["../../../src/integrations/mysql.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,iDAAiD;AACjD,2CAA2C;AAC3C,gEAAgE;;AAuBhE,kCAsMC;AA1ND,yCAAsF;AACtF,mDAAmD;AASnD;;;;;;;;;GASG;AACH,SAAgB,WAAW,CAAC,IAAa,EAAE,OAA4B;IACnE,IAAI,CAAC;QACD,IAAI,IAAI,CAAC,qBA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G,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACzC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,0EAA0E,CAAC,CAAC;IACxG,IAAI,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACpB,4BAA4B;QAC5B,MAAM,UAAU,GAAG,GAAG,CAAC,KAAK,CAAC,oDAAoD,CAAC,CAAC;QACnF,MAAM,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9C,OAAO,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACzC,CAAC;IACD,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ─────────────────────────────────────────────────────────────
|
|
3
|
+
// flow-debugger — PostgreSQL Auto-Instrument (pg)
|
|
4
|
+
// Patches pg Pool/Client to trace all queries
|
|
5
|
+
// ─────────────────────────────────────────────────────────────
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.pgTracer = pgTracer;
|
|
8
|
+
const types_1 = require("../core/types");
|
|
9
|
+
const Classifier_1 = require("../core/Classifier");
|
|
10
|
+
/**
|
|
11
|
+
* Auto-instrument pg Pool/Client to trace all queries.
|
|
12
|
+
*
|
|
13
|
+
* Usage:
|
|
14
|
+
* pgTracer(pool, { getTracer: () => currentTracer })
|
|
15
|
+
*
|
|
16
|
+
* Output:
|
|
17
|
+
* Postgres SELECT orders → 18ms ✔
|
|
18
|
+
* Postgres UPDATE invoice → timeout ❌
|
|
19
|
+
*/
|
|
20
|
+
function pgTracer(pool, options) {
|
|
21
|
+
try {
|
|
22
|
+
if (pool.__flowDebuggerPatched)
|
|
23
|
+
return;
|
|
24
|
+
pool.__flowDebuggerPatched = true;
|
|
25
|
+
const config = { ...types_1.DEFAULT_CONFIG, ...options?.config };
|
|
26
|
+
const originalQuery = pool.query.bind(pool);
|
|
27
|
+
pool.query = function tracedQuery(...args) {
|
|
28
|
+
const tracer = options?.getTracer?.();
|
|
29
|
+
if (!tracer) {
|
|
30
|
+
return originalQuery(...args);
|
|
31
|
+
}
|
|
32
|
+
const sql = extractSql(args);
|
|
33
|
+
const operation = extractPgOperation(sql);
|
|
34
|
+
const stepName = `Postgres ${operation}`;
|
|
35
|
+
const startTime = performance.now();
|
|
36
|
+
// Detect if callback is provided
|
|
37
|
+
const lastArg = args[args.length - 1];
|
|
38
|
+
if (typeof lastArg === 'function') {
|
|
39
|
+
// Callback style
|
|
40
|
+
const callback = lastArg;
|
|
41
|
+
args[args.length - 1] = function (err, result) {
|
|
42
|
+
recordStep(tracer, stepName, startTime, err, sql, operation, config);
|
|
43
|
+
callback(err, result);
|
|
44
|
+
};
|
|
45
|
+
return originalQuery(...args);
|
|
46
|
+
}
|
|
47
|
+
// Promise style
|
|
48
|
+
const promise = originalQuery(...args);
|
|
49
|
+
if (promise && typeof promise.then === 'function') {
|
|
50
|
+
return promise.then((result) => {
|
|
51
|
+
recordStep(tracer, stepName, startTime, null, sql, operation, config);
|
|
52
|
+
return result;
|
|
53
|
+
}, (err) => {
|
|
54
|
+
recordStep(tracer, stepName, startTime, err, sql, operation, config);
|
|
55
|
+
throw err;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return promise;
|
|
59
|
+
};
|
|
60
|
+
// Also patch connect() to instrument clients from the pool
|
|
61
|
+
if (pool.connect && !pool.__connectPatched) {
|
|
62
|
+
pool.__connectPatched = true;
|
|
63
|
+
const originalConnect = pool.connect.bind(pool);
|
|
64
|
+
pool.connect = function tracedConnect(...connectArgs) {
|
|
65
|
+
const lastArg = connectArgs[connectArgs.length - 1];
|
|
66
|
+
if (typeof lastArg === 'function') {
|
|
67
|
+
// Callback style: connect((err, client, done) => { ... })
|
|
68
|
+
const callback = lastArg;
|
|
69
|
+
connectArgs[connectArgs.length - 1] = function (err, client, done) {
|
|
70
|
+
if (client && !client.__flowDebuggerPatched) {
|
|
71
|
+
patchClient(client, options, config);
|
|
72
|
+
}
|
|
73
|
+
callback(err, client, done);
|
|
74
|
+
};
|
|
75
|
+
return originalConnect(...connectArgs);
|
|
76
|
+
}
|
|
77
|
+
// Promise style
|
|
78
|
+
const promise = originalConnect(...connectArgs);
|
|
79
|
+
if (promise && typeof promise.then === 'function') {
|
|
80
|
+
return promise.then((client) => {
|
|
81
|
+
if (client && !client.__flowDebuggerPatched) {
|
|
82
|
+
patchClient(client, options, config);
|
|
83
|
+
}
|
|
84
|
+
return client;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return promise;
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
catch (_) {
|
|
92
|
+
// Production-safe: never crash
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Patch an individual pg Client's query method */
|
|
96
|
+
function patchClient(client, options, config) {
|
|
97
|
+
try {
|
|
98
|
+
client.__flowDebuggerPatched = true;
|
|
99
|
+
const originalClientQuery = client.query.bind(client);
|
|
100
|
+
client.query = function tracedClientQuery(...args) {
|
|
101
|
+
const tracer = options?.getTracer?.();
|
|
102
|
+
if (!tracer) {
|
|
103
|
+
return originalClientQuery(...args);
|
|
104
|
+
}
|
|
105
|
+
const sql = extractSql(args);
|
|
106
|
+
const operation = extractPgOperation(sql);
|
|
107
|
+
const stepName = `Postgres ${operation}`;
|
|
108
|
+
const startTime = performance.now();
|
|
109
|
+
const lastArg = args[args.length - 1];
|
|
110
|
+
if (typeof lastArg === 'function') {
|
|
111
|
+
const callback = lastArg;
|
|
112
|
+
args[args.length - 1] = function (err, result) {
|
|
113
|
+
recordStep(tracer, stepName, startTime, err, sql, operation, config);
|
|
114
|
+
callback(err, result);
|
|
115
|
+
};
|
|
116
|
+
return originalClientQuery(...args);
|
|
117
|
+
}
|
|
118
|
+
const promise = originalClientQuery(...args);
|
|
119
|
+
if (promise && typeof promise.then === 'function') {
|
|
120
|
+
return promise.then((result) => {
|
|
121
|
+
recordStep(tracer, stepName, startTime, null, sql, operation, config);
|
|
122
|
+
return result;
|
|
123
|
+
}, (err) => {
|
|
124
|
+
recordStep(tracer, stepName, startTime, err, sql, operation, config);
|
|
125
|
+
throw err;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
return promise;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
catch (_) { }
|
|
132
|
+
}
|
|
133
|
+
/** Record a step from query execution */
|
|
134
|
+
function recordStep(tracer, stepName, startTime, err, sql, operation, config) {
|
|
135
|
+
try {
|
|
136
|
+
const endTime = performance.now();
|
|
137
|
+
const duration = endTime - startTime;
|
|
138
|
+
const status = err ? 'error' : 'success';
|
|
139
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
140
|
+
const step = {
|
|
141
|
+
name: stepName,
|
|
142
|
+
service: 'postgres',
|
|
143
|
+
status,
|
|
144
|
+
classification,
|
|
145
|
+
startTime,
|
|
146
|
+
endTime,
|
|
147
|
+
duration,
|
|
148
|
+
error: err?.message,
|
|
149
|
+
stackTrace: err?.stack,
|
|
150
|
+
metadata: { sql: sql.substring(0, 200), operation },
|
|
151
|
+
};
|
|
152
|
+
tracer.addStep(step);
|
|
153
|
+
if (duration > (config.slowQueryThreshold ?? 300)) {
|
|
154
|
+
try {
|
|
155
|
+
(config.logger || console.log)(`\x1b[33m⚠ Slow PostgreSQL query: ${stepName} (${Math.round(duration)}ms)\n ${sql.substring(0, 100)}\x1b[0m`);
|
|
156
|
+
}
|
|
157
|
+
catch (_) { }
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
catch (_) { }
|
|
161
|
+
}
|
|
162
|
+
/** Extract SQL string from query arguments */
|
|
163
|
+
function extractSql(args) {
|
|
164
|
+
if (typeof args[0] === 'string')
|
|
165
|
+
return args[0];
|
|
166
|
+
if (args[0] && typeof args[0] === 'object' && args[0].text)
|
|
167
|
+
return args[0].text;
|
|
168
|
+
return 'unknown';
|
|
169
|
+
}
|
|
170
|
+
/** Extract the SQL operation type from a query string */
|
|
171
|
+
function extractPgOperation(sql) {
|
|
172
|
+
const trimmed = sql.trim().toUpperCase();
|
|
173
|
+
const match = trimmed.match(/^(SELECT|INSERT|UPDATE|DELETE|CREATE|ALTER|DROP|TRUNCATE|WITH)\b/);
|
|
174
|
+
if (match) {
|
|
175
|
+
const op = match[1];
|
|
176
|
+
const tableMatch = sql.match(/(?:FROM|INTO|UPDATE|TABLE|JOIN)\s+"?(\w+)"?/i);
|
|
177
|
+
const table = tableMatch ? tableMatch[1] : '';
|
|
178
|
+
return table ? `${op} ${table}` : op;
|
|
179
|
+
}
|
|
180
|
+
return sql.substring(0, 30);
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=postgres.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"postgres.js","sourceRoot":"","sources":["../../../src/integrations/postgres.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,kDAAkD;AAClD,8CAA8C;AAC9C,gEAAgE;;AAuBhE,4BAoFC;AAxGD,yCAAsF;AACtF,mDAAmD;AASnD;;;;;;;;;GASG;AACH,SAAgB,QAAQ,CAAC,IAAa,EAAE,OAAyB;IAC7D,IAAI,CAAC;QACD,IAAI,IAAI,CAAC,qBAAqB;YAAE,OAAO;QACvC,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;QAElC,MAAM,MAAM,GAAG,EAAE,GAAG,sBAAc,EAAE,GAAG,OAAO,EAAE,MAAM,EAAE,CAAC;QACzD,MAAM,aAAa,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAE5C,IAAI,CAAC,KAAK,GAAG,SAAS,WAAW,CAAC,GAAG,IAAW;YAC5C,MA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AAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAChF,OAAO,SAAS,CAAC;AACrB,CAAC;AAED,yDAAyD;AACzD,SAAS,kBAAkB,CAAC,GAAW;IACnC,MAAM,OAAO,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACzC,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC,kEAAkE,CAAC,CAAC;IAChG,IAAI,KAAK,EAAE,CAAC;QACR,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACpB,MAAM,UAAU,GAAG,GAAG,CAAC,KAAK,CAAC,8CAA8C,CAAC,CAAC;QAC7E,MAAM,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9C,OAAO,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;IACzC,CAAC;IACD,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAChC,CAAC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// ─────────────────────────────────────────────────────────────
|
|
3
|
+
// flow-debugger — Redis Auto-Instrument
|
|
4
|
+
// Wraps Redis client commands to trace all operations
|
|
5
|
+
// ─────────────────────────────────────────────────────────────
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.redisTracer = redisTracer;
|
|
8
|
+
const types_1 = require("../core/types");
|
|
9
|
+
const Classifier_1 = require("../core/Classifier");
|
|
10
|
+
/** Commands to trace */
|
|
11
|
+
const TRACED_COMMANDS = [
|
|
12
|
+
'get', 'set', 'del', 'exists', 'expire', 'ttl',
|
|
13
|
+
'hget', 'hset', 'hdel', 'hgetall', 'hmset', 'hmget',
|
|
14
|
+
'lpush', 'rpush', 'lpop', 'rpop', 'lrange', 'llen',
|
|
15
|
+
'sadd', 'srem', 'smembers', 'sismember',
|
|
16
|
+
'zadd', 'zrem', 'zrange', 'zrank', 'zscore',
|
|
17
|
+
'incr', 'decr', 'incrby', 'decrby',
|
|
18
|
+
'setex', 'setnx', 'mget', 'mset',
|
|
19
|
+
'publish', 'subscribe',
|
|
20
|
+
'eval', 'evalsha',
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* Auto-instrument a Redis client (ioredis or node-redis v4).
|
|
24
|
+
*
|
|
25
|
+
* Usage:
|
|
26
|
+
* redisTracer(redisClient, { getTracer: () => currentTracer })
|
|
27
|
+
*
|
|
28
|
+
* Output:
|
|
29
|
+
* Redis SET session → 3ms ✔
|
|
30
|
+
* Redis GET cache → timeout ❌
|
|
31
|
+
*/
|
|
32
|
+
function redisTracer(client, options) {
|
|
33
|
+
try {
|
|
34
|
+
if (client.__flowDebuggerPatched)
|
|
35
|
+
return;
|
|
36
|
+
client.__flowDebuggerPatched = true;
|
|
37
|
+
const config = { ...types_1.DEFAULT_CONFIG, ...options?.config };
|
|
38
|
+
for (const command of TRACED_COMMANDS) {
|
|
39
|
+
if (typeof client[command] !== 'function')
|
|
40
|
+
continue;
|
|
41
|
+
const original = client[command].bind(client);
|
|
42
|
+
client[command] = async function tracedCommand(...args) {
|
|
43
|
+
const tracer = options?.getTracer?.();
|
|
44
|
+
if (!tracer) {
|
|
45
|
+
return original(...args);
|
|
46
|
+
}
|
|
47
|
+
const key = args[0] !== undefined ? String(args[0]).substring(0, 50) : '';
|
|
48
|
+
const stepName = `Redis ${command.toUpperCase()} ${key}`;
|
|
49
|
+
const startTime = performance.now();
|
|
50
|
+
let status = 'success';
|
|
51
|
+
let error;
|
|
52
|
+
let stackTrace;
|
|
53
|
+
try {
|
|
54
|
+
const result = await original(...args);
|
|
55
|
+
const endTime = performance.now();
|
|
56
|
+
const duration = endTime - startTime;
|
|
57
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
58
|
+
tracer.addStep({
|
|
59
|
+
name: stepName,
|
|
60
|
+
service: 'redis',
|
|
61
|
+
status,
|
|
62
|
+
classification,
|
|
63
|
+
startTime,
|
|
64
|
+
endTime,
|
|
65
|
+
duration,
|
|
66
|
+
metadata: { command: command.toUpperCase(), key },
|
|
67
|
+
});
|
|
68
|
+
return result;
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
const e = err instanceof Error ? err : new Error(String(err));
|
|
72
|
+
error = e.message;
|
|
73
|
+
stackTrace = e.stack;
|
|
74
|
+
// Detect connection errors as CRITICAL
|
|
75
|
+
const isConnectionError = error.includes('ECONNREFUSED') ||
|
|
76
|
+
error.includes('ECONNRESET') ||
|
|
77
|
+
error.includes('ETIMEDOUT') ||
|
|
78
|
+
error.includes('connection') ||
|
|
79
|
+
error.includes('Connection');
|
|
80
|
+
status = isConnectionError ? 'timeout' : 'error';
|
|
81
|
+
const endTime = performance.now();
|
|
82
|
+
const duration = endTime - startTime;
|
|
83
|
+
const classification = (0, Classifier_1.classifyQuery)(duration, status, config);
|
|
84
|
+
tracer.addStep({
|
|
85
|
+
name: stepName,
|
|
86
|
+
service: 'redis',
|
|
87
|
+
status,
|
|
88
|
+
classification,
|
|
89
|
+
startTime,
|
|
90
|
+
endTime,
|
|
91
|
+
duration,
|
|
92
|
+
error,
|
|
93
|
+
stackTrace,
|
|
94
|
+
metadata: { command: command.toUpperCase(), key },
|
|
95
|
+
});
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
catch (_) {
|
|
102
|
+
// Production-safe: never crash
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=redis.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis.js","sourceRoot":"","sources":["../../../src/integrations/redis.ts"],"names":[],"mappings":";AAAA,gEAAgE;AAChE,wCAAwC;AACxC,sDAAsD;AACtD,gEAAgE;;AA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