feat(cloudflare): Add R2 bucket auto-instrumentation#21327
Conversation
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.
Reviewed by Cursor Bugbot for commit 3605367. Configure here.
| export function isR2Bucket(item: unknown): item is R2Bucket { | ||
| return ( | ||
| item != null && | ||
| isNotJSRPC(item) && | ||
| typeof item.head === 'function' && | ||
| typeof item.put === 'function' && | ||
| typeof item.createMultipartUpload === 'function' | ||
| ); |
There was a problem hiding this comment.
Bug: The isR2Bucket function uses fragile duck-typing to detect R2 buckets, which could misidentify other objects and lacks test coverage for validation.
Severity: LOW
Suggested Fix
Add comprehensive unit tests for the isR2Bucket function in isBinding.test.ts. The tests should cover positive and negative cases, including objects that have some but not all of the required methods, to ensure the detection logic is robust and prevent future regressions.
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/utils/isBinding.ts#L75-L82
Potential issue: The `isR2Bucket` function identifies R2 buckets by checking for the
presence of `head`, `put`, and `createMultipartUpload` methods. This duck-typing
approach is inherently fragile and could lead to false positives. If a different type of
object, such as a custom user object or a future Cloudflare binding, happens to have the
same method names, it would be incorrectly wrapped for R2 instrumentation. This would
not cause a crash but would generate incorrect telemetry spans, which could mislead
debugging efforts. The analysis highlights that this new function lacks any test
coverage in `isBinding.test.ts`, unlike other binding detection functions in the same
file.
Also affects:
packages/cloudflare/src/instrumentations/worker/instrumentEnv.ts:57~62
Did we get this right? 👍 / 👎 to inform future reviews.
size-limit report 📦
|
Auto-detect R2 bucket bindings via duck-typing in instrumentEnv and wrap all operations with spans. Span names and attributes align with Cloudflare's built-in tracing conventions (r2_get, r2_put, etc.). Instrumented methods: get, head, put, delete, list, createMultipartUpload, resumeMultipartUpload, and the resulting multipart upload operations (uploadPart, abort, complete). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
3605367 to
3671f29
Compare

closes #20836
closes JS-2448
This implements R2 spans. All attributes were taken from Cloudflare itself. The only thing which does not exist in Cloudflare and is added here (inspired by AWS S3), is
cloudflare.r2.bucketand gives information of the binding itself.This should be merged after the Sentry conventions PR
AI generated text:
Auto-detect R2 bucket bindings via duck-typing in instrumentEnv and wrap all operations with spans. Span names and attributes align with Cloudflare's built-in tracing conventions (r2_get, r2_put, etc.).
Instrumented methods: get, head, put, delete, list, createMultipartUpload, resumeMultipartUpload, and the resulting multipart upload operations (uploadPart, abort, complete).