LLM Observability context lost when using Kitlin coroutines
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 38/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 領域
- backend, observability
調査の方向性
ThreadLocalContextManager.java と DDLLMObsSpan.java のコンストラクターから始めて、LLMObsState がコンテキストをどのように読み取り、アタッチするかを追跡します。既存の Kotlin コルーチンインストルメンテーションを確認し、提供された並列 Dispatchers.IO シナリオで問題を再現します。一時停止後に作成された子 LLM スパンが、期待される親スパン ID と階層を保持すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Tracer Version(s)
1.58.2
Java Version(s)
21.0.7
JVM Vendor
Amazon Corretto
Bug Report
LLM Observability context lost when using Kotlin coroutines
When using LLM Observability with Kotlin coroutines, the LLMObsState context is lost when coroutines suspend and resume on different threads. This breaks parent-child span relationships and can cause spans to have incorrect or missing parent IDs.
Environment
- dd-trace-java version: 1.58.2
- Kotlin with coroutines
- Spring WebFlux / suspend functions
Root Cause
LLMObsState relies on datadog.context.Context which is backed by ThreadLocalContextManager:
// ThreadLocalContextManager.java
final class ThreadLocalContextManager implements ContextManager {
private static final ThreadLocal<Context[]> CURRENT_HOLDER =
ThreadLocal.withInitial(() -> new Context[] {EmptyContext.INSTANCE});
@Override
public Context current() {
return CURRENT_HOLDER.get()[0]; // Plain ThreadLocal - not coroutine-safe
}
}
When DDLLMObsSpan is created, it attaches to the current context and sets the parent:
// DDLLMObsSpan.java constructor
AgentSpanContext parent = LLMObsState.getLLMObsParentContext(); // Returns null on different thread
// ...
this.scope = LLMObsState.attach();
LLMObsState.setLLMObsParentContext(this.span.context());
Impact
This is particularly problematic for:
- Batch LLM operations (parallel embeddings, parallel completions)
- Agent workflows with parallel tool execution
- Any async/concurrent LLM workloads using Kotlin coroutines or reactive frameworks such as Spring WebFlux
Potential Solutions
-
Leverage existing Kotlin coroutine instrumentation - dd-trace-java has coroutine support for APM traces. Could similar context propagation be applied to
LLMObsState? -
Use
CoroutineContextelements - Allow users to propagate LLM Obs context explicitly via coroutine context, similar to how MDC context can be propagated. -
Integrate with
datadog.context.Contextpropagation - If there's existing machinery for propagatingContextacross async boundaries, ensureLLMObsStatebenefits from it.
Contribution
I would be happy to contribute a fix for this if the approach is welcomed. Please let me know the preferred direction and any guidance on implementation.
Expected Behavior
Child LLM spans created within coroutines should correctly reference their parent span, maintaining the trace hierarchy.
Actual Behavior
LLMObsState.getLLMObsParentContext()returnsnullwhen called from a different thread- Child spans get
parent_id="undefined"instead of the actual parent span ID - Trace hierarchy is broken in the Datadog LLM Observability UI
Reproduction Code
Reproduction Scenario
// Parent span created on Thread-1
val parentSpan = LLMObs.startAgentSpan("agent", mlApp, sessionId)
coroutineScope {
// Child spans created in parallel coroutines
// These may run on Thread-2, Thread-3, etc.
listOf(task1, task2, task3).map { task ->
async([Dispatchers.IO](http://dispatchers.io/)) {
// LLMObsState.getLLMObsParentContext() returns null here
// because ThreadLocal from Thread-1 is not available
val childSpan = LLMObs.startLLMSpan("llm.inference", model, provider, mlApp, sessionId)
// ... do work
childSpan.finish()
}
}.awaitAll()
}
parentSpan.finish()
- 主要言語
- Java
- スター
- 737
- フォーク
- 361
- 平均マージ
- 3日 20時間
- マージ済み PR(30日)
- 173
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
DataDog/dd-trace-java のほかの issue
-
type: feature request
難易度 1/5 1〜3時間 初心者へのやさしさ 70/100
DataDog/dd-trace-java#10245 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 62/100
DataDog/dd-trace-java#12608 ·
-
type: bug report
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
DataDog/dd-trace-java#12597 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
DataDog/dd-trace-java#12540 · コメント 4 件 · 担当者 1 名 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
DataDog/dd-trace-java#12480 ·
DataDog/dd-trace-java の issue をすべて見る
似ている issue
-
area/plugin
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
kestra-io/plugin-kestra#190 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
apache/rocketmq-dashboard#5064 ·