trickle-observe 0.2.101 → 0.2.103
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/dist/fetch-observer.js +24 -0
- package/dist/observe-register.js +19 -0
- package/package.json +1 -1
- package/src/fetch-observer.ts +23 -0
- package/src/observe-register.ts +17 -0
package/dist/fetch-observer.js
CHANGED
|
@@ -61,6 +61,17 @@ function patchFetch(environment, debugMode) {
|
|
|
61
61
|
if (url.includes('/api/ingest') || url.includes('/api/functions') || url.includes('/api/health')) {
|
|
62
62
|
return originalFetch.call(globalThis, input, init);
|
|
63
63
|
}
|
|
64
|
+
// Inject distributed trace headers
|
|
65
|
+
if (!init)
|
|
66
|
+
init = {};
|
|
67
|
+
if (!init.headers)
|
|
68
|
+
init.headers = {};
|
|
69
|
+
const traceId = globalThis.__trickle_trace_id || require('crypto').randomBytes(8).toString('hex');
|
|
70
|
+
globalThis.__trickle_trace_id = traceId;
|
|
71
|
+
if (typeof init.headers === 'object' && !Array.isArray(init.headers)) {
|
|
72
|
+
init.headers['X-Trickle-Trace-Id'] = traceId;
|
|
73
|
+
init.headers['X-Trickle-Service'] = process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd());
|
|
74
|
+
}
|
|
64
75
|
// Make the actual request with timing
|
|
65
76
|
const startTime = performance.now();
|
|
66
77
|
const response = await originalFetch.call(globalThis, input, init);
|
|
@@ -71,6 +82,19 @@ function patchFetch(environment, debugMode) {
|
|
|
71
82
|
if (!contentType.includes('json')) {
|
|
72
83
|
return response;
|
|
73
84
|
}
|
|
85
|
+
// Write distributed trace span
|
|
86
|
+
try {
|
|
87
|
+
const tracesDir = process.env.TRICKLE_LOCAL_DIR || require('path').join(process.cwd(), '.trickle');
|
|
88
|
+
const tracesFile = require('path').join(tracesDir, 'traces.jsonl');
|
|
89
|
+
const span = {
|
|
90
|
+
kind: 'trace', traceId, spanId: require('crypto').randomBytes(4).toString('hex'),
|
|
91
|
+
parentSpanId: '0', service: process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd()),
|
|
92
|
+
operation: `${method} ${url}`, durationMs, status: String(statusCode), timestamp: Date.now(),
|
|
93
|
+
metadata: { direction: 'outgoing' },
|
|
94
|
+
};
|
|
95
|
+
require('fs').appendFileSync(tracesFile, JSON.stringify(span) + '\n');
|
|
96
|
+
}
|
|
97
|
+
catch { }
|
|
74
98
|
// Clone and intercept: read the clone's JSON in background
|
|
75
99
|
try {
|
|
76
100
|
const cloned = response.clone();
|
package/dist/observe-register.js
CHANGED
|
@@ -1046,6 +1046,25 @@ if (enabled) {
|
|
|
1046
1046
|
fs_1.default.writeFileSync(path_1.default.join(envDir, 'environment.json'), JSON.stringify(envSnapshot, null, 2));
|
|
1047
1047
|
}
|
|
1048
1048
|
catch { }
|
|
1049
|
+
// ── Hook 0d: Memory profiling ──
|
|
1050
|
+
try {
|
|
1051
|
+
const profileDir = process.env.TRICKLE_LOCAL_DIR || path_1.default.join(process.cwd(), '.trickle');
|
|
1052
|
+
fs_1.default.mkdirSync(profileDir, { recursive: true });
|
|
1053
|
+
const profileFile = path_1.default.join(profileDir, 'profile.jsonl');
|
|
1054
|
+
fs_1.default.writeFileSync(profileFile, '');
|
|
1055
|
+
const mem = process.memoryUsage();
|
|
1056
|
+
const startProfile = { kind: 'profile', event: 'start', rssKb: Math.round(mem.rss / 1024), heapKb: Math.round(mem.heapUsed / 1024), peakHeapKb: Math.round(mem.heapTotal / 1024), timestamp: Date.now() };
|
|
1057
|
+
fs_1.default.appendFileSync(profileFile, JSON.stringify(startProfile) + '\n');
|
|
1058
|
+
process.on('exit', () => {
|
|
1059
|
+
try {
|
|
1060
|
+
const endMem = process.memoryUsage();
|
|
1061
|
+
const endProfile = { kind: 'profile', event: 'end', rssKb: Math.round(endMem.rss / 1024), heapKb: Math.round(endMem.heapUsed / 1024), peakHeapKb: Math.round(endMem.heapTotal / 1024), timestamp: Date.now() };
|
|
1062
|
+
fs_1.default.appendFileSync(profileFile, JSON.stringify(endProfile) + '\n');
|
|
1063
|
+
}
|
|
1064
|
+
catch { }
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
catch { }
|
|
1049
1068
|
// ── Hook 1: Module._compile — transform source to wrap function declarations ──
|
|
1050
1069
|
// This catches ALL functions including entry file and non-exported helpers.
|
|
1051
1070
|
M.prototype._compile = function hookedCompile(content, filename) {
|
package/package.json
CHANGED
package/src/fetch-observer.ts
CHANGED
|
@@ -66,6 +66,16 @@ export function patchFetch(environment: string, debugMode: boolean): void {
|
|
|
66
66
|
return originalFetch.call(globalThis, input, init);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
+
// Inject distributed trace headers
|
|
70
|
+
if (!init) init = {};
|
|
71
|
+
if (!init.headers) init.headers = {};
|
|
72
|
+
const traceId = (globalThis as any).__trickle_trace_id || require('crypto').randomBytes(8).toString('hex');
|
|
73
|
+
(globalThis as any).__trickle_trace_id = traceId;
|
|
74
|
+
if (typeof init.headers === 'object' && !Array.isArray(init.headers)) {
|
|
75
|
+
(init.headers as any)['X-Trickle-Trace-Id'] = traceId;
|
|
76
|
+
(init.headers as any)['X-Trickle-Service'] = process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd());
|
|
77
|
+
}
|
|
78
|
+
|
|
69
79
|
// Make the actual request with timing
|
|
70
80
|
const startTime = performance.now();
|
|
71
81
|
const response = await originalFetch.call(globalThis, input, init);
|
|
@@ -78,6 +88,19 @@ export function patchFetch(environment: string, debugMode: boolean): void {
|
|
|
78
88
|
return response;
|
|
79
89
|
}
|
|
80
90
|
|
|
91
|
+
// Write distributed trace span
|
|
92
|
+
try {
|
|
93
|
+
const tracesDir = process.env.TRICKLE_LOCAL_DIR || require('path').join(process.cwd(), '.trickle');
|
|
94
|
+
const tracesFile = require('path').join(tracesDir, 'traces.jsonl');
|
|
95
|
+
const span = {
|
|
96
|
+
kind: 'trace', traceId, spanId: require('crypto').randomBytes(4).toString('hex'),
|
|
97
|
+
parentSpanId: '0', service: process.env.TRICKLE_SERVICE_NAME || require('path').basename(process.cwd()),
|
|
98
|
+
operation: `${method} ${url}`, durationMs, status: String(statusCode), timestamp: Date.now(),
|
|
99
|
+
metadata: { direction: 'outgoing' },
|
|
100
|
+
};
|
|
101
|
+
require('fs').appendFileSync(tracesFile, JSON.stringify(span) + '\n');
|
|
102
|
+
} catch {}
|
|
103
|
+
|
|
81
104
|
// Clone and intercept: read the clone's JSON in background
|
|
82
105
|
try {
|
|
83
106
|
const cloned = response.clone();
|
package/src/observe-register.ts
CHANGED
|
@@ -1031,6 +1031,23 @@ if (enabled) {
|
|
|
1031
1031
|
fs.writeFileSync(path.join(envDir, 'environment.json'), JSON.stringify(envSnapshot, null, 2));
|
|
1032
1032
|
} catch {}
|
|
1033
1033
|
|
|
1034
|
+
// ── Hook 0d: Memory profiling ──
|
|
1035
|
+
try {
|
|
1036
|
+
const profileDir = process.env.TRICKLE_LOCAL_DIR || path.join(process.cwd(), '.trickle');
|
|
1037
|
+
fs.mkdirSync(profileDir, { recursive: true });
|
|
1038
|
+
const profileFile = path.join(profileDir, 'profile.jsonl');
|
|
1039
|
+
fs.writeFileSync(profileFile, '');
|
|
1040
|
+
const mem = process.memoryUsage();
|
|
1041
|
+
const startProfile = { kind: 'profile', event: 'start', rssKb: Math.round(mem.rss / 1024), heapKb: Math.round(mem.heapUsed / 1024), peakHeapKb: Math.round(mem.heapTotal / 1024), timestamp: Date.now() };
|
|
1042
|
+
fs.appendFileSync(profileFile, JSON.stringify(startProfile) + '\n');
|
|
1043
|
+
process.on('exit', () => {
|
|
1044
|
+
try {
|
|
1045
|
+
const endMem = process.memoryUsage();
|
|
1046
|
+
const endProfile = { kind: 'profile', event: 'end', rssKb: Math.round(endMem.rss / 1024), heapKb: Math.round(endMem.heapUsed / 1024), peakHeapKb: Math.round(endMem.heapTotal / 1024), timestamp: Date.now() };
|
|
1047
|
+
fs.appendFileSync(profileFile, JSON.stringify(endProfile) + '\n');
|
|
1048
|
+
} catch {}
|
|
1049
|
+
});
|
|
1050
|
+
} catch {}
|
|
1034
1051
|
|
|
1035
1052
|
// ── Hook 1: Module._compile — transform source to wrap function declarations ──
|
|
1036
1053
|
// This catches ALL functions including entry file and non-exported helpers.
|