@lark-apaas/client-toolkit 1.2.1-alpha.1 → 1.2.1-alpha.11

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.
@@ -0,0 +1 @@
1
+ export * from '../trace';
@@ -0,0 +1 @@
1
+ export * from "../trace/index.js";
@@ -6,6 +6,7 @@ const initObservable = ()=>{
6
6
  env: 'development' === process.env.NODE_ENV ? AppEnv.Dev : AppEnv.Prod,
7
7
  collectorUrl: {
8
8
  log: `/spark/app/${window.appId}/runtime/api/v1/observability/logs/collect`,
9
+ trace: `/spark/app/${window.appId}/runtime/api/v1/observability/traces/collect`,
9
10
  metric: `/spark/app/${window.appId}/runtime/api/v1/observability/metrics/collect`
10
11
  }
11
12
  });
@@ -0,0 +1,2 @@
1
+ declare const trace: any;
2
+ export { trace };
@@ -0,0 +1,3 @@
1
+ import { observable } from "@lark-apaas/observable-web";
2
+ const trace = observable.trace.bind(observable);
3
+ export { trace };
@@ -1,4 +1,14 @@
1
1
  import { AxiosInstance } from 'axios';
2
+ declare module 'axios' {
3
+ interface AxiosRequestConfig {
4
+ /** @internal 存储 Span 实例 */
5
+ __span?: any;
6
+ /** @internal 请求开始时间 */
7
+ _startTime?: number;
8
+ /** @internal 请求唯一标识 */
9
+ _requestUUID?: string;
10
+ }
11
+ }
2
12
  /**
3
13
  * axios配置内容:拦截器、日志等
4
14
  */
@@ -1,6 +1,8 @@
1
1
  import axios from "axios";
2
+ import { observable } from "@lark-apaas/observable-web";
2
3
  import { logger } from "../apis/logger.js";
3
4
  import { getStacktrace } from "../logger/selected-logs.js";
5
+ import { safeStringify } from "./safeStringify.js";
4
6
  const isValidResponse = (resp)=>null != resp && 'object' == typeof resp && 'config' in resp && null !== resp.config && void 0 !== resp.config && 'object' == typeof resp.config && 'status' in resp && 'number' == typeof resp.status && 'data' in resp;
5
7
  async function logResponse(ok, responseOrError) {
6
8
  if (isValidResponse(responseOrError)) {
@@ -52,7 +54,7 @@ async function logResponse(ok, responseOrError) {
52
54
  logTraceID
53
55
  };
54
56
  if (stacktrace) logMeta.stacktrace = stacktrace;
55
- if ('development' === process.env.NODE_ENV) logger.log({
57
+ logger.debug({
56
58
  level: ok,
57
59
  args: [
58
60
  parts.join(''),
@@ -108,7 +110,7 @@ async function logResponse(ok, responseOrError) {
108
110
  const stacktrace = await requestStacktraceMap.get(requestUUID);
109
111
  const logMeta = {};
110
112
  if (stacktrace) logMeta.stacktrace = stacktrace;
111
- logger.log({
113
+ logger.debug({
112
114
  level: 'error',
113
115
  args: [
114
116
  parts.join(''),
@@ -118,7 +120,7 @@ async function logResponse(ok, responseOrError) {
118
120
  });
119
121
  return;
120
122
  }
121
- logger.log({
123
+ logger.debug({
122
124
  level: 'error',
123
125
  args: [
124
126
  '请求失败:无响应对象或配置信息'
@@ -129,6 +131,70 @@ async function logResponse(ok, responseOrError) {
129
131
  const requestStacktraceMap = new Map();
130
132
  function initAxiosConfig(axiosInstance) {
131
133
  if (!axiosInstance) axiosInstance = axios;
134
+ if (axiosInstance && !axiosInstance._isTraced) {
135
+ axiosInstance._isTraced = true;
136
+ const originalRequest = axiosInstance.request;
137
+ axiosInstance.request = function(urlOrConfig, config) {
138
+ try {
139
+ const finalConfig = 'string' == typeof urlOrConfig ? config = config || {} : urlOrConfig = urlOrConfig || {};
140
+ const method = (finalConfig.method || 'GET').toUpperCase();
141
+ const url = finalConfig.url || ('string' == typeof urlOrConfig ? urlOrConfig : '');
142
+ const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
143
+ const span = observable.startSpan(`${method} ${cleanedPath}`);
144
+ if (span) {
145
+ const spanContext = span.spanContext();
146
+ if (spanContext && spanContext.traceId) {
147
+ const traceInfo = `${spanContext.traceId}-${spanContext.spanId}`;
148
+ if (!finalConfig.headers) finalConfig.headers = {};
149
+ finalConfig.headers['X-Tt-TraceInfo'] = traceInfo;
150
+ finalConfig.__span = span;
151
+ }
152
+ }
153
+ } catch (error) {
154
+ console.error('Trace start failed:', error);
155
+ }
156
+ return originalRequest.apply(this, arguments);
157
+ };
158
+ }
159
+ axiosInstance.interceptors.response.use((response)=>{
160
+ const { __span: span, _startTime: startTime, url = "" } = response.config || {};
161
+ const method = (response.config.method || 'GET').toUpperCase();
162
+ const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
163
+ if (span) {
164
+ observable.log('INFO', safeStringify({
165
+ method,
166
+ path: cleanedPath,
167
+ url,
168
+ user_agent: response.config.headers["user-agent"],
169
+ duration_ms: startTime ? Date.now() - startTime : void 0,
170
+ status: response.status,
171
+ request_body: response.config.data,
172
+ response: response.data
173
+ }), {}, span);
174
+ 'function' == typeof span.end && span.end();
175
+ }
176
+ return response;
177
+ }, (error)=>{
178
+ const { config: errorConfig = {}, response: errorResponse = {}, message: errorMessage = '未知错误' } = error;
179
+ const { __span: span, _startTime: startTime, url = "" } = errorConfig || {};
180
+ const method = (errorConfig.method || 'GET').toUpperCase();
181
+ const cleanedPath = url.split('?')[0].replace(/^\/spark\/p\/app_\w+/, '') || '/';
182
+ if (span) {
183
+ observable.log('ERROR', safeStringify({
184
+ method,
185
+ path: cleanedPath,
186
+ url,
187
+ user_agent: errorConfig.headers?.["user-agent"],
188
+ duration_ms: startTime ? Date.now() - startTime : void 0,
189
+ status: errorResponse.status || 200,
190
+ request_body: errorConfig.data,
191
+ response: errorResponse.data || {},
192
+ error_message: errorMessage
193
+ }), {}, span);
194
+ 'function' == typeof span.end && span.end();
195
+ }
196
+ return Promise.reject(error);
197
+ });
132
198
  axiosInstance.interceptors.request.use((config)=>{
133
199
  const requestUUID = crypto.randomUUID();
134
200
  if ('production' !== process.env.NODE_ENV) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.1-alpha.1",
3
+ "version": "1.2.1-alpha.11",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -31,6 +31,11 @@
31
31
  "require": "./lib/apis/logger.js",
32
32
  "types": "./lib/apis/logger.d.ts"
33
33
  },
34
+ "./trace": {
35
+ "import": "./lib/apis/trace.js",
36
+ "require": "./lib/apis/trace.js",
37
+ "types": "./lib/apis/trace.d.ts"
38
+ },
34
39
  "./udt-types": {
35
40
  "import": "./lib/apis/udt-types.js",
36
41
  "require": "./lib/apis/udt-types.js",
@@ -83,7 +88,7 @@
83
88
  "@data-loom/js": "^0.4.3",
84
89
  "@lark-apaas/client-capability": "^0.1.1",
85
90
  "@lark-apaas/miaoda-inspector": "^1.0.8",
86
- "@lark-apaas/observable-web": "^1.0.0",
91
+ "@lark-apaas/observable-web": "1.0.1-alpha.2",
87
92
  "@radix-ui/react-avatar": "^1.1.10",
88
93
  "@radix-ui/react-popover": "^1.1.15",
89
94
  "@radix-ui/react-slot": "^1.2.3",
@@ -157,4 +162,4 @@
157
162
  "react-router-dom": ">=6.26.2",
158
163
  "styled-jsx": ">=5.0.0"
159
164
  }
160
- }
165
+ }