@zintrust/trace 1.6.5 → 1.6.7
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/watchers/HttpWatcher.js +23 -8
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Logger } from '@zintrust/core';
|
|
1
2
|
import { TraceContext } from '../context.js';
|
|
2
3
|
import { EntryType } from '../types.js';
|
|
3
4
|
import { AuthTag } from '../utils/authTag.js';
|
|
@@ -35,8 +36,9 @@ const resolveRequestPayload = (req, config) => {
|
|
|
35
36
|
};
|
|
36
37
|
const registerCompletionHandler = (response, onComplete) => {
|
|
37
38
|
const raw = response.getRaw();
|
|
38
|
-
if (typeof raw.once !== 'function')
|
|
39
|
-
return () => undefined;
|
|
39
|
+
if (typeof raw.once !== 'function') {
|
|
40
|
+
return { attached: false, cleanup: () => undefined };
|
|
41
|
+
}
|
|
40
42
|
let completed = false;
|
|
41
43
|
const cleanup = () => {
|
|
42
44
|
if (typeof raw.off === 'function') {
|
|
@@ -53,7 +55,11 @@ const registerCompletionHandler = (response, onComplete) => {
|
|
|
53
55
|
};
|
|
54
56
|
raw.once('finish', markCompleted);
|
|
55
57
|
raw.once('close', markCompleted);
|
|
56
|
-
return cleanup;
|
|
58
|
+
return { attached: true, cleanup };
|
|
59
|
+
};
|
|
60
|
+
const isResponseComplete = (response) => {
|
|
61
|
+
const raw = response.getRaw();
|
|
62
|
+
return raw.writableEnded === true || raw.finished === true;
|
|
57
63
|
};
|
|
58
64
|
const captureResponse = (response, config) => {
|
|
59
65
|
const headers = {};
|
|
@@ -147,8 +153,7 @@ export const HttpWatcher = Object.freeze({
|
|
|
147
153
|
if (content.responseStatus >= 500)
|
|
148
154
|
tags.push('failed');
|
|
149
155
|
responseCapture.restore();
|
|
150
|
-
|
|
151
|
-
.writeEntry({
|
|
156
|
+
const entry = {
|
|
152
157
|
uuid: crypto.randomUUID(),
|
|
153
158
|
batchId,
|
|
154
159
|
type: EntryType.REQUEST,
|
|
@@ -156,11 +161,21 @@ export const HttpWatcher = Object.freeze({
|
|
|
156
161
|
tags,
|
|
157
162
|
isLatest: true,
|
|
158
163
|
createdAt: TraceContext.now(),
|
|
159
|
-
}
|
|
160
|
-
|
|
164
|
+
};
|
|
165
|
+
storage.writeEntry(entry).catch((error) => {
|
|
166
|
+
Logger.warn('[trace] HttpWatcher writeEntry failed', {
|
|
167
|
+
method: content.method,
|
|
168
|
+
uri: content.uri,
|
|
169
|
+
entryUuid: entry.uuid,
|
|
170
|
+
error: error instanceof Error ? error.message : String(error),
|
|
171
|
+
});
|
|
172
|
+
}); // fire-and-forget
|
|
161
173
|
};
|
|
162
|
-
registerCompletionHandler(response, persistEntry);
|
|
174
|
+
const completionHandler = registerCompletionHandler(response, persistEntry);
|
|
163
175
|
await next();
|
|
176
|
+
if (!completionHandler.attached || isResponseComplete(response)) {
|
|
177
|
+
persistEntry();
|
|
178
|
+
}
|
|
164
179
|
};
|
|
165
180
|
registerMiddleware(middleware);
|
|
166
181
|
return () => undefined;
|