@lark-apaas/client-toolkit 1.2.1-alpha.11 → 1.2.1-alpha.13
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/lib/utils/axiosConfig.js +67 -23
- package/package.json +1 -1
package/lib/utils/axiosConfig.js
CHANGED
|
@@ -131,30 +131,74 @@ async function logResponse(ok, responseOrError) {
|
|
|
131
131
|
const requestStacktraceMap = new Map();
|
|
132
132
|
function initAxiosConfig(axiosInstance) {
|
|
133
133
|
if (!axiosInstance) axiosInstance = axios;
|
|
134
|
-
|
|
135
|
-
axiosInstance._isTraced
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
134
|
+
try {
|
|
135
|
+
if (axiosInstance && !axiosInstance._isTraced) {
|
|
136
|
+
axiosInstance._isTraced = true;
|
|
137
|
+
const methodsWithNoData = [
|
|
138
|
+
'get',
|
|
139
|
+
'delete',
|
|
140
|
+
'head',
|
|
141
|
+
'options'
|
|
142
|
+
];
|
|
143
|
+
const methodsWithData = [
|
|
144
|
+
'post',
|
|
145
|
+
'put',
|
|
146
|
+
'patch',
|
|
147
|
+
"postForm",
|
|
148
|
+
"putForm",
|
|
149
|
+
"patchForm"
|
|
150
|
+
];
|
|
151
|
+
const allMethods = [
|
|
152
|
+
'request',
|
|
153
|
+
...methodsWithNoData,
|
|
154
|
+
...methodsWithData
|
|
155
|
+
];
|
|
156
|
+
allMethods.forEach((methodName)=>{
|
|
157
|
+
const originalMethod = axiosInstance[methodName];
|
|
158
|
+
if ('function' != typeof originalMethod) return;
|
|
159
|
+
axiosInstance[methodName] = function() {
|
|
160
|
+
const args = Array.prototype.slice.call(arguments);
|
|
161
|
+
try {
|
|
162
|
+
let config;
|
|
163
|
+
let url = '';
|
|
164
|
+
let actualMethod = methodName.toUpperCase();
|
|
165
|
+
if ('request' === methodName) if ('string' == typeof args[0]) {
|
|
166
|
+
url = args[0];
|
|
167
|
+
config = args[1] = args[1] || {};
|
|
168
|
+
} else {
|
|
169
|
+
config = args[0] = args[0] || {};
|
|
170
|
+
url = config.url || '';
|
|
171
|
+
actualMethod = (config.method || 'GET').toUpperCase();
|
|
172
|
+
}
|
|
173
|
+
else if (methodsWithNoData.includes(methodName)) {
|
|
174
|
+
url = args[0];
|
|
175
|
+
config = args[1] = args[1] || {};
|
|
176
|
+
} else if (methodsWithData.includes(methodName)) {
|
|
177
|
+
url = args[0];
|
|
178
|
+
config = args[2] = args[2] || {};
|
|
179
|
+
}
|
|
180
|
+
if (config && !config.__span) {
|
|
181
|
+
const cleanedPath = (url || '').split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
|
|
182
|
+
const span = observable.startSpan(`${actualMethod} ${cleanedPath}`);
|
|
183
|
+
if (span) {
|
|
184
|
+
const spanContext = span.spanContext();
|
|
185
|
+
if (spanContext && spanContext.traceId) {
|
|
186
|
+
const traceInfo = `${spanContext.traceId}-${spanContext.spanId}`;
|
|
187
|
+
if (!config.headers) config.headers = {};
|
|
188
|
+
config.headers['X-Tt-TraceInfo'] = traceInfo;
|
|
189
|
+
config.__span = span;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} catch (innerError) {
|
|
194
|
+
console.error(`Trace wrapper inner error [${methodName}]:`, innerError);
|
|
151
195
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
196
|
+
return originalMethod.apply(this, args);
|
|
197
|
+
};
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
} catch (e) {
|
|
201
|
+
console.error('Failed to init axios trace config wrapper:', e);
|
|
158
202
|
}
|
|
159
203
|
axiosInstance.interceptors.response.use((response)=>{
|
|
160
204
|
const { __span: span, _startTime: startTime, url = "" } = response.config || {};
|