LLM Observability context lost when using Kitlin coroutines
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 38/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- backend, observability
Hướng nghiên cứu
Bắt đầu với ThreadLocalContextManager.java và constructor của DDLLMObsSpan.java để theo dõi cách LLMObsState đọc và gắn context. Xem lại phần instrumentation coroutine Kotlin hiện có và tái hiện vấn đề với kịch bản Dispatchers.IO song song được cung cấp. Được xem là hoàn tất khi các LLM span con được tạo sau khi suspension vẫn giữ các parent span ID và hệ phân cấp mong đợi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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()
- Ngôn ngữ chính
- Java
- Star
- 737
- Fork
- 361
- Merge trung bình
- 3 ngày 20 giờ
- Pull request đã merge (30 ngày)
- 173
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DataDog/dd-trace-java
-
type: feature request
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 70/100
DataDog/dd-trace-java#10245 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 62/100
DataDog/dd-trace-java#12608 ·
-
type: bug report
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
DataDog/dd-trace-java#12597 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
DataDog/dd-trace-java#12540 · 4 bình luận · 1 người được giao ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 25/100
DataDog/dd-trace-java#12480 ·
Tất cả issue của DataDog/dd-trace-java
Issue tương tự
-
certification
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 80/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
[BUG] ECR GetAuthorizationToken returns a proxyEndpoint for the default region, not the request's Đang mởbug ecr
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Needs: Triage Type: Feature request
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
AntennaPod/AntennaPod#8794 ·
-
agentic-workflows
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
github/copilot-sdk#2760 ·