-
Notifications
You must be signed in to change notification settings - Fork 536
feat(llmobs): agent attribution on spans (Go SDK) #5034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yahya-mouman
wants to merge
6
commits into
main
Choose a base branch
from
yahya/llmobs-agent-attribution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d3d5bb9
feat(llmobs): resolve nearest agent ancestor at span start
yahya-mouman a2e5d9e
feat(llmobs): serialize meta.agent_attribution block
yahya-mouman a48aa5b
feat(llmobs): add agentNameWireSafe wire-safety check
yahya-mouman 3b96907
feat(llmobs): add InjectContext/ExtractContext for agent attribution
yahya-mouman 56f640c
test(llmobs): add end-to-end agent attribution round-trip
yahya-mouman 6ba23ee
refactor(llmobs): drop InjectContext/ExtractContext, consolidate tests
yahya-mouman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2025 Datadog, Inc. | ||
|
|
||
| package llmobs | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/DataDog/dd-trace-go/v2/internal/llmobs/config" | ||
| ) | ||
|
|
||
| // testAPMSpan is a minimal APMSpan implementation returning fixed values. | ||
| type testAPMSpan struct { | ||
| spanID string | ||
| traceID string | ||
| } | ||
|
|
||
| func (s testAPMSpan) Finish(_ FinishAPMSpanConfig) {} | ||
| func (s testAPMSpan) AddLink(_ SpanLink) {} | ||
| func (s testAPMSpan) SpanID() string { return s.spanID } | ||
| func (s testAPMSpan) TraceID() string { return s.traceID } | ||
| func (s testAPMSpan) SetBaggageItem(_ string, _ string) {} | ||
| func (s testAPMSpan) BaggageItem(_ string) string { return "" } | ||
|
Comment on lines
+17
to
+27
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think there's a need to test using mock spans. Prefer to not use mocks unless its absolutely necessary. |
||
|
|
||
| // resolveTestTracer is a minimal Tracer that returns testAPMSpans with unique IDs. | ||
| type resolveTestTracer struct { | ||
| next int | ||
| } | ||
|
|
||
| func (tr *resolveTestTracer) StartSpan(ctx context.Context, _ string, _ StartAPMSpanConfig) (APMSpan, context.Context) { | ||
| tr.next++ | ||
| return testAPMSpan{spanID: fmt.Sprintf("id-%d", tr.next)}, ctx | ||
| } | ||
|
|
||
| func newTestLLMObsForResolve(t *testing.T) *LLMObs { | ||
| t.Helper() | ||
| return &LLMObs{ | ||
| Config: &config.Config{Enabled: true, MLApp: "gotest"}, | ||
| Tracer: &resolveTestTracer{}, | ||
| } | ||
| } | ||
|
|
||
| func TestResolveParentAgent(t *testing.T) { | ||
| t.Run("no-parent", func(t *testing.T) { | ||
| name, id := resolveParentAgent(nil, nil) | ||
| if name != "" || id != "" { | ||
| t.Fatalf("expected empty, got name=%q id=%q", name, id) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("parent-is-agent", func(t *testing.T) { | ||
| parent := &Span{name: "my_agent", spanKind: SpanKindAgent} | ||
| parent.apm = testAPMSpan{spanID: "111"} | ||
| name, id := resolveParentAgent(parent, nil) | ||
| if name != "my_agent" || id != "111" { | ||
| t.Fatalf("expected (my_agent,111), got (%q,%q)", name, id) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("parent-other-kind-inherits", func(t *testing.T) { | ||
| parent := &Span{ | ||
| name: "tool_x", | ||
| spanKind: SpanKindTool, | ||
| parentAgentName: "top_agent", | ||
| parentAgentSpanID: "222", | ||
| } | ||
| parent.apm = testAPMSpan{spanID: "333"} | ||
| name, id := resolveParentAgent(parent, nil) | ||
| if name != "top_agent" || id != "222" { | ||
| t.Fatalf("expected inherited (top_agent,222), got (%q,%q)", name, id) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("propagated-parent", func(t *testing.T) { | ||
| prop := &PropagatedLLMSpan{ParentAgentName: "remote_agent", ParentAgentSpanID: "444"} | ||
| name, id := resolveParentAgent(nil, prop) | ||
| if name != "remote_agent" || id != "444" { | ||
| t.Fatalf("expected (remote_agent,444), got (%q,%q)", name, id) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("parent-takes-precedence-over-propagated", func(t *testing.T) { | ||
| parent := &Span{name: "local_agent", spanKind: SpanKindAgent} | ||
| parent.apm = testAPMSpan{spanID: "555"} | ||
| prop := &PropagatedLLMSpan{ParentAgentName: "remote_agent", ParentAgentSpanID: "444"} | ||
| name, id := resolveParentAgent(parent, prop) | ||
| if name != "local_agent" || id != "555" { | ||
| t.Fatalf("expected local (local_agent,555), got (%q,%q)", name, id) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func TestStartSpanResolvesParentAgent(t *testing.T) { | ||
| l := newTestLLMObsForResolve(t) | ||
| ctx := context.Background() | ||
|
|
||
| t.Run("tool-under-agent", func(t *testing.T) { | ||
| agent, agentCtx := l.StartSpan(ctx, SpanKindAgent, "my_agent", StartSpanConfig{}) | ||
| tool, _ := l.StartSpan(agentCtx, SpanKindTool, "my_tool", StartSpanConfig{}) | ||
| if tool.parentAgentName != "my_agent" { | ||
| t.Fatalf("tool.parentAgentName = %q, want my_agent", tool.parentAgentName) | ||
| } | ||
| if tool.parentAgentSpanID != agent.SpanID() { | ||
| t.Fatalf("tool.parentAgentSpanID = %q, want %q", tool.parentAgentSpanID, agent.SpanID()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("agent-workflow-tool-indirect-nesting", func(t *testing.T) { | ||
| agent, agentCtx := l.StartSpan(ctx, SpanKindAgent, "my_agent", StartSpanConfig{}) | ||
| wf, wfCtx := l.StartSpan(agentCtx, SpanKindWorkflow, "wf", StartSpanConfig{}) | ||
| tool, _ := l.StartSpan(wfCtx, SpanKindTool, "tool", StartSpanConfig{}) | ||
| if wf.parentAgentSpanID != agent.SpanID() || wf.parentAgentName != "my_agent" { | ||
| t.Fatalf("workflow should attribute to top agent, got (%q,%q)", wf.parentAgentName, wf.parentAgentSpanID) | ||
| } | ||
| if tool.parentAgentSpanID != agent.SpanID() || tool.parentAgentName != "my_agent" { | ||
| t.Fatalf("tool should attribute to top agent, got (%q,%q)", tool.parentAgentName, tool.parentAgentSpanID) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("sub-agent-under-agent", func(t *testing.T) { | ||
| outer, outerCtx := l.StartSpan(ctx, SpanKindAgent, "outer_agent", StartSpanConfig{}) | ||
| inner, innerCtx := l.StartSpan(outerCtx, SpanKindAgent, "inner_agent", StartSpanConfig{}) | ||
| innerTool, _ := l.StartSpan(innerCtx, SpanKindTool, "inner_tool", StartSpanConfig{}) | ||
| if inner.parentAgentSpanID != outer.SpanID() || inner.parentAgentName != "outer_agent" { | ||
| t.Fatalf("inner agent should attribute to outer, got (%q,%q)", inner.parentAgentName, inner.parentAgentSpanID) | ||
| } | ||
| if innerTool.parentAgentSpanID != inner.SpanID() || innerTool.parentAgentName != "inner_agent" { | ||
| t.Fatalf("inner tool should attribute to inner agent, got (%q,%q)", innerTool.parentAgentName, innerTool.parentAgentSpanID) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("top-level-agent-has-no-attribution", func(t *testing.T) { | ||
| agent, _ := l.StartSpan(ctx, SpanKindAgent, "top_agent", StartSpanConfig{}) | ||
| if agent.parentAgentName != "" || agent.parentAgentSpanID != "" { | ||
| t.Fatalf("top-level agent must have empty attribution, got (%q,%q)", agent.parentAgentName, agent.parentAgentSpanID) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("top-level-llm-has-no-attribution", func(t *testing.T) { | ||
| llm, _ := l.StartSpan(ctx, SpanKindLLM, "top_llm", StartSpanConfig{}) | ||
| if llm.parentAgentName != "" || llm.parentAgentSpanID != "" { | ||
| t.Fatalf("top-level llm must have empty attribution, got (%q,%q)", llm.parentAgentName, llm.parentAgentSpanID) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("workflow-then-tool-no-agent-anywhere", func(t *testing.T) { | ||
| _, wfCtx := l.StartSpan(ctx, SpanKindWorkflow, "wf", StartSpanConfig{}) | ||
| tool, _ := l.StartSpan(wfCtx, SpanKindTool, "tool", StartSpanConfig{}) | ||
| if tool.parentAgentName != "" || tool.parentAgentSpanID != "" { | ||
| t.Fatalf("tool with no agent ancestor must have empty attribution, got (%q,%q)", tool.parentAgentName, tool.parentAgentSpanID) | ||
| } | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no need to create separate test files for these new tests, they can go into the existing ones based on where the tested functionality file name exists.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, consolidated both test files into one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unresolving as this test file still exists.