Do we have plan to support ScopedValue rather than ThreadLocal?

未关闭
#8,282 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
冷清
技术栈
java
领域
backend

调研方向

首先检查 io.opentelemetry.context.ThreadLocalContextStorage,以及 issue 中描述的 JDK 虚拟线程行为。确认 ScopedValue 是否与上下文存储要求兼容,并确定比较这些方案所需的基准测试或测试。完成的标准应是一项有文档记录的设计决策或一份达成共识的实现计划,而不只是代码更改。

由索引模型根据 Issue 内容生成。

描述

Feature Request

I saw the impl heavily relies on ThreadLocal like io.opentelemetry.context.ThreadLocalContextStorage.
While in our LnP test against JDK virtual thread, we found threadlocal could be the bottleneck.
Is there any plan to switch to ScopedValue?

Here's also some comments from AI:

The Performance Overhead Sources

  1. Thread.currentThread() lookup: Each get()/set() needs to get the current thread. With virtual threads, this involves:
    - Finding the carrier thread
    - Looking up which virtual thread is currently mounted
    - This has more indirection than with platform threads
  2. Mounting/unmounting cost: When a virtual thread blocks (IO), it's unmounted. On resumption, there's overhead. ThreadLocal access
    during this transition adds to the cost.
  3. Potential JVM internal contention: While ThreadLocalMap itself is largely lock-free, some JVM implementations have shown
    contention-like bottlenecks at scale due to:
    - Internal hash map operations
    - Memory barrier costs with millions of concurrent threads

The Reality Check

┌────────────────────────────────┬────────────────────────────────────────┐
│ Aspect │ Reality │
├────────────────────────────────┼────────────────────────────────────────┤
│ Traditional lock contention? │ No │
├────────────────────────────────┼────────────────────────────────────────┤
│ Per-operation overhead higher? │ Yes, marginally │
├────────────────────────────────┼────────────────────────────────────────┤
│ Scales to millions of threads? │ Debatable - depends on access patterns │
├────────────────────────────────┼────────────────────────────────────────┤
│ ScopedValue better? │ Yes, designed for virtual threads │
└────────────────────────────────┴────────────────────────────────────────┘

Recommendation

For new code with virtual threads, prefer ScopedValue (Java 21+) over ThreadLocal. It's semantics-matched to virtual thread model and
avoids some of the historical design choices in ThreadLocal that weren't made with millions of threads in mind.

If stuck with ThreadLocal, keep access patterns minimal and benchmark your specific use case.

主要语言
Java
星标
2.5k
派生
1.1k
平均合并
5 天 11 小时
30 天内合并 PR
41

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

open-telemetry/opentelemetry-java 的其他 Issue

查看 open-telemetry/opentelemetry-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。