Repository navigation
feat(cloudflare): instrument Workers KV bindings #25488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
faizansaiyed123
wants to merge
5
commits into
getsentry:develop
Choose a base branch
from
faizansaiyed123:feat/cloudflare-kv-instrumentation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+594
−2
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7803e69
feat(cloudflare): instrument Workers KV bindings
faizansaiyed123 7c1a267
test(cloudflare): fix KV binding test import
faizansaiyed123 1a91897
test(cloudflare): align KV database system attribute
faizansaiyed123 c23d2c8
test(cloudflare): use conventions in KV instrumentation
faizansaiyed123 8557281
test(cloudflare): tighten Workers KV regression assertions
faizansaiyed123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
dev-packages/cloudflare-integration-tests/suites/kv/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import type { KVNamespace } from '@cloudflare/workers-types'; | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| MY_KV: KVNamespace; | ||
| } | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1, | ||
| }), | ||
| { | ||
| async fetch(request, env) { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === '/kv/get') { | ||
| await env.MY_KV.put('get-key', 'get-value'); | ||
| const value = await env.MY_KV.get('get-key'); | ||
| return new Response(value); | ||
| } | ||
|
|
||
| if (url.pathname === '/kv/bulk-get') { | ||
| await env.MY_KV.put('bulk-a', 'a'); | ||
| await env.MY_KV.put('bulk-b', 'b'); | ||
| const values = await env.MY_KV.get(['bulk-a', 'bulk-b']); | ||
| return Response.json(Object.fromEntries(values)); | ||
| } | ||
|
|
||
| if (url.pathname === '/kv/get-with-metadata') { | ||
| await env.MY_KV.put('metadata-key', 'metadata-value', { metadata: { source: 'test' } }); | ||
| const value = await env.MY_KV.getWithMetadata('metadata-key'); | ||
| return Response.json(value); | ||
| } | ||
|
|
||
| if (url.pathname === '/kv/put') { | ||
| await env.MY_KV.put('put-key', 'put-value'); | ||
| return new Response('OK'); | ||
| } | ||
|
|
||
| if (url.pathname === '/kv/list') { | ||
| await env.MY_KV.put('list-a', 'a'); | ||
| await env.MY_KV.put('list-b', 'b'); | ||
| await env.MY_KV.list({ prefix: 'list-' }); | ||
| return new Response('OK'); | ||
| } | ||
|
|
||
| if (url.pathname === '/kv/delete') { | ||
| await env.MY_KV.put('delete-key', 'gone'); | ||
| await env.MY_KV.delete('delete-key'); | ||
| return new Response('OK'); | ||
| } | ||
|
|
||
| return new Response('not found', { status: 404 }); | ||
| }, | ||
| } as ExportedHandler<Env>, | ||
| ); |
163 changes: 163 additions & 0 deletions
163
dev-packages/cloudflare-integration-tests/suites/kv/test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import { | ||
| CACHE_HIT, | ||
| CACHE_KEY, | ||
| CACHE_OPERATION, | ||
| DB_COLLECTION_NAME, | ||
| DB_OPERATION_NAME, | ||
| DB_SYSTEM_NAME, | ||
| SENTRY_OP, | ||
| SENTRY_ORIGIN, | ||
| } from '@sentry/conventions/attributes'; | ||
| import { CACHE_GET, CACHE_PUT, CACHE_REMOVE } from '@sentry/conventions/op'; | ||
| import { CACHE_OPERATION_NAMES } from '@sentry/core'; | ||
| import { expect, it } from 'vitest'; | ||
| import { createRunner } from '../../runner'; | ||
| import { getSpansFromEnvelope } from '../../spanUtils'; | ||
|
|
||
| it('emits cache.get for a KV read with key and hit attributes', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const spans = getSpansFromEnvelope(envelope).filter(span => span.name === CACHE_GET); | ||
| expect(spans).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| attributes: expect.objectContaining({ | ||
| [SENTRY_OP]: { type: 'string', value: CACHE_GET }, | ||
| [SENTRY_ORIGIN]: { type: 'string', value: 'auto.cache.cloudflare.kv' }, | ||
| [CACHE_OPERATION]: { type: 'string', value: CACHE_OPERATION_NAMES[CACHE_GET] }, | ||
| [CACHE_KEY]: { type: 'string', value: 'get-key' }, | ||
| [CACHE_HIT]: { type: 'boolean', value: true }, | ||
| [DB_OPERATION_NAME]: { type: 'string', value: 'get' }, | ||
| [DB_COLLECTION_NAME]: { type: 'string', value: 'MY_KV' }, | ||
| [DB_SYSTEM_NAME]: { type: 'string', value: 'cloudflare-kv' }, | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/kv/get'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('emits cache.get for a bulk KV read with all keys in cache.key', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const spans = getSpansFromEnvelope(envelope).filter( | ||
| span => span.name === CACHE_GET && span.attributes[DB_OPERATION_NAME]?.value === 'get', | ||
| ); | ||
|
|
||
| expect(spans).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| attributes: expect.objectContaining({ | ||
| [CACHE_KEY]: expect.objectContaining({ | ||
| type: 'array', | ||
| value: expect.arrayContaining(['bulk-a', 'bulk-b']), | ||
| }), | ||
| [CACHE_HIT]: { type: 'boolean', value: true }, | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/kv/bulk-get'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('emits cache.get for getWithMetadata', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const spans = getSpansFromEnvelope(envelope).filter( | ||
| span => span.name === CACHE_GET && span.attributes[DB_OPERATION_NAME]?.value === 'getWithMetadata', | ||
| ); | ||
|
|
||
| expect(spans).toEqual([ | ||
| expect.objectContaining({ | ||
| attributes: expect.objectContaining({ | ||
| [CACHE_KEY]: { type: 'string', value: 'metadata-key' }, | ||
| [CACHE_HIT]: { type: 'boolean', value: true }, | ||
| }), | ||
| }), | ||
| ]); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/kv/get-with-metadata'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('emits cache.put for KV writes', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const spans = getSpansFromEnvelope(envelope).filter(span => span.name === CACHE_PUT); | ||
| expect(spans).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| attributes: expect.objectContaining({ | ||
| [SENTRY_OP]: { type: 'string', value: CACHE_PUT }, | ||
| [CACHE_OPERATION]: { type: 'string', value: CACHE_OPERATION_NAMES[CACHE_PUT] }, | ||
| [CACHE_KEY]: { type: 'string', value: 'put-key' }, | ||
| [DB_OPERATION_NAME]: { type: 'string', value: 'put' }, | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/kv/put'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('emits cache.remove for KV deletes', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const spans = getSpansFromEnvelope(envelope).filter(span => span.name === CACHE_REMOVE); | ||
| expect(spans).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| attributes: expect.objectContaining({ | ||
| [SENTRY_OP]: { type: 'string', value: CACHE_REMOVE }, | ||
| [CACHE_OPERATION]: { type: 'string', value: CACHE_OPERATION_NAMES[CACHE_REMOVE] }, | ||
| [CACHE_KEY]: { type: 'string', value: 'delete-key' }, | ||
| [DB_OPERATION_NAME]: { type: 'string', value: 'delete' }, | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/kv/delete'); | ||
| await runner.completed(); | ||
| }); | ||
|
|
||
| it('emits cache.get for KV list operations', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const spans = getSpansFromEnvelope(envelope).filter( | ||
| span => span.name === CACHE_GET && span.attributes[DB_OPERATION_NAME]?.value === 'list', | ||
| ); | ||
|
|
||
| expect(spans).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| attributes: expect.objectContaining({ | ||
| [SENTRY_OP]: { type: 'string', value: CACHE_GET }, | ||
| [CACHE_OPERATION]: { type: 'string', value: CACHE_OPERATION_NAMES[CACHE_GET] }, | ||
| [DB_OPERATION_NAME]: { type: 'string', value: 'list' }, | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| expect(spans[0]?.attributes[CACHE_KEY]).toBeUndefined(); | ||
| }) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/kv/list'); | ||
| await runner.completed(); | ||
| }); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
12 changes: 12 additions & 0 deletions
12
dev-packages/cloudflare-integration-tests/suites/kv/wrangler.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "name": "worker-name", | ||
| "compatibility_date": "2025-06-17", | ||
| "main": "index.ts", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| "kv_namespaces": [ | ||
| { | ||
| "binding": "MY_KV", | ||
| "id": "00000000000000000000000000000000", | ||
| }, | ||
| ], | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/cloudflare/src/instrumentations/worker/instrumentKV.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import type { KVNamespace } from '@cloudflare/workers-types'; | ||
| import { | ||
| CACHE_HIT, | ||
| CACHE_KEY, | ||
| CACHE_OPERATION, | ||
| DB_COLLECTION_NAME, | ||
| DB_OPERATION_NAME, | ||
| DB_SYSTEM_NAME, | ||
| SENTRY_OP, | ||
| SENTRY_ORIGIN, | ||
| } from '@sentry/conventions/attributes'; | ||
| import { CACHE_GET, CACHE_PUT, CACHE_REMOVE } from '@sentry/conventions/op'; | ||
| import { CACHE_OPERATION_NAMES, getClient, hasSpanStreamingEnabled, isObjectLike, startSpan } from '@sentry/core'; | ||
|
|
||
| const ORIGIN = 'auto.cache.cloudflare.kv'; | ||
|
|
||
| type KVOperation = 'get' | 'getWithMetadata' | 'put' | 'delete' | 'list'; | ||
|
|
||
| const OPERATION_SPAN_OPS = { | ||
| get: CACHE_GET, | ||
| getWithMetadata: CACHE_GET, | ||
| put: CACHE_PUT, | ||
| delete: CACHE_REMOVE, | ||
| list: CACHE_GET, | ||
| } as const satisfies Record<KVOperation, string>; | ||
|
|
||
| function getCacheKeys(operation: KVOperation, args: unknown[]): string[] | undefined { | ||
| if (operation === 'list') { | ||
| return undefined; | ||
| } | ||
|
|
||
| const key = args[0]; | ||
|
|
||
| if (Array.isArray(key)) { | ||
| return key.map(value => String(value)); | ||
| } | ||
|
|
||
| return typeof key === 'string' ? [key] : key == null ? undefined : [String(key)]; | ||
| } | ||
|
|
||
| function createSpanOptions(bindingName: string, operation: KVOperation, args: unknown[]) { | ||
| const cacheOperation = OPERATION_SPAN_OPS[operation]; | ||
| const cacheOperationName = CACHE_OPERATION_NAMES[cacheOperation]; | ||
| const keys = getCacheKeys(operation, args); | ||
| const client = getClient(); | ||
|
|
||
| return { | ||
| name: client && hasSpanStreamingEnabled(client) ? cacheOperation : keys?.join(', ') || operation, | ||
| attributes: { | ||
| [SENTRY_OP]: cacheOperation, | ||
| [CACHE_OPERATION]: cacheOperationName, | ||
| [SENTRY_ORIGIN]: ORIGIN, | ||
| ...(keys && keys.length > 0 && { [CACHE_KEY]: keys.length > 1 ? keys : keys[0] }), | ||
| [DB_OPERATION_NAME]: operation, | ||
| [DB_COLLECTION_NAME]: bindingName, | ||
| [DB_SYSTEM_NAME]: 'cloudflare-kv', | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function resolveCacheHit(operation: KVOperation, result: unknown): boolean { | ||
| if (operation === 'list') { | ||
| return false; | ||
| } | ||
|
|
||
| if (result instanceof Map) { | ||
| return Array.from(result.values()).some(value => resolveCacheHit(operation, value)); | ||
| } | ||
|
|
||
| if (operation === 'getWithMetadata' && isObjectLike(result) && 'value' in result) { | ||
| return result.value != null; | ||
| } | ||
|
|
||
| return result != null; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps a Cloudflare Workers KV binding to create cache spans on namespace operations. | ||
| * | ||
| * Instrumented methods: get, getWithMetadata, put, delete, and list. | ||
| */ | ||
| export function instrumentKV<T extends KVNamespace>(kv: T, bindingName: string): T { | ||
| return new Proxy(kv, { | ||
| get(target, prop, receiver) { | ||
| if (prop === 'get' || prop === 'getWithMetadata' || prop === 'put' || prop === 'delete' || prop === 'list') { | ||
| const original = Reflect.get(target, prop, receiver); | ||
|
|
||
| if (typeof original !== 'function') { | ||
| return original; | ||
| } | ||
|
|
||
| return function (this: unknown, ...args: unknown[]) { | ||
| const operation = prop as KVOperation; | ||
|
|
||
| return startSpan(createSpanOptions(bindingName, operation, args), async span => { | ||
| const result = await Reflect.apply(original, target, args); | ||
|
|
||
| if (operation === 'get' || operation === 'getWithMetadata') { | ||
| span.setAttribute(CACHE_HIT, resolveCacheHit(operation, result)); | ||
| } | ||
|
|
||
| return result; | ||
| }); | ||
| }; | ||
| } | ||
|
|
||
| return Reflect.get(target, prop, receiver); | ||
| }, | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.