Skip to content

fix(java): 部署级字段从全局配置读取,不再依赖线程本地 MDC - #14

Merged
wyhhai merged 1 commit into
opensourceways:mainfrom
wyhhai:fix/java-deployment-fields-thread
Sep 20, 2026
Merged

wyhhai merged 1 commit into
opensourceways:mainfrom
wyhhai:fix/java-deployment-fields-thread

Conversation

@wyhhai

@wyhhai wyhhai commented Sep 20, 2026

Copy link
Copy Markdown
Collaborator

缺陷

Java SDK 输出的结构化日志中,契约要求的固定标签 service / env / instance 整条缺失(om-webserver 接入实测)。

实际输出:
{"time":"...","level":"info","msg":"...","community":"unknown","request_id":"...","logger":"LogUtil.java:107"}

根因

ObsLogging.init() 把部署级字段写入 SLF4J MDC(线程本地存储):

  • init() 在应用启动线程执行,只有该线程的 MDC 有这四个值
  • 业务请求运行在 web 容器工作线程,其 MDC 仅含 enrich() 写入的 community / request_id
  • ObsJsonProvider.writeTo() 从事件线程的 MDC 取这些键,取不到即省略

部署级字段本质是全局静态配置(来源 OBS_* 环境变量),用线程本地 MDC 承载属于错误建模。

修复

  1. ObsLogging 增加公开静态访问器 deploymentService() / deploymentEnv() / deploymentInstance(),
    deploymentCommunity() 改为公开;init() 不再写 MDC,未 init 时回退契约默认 Env.DEFAULT_VALUE
  2. ObsJsonProvider.writeTo():
    • service / env / instance:直接从全局配置读取(不依赖线程 MDC,恒非空)
    • community:保持「MDC 请求级覆盖 > 部署默认」语义不变(恒非空)
    • request_id / trace_id / span_id:仍走 MDC,空值省略
  3. 补回归测试:模拟请求线程(新线程)打日志,验证部署字段完整输出;未 init 时访问器回退契约默认

验证

  • mvn test:42 个测试全部通过(新增 4 个)
  • 覆盖:ObsJsonProviderTest +2(请求线程模拟、无请求级 MDC 回退默认);ObsLoggingTest +2(访问器取值、未 init 回退 unknown)

请求线程打日志时 service/env/instance 曾因 init() 只写启动线程 MDC 而整条缺失;
ObsJsonProvider 改为直接读 ObsLogging 全局静态访问器(未 init 回退契约默认),
community 保持 MDC 请求级覆盖 > 部署默认语义。补新线程回归测试。
@opensourceways-bot

Copy link
Copy Markdown

Welcome To opensourceways Community

Hey @wyhhai , thanks for your contribution to the community.

Bot Usage Manual

I'm the Bot here serving you. You can find the instructions on how to interact with me at Here . That means you can comment below every pull request or issue to trigger Bot Commands.

Contact Guide

If you have any questions, please contact the SIG: infratructure ,
and any of the maintainers: @GeorgeCao-hw, @TangJia025, @pkking, @zhongjun2 ,
and any of the committers: @GeorgeCao-hw, @TangJia025, @pkking, @zkhzkhz .

@opensourceways-bot

Copy link
Copy Markdown

CLA Signature Pass

wyhhai, thanks for your pull request. All authors of the commits have signed the CLA. 👍

@opensourceways-bot

Copy link
Copy Markdown

Linking Issue Notice

@wyhhai , the pull request must be linked to at least one issue.
If an issue has already been linked, but the needs-issue label remains, you can remove the label by commenting /check-issue .

@wyhhai
wyhhai merged commit 9d17591 into opensourceways:main Sep 20, 2026
7 checks passed
@wyhhai
wyhhai deleted the fix/java-deployment-fields-thread branch September 20, 2026 03:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants