From 27fa5923915ce700fec21480807c67308f53ee51 Mon Sep 17 00:00:00 2001 From: Cheng Jinbao Date: Sat, 29 Aug 2026 21:29:36 +0800 Subject: [PATCH] =?UTF-8?q?feat(runtime):=20=E6=B7=BB=E5=8A=A0=20W3C=20tra?= =?UTF-8?q?ceparent=20=E5=88=86=E5=B8=83=E5=BC=8F=E8=BF=BD=E8=B8=AA?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现从 traceparent 头解析 traceId 并在函数调用链路中传递 - 修改 Deno 运行时从 W3C traceparent 提取 traceId 并注入 SDK - 调整 Java 服务端注入 traceparent 头确保跨语言追踪链路完整 - 更新日志过滤器记录 traceId 并在响应头返回便于排查问题 - 重构观察性日志模块结构将 API 日志相关功能移至 observability 包 - 移除废弃的日志统计相关实体类与资源端点 - 修改 SDK 导入逻辑支持本地内联与 npm 两种部署模式 - 在函数执行结果中包含日志缓冲区用于测试面板展示 --- typescript/package.json | 2 +- typescript/src/client.ts | 20 +++++++++++++++----- typescript/src/http.ts | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/typescript/package.json b/typescript/package.json index bfbf791..9e4c4c4 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@flexmodel/sdk", - "version": "0.0.9", + "version": "0.0.10", "description": "Flexmodel TypeScript SDK — Fluent query builder for data CRUD with API Key auth", "type": "module", "main": "./dist/index.cjs", diff --git a/typescript/src/client.ts b/typescript/src/client.ts index 0b8c71a..c94b01b 100644 --- a/typescript/src/client.ts +++ b/typescript/src/client.ts @@ -161,11 +161,16 @@ export class FlexmodelClient< return this as unknown as FlexmodelClient } - /** 设置当前请求的认证令牌(优先级高于构造函数中的 apiKey)。 */ + /** 设置当前请求的认证令牌(优先级高于构造函数中的 apiKey)。 */ setAuthToken(token?: string): void { this.http.setAuthToken(token) } + /** 设置当前链路追踪 ID,设置后 outgoing 请求自动注入 W3C traceparent 头。 */ + setTraceId(traceId?: string): void { + this.http.setTraceId(traceId) + } + /** 更新 API 基础地址(运行时可变)。 */ setBaseURL(baseURL: string): void { this.http.setBaseURL(baseURL) @@ -302,10 +307,15 @@ export class FlexmodelAdminClient< return this as unknown as FlexmodelAdminClient } - /** 设置认证令牌。 */ - setAuthToken(token?: string): void { - this.http.setAuthToken(token) - } + /** 设置认证令牌。 */ + setAuthToken(token?: string): void { + this.http.setAuthToken(token) + } + + /** 设置当前链路追踪 ID,设置后 outgoing 请求自动注入 W3C traceparent 头。 */ + setTraceId(traceId?: string): void { + this.http.setTraceId(traceId) + } /** 更新 API 基础地址。 */ setBaseURL(baseURL: string): void { diff --git a/typescript/src/http.ts b/typescript/src/http.ts index 67862c5..3265902 100644 --- a/typescript/src/http.ts +++ b/typescript/src/http.ts @@ -29,6 +29,19 @@ export class HttpTransport { this.activeToken = token } + /** 当前 traceId,用于 W3C traceparent 头传播(函数调用 → SDK → Java 后端链路关联) */ + private activeTraceId?: string + + /** + * 设置当前链路追踪 ID。 + * 设置后所有 outgoing 请求会自动注入 W3C traceparent 头, + * 使 Java 后端能关联到同一 traceId。 + * 传入 undefined 清除。 + */ + setTraceId(traceId?: string): void { + this.activeTraceId = traceId + } + /** * 更新 API 基础地址(运行时可变)。 * 自动去除尾部斜杠。 @@ -71,6 +84,14 @@ export class HttpTransport { } else if (this.apiKey) { headers['Authorization'] = `Bearer ${this.apiKey}` } + if (this.activeTraceId) { + // W3C traceparent: 00-{traceId}-{spanId}-{flags} + // spanId 用 16 hex 随机值(SDK 侧无 OTel SDK,生成伪 spanId 满足格式要求) + const spanId = Array.from({length: 8}, () => + Math.floor(Math.random() * 256).toString(16).padStart(2, '0') + ).join('') + headers['traceparent'] = `00-${this.activeTraceId}-${spanId}-01` + } return headers }