feat(cloudflare): Auto-instrument Workers AI binding via env instrumentation#22126
feat(cloudflare): Auto-instrument Workers AI binding via env instrumentation#22126JPeer264 wants to merge 2 commits into
Conversation
size-limit report 📦
|
c626710 to
a8b1d8b
Compare
0aa52a5 to
8011b0b
Compare
a8b1d8b to
cd876d4
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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.
b6a0a04 to
0284a1b
Compare
b7852b2 to
90177eb
Compare
17374ab to
3a42b84
Compare
1b95295 to
16771a7
Compare
…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>
3a42b84 to
6b08f07
Compare
| } | ||
|
|
||
| return { | ||
| response: 'The capital of France is Paris.', | ||
| usage: { | ||
| prompt_tokens: 12, | ||
| completion_tokens: 7, | ||
| total_tokens: 19, | ||
| }, | ||
| }; | ||
| } |
There was a problem hiding this comment.
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~10packages/cloudflare/src/utils/isBinding.ts:94~96packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts:91~95

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.