Skip to content

feat(ai, llmobs): properly support ToolLoopAgent via existing patching#7571

Merged
gh-worker-dd-mergequeue-cf854d[bot] merged 9 commits intomasterfrom
sabrenner/vercel-ai-use-orchestrion
Feb 27, 2026
Merged

feat(ai, llmobs): properly support ToolLoopAgent via existing patching#7571
gh-worker-dd-mergequeue-cf854d[bot] merged 9 commits intomasterfrom
sabrenner/vercel-ai-use-orchestrion

Conversation

@sabrenner
Copy link
Copy Markdown
Collaborator

@sabrenner sabrenner commented Feb 18, 2026

What does this PR do?

Properly supports ToolLoopAgent invocations by properly patching the traced methods.

Before, what we were doing was individually patching generateText, generateStream, etc. and patching the tracer, or defaulting to a no-op one, or not doing anything if explicitly disabled. The problem here is the ToolLoopAgent is defined in the same file, and since we're patching the exports, it was never hitting the patched functions.

Now, we're using our orchestrion-js implementation to rewrite the source to patch getTracer and selectTelemetryAttributes, such that anytime a function calls those functions to perform telemetry operations, we patch them at that point (with the same patching de-duping logic we already had). This means that we should properly support ToolLoopAgent, since it seems it's a relatively thin abstraction layer over just calling generateText and streamText.

Motivation

Closes #7146

MLOB-5644

Additional Notes

  • Most LOC are from cassette fixture diffs for testing, and adding the tests for ToolLoopAgent
  • Need to add support for rerank in a follow-up, should be an easy win, just don't want to pollute this PR with out-of-scope stuff (should now not require an instrumentation/patching change)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Feb 18, 2026

Overall package size

Self size: 4.79 MB
Deduped: 5.63 MB
No deduping: 5.63 MB

Dependency sizes | name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 2.0.6 | 81.92 kB | 816.75 kB | | dc-polyfill | 0.1.10 | 26.73 kB | 26.73 kB |

🤖 This report was automatically generated by heaviest-objects-in-the-universe

@datadog-datadog-prod-us1

This comment has been minimized.

// while we don't want to patch the noopSpan more than once, we do want to treat each as a
// fresh instance. However, this is really not necessary for non-noop spans, but not sure
// how to differentiate.
const freshSpan = Object.create(span) // TODO: does this cause memory leaks?
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see note here - not sure if we'd really leak here since we should be 1:1 with already-created spans that will be GC'd anyways, so this should be as well.

i also thought about trying to detect the no-op span via a WeakSet, potentially with just one entry, as the only time we should see the same span reference here is that case, and then do this cloning if so. Otherwise, if from a custom or real OpenTelemetry tracer, all spans should be unique.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't we just check if it was patched instead of just making it so that it's always patched through a wrapper?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i had tried that, but the problem is that the first time it's patched some of the variables the span instance patching depends on (name, attributes) are already bound to the wrapper function, which we want to be different on subsequent spans. the problem is, for no-op cases where a user does not provide their own tracer, the ai sdk uses the same global span instance, instead of a new one per span start of the no-op tracer. so i found that just creating a fresh object was ok for this.

Comment thread packages/datadog-plugin-ai/test/index.spec.js
@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented Feb 18, 2026

Benchmarks

Benchmark execution time: 2026-02-24 17:25:22

Comparing candidate commit a9ab279 in PR branch sabrenner/vercel-ai-use-orchestrion with baseline commit c69f877 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 230 metrics, 30 unstable metrics.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 18, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.32%. Comparing base (c69f877) to head (a9ab279).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7571      +/-   ##
==========================================
+ Coverage   80.30%   80.32%   +0.02%     
==========================================
  Files         733      734       +1     
  Lines       31565    31547      -18     
==========================================
- Hits        25348    25341       -7     
+ Misses       6217     6206      -11     
Flag Coverage Δ
aiguard-macos 38.96% <16.66%> (-0.10%) ⬇️
aiguard-ubuntu 39.08% <16.66%> (-0.10%) ⬇️
aiguard-windows 38.81% <16.66%> (-0.10%) ⬇️
apm-capabilities-tracing-macos 48.61% <18.18%> (-0.01%) ⬇️
apm-capabilities-tracing-ubuntu 48.64% <18.18%> (-0.01%) ⬇️
apm-capabilities-tracing-windows 48.33% <18.18%> (-0.02%) ⬇️
apm-integrations-child-process 38.54% <16.66%> (-0.10%) ⬇️
apm-integrations-couchbase-18 37.45% <16.66%> (-0.10%) ⬇️
apm-integrations-couchbase-eol 37.78% <16.66%> (-0.10%) ⬇️
apm-integrations-oracledb 37.76% <16.66%> (-0.10%) ⬇️
appsec-express 55.53% <16.66%> (-0.08%) ⬇️
appsec-fastify 51.86% <16.66%> (-0.07%) ⬇️
appsec-graphql 52.04% <16.66%> (-0.07%) ⬇️
appsec-kafka 44.48% <16.66%> (-0.08%) ⬇️
appsec-ldapjs 44.11% <16.66%> (-0.08%) ⬇️
appsec-lodash 43.80% <16.66%> (-0.08%) ⬇️
appsec-macos 58.62% <16.66%> (-0.07%) ⬇️
appsec-mongodb-core 48.86% <16.66%> (-0.08%) ⬇️
appsec-mongoose 49.66% <16.66%> (-0.08%) ⬇️
appsec-mysql 51.03% <16.66%> (-0.07%) ⬇️
appsec-node-serialize 43.31% <16.66%> (-0.08%) ⬇️
appsec-passport 47.80% <16.66%> (-0.09%) ⬇️
appsec-postgres 50.78% <16.66%> (-0.08%) ⬇️
appsec-sourcing 42.66% <16.66%> (-0.08%) ⬇️
appsec-template 43.48% <16.66%> (-0.08%) ⬇️
appsec-ubuntu 58.70% <16.66%> (-0.07%) ⬇️
appsec-windows 58.46% <16.66%> (-0.07%) ⬇️
instrumentations-instrumentation-bluebird 32.24% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-body-parser 40.54% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-child_process 37.85% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-cookie-parser 34.27% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-express 34.61% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-express-mongo-sanitize 34.41% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-express-session 40.16% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-fs 31.84% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-generic-pool 29.83% <16.66%> (+<0.01%) ⬆️
instrumentations-instrumentation-http 39.88% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-knex 32.24% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-mongoose 33.40% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-multer 40.28% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-mysql2 38.32% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-passport 44.11% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-passport-http 43.78% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-passport-local 44.33% <16.66%> (-0.09%) ⬇️
instrumentations-instrumentation-pg 37.74% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-promise 32.17% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-promise-js 32.17% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-q 32.22% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-url 32.14% <16.66%> (-0.10%) ⬇️
instrumentations-instrumentation-when 32.19% <16.66%> (-0.10%) ⬇️
llmobs-ai 41.92% <100.00%> (+0.47%) ⬆️
llmobs-anthropic 40.35% <16.66%> (-0.09%) ⬇️
llmobs-bedrock 39.28% <16.66%> (-0.08%) ⬇️
llmobs-google-genai 39.87% <16.66%> (-0.09%) ⬇️
llmobs-langchain 39.47% <100.00%> (-0.06%) ⬇️
llmobs-openai 44.24% <83.33%> (-0.01%) ⬇️
llmobs-vertex-ai 40.14% <16.66%> (-0.02%) ⬇️
platform-core 29.71% <ø> (ø)
platform-esbuild 32.89% <ø> (ø)
platform-instrumentations-misc 40.79% <100.00%> (+0.25%) ⬆️
platform-shimmer 36.14% <ø> (ø)
platform-unit-guardrails 31.27% <ø> (ø)
plugins-azure-event-hubs 24.14% <100.00%> (+0.12%) ⬆️
plugins-azure-service-bus 23.54% <100.00%> (+0.12%) ⬆️
plugins-bullmq 43.65% <100.00%> (-0.08%) ⬇️
plugins-cassandra 37.80% <16.66%> (-0.10%) ⬇️
plugins-cookie 25.20% <100.00%> (+0.12%) ⬆️
plugins-cookie-parser 25.00% <100.00%> (+0.12%) ⬆️
plugins-crypto 24.72% <ø> (ø)
plugins-dd-trace-api 38.39% <16.66%> (-0.10%) ⬇️
plugins-express-mongo-sanitize 25.16% <100.00%> (+0.12%) ⬆️
plugins-express-session 24.95% <100.00%> (+0.12%) ⬆️
plugins-fastify 42.30% <16.66%> (-0.09%) ⬇️
plugins-fetch 38.38% <16.66%> (-0.07%) ⬇️
plugins-fs 38.64% <16.66%> (-0.10%) ⬇️
plugins-generic-pool 24.18% <100.00%> (+0.12%) ⬆️
plugins-google-cloud-pubsub 45.46% <16.66%> (-0.12%) ⬇️
plugins-grpc 41.00% <16.66%> (-0.09%) ⬇️
plugins-handlebars 25.20% <100.00%> (+0.12%) ⬆️
plugins-hapi 40.17% <16.66%> (-0.10%) ⬇️
plugins-hono 40.44% <16.66%> (-0.10%) ⬇️
plugins-ioredis 38.45% <16.66%> (-0.10%) ⬇️
plugins-knex 24.92% <100.00%> (+0.11%) ⬆️
plugins-ldapjs 22.73% <100.00%> (+0.11%) ⬆️
plugins-light-my-request 24.60% <100.00%> (+0.11%) ⬆️
plugins-limitd-client 32.53% <16.66%> (-0.10%) ⬇️
plugins-lodash 24.26% <100.00%> (+0.12%) ⬆️
plugins-mariadb 39.52% <16.66%> (-0.10%) ⬇️
plugins-memcached 38.18% <16.66%> (-0.10%) ⬇️
plugins-microgateway-core 39.28% <16.66%> (-0.02%) ⬇️
plugins-moleculer 40.56% <16.66%> (-0.10%) ⬇️
plugins-mongodb 39.23% <16.66%> (-0.10%) ⬇️
plugins-mongodb-core 39.06% <16.66%> (-0.10%) ⬇️
plugins-mongoose 38.88% <16.66%> (-0.10%) ⬇️
plugins-multer 24.95% <100.00%> (+0.12%) ⬆️
plugins-mysql 39.20% <16.66%> (-0.10%) ⬇️
plugins-mysql2 39.30% <16.66%> (-0.10%) ⬇️
plugins-node-serialize 25.24% <100.00%> (+0.12%) ⬆️
plugins-opensearch 37.63% <16.66%> (-0.10%) ⬇️
plugins-passport-http 25.04% <100.00%> (+0.12%) ⬆️
plugins-postgres 35.72% <16.66%> (-0.09%) ⬇️
plugins-process 24.72% <ø> (ø)
plugins-pug 25.20% <100.00%> (+0.12%) ⬆️
plugins-redis 38.92% <16.66%> (-0.10%) ⬇️
plugins-router 43.05% <16.66%> (-0.10%) ⬇️
plugins-sequelize 23.78% <100.00%> (+0.12%) ⬆️
plugins-test-and-upstream-amqp10 38.52% <16.66%> (+0.05%) ⬆️
plugins-test-and-upstream-amqplib 43.93% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-apollo 39.06% <16.66%> (-0.09%) ⬇️
plugins-test-and-upstream-avsc 38.73% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-bunyan 33.83% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-connect 40.84% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-graphql 40.18% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-koa 40.42% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-protobufjs 38.96% <16.66%> (-0.10%) ⬇️
plugins-test-and-upstream-rhea 44.13% <16.66%> (-0.13%) ⬇️
plugins-undici 39.14% <16.66%> (-0.09%) ⬇️
plugins-url 24.72% <ø> (ø)
plugins-valkey 38.07% <16.66%> (-0.13%) ⬇️
plugins-vm 24.72% <ø> (ø)
plugins-winston 34.03% <16.66%> (-0.10%) ⬇️
plugins-ws 41.94% <16.66%> (-0.10%) ⬇️
profiling-macos 39.88% <16.66%> (-0.10%) ⬇️
profiling-ubuntu 40.00% <16.66%> (-0.10%) ⬇️
profiling-windows 41.22% <16.66%> (-0.10%) ⬇️
serverless-azure-functions-client 23.87% <100.00%> (+0.12%) ⬆️
serverless-azure-functions-eventhubs 23.87% <100.00%> (+0.12%) ⬆️
serverless-azure-functions-servicebus 23.87% <100.00%> (+0.12%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread packages/datadog-instrumentations/src/helpers/rewriter/index.js
@sabrenner sabrenner changed the title feat(ai): properly support ToolLoopAgent via existing patching feat(ai, llmobs): properly support ToolLoopAgent via existing patching Feb 18, 2026
Comment thread packages/datadog-instrumentations/src/helpers/rewriter/index.js Outdated
@sabrenner sabrenner marked this pull request as ready for review February 19, 2026 16:38
@sabrenner sabrenner requested review from a team as code owners February 19, 2026 16:38
Copy link
Copy Markdown
Member

@rochdev rochdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although there are some bits in the instrumentation that would benefit from moving to Orchestrion.

@gabrielsch
Copy link
Copy Markdown

ty for adding support to this! We very much need this at our organization.

@sabrenner
Copy link
Copy Markdown
Collaborator Author

LGTM, although there are some bits in the instrumentation that would benefit from moving to Orchestrion.

curious what your thoughts are on what else could be moved. i think the only things left in the actual "instrumentation" file are instance patching, which could (maybe?) be a common use case still with orchestrion.

with that said, to resolve the issue, i'm going to merge for now

@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot merged commit 6244195 into master Feb 27, 2026
793 checks passed
@gh-worker-dd-mergequeue-cf854d gh-worker-dd-mergequeue-cf854d Bot deleted the sabrenner/vercel-ai-use-orchestrion branch February 27, 2026 15:20
dd-octo-sts Bot pushed a commit that referenced this pull request Feb 27, 2026
#7571)

wip - mostly working

everything works but later 5.x versions in tests

test fixes

add tests for toolloopagent generating spans

remove load publish

remove hook file instead of deleting, with comment

address review comment

use sets instead of arrays for orchestrion supported version checks

Merge branch 'master' into sabrenner/vercel-ai-use-orchestrion

Co-authored-by: sam.brenner <sam.brenner@datadoghq.com>
@dd-octo-sts dd-octo-sts Bot mentioned this pull request Feb 27, 2026
@wheeler-pushpress
Copy link
Copy Markdown

Thank you @sabrenner !

juan-fernandez pushed a commit that referenced this pull request Mar 5, 2026
#7571)

wip - mostly working

everything works but later 5.x versions in tests

test fixes

add tests for toolloopagent generating spans

remove load publish

remove hook file instead of deleting, with comment

address review comment

use sets instead of arrays for orchestrion supported version checks

Merge branch 'master' into sabrenner/vercel-ai-use-orchestrion

Co-authored-by: sam.brenner <sam.brenner@datadoghq.com>
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.

[BUG]: ToolLoopAgent in AI SDK 6 is not captured in LLM Observability

4 participants