Skip to content

feat(cloudflare): Auto-instrument Workers AI binding via env instrumentation#22126

Open
JPeer264 wants to merge 2 commits into
jp/cloudflare-workers-aifrom
jp/cloudflare-workers-ai-cf
Open

feat(cloudflare): Auto-instrument Workers AI binding via env instrumentation#22126
JPeer264 wants to merge 2 commits into
jp/cloudflare-workers-aifrom
jp/cloudflare-workers-ai-cf

Conversation

@JPeer264

@JPeer264 JPeer264 commented Jul 9, 2026

Copy link
Copy Markdown
Member

This auto-wraps workers-ai bindings via instrumentEnv. Unfortunately locally there is no AI binding available and would require an actual LLM on Cloudflare, so in the integration tests there is only a MockAi. However, the autobinding is tested via unit tests and have the same pattern as the other instrumentations.

@JPeer264 JPeer264 self-assigned this Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

size-limit report 📦

⚠️ Warning: Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.

Path Size % Change Change
@sentry/browser 27.6 kB - -
@sentry/browser - with treeshaking flags 26.04 kB - -
@sentry/browser (incl. Tracing) 46.35 kB - -
@sentry/browser (incl. Tracing + Span Streaming) 48.14 kB - -
@sentry/browser (incl. Tracing, Profiling) 51.13 kB - -
@sentry/browser (incl. Tracing, Replay) 85.62 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.26 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 90.33 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 102.99 kB - -
@sentry/browser (incl. Feedback) 44.78 kB - -
@sentry/browser (incl. sendFeedback) 32.4 kB - -
@sentry/browser (incl. FeedbackAsync) 37.53 kB - -
@sentry/browser (incl. Metrics) 28.68 kB - -
@sentry/browser (incl. Logs) 28.93 kB - -
@sentry/browser (incl. Metrics & Logs) 29.61 kB - -
@sentry/react 29.39 kB - -
@sentry/react (incl. Tracing) 48.62 kB - -
@sentry/vue 33.03 kB - -
@sentry/vue (incl. Tracing) 48.33 kB - -
@sentry/svelte 27.63 kB - -
CDN Bundle 30 kB - -
CDN Bundle (incl. Tracing) 48.33 kB - -
CDN Bundle (incl. Logs, Metrics) 31.58 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) 49.65 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) 70.82 kB - -
CDN Bundle (incl. Tracing, Replay) 85.84 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.16 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 91.64 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 92.92 kB - -
CDN Bundle - uncompressed 89.33 kB - -
CDN Bundle (incl. Tracing) - uncompressed 146.07 kB - -
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.03 kB - -
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.05 kB - -
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 218.76 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.28 kB - -
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.24 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 278.98 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 282.93 kB - -
@sentry/nextjs (client) 51.17 kB - -
@sentry/sveltekit (client) 46.8 kB - -
@sentry/core/server 79.32 kB +1.15% +900 B 🔺
@sentry/core/browser 65.68 kB +1.41% +912 B 🔺
@sentry/node-core 62.73 kB -0.01% -1 B 🔽
@sentry/node 125.09 kB +0.01% +1 B 🔺
@sentry/node (incl. diagnostics channel injection) 139.35 kB - -
@sentry/node/import (ESM hook with diagnostics-channel injection) 69.96 kB - -
@sentry/node/light 50.73 kB - -
@sentry/node - without tracing 74.1 kB - -
@sentry/aws-serverless 83.32 kB -0.01% -1 B 🔽
@sentry/cloudflare (withSentry) - minified 193.48 kB +6.56% +11.9 kB 🔺
@sentry/cloudflare (withSentry) 477.19 kB +6.17% +27.7 kB 🔺

View base workflow run

@JPeer264 JPeer264 force-pushed the jp/cloudflare-workers-ai-cf branch from c626710 to a8b1d8b Compare July 13, 2026 15:09
@JPeer264 JPeer264 force-pushed the jp/cloudflare-workers-ai branch from 0aa52a5 to 8011b0b Compare July 13, 2026 15:10
JPeer264 and others added 2 commits July 13, 2026 17:12
…ntation

Detect the Workers AI binding (env.AI) in instrumentEnv via duck-typing
(run + gateway + toMarkdown) and wrap it automatically, matching how D1,
R2, and Queue bindings are instrumented. Manual wrapping via
instrumentWorkersAiClient remains available for custom options and is now
guarded against double-wrapping. Also removes the unused
WORKERS_AI_INTEGRATION_NAME constant and aligns the integration test
wrangler config with sibling suites (nodejs_als).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@JPeer264 JPeer264 force-pushed the jp/cloudflare-workers-ai-cf branch from a8b1d8b to cd876d4 Compare July 13, 2026 15:12
@JPeer264 JPeer264 marked this pull request as ready for review July 13, 2026 15:16
@JPeer264 JPeer264 requested a review from a team as a code owner July 13, 2026 15:16
@JPeer264 JPeer264 requested review from andreiborza, isaacs and mydea and removed request for a team July 13, 2026 15:16

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cd876d4. Configure here.

const instrumented = instrumentWorkersAiClient(item);
instrumentedBindings.set(item, instrumented);
return instrumented;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Double-wrap of AI bindings

Medium Severity

instrumentEnv always wraps AI bindings with instrumentWorkersAiClient, but that helper has no double-wrap guard (unlike D1’s ensureInstrumented). Calling instrumentWorkersAiClient(env.AI, options) for custom options — the path the API docs call out — nests a second proxy and emits duplicate gen_ai spans.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit cd876d4. Configure here.

Comment on lines +81 to +85
if (isAiBinding(item)) {
const instrumented = instrumentWorkersAiClient(item);
instrumentedBindings.set(item, instrumented);
return instrumented;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: Manually instrumenting an AI client with instrumentWorkersAiClient and then passing it to instrumentEnv causes double-wrapping and duplicate spans, as the existing instrumentation is not detected.
Severity: MEDIUM

Suggested Fix

Add a guard to instrumentWorkersAiClient to prevent re-instrumenting an already-wrapped client. This could be achieved by using a mechanism similar to the ensureInstrumented() helper used for D1 bindings, which checks for a unique symbol or property on the object before applying instrumentation.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts#L81-L85

Potential issue: The `instrumentWorkersAiClient` function lacks a guard to prevent
double instrumentation. If a user manually instruments an AI client and then also uses
the automatic `instrumentEnv` on an environment containing this already-instrumented
client, the client will be wrapped a second time. This happens because the caching in
`instrumentEnv` keys on the original object reference, which is lost after manual
wrapping. The already-instrumented proxy still passes the `isAiBinding` check, leading
to re-instrumentation and resulting in duplicate spans.

Did we get this right? 👍 / 👎 to inform future reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant