@lark-apaas/client-toolkit 1.2.76-alpha.20261020072412 → 1.2.76-alpha.20261020120304
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.
|
@@ -33,10 +33,16 @@ export declare class BatchLogger {
|
|
|
33
33
|
* 刷新日志队列,全部发送
|
|
34
34
|
*/
|
|
35
35
|
flush(): Promise<void>;
|
|
36
|
+
private toCollectLog;
|
|
37
|
+
private serializeRequests;
|
|
38
|
+
private isBodyWithinBudget;
|
|
39
|
+
private prepareLog;
|
|
40
|
+
private createBatches;
|
|
36
41
|
/**
|
|
37
|
-
*
|
|
42
|
+
* 发送日志批次到后端。请求串行发送,只有显式 2xx 才提交当前批次。
|
|
38
43
|
*/
|
|
39
44
|
private sendBatch;
|
|
45
|
+
private sendRequest;
|
|
40
46
|
/**
|
|
41
47
|
* 执行实际的fetch请求
|
|
42
48
|
*/
|
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { COLLECTOR_BODY_MAX_BYTES, createCollectorMessageSummary, createEnvelopeOverBudgetDiagnostic, utf8ByteLength } from "./log-payload-budget.js";
|
|
2
|
+
class BatchDeliveryError extends Error {
|
|
3
|
+
entriesToRequeue;
|
|
4
|
+
deliveryError;
|
|
5
|
+
constructor(entriesToRequeue, deliveryError){
|
|
6
|
+
super('A collector batch could not be explicitly committed'), this.entriesToRequeue = entriesToRequeue, this.deliveryError = deliveryError;
|
|
7
|
+
this.name = 'BatchDeliveryError';
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
const MAX_RECORDS_PER_REQUEST = 20;
|
|
1
11
|
class BatchLogger {
|
|
2
12
|
config;
|
|
3
13
|
logQueue = [];
|
|
@@ -44,34 +54,102 @@ class BatchLogger {
|
|
|
44
54
|
try {
|
|
45
55
|
await this.sendBatch(logsToSend);
|
|
46
56
|
} catch (error) {
|
|
47
|
-
|
|
57
|
+
const entriesToRequeue = error instanceof BatchDeliveryError ? error.entriesToRequeue : logsToSend;
|
|
58
|
+
this.logQueue.unshift(...entriesToRequeue);
|
|
48
59
|
} finally{
|
|
49
60
|
this.isProcessing = false;
|
|
50
61
|
}
|
|
51
62
|
}
|
|
63
|
+
toCollectLog(log, message = log.message) {
|
|
64
|
+
return {
|
|
65
|
+
level: log.level,
|
|
66
|
+
message,
|
|
67
|
+
time: new Date(log.timestamp).toISOString(),
|
|
68
|
+
source: log.source,
|
|
69
|
+
user_id: this.config.userId,
|
|
70
|
+
tenant_id: this.config.tenantId,
|
|
71
|
+
app_id: this.config.appId
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
serializeRequests(requests) {
|
|
75
|
+
return JSON.stringify(requests);
|
|
76
|
+
}
|
|
77
|
+
isBodyWithinBudget(requests) {
|
|
78
|
+
return utf8ByteLength(this.serializeRequests(requests)) <= COLLECTOR_BODY_MAX_BYTES;
|
|
79
|
+
}
|
|
80
|
+
prepareLog(log) {
|
|
81
|
+
const originalRequest = this.toCollectLog(log);
|
|
82
|
+
if (this.isBodyWithinBudget([
|
|
83
|
+
originalRequest
|
|
84
|
+
])) return {
|
|
85
|
+
entry: log,
|
|
86
|
+
request: originalRequest
|
|
87
|
+
};
|
|
88
|
+
const summaryMessage = createCollectorMessageSummary(log.message, (candidateMessage)=>this.isBodyWithinBudget([
|
|
89
|
+
this.toCollectLog(log, candidateMessage)
|
|
90
|
+
]));
|
|
91
|
+
const summarizedRequest = this.toCollectLog(log, summaryMessage);
|
|
92
|
+
if (!this.isBodyWithinBudget([
|
|
93
|
+
summarizedRequest
|
|
94
|
+
])) {
|
|
95
|
+
this.originConsole.error(createEnvelopeOverBudgetDiagnostic(log.id, log.message));
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
entry: log,
|
|
100
|
+
request: summarizedRequest
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
createBatches(logs) {
|
|
104
|
+
const preparedLogs = logs.map((log)=>this.prepareLog(log)).filter((log)=>null !== log);
|
|
105
|
+
const batches = [];
|
|
106
|
+
let current = [];
|
|
107
|
+
const commitCurrent = ()=>{
|
|
108
|
+
if (0 === current.length) return;
|
|
109
|
+
batches.push({
|
|
110
|
+
entries: current.map((log)=>log.entry),
|
|
111
|
+
body: this.serializeRequests(current.map((log)=>log.request))
|
|
112
|
+
});
|
|
113
|
+
current = [];
|
|
114
|
+
};
|
|
115
|
+
for (const preparedLog of preparedLogs){
|
|
116
|
+
if (current.length >= MAX_RECORDS_PER_REQUEST) commitCurrent();
|
|
117
|
+
const candidate = [
|
|
118
|
+
...current,
|
|
119
|
+
preparedLog
|
|
120
|
+
];
|
|
121
|
+
if (current.length > 0 && !this.isBodyWithinBudget(candidate.map((log)=>log.request))) commitCurrent();
|
|
122
|
+
current.push(preparedLog);
|
|
123
|
+
}
|
|
124
|
+
commitCurrent();
|
|
125
|
+
return batches;
|
|
126
|
+
}
|
|
52
127
|
async sendBatch(logs) {
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
while(retries <= this.config.maxRetries)try {
|
|
64
|
-
await this.execFetch(this.config.endpoint, {
|
|
128
|
+
const batches = this.createBatches(logs);
|
|
129
|
+
for(let index = 0; index < batches.length; index++)try {
|
|
130
|
+
await this.sendRequest(batches[index].body);
|
|
131
|
+
} catch (error) {
|
|
132
|
+
throw new BatchDeliveryError(batches.slice(index).flatMap((batch)=>batch.entries), error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
async sendRequest(body) {
|
|
136
|
+
for(let attempt = 0; attempt <= this.config.maxRetries; attempt++)try {
|
|
137
|
+
const response = await this.execFetch(this.config.endpoint, {
|
|
65
138
|
method: 'POST',
|
|
66
139
|
headers: this.config.headers,
|
|
67
|
-
body
|
|
140
|
+
body
|
|
68
141
|
});
|
|
142
|
+
if (!response || 'number' != typeof response.status || response.status < 200 || response.status >= 300) throw new Error(`Collector returned HTTP ${response?.status ?? 'unknown'}`);
|
|
69
143
|
return;
|
|
70
144
|
} catch (error) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
145
|
+
const isFinalAttempt = attempt === this.config.maxRetries;
|
|
146
|
+
const attemptNumber = attempt + 1;
|
|
147
|
+
if (isFinalAttempt) {
|
|
148
|
+
this.originConsole.error(`Failed to send logs after ${attemptNumber} attempt(s).`);
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
this.originConsole.warn(`Failed to send logs (attempt ${attemptNumber}), retrying in ${this.config.retryDelay * attemptNumber}ms...`);
|
|
152
|
+
await this.delay(this.config.retryDelay * attemptNumber);
|
|
75
153
|
}
|
|
76
154
|
}
|
|
77
155
|
async execFetch(url, options) {
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export declare const AXIOS_RESPONSE_PREVIEW_MAX_BYTES: number;
|
|
2
|
+
export declare const SELECTED_LOG_MESSAGE_MAX_BYTES: number;
|
|
3
|
+
export declare const COLLECTOR_BODY_MAX_BYTES: number;
|
|
4
|
+
export declare const LOG_PAYLOAD_SUMMARY_MARKER = "log-payload-summary-v1";
|
|
5
|
+
export interface LogPayloadSummary extends Record<string, unknown> {
|
|
6
|
+
type: 'log-payload-summary';
|
|
7
|
+
marker: typeof LOG_PAYLOAD_SUMMARY_MARKER;
|
|
8
|
+
complete: false;
|
|
9
|
+
reason: string;
|
|
10
|
+
context: string;
|
|
11
|
+
originalDigest: string;
|
|
12
|
+
originalBytes: number;
|
|
13
|
+
preview: string;
|
|
14
|
+
previewBytes: number;
|
|
15
|
+
omittedBytes: number;
|
|
16
|
+
}
|
|
17
|
+
interface SelectedLogLike {
|
|
18
|
+
type: string;
|
|
19
|
+
level: string;
|
|
20
|
+
id: string;
|
|
21
|
+
args: unknown[];
|
|
22
|
+
meta: Record<string, unknown>;
|
|
23
|
+
}
|
|
24
|
+
export interface BoundedSelectedLog {
|
|
25
|
+
message: string;
|
|
26
|
+
originalDigest: string;
|
|
27
|
+
originalBytes: number;
|
|
28
|
+
truncated: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare function utf8ByteLength(value: string): number;
|
|
31
|
+
export declare function digestSerializedValue(serializedValue: string): string;
|
|
32
|
+
/**
|
|
33
|
+
* Serialize logs with the existing Error representation. The fallback only
|
|
34
|
+
* applies to values that JSON.stringify could not previously serialize.
|
|
35
|
+
*/
|
|
36
|
+
export declare function stringifyLogValue(value: unknown): string | undefined;
|
|
37
|
+
export declare function createAutomaticResponsePreview(value: unknown): unknown;
|
|
38
|
+
export declare function createBoundedSelectedLog(log: SelectedLogLike): BoundedSelectedLog;
|
|
39
|
+
export declare function createCollectorMessageSummary(originalMessage: string, fits: (message: string) => boolean): string;
|
|
40
|
+
export declare function createEnvelopeOverBudgetDiagnostic(logId: string, originalMessage: string): string;
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import sha256 from "crypto-js/sha256";
|
|
2
|
+
const AXIOS_RESPONSE_PREVIEW_MAX_BYTES = 65536;
|
|
3
|
+
const SELECTED_LOG_MESSAGE_MAX_BYTES = 262144;
|
|
4
|
+
const COLLECTOR_BODY_MAX_BYTES = 1048576;
|
|
5
|
+
const LOG_PAYLOAD_SUMMARY_MARKER = 'log-payload-summary-v1';
|
|
6
|
+
function utf8ByteLength(value) {
|
|
7
|
+
return new TextEncoder().encode(value).byteLength;
|
|
8
|
+
}
|
|
9
|
+
function digestSerializedValue(serializedValue) {
|
|
10
|
+
return `sha256:${sha256(serializedValue).toString()}`;
|
|
11
|
+
}
|
|
12
|
+
function errorReplacer(_key, value) {
|
|
13
|
+
if (value instanceof Error) return {
|
|
14
|
+
message: value.message,
|
|
15
|
+
stack: value.stack,
|
|
16
|
+
name: value.name
|
|
17
|
+
};
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
function stringifyLogValue(value) {
|
|
21
|
+
try {
|
|
22
|
+
return JSON.stringify(value, errorReplacer);
|
|
23
|
+
} catch {
|
|
24
|
+
const seen = new WeakSet();
|
|
25
|
+
return JSON.stringify(value, (_key, nestedValue)=>{
|
|
26
|
+
if (nestedValue instanceof Error) return {
|
|
27
|
+
message: nestedValue.message,
|
|
28
|
+
stack: nestedValue.stack,
|
|
29
|
+
name: nestedValue.name
|
|
30
|
+
};
|
|
31
|
+
if ('bigint' == typeof nestedValue) return nestedValue.toString();
|
|
32
|
+
if ('object' == typeof nestedValue && null !== nestedValue) {
|
|
33
|
+
if (seen.has(nestedValue)) return '[Circular]';
|
|
34
|
+
seen.add(nestedValue);
|
|
35
|
+
}
|
|
36
|
+
return nestedValue;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function prefixWithoutSplitSurrogate(value, length) {
|
|
41
|
+
let end = Math.min(Math.max(length, 0), value.length);
|
|
42
|
+
if (end > 0 && end < value.length && value.charCodeAt(end - 1) >= 0xd800 && value.charCodeAt(end - 1) <= 0xdbff) end--;
|
|
43
|
+
return value.slice(0, end);
|
|
44
|
+
}
|
|
45
|
+
function buildSummary(originalSerialized, preview, options, originalBytes = utf8ByteLength(originalSerialized), originalDigest = options.originalDigest || digestSerializedValue(originalSerialized)) {
|
|
46
|
+
const previewBytes = utf8ByteLength(preview);
|
|
47
|
+
return {
|
|
48
|
+
type: 'log-payload-summary',
|
|
49
|
+
marker: LOG_PAYLOAD_SUMMARY_MARKER,
|
|
50
|
+
complete: false,
|
|
51
|
+
reason: options.reason,
|
|
52
|
+
context: options.context,
|
|
53
|
+
originalDigest,
|
|
54
|
+
originalBytes,
|
|
55
|
+
preview,
|
|
56
|
+
previewBytes,
|
|
57
|
+
omittedBytes: Math.max(0, originalBytes - previewBytes),
|
|
58
|
+
...options.extra || {}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function fitSummary(originalSerialized, options, fits) {
|
|
62
|
+
let low = 0;
|
|
63
|
+
let high = Math.min(originalSerialized.length, options.maxPreviewCodeUnits);
|
|
64
|
+
const originalBytes = utf8ByteLength(originalSerialized);
|
|
65
|
+
const originalDigest = options.originalDigest || digestSerializedValue(originalSerialized);
|
|
66
|
+
const build = (preview)=>buildSummary(originalSerialized, preview, options, originalBytes, originalDigest);
|
|
67
|
+
let best = build('');
|
|
68
|
+
if (!fits(best)) return best;
|
|
69
|
+
while(low <= high){
|
|
70
|
+
const middle = low + Math.floor((high - low) / 2);
|
|
71
|
+
const preview = prefixWithoutSplitSurrogate(originalSerialized, middle);
|
|
72
|
+
const candidate = build(preview);
|
|
73
|
+
if (fits(candidate)) {
|
|
74
|
+
best = candidate;
|
|
75
|
+
low = middle + 1;
|
|
76
|
+
} else high = middle - 1;
|
|
77
|
+
}
|
|
78
|
+
return best;
|
|
79
|
+
}
|
|
80
|
+
function describeValue(value) {
|
|
81
|
+
if (Array.isArray(value)) return {
|
|
82
|
+
valueType: 'array',
|
|
83
|
+
itemCount: value.length
|
|
84
|
+
};
|
|
85
|
+
if (null === value) return {
|
|
86
|
+
valueType: 'null'
|
|
87
|
+
};
|
|
88
|
+
if ('object' == typeof value) return {
|
|
89
|
+
valueType: 'object',
|
|
90
|
+
itemCount: Object.keys(value).length
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
valueType: typeof value
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function createAutomaticResponsePreview(value) {
|
|
97
|
+
const serialized = stringifyLogValue(value);
|
|
98
|
+
if (void 0 === serialized || utf8ByteLength(serialized) <= AXIOS_RESPONSE_PREVIEW_MAX_BYTES) return value;
|
|
99
|
+
return fitSummary(serialized, {
|
|
100
|
+
reason: 'automatic_response_preview_over_budget',
|
|
101
|
+
context: 'axios-response-data',
|
|
102
|
+
extra: describeValue(value),
|
|
103
|
+
maxPreviewCodeUnits: AXIOS_RESPONSE_PREVIEW_MAX_BYTES
|
|
104
|
+
}, (summary)=>utf8ByteLength(JSON.stringify(summary)) <= AXIOS_RESPONSE_PREVIEW_MAX_BYTES);
|
|
105
|
+
}
|
|
106
|
+
function selectedLogDedupSource(log) {
|
|
107
|
+
const { isDuplicate: _isDuplicate, duplicateCount: _duplicateCount, duplicateOfId: _duplicateOfId, ...meta } = log.meta;
|
|
108
|
+
const withoutId = {
|
|
109
|
+
type: log.type,
|
|
110
|
+
level: log.level,
|
|
111
|
+
args: log.args,
|
|
112
|
+
meta
|
|
113
|
+
};
|
|
114
|
+
return stringifyLogValue(withoutId) || '';
|
|
115
|
+
}
|
|
116
|
+
function boundedContextText(value) {
|
|
117
|
+
if ('string' != typeof value) return;
|
|
118
|
+
return prefixWithoutSplitSurrogate(value, 4096);
|
|
119
|
+
}
|
|
120
|
+
function boundedMeta(meta) {
|
|
121
|
+
const retained = {};
|
|
122
|
+
for (const key of [
|
|
123
|
+
'logTraceID',
|
|
124
|
+
'type',
|
|
125
|
+
'skipFrame',
|
|
126
|
+
'noStacktrace',
|
|
127
|
+
'isDuplicate',
|
|
128
|
+
'duplicateCount',
|
|
129
|
+
'duplicateOfId'
|
|
130
|
+
]){
|
|
131
|
+
const value = meta[key];
|
|
132
|
+
if ('string' == typeof value) retained[key] = prefixWithoutSplitSurrogate(value, 1024);
|
|
133
|
+
else if ('number' == typeof value || 'boolean' == typeof value) retained[key] = value;
|
|
134
|
+
}
|
|
135
|
+
return retained;
|
|
136
|
+
}
|
|
137
|
+
function createBoundedSelectedLog(log) {
|
|
138
|
+
const message = stringifyLogValue(log) || '';
|
|
139
|
+
const dedupSource = selectedLogDedupSource(log);
|
|
140
|
+
const originalDigest = digestSerializedValue(dedupSource);
|
|
141
|
+
const originalBytes = utf8ByteLength(message);
|
|
142
|
+
if (originalBytes <= SELECTED_LOG_MESSAGE_MAX_BYTES) return {
|
|
143
|
+
message,
|
|
144
|
+
originalDigest,
|
|
145
|
+
originalBytes,
|
|
146
|
+
truncated: false
|
|
147
|
+
};
|
|
148
|
+
const contextText = boundedContextText(log.args[0]);
|
|
149
|
+
const render = (summary)=>JSON.stringify({
|
|
150
|
+
type: log.type,
|
|
151
|
+
level: log.level,
|
|
152
|
+
id: log.id,
|
|
153
|
+
args: [
|
|
154
|
+
...void 0 === contextText ? [] : [
|
|
155
|
+
contextText
|
|
156
|
+
],
|
|
157
|
+
summary
|
|
158
|
+
],
|
|
159
|
+
meta: boundedMeta(log.meta)
|
|
160
|
+
});
|
|
161
|
+
const summary = fitSummary(message, {
|
|
162
|
+
reason: 'selected_log_message_over_budget',
|
|
163
|
+
context: 'selected-log-batch-and-postMessage',
|
|
164
|
+
originalDigest,
|
|
165
|
+
maxPreviewCodeUnits: SELECTED_LOG_MESSAGE_MAX_BYTES
|
|
166
|
+
}, (candidate)=>utf8ByteLength(render(candidate)) <= SELECTED_LOG_MESSAGE_MAX_BYTES);
|
|
167
|
+
const boundedMessage = render(summary);
|
|
168
|
+
if (utf8ByteLength(boundedMessage) > SELECTED_LOG_MESSAGE_MAX_BYTES) {
|
|
169
|
+
const minimal = {
|
|
170
|
+
type: log.type,
|
|
171
|
+
level: log.level,
|
|
172
|
+
id: prefixWithoutSplitSurrogate(log.id, 1024),
|
|
173
|
+
args: [
|
|
174
|
+
buildSummary(message, '', {
|
|
175
|
+
reason: 'selected_log_message_over_budget',
|
|
176
|
+
context: 'selected-log-batch-and-postMessage',
|
|
177
|
+
originalDigest,
|
|
178
|
+
maxPreviewCodeUnits: SELECTED_LOG_MESSAGE_MAX_BYTES
|
|
179
|
+
})
|
|
180
|
+
],
|
|
181
|
+
meta: {}
|
|
182
|
+
};
|
|
183
|
+
return {
|
|
184
|
+
message: JSON.stringify(minimal),
|
|
185
|
+
originalDigest,
|
|
186
|
+
originalBytes,
|
|
187
|
+
truncated: true
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
message: boundedMessage,
|
|
192
|
+
originalDigest,
|
|
193
|
+
originalBytes,
|
|
194
|
+
truncated: true
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function createCollectorMessageSummary(originalMessage, fits) {
|
|
198
|
+
const digestSource = JSON.stringify(originalMessage);
|
|
199
|
+
const render = (summary)=>JSON.stringify(summary);
|
|
200
|
+
const summary = fitSummary(originalMessage, {
|
|
201
|
+
reason: 'collector_message_over_budget',
|
|
202
|
+
context: 'final-collector-json-body',
|
|
203
|
+
originalDigest: digestSerializedValue(digestSource),
|
|
204
|
+
maxPreviewCodeUnits: SELECTED_LOG_MESSAGE_MAX_BYTES
|
|
205
|
+
}, (candidate)=>{
|
|
206
|
+
const rendered = render(candidate);
|
|
207
|
+
return utf8ByteLength(rendered) <= SELECTED_LOG_MESSAGE_MAX_BYTES && fits(rendered);
|
|
208
|
+
});
|
|
209
|
+
return render(summary);
|
|
210
|
+
}
|
|
211
|
+
function createEnvelopeOverBudgetDiagnostic(logId, originalMessage) {
|
|
212
|
+
const diagnostic = {
|
|
213
|
+
type: 'collector-budget',
|
|
214
|
+
marker: LOG_PAYLOAD_SUMMARY_MARKER,
|
|
215
|
+
complete: false,
|
|
216
|
+
reason: 'envelope_over_budget',
|
|
217
|
+
logId: prefixWithoutSplitSurrogate(logId, 1024),
|
|
218
|
+
originalDigest: digestSerializedValue(JSON.stringify(originalMessage)),
|
|
219
|
+
originalBytes: utf8ByteLength(originalMessage),
|
|
220
|
+
message: 'Log entry was not fetched because its fixed collector envelope exceeds the 1 MiB budget.'
|
|
221
|
+
};
|
|
222
|
+
return JSON.stringify(diagnostic);
|
|
223
|
+
}
|
|
224
|
+
export { AXIOS_RESPONSE_PREVIEW_MAX_BYTES, COLLECTOR_BODY_MAX_BYTES, LOG_PAYLOAD_SUMMARY_MARKER, SELECTED_LOG_MESSAGE_MAX_BYTES, createAutomaticResponsePreview, createBoundedSelectedLog, createCollectorMessageSummary, createEnvelopeOverBudgetDiagnostic, digestSerializedValue, stringifyLogValue, utf8ByteLength };
|
|
@@ -3,6 +3,7 @@ import "../utils/utils.js";
|
|
|
3
3
|
import { mappingText } from "./source-map-mappings-wasm.js";
|
|
4
4
|
import stacktrace_js from "stacktrace-js";
|
|
5
5
|
import { batchLogInfo } from "./batch-logger.js";
|
|
6
|
+
import { createBoundedSelectedLog } from "./log-payload-budget.js";
|
|
6
7
|
function getBrowserTransportLevel(level) {
|
|
7
8
|
return 'success' === level ? 'info' : level;
|
|
8
9
|
}
|
|
@@ -74,49 +75,25 @@ async function sendSelectedLog(logWithoutID) {
|
|
|
74
75
|
} catch (postError) {}
|
|
75
76
|
}
|
|
76
77
|
if (void 0 === log.meta.skipFrame) log.meta.skipFrame = 2;
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
message: value.message,
|
|
80
|
-
stack: value.stack,
|
|
81
|
-
name: value.name
|
|
82
|
-
};
|
|
83
|
-
return value;
|
|
84
|
-
};
|
|
85
|
-
const logJSON = JSON.stringify(log, errorReplacer);
|
|
86
|
-
const logForDedup = {
|
|
87
|
-
...log
|
|
88
|
-
};
|
|
89
|
-
delete logForDedup.id;
|
|
90
|
-
const logContentForDedup = JSON.stringify(logForDedup, errorReplacer);
|
|
91
|
-
if (lastLogInfo && lastLogInfo.content === logContentForDedup) {
|
|
78
|
+
const initialRepresentation = createBoundedSelectedLog(log);
|
|
79
|
+
if (lastLogInfo && lastLogInfo.digest === initialRepresentation.originalDigest) {
|
|
92
80
|
lastLogInfo.count++;
|
|
93
81
|
log.meta.isDuplicate = true;
|
|
94
82
|
log.meta.duplicateCount = lastLogInfo.count;
|
|
95
83
|
log.meta.duplicateOfId = lastLogInfo.id;
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
batchLogInfo(getBrowserTransportLevel(log.level), updatedLogJSON);
|
|
99
|
-
} catch (e) {}
|
|
100
|
-
if (window.parent !== window) try {
|
|
101
|
-
window.parent.postMessage({
|
|
102
|
-
type: 'SELECTED_LOG',
|
|
103
|
-
payload: updatedLogJSON
|
|
104
|
-
}, '*');
|
|
105
|
-
} catch (e) {}
|
|
106
|
-
return;
|
|
107
|
-
}
|
|
108
|
-
lastLogInfo = {
|
|
109
|
-
content: logContentForDedup,
|
|
84
|
+
} else lastLogInfo = {
|
|
85
|
+
digest: initialRepresentation.originalDigest,
|
|
110
86
|
id: log.id,
|
|
111
87
|
count: 1
|
|
112
88
|
};
|
|
89
|
+
const boundedMessage = log.meta.isDuplicate ? createBoundedSelectedLog(log).message : initialRepresentation.message;
|
|
113
90
|
try {
|
|
114
|
-
batchLogInfo(getBrowserTransportLevel(log.level),
|
|
91
|
+
batchLogInfo(getBrowserTransportLevel(log.level), boundedMessage);
|
|
115
92
|
} catch (e) {}
|
|
116
93
|
if (window.parent !== window) try {
|
|
117
94
|
window.parent.postMessage({
|
|
118
95
|
type: 'SELECTED_LOG',
|
|
119
|
-
payload:
|
|
96
|
+
payload: boundedMessage
|
|
120
97
|
}, '*');
|
|
121
98
|
} catch (e) {}
|
|
122
99
|
} catch (e) {}
|
package/lib/utils/axiosConfig.js
CHANGED
|
@@ -6,6 +6,7 @@ import { safeStringify } from "./safeStringify.js";
|
|
|
6
6
|
import { slardar } from "@lark-apaas/internal-slardar";
|
|
7
7
|
import { normalizeBasePath } from "./utils.js";
|
|
8
8
|
import { t } from "../locales/index.js";
|
|
9
|
+
import { createAutomaticResponsePreview } from "../logger/log-payload-budget.js";
|
|
9
10
|
const APP_CLIENT_API_LOG_TYPE = 'app_client_api_log';
|
|
10
11
|
function stripBasePath(urlPath) {
|
|
11
12
|
const base = normalizeBasePath(process.env.CLIENT_BASE_PATH);
|
|
@@ -127,7 +128,7 @@ async function logResponse(ok, responseOrError) {
|
|
|
127
128
|
...Object.keys(queryParams).length > 0 && {
|
|
128
129
|
查询参数: queryParams
|
|
129
130
|
},
|
|
130
|
-
返回数据: response.data,
|
|
131
|
+
返回数据: createAutomaticResponsePreview(response.data),
|
|
131
132
|
请求摘要: {
|
|
132
133
|
方法: method,
|
|
133
134
|
TraceID: logTraceID,
|
|
@@ -224,7 +225,7 @@ function handleSpanEnd(cfg, response, error) {
|
|
|
224
225
|
const startTime = cfg._startTime;
|
|
225
226
|
const errorResponse = error?.response || {};
|
|
226
227
|
const errorMessage = error?.message || '未知错误';
|
|
227
|
-
const url = response?.request?.responseURL || errorResponse?.request?.responseURL || cfg.url ||
|
|
228
|
+
const url = response?.request?.responseURL || errorResponse?.request?.responseURL || cfg.url || '';
|
|
228
229
|
const method = (cfg.method || 'GET').toUpperCase();
|
|
229
230
|
const rawPath = url.split('?')[0].replace(/^https?:\/\/[^/]+/, '') || '/';
|
|
230
231
|
const path = stripBasePath(rawPath);
|
|
@@ -279,7 +280,7 @@ AxiosProto.request = function(configOrUrl, config) {
|
|
|
279
280
|
finalConfig.url = configOrUrl;
|
|
280
281
|
} else finalConfig = configOrUrl || {};
|
|
281
282
|
if (finalConfig.__span) return originalAxiosRequest.call(this, finalConfig);
|
|
282
|
-
const fullUrl = this.getUri ? this.getUri(finalConfig) : finalConfig.url ||
|
|
283
|
+
const fullUrl = this.getUri ? this.getUri(finalConfig) : finalConfig.url || '';
|
|
283
284
|
const actualMethod = (finalConfig.method || 'GET').toUpperCase();
|
|
284
285
|
const cleanedPath = fullUrl.split('?')[0].replace(/^https?:\/\/[^/]+/, '') || '/';
|
|
285
286
|
const span = observable.startSpan(`${actualMethod} ${cleanedPath}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lark-apaas/client-toolkit",
|
|
3
|
-
"version": "1.2.76-alpha.
|
|
3
|
+
"version": "1.2.76-alpha.20261020120304",
|
|
4
4
|
"types": "./lib/index.d.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -102,7 +102,7 @@
|
|
|
102
102
|
"@lark-apaas/aily-web-sdk": "^0.0.12",
|
|
103
103
|
"@lark-apaas/auth-sdk": "^0.1.5",
|
|
104
104
|
"@lark-apaas/client-capability": "^0.1.7",
|
|
105
|
-
"@lark-apaas/dataloom": "0.1.
|
|
105
|
+
"@lark-apaas/dataloom": "^0.1.5",
|
|
106
106
|
"@lark-apaas/internal-slardar": "^0.0.3",
|
|
107
107
|
"@lark-apaas/miaoda-inspector": "^1.0.25",
|
|
108
108
|
"@lark-apaas/observable-web": "^1.0.6",
|