trickle-observe 0.2.91 → 0.2.92
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 +8 -4
- package/package.json +1 -1
- package/src/fetch-observer.ts +11 -5
package/dist/fetch-observer.js
CHANGED
|
@@ -61,8 +61,11 @@ 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
|
-
// Make the actual request
|
|
64
|
+
// Make the actual request with timing
|
|
65
|
+
const startTime = performance.now();
|
|
65
66
|
const response = await originalFetch.call(globalThis, input, init);
|
|
67
|
+
const durationMs = Math.round((performance.now() - startTime) * 100) / 100;
|
|
68
|
+
const statusCode = response.status;
|
|
66
69
|
// Only observe JSON responses
|
|
67
70
|
const contentType = response.headers.get('content-type') || '';
|
|
68
71
|
if (!contentType.includes('json')) {
|
|
@@ -73,7 +76,7 @@ function patchFetch(environment, debugMode) {
|
|
|
73
76
|
const cloned = response.clone();
|
|
74
77
|
cloned.json().then((data) => {
|
|
75
78
|
try {
|
|
76
|
-
captureHttpResponse(method, url, init?.body, data, environment, debugMode);
|
|
79
|
+
captureHttpResponse(method, url, init?.body, data, environment, debugMode, statusCode, durationMs);
|
|
77
80
|
}
|
|
78
81
|
catch {
|
|
79
82
|
// Never interfere
|
|
@@ -113,7 +116,7 @@ function parseUrl(method, rawUrl) {
|
|
|
113
116
|
/**
|
|
114
117
|
* Capture the HTTP response type and enqueue it to the backend.
|
|
115
118
|
*/
|
|
116
|
-
function captureHttpResponse(method, url, requestBody, responseData, environment, debugMode) {
|
|
119
|
+
function captureHttpResponse(method, url, requestBody, responseData, environment, debugMode, statusCode, durationMs) {
|
|
117
120
|
const { functionName, module: moduleName } = parseUrl(method, url);
|
|
118
121
|
// Infer types
|
|
119
122
|
const returnType = (0, type_inference_1.inferType)(responseData, 5);
|
|
@@ -148,7 +151,7 @@ function captureHttpResponse(method, url, requestBody, responseData, environment
|
|
|
148
151
|
}
|
|
149
152
|
}
|
|
150
153
|
const payload = {
|
|
151
|
-
functionName,
|
|
154
|
+
functionName: statusCode ? `${functionName} [${statusCode}]` : functionName,
|
|
152
155
|
module: moduleName,
|
|
153
156
|
language: 'js',
|
|
154
157
|
environment,
|
|
@@ -157,6 +160,7 @@ function captureHttpResponse(method, url, requestBody, responseData, environment
|
|
|
157
160
|
returnType,
|
|
158
161
|
sampleInput: sampleInput ? [sampleInput] : undefined,
|
|
159
162
|
sampleOutput: sanitizeSample(responseData),
|
|
163
|
+
durationMs,
|
|
160
164
|
};
|
|
161
165
|
(0, transport_1.enqueue)(payload);
|
|
162
166
|
if (debugMode) {
|
package/package.json
CHANGED
package/src/fetch-observer.ts
CHANGED
|
@@ -66,8 +66,11 @@ export function patchFetch(environment: string, debugMode: boolean): void {
|
|
|
66
66
|
return originalFetch.call(globalThis, input, init);
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
// Make the actual request
|
|
69
|
+
// Make the actual request with timing
|
|
70
|
+
const startTime = performance.now();
|
|
70
71
|
const response = await originalFetch.call(globalThis, input, init);
|
|
72
|
+
const durationMs = Math.round((performance.now() - startTime) * 100) / 100;
|
|
73
|
+
const statusCode = response.status;
|
|
71
74
|
|
|
72
75
|
// Only observe JSON responses
|
|
73
76
|
const contentType = response.headers.get('content-type') || '';
|
|
@@ -80,7 +83,7 @@ export function patchFetch(environment: string, debugMode: boolean): void {
|
|
|
80
83
|
const cloned = response.clone();
|
|
81
84
|
cloned.json().then((data: any) => {
|
|
82
85
|
try {
|
|
83
|
-
captureHttpResponse(method, url, init?.body, data, environment, debugMode);
|
|
86
|
+
captureHttpResponse(method, url, init?.body, data, environment, debugMode, statusCode, durationMs);
|
|
84
87
|
} catch {
|
|
85
88
|
// Never interfere
|
|
86
89
|
}
|
|
@@ -128,6 +131,8 @@ function captureHttpResponse(
|
|
|
128
131
|
responseData: unknown,
|
|
129
132
|
environment: string,
|
|
130
133
|
debugMode: boolean,
|
|
134
|
+
statusCode?: number,
|
|
135
|
+
durationMs?: number,
|
|
131
136
|
): void {
|
|
132
137
|
const { functionName, module: moduleName } = parseUrl(method, url);
|
|
133
138
|
|
|
@@ -164,8 +169,8 @@ function captureHttpResponse(
|
|
|
164
169
|
}
|
|
165
170
|
}
|
|
166
171
|
|
|
167
|
-
const payload: IngestPayload = {
|
|
168
|
-
functionName,
|
|
172
|
+
const payload: IngestPayload & { statusCode?: number } = {
|
|
173
|
+
functionName: statusCode ? `${functionName} [${statusCode}]` : functionName,
|
|
169
174
|
module: moduleName,
|
|
170
175
|
language: 'js',
|
|
171
176
|
environment,
|
|
@@ -174,9 +179,10 @@ function captureHttpResponse(
|
|
|
174
179
|
returnType,
|
|
175
180
|
sampleInput: sampleInput ? [sampleInput] : undefined,
|
|
176
181
|
sampleOutput: sanitizeSample(responseData),
|
|
182
|
+
durationMs,
|
|
177
183
|
};
|
|
178
184
|
|
|
179
|
-
enqueue(payload);
|
|
185
|
+
enqueue(payload as IngestPayload);
|
|
180
186
|
|
|
181
187
|
if (debugMode) {
|
|
182
188
|
console.log(`[trickle/fetch] Captured ${functionName} → ${describeType(returnType)}`);
|