Skip to content

Conversation

@rinfx
Copy link
Collaborator

@rinfx rinfx commented Jun 24, 2025

Ⅰ. Describe what this PR did

  1. 现在的配置在记录内容时,记录到日志和记录到span attribute中使用的key是一样的,增加 trace_span_key 配置使二者可以不同名
  2. 现在记录日志内容时,所有的字段会放在一个json字段中,增加 as_seperate_log_field 配置使记录内容可以作为单独的字段

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

Ⅴ. Special notes for reviews


Ⅰ. Describe what this PR did

  1. When recording content, the key used in logging and recording to span attribute is the same. Adding the trace_span_key configuration allows the two to have different names.
  2. When recording log content, all fields will be placed in a json field. Add as_seperate_log_field configuration to enable the record content to be used as a separate field.

Ⅱ. Does this pull request fix one issue?

Ⅲ. Why don't you add test cases (unit test/integration test)?

Ⅳ. Describe how to verify it

V. Special notes for reviews

@rinfx rinfx requested review from CH3CHO and johnlanni as code owners June 24, 2025 09:35
@lingma-agents
Copy link

lingma-agents bot commented Jun 24, 2025

添加 trace_span_key 和 as_seperate_log_field 配置支持

变更概述

配置调整

  • 新增 trace_span_key 配置项:

    • 影响组件:Attribute 结构体配置(main.go
    • 实现:在 Attribute 结构体中增加 TraceSpanKey string 字段,通过 json:"trace_span_key,omitempty" 映射配置
    • 目的:允许链路追踪的 attribute key 与日志 key 独立配置,默认使用原 key
    • 关联逻辑:在设置 span attribute 时优先使用非空的 TraceSpanKeysetSpanAttribute 调用前判断)
  • 新增 as_seperate_log_field 配置项:

    • 影响组件:日志记录逻辑(main.goplugin_wrapper.go
    • 实现:在 Attribute 结构体中增加 AsSeperateLogField bool 字段
    • 技术细节:
      1. 当配置启用时,使用 MarshalStr 将值序列化为 JSON 字符串并单独存储(SetProperty 调用)
      2. 未启用时直接调用 SetUserAttribute 写入原始字段
    • 目的:提供日志字段扁平化或嵌套的灵活控制

重构

  • 函数名标准化:
    • 修改 unmarshalStrUnmarshalStr
    • 修改 marshalStrMarshalStr
    • 影响文件:utils.go
    • 目的:符合 Go 语言导出函数命名规范,增强可维护性
变更文件
文件路径 变更说明
plugins/​wasm-go/​extensions/​ai-statistics/​README.​md 新增 `trace_span_key` 和 `as_seperate_log_field` 配置项说明
plugins/​wasm-go/​extensions/​ai-statistics/​README_​EN.​md 新增英文版配置项说明
plugins/​wasm-go/​extensions/​ai-statistics/​main.​go 新增配置字段定义及条件化日志/链路追踪处理逻辑
plugins/​wasm-go/​pkg/​wrapper/​plugin_​wrapper.​go 适配新的 JSON 序列化函数调用
plugins/​wasm-go/​pkg/​wrapper/​utils.​go 修改序列化工具函数命名并调整导出权限

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

Copy link

@lingma-agents lingma-agents bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔎 代码评审报告

🎯 评审意见概览
严重度 数量 说明
🔴 Blocker 0 阻断性问题,需立即修复。例如:系统崩溃、关键功能不可用或严重安全漏洞。
🟠 Critical 1 严重问题,高优先级修复。例如:核心功能异常或性能瓶颈影响用户体验。
🟡 Major 0 主要问题,建议修复。例如:非核心功能缺陷或代码维护性较差。
🟢 Minor 2 次要问题,酬情优化。例如:代码格式不规范或注释缺失。

总计: 3 个问题

📋 评审意见详情
💡 代码实现建议
以下是文件级别的代码建议,聚焦于代码的可读性、可维护性和潜在问题。
📖 plugins/wasm-go/extensions/ai-statistics/README.md (1 💬)
📖 plugins/wasm-go/extensions/ai-statistics/README_EN.md (1 💬)
🔹 plugins/wasm-go/extensions/ai-statistics/main.go (1 💬)
🚀 架构设计建议
以下是对代码架构和设计的综合分析,聚焦于跨文件交互、系统一致性和潜在优化空间。
🔍1. 潜在的JSON序列化性能问题及重复逻辑

setAttributeBySourceWriteUserAttributeToLogWithKey方法中频繁使用MarshalStrUnmarshalStr进行JSON序列化操作,可能导致性能损耗。特别是在高并发场景下,重复的序列化操作会增加计算开销。此外,plugin_wrapper.goutils.go中的JSON处理逻辑与main.go中存在重复,建议将公共逻辑抽象到工具函数中以减少冗余。

📌 关键代码

marshalledJsonStr := wrapper.MarshalStr(fmt.Sprint(value))
preJsonLogStr := UnmarshalStr(fmt.Sprintf(`"%s"`, string(preMarshalledJsonLogStr)))

⚠️ 潜在风险

高负载时响应延迟增加,资源消耗过高;重复代码增加维护成本,可能导致不同模块间序列化逻辑不一致。

🔍2. 链路追踪键冲突风险

当多个Attribute配置共享相同的trace_span_key时,可能存在键覆盖风险。当前设计未对TraceSpanKey的唯一性进行校验,若不同配置项使用相同键值,会导致链路追踪数据被覆盖,影响数据准确性。需增加配置校验机制或明确文档说明该风险。

📌 关键代码

TraceSpanKey       string `json:"trace_span_key,omitempty"`

⚠️ 潜在风险

链路追踪数据不一致或丢失,导致故障排查困难。

🔍3. 测试覆盖不完整

新增的as_seperate_log_fieldtrace_span_key配置逻辑未在单元测试中体现。需补充测试用例验证:1) 日志字段分离功能是否正常;2) 链路追踪键是否按配置生效;3) 配置项组合场景下的行为(如同时启用apply_to_logapply_to_span)。

⚠️ 潜在风险

未覆盖场景可能导致线上配置失效或数据丢失。

🔍4. 配置项与字段命名不一致

虽然文件级建议提到字段名与JSON标签需统一,但Attribute结构体中AsSeperateLogField的JSON标签为as_seperate_log_field,而配置文档中对应字段名为as_seperate_log_field(拼写正确),但需确认其他字段是否存在类似不一致(如trace_span_keyTraceSpanKey)。

📌 关键代码

AsSeperateLogField bool   `json:"as_seperate_log_field,omitempty"`

⚠️ 潜在风险

配置解析错误导致功能失效,需确保文档与代码完全一致。

审查详情
📒 文件清单 (5 个文件)
📝 变更: 5 个文件

📝 变更文件:

  • plugins/wasm-go/extensions/ai-statistics/README.md
  • plugins/wasm-go/extensions/ai-statistics/README_EN.md
  • plugins/wasm-go/extensions/ai-statistics/main.go
  • plugins/wasm-go/pkg/wrapper/plugin_wrapper.go
  • plugins/wasm-go/pkg/wrapper/utils.go

💡 小贴士

与 lingma-agents 交流的方式

📜 直接回复评论
直接回复本条评论,lingma-agents 将自动处理您的请求。例如:

  • 在当前代码中添加详细的注释说明。

  • 请详细介绍一下你说的 LRU 改造方案,并使用伪代码加以说明。

📜 在代码行处标记
在文件的特定位置创建评论并 @lingma-agents。例如:

  • @lingma-agents 分析这个方法的性能瓶颈并提供优化建议。

  • @lingma-agents 对这个方法生成优化代码。

📜 在讨论中提问
在任何讨论中 @lingma-agents 来获取帮助。例如:

  • @lingma-agents 请总结上述讨论并提出解决方案。

  • @lingma-agents 请根据讨论内容生成优化代码。

@codecov-commenter
Copy link

codecov-commenter commented Jun 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 46.04%. Comparing base (ef31e09) to head (0ce45ff).
Report is 583 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main    #2488       +/-   ##
===========================================
+ Coverage   35.91%   46.04%   +10.13%     
===========================================
  Files          69       81       +12     
  Lines       11576    13020     +1444     
===========================================
+ Hits         4157     5995     +1838     
+ Misses       7104     6680      -424     
- Partials      315      345       +30     

see 78 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@rinfx rinfx force-pushed the statistic-update-0624 branch from 9fda9c7 to a3b972b Compare June 24, 2025 11:10
@rinfx rinfx changed the title update add trace_span_key & as_seperate_log_field configuration for ai-statistics Jun 24, 2025
Copy link
Collaborator

@johnlanni johnlanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@johnlanni johnlanni merged commit 76ada0b into alibaba:main Jun 25, 2025
12 checks passed
ink-hz pushed a commit to ink-hz/higress-ai-capability-auth that referenced this pull request Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants