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 📦

Path Size % Change Change
@sentry/browser 27.74 kB -0.02% -3 B 🔽
@sentry/browser - with treeshaking flags 26.18 kB -0.02% -5 B 🔽
@sentry/browser (incl. Tracing) 46.55 kB -0.03% -12 B 🔽
@sentry/browser (incl. Tracing + Span Streaming) 48.35 kB -0.05% -20 B 🔽
@sentry/browser (incl. Tracing, Profiling) 51.36 kB -0.01% -1 B 🔽
@sentry/browser (incl. Tracing, Replay) 85.81 kB -0.02% -13 B 🔽
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 75.46 kB -0.01% -7 B 🔽
@sentry/browser (incl. Tracing, Replay with Canvas) 90.52 kB -0.04% -30 B 🔽
@sentry/browser (incl. Tracing, Replay, Feedback) 103.2 kB -0.01% -6 B 🔽
@sentry/browser (incl. Feedback) 44.93 kB -0.01% -1 B 🔽
@sentry/browser (incl. sendFeedback) 32.54 kB -0.04% -12 B 🔽
@sentry/browser (incl. FeedbackAsync) 37.68 kB -0.04% -12 B 🔽
@sentry/browser (incl. Metrics) 28.83 kB -0.02% -3 B 🔽
@sentry/browser (incl. Logs) 29.06 kB +0.04% +9 B 🔺
@sentry/browser (incl. Metrics & Logs) 29.75 kB -0.02% -3 B 🔽
@sentry/react 29.54 kB -0.02% -4 B 🔽
@sentry/react (incl. Tracing) 48.81 kB -0.06% -29 B 🔽
@sentry/vue 33.17 kB -0.02% -4 B 🔽
@sentry/vue (incl. Tracing) 48.52 kB +0.02% +8 B 🔺
@sentry/svelte 27.77 kB -0.02% -3 B 🔽
CDN Bundle 30.14 kB -0.02% -4 B 🔽
CDN Bundle (incl. Tracing) 48.52 kB -0.02% -6 B 🔽
CDN Bundle (incl. Logs, Metrics) 31.73 kB -0.01% -2 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) 49.82 kB -0.03% -14 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) 70.97 kB -0.01% -4 B 🔽
CDN Bundle (incl. Tracing, Replay) 86.03 kB -0.02% -15 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) 87.34 kB -0.01% -3 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) 91.83 kB -0.01% -4 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) 93.09 kB -0.03% -19 B 🔽
CDN Bundle - uncompressed 89.87 kB -0.01% -8 B 🔽
CDN Bundle (incl. Tracing) - uncompressed 146.68 kB -0.04% -46 B 🔽
CDN Bundle (incl. Logs, Metrics) - uncompressed 94.58 kB -0.01% -8 B 🔽
CDN Bundle (incl. Tracing, Logs, Metrics) - uncompressed 150.66 kB -0.04% -46 B 🔽
CDN Bundle (incl. Replay, Logs, Metrics) - uncompressed 219.32 kB -0.01% -8 B 🔽
CDN Bundle (incl. Tracing, Replay) - uncompressed 265.89 kB -0.02% -46 B 🔽
CDN Bundle (incl. Tracing, Replay, Logs, Metrics) - uncompressed 269.86 kB -0.02% -46 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 279.6 kB -0.02% -46 B 🔽
CDN Bundle (incl. Tracing, Replay, Feedback, Logs, Metrics) - uncompressed 283.55 kB -0.02% -46 B 🔽
@sentry/nextjs (client) 51.36 kB +0.01% +3 B 🔺
@sentry/sveltekit (client) 46.98 kB -0.03% -10 B 🔽
@sentry/core/server 79.87 kB +1.56% +1.22 kB 🔺
@sentry/core/browser 66.22 kB +1.77% +1.15 kB 🔺
@sentry/node-core 63.21 kB -0.01% -4 B 🔽
@sentry/node 125.52 kB -0.02% -24 B 🔽
@sentry/node (incl. diagnostics channel injection) 148.06 kB -0.29% -429 B 🔽
@sentry/node/import (ESM hook with diagnostics-channel injection) 70.03 kB - -
@sentry/node/light 51.32 kB -0.01% -1 B 🔽
@sentry/node - without tracing 74.71 kB - -
@sentry/aws-serverless 83.92 kB -0.04% -32 B 🔽
@sentry/cloudflare (withSentry) - minified 194.68 kB +5.81% +10.67 kB 🔺
@sentry/cloudflare (withSentry) 479.41 kB +5.27% +24 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
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.

Comment thread packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts
Comment thread packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts

@isaacs isaacs left a comment

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.

Silly clankers. LGTM.

Comment thread packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts
@JPeer264
JPeer264 force-pushed the jp/cloudflare-workers-ai-cf branch from b6a0a04 to 0284a1b Compare July 17, 2026 09:52
Comment thread packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts
@JPeer264
JPeer264 force-pushed the jp/cloudflare-workers-ai branch from b7852b2 to 90177eb Compare July 17, 2026 10:00
@JPeer264
JPeer264 force-pushed the jp/cloudflare-workers-ai-cf branch 2 times, most recently from 17374ab to 3a42b84 Compare July 17, 2026 10:10
Comment thread packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts
@JPeer264
JPeer264 force-pushed the jp/cloudflare-workers-ai branch from 1b95295 to 16771a7 Compare July 17, 2026 10:28
JPeer264 and others added 2 commits July 17, 2026 19:04
…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 3a42b84 to 6b08f07 Compare July 17, 2026 17:04
Comment on lines +34 to +44
}

return {
response: 'The capital of France is Paris.',
usage: {
prompt_tokens: 12,
completion_tokens: 7,
total_tokens: 19,
},
};
}

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: The MockAi class in integration tests is missing gateway and toMarkdown methods, which means the new auto-instrumentation logic for AI bindings is not being tested.
Severity: LOW

Suggested Fix

Update the MockAi class in dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/mocks.ts to include mock implementations for the gateway and toMarkdown methods. Modify the integration test to rely on instrumentEnv to automatically detect and instrument the env.AI binding, which will properly test the new auto-instrumentation code path.

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:
dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/mocks.ts#L15-L44

Potential issue: The integration test for Workers AI uses a `MockAi` class that only
implements the `run` method. The new auto-instrumentation logic in `instrumentEnv`
checks for the presence of `run`, `gateway`, and `toMarkdown` to identify an AI binding.
Because the mock is incomplete, the auto-detection path is never triggered in the test
suite. Instead, the test manually instruments the client, bypassing the new logic. This
creates a gap in test coverage, as the auto-instrumentation feature for AI bindings is
not validated by the integration tests.

Also affects:

  • dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/index.ts:10~10
  • packages/cloudflare/src/utils/isBinding.ts:94~96
  • packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts:91~95

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.

2 participants