-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Instrument sync KV #21316
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
Merged
Merged
Changes from all commits
Commits
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
51 changes: 51 additions & 0 deletions
51
dev-packages/cloudflare-integration-tests/suites/tracing/durableobject-sync-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,51 @@ | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
| import { DurableObject } from 'cloudflare:workers'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| TEST_DURABLE_OBJECT: DurableObjectNamespace; | ||
| } | ||
|
|
||
| class SyncKvDurableObjectBase extends DurableObject<Env> { | ||
| public constructor(ctx: DurableObjectState, env: Env) { | ||
| super(ctx, env); | ||
| } | ||
|
|
||
| async fetch(): Promise<Response> { | ||
| this.ctx.storage.kv.put('test-key', { value: 'hello' }); | ||
| const val = this.ctx.storage.kv.get('test-key'); | ||
| const entries = [...this.ctx.storage.kv.list()]; | ||
| const deleted = this.ctx.storage.kv.delete('test-key'); | ||
|
|
||
| return Response.json({ get: val, listSize: entries.length, deleted }); | ||
| } | ||
| } | ||
|
|
||
| export const TestDurableObject = Sentry.instrumentDurableObjectWithSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| }), | ||
| SyncKvDurableObjectBase, | ||
| ); | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| }), | ||
| { | ||
| async fetch(request: Request, env: Env): Promise<Response> { | ||
| const url = new URL(request.url); | ||
|
|
||
| if (url.pathname === '/flush-marker') { | ||
| Sentry.captureMessage('flush-marker'); | ||
| return new Response(JSON.stringify({ ok: true }), { headers: { 'Content-Type': 'application/json' } }); | ||
| } | ||
|
|
||
| const id = env.TEST_DURABLE_OBJECT.idFromName('test'); | ||
| const stub = env.TEST_DURABLE_OBJECT.get(id); | ||
| return stub.fetch(request); | ||
| }, | ||
| } satisfies ExportedHandler<Env>, | ||
| ); |
77 changes: 77 additions & 0 deletions
77
dev-packages/cloudflare-integration-tests/suites/tracing/durableobject-sync-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,77 @@ | ||
| import type { Envelope } from '@sentry/core'; | ||
| import { expect, it } from 'vitest'; | ||
| import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; | ||
| import { createRunner } from '../../../runner'; | ||
|
|
||
| const flushMarkerMatcher = (envelope: Envelope): void => { | ||
| const [, items] = envelope; | ||
| const [itemHeader, itemBody] = items[0] as [{ type: string }, Record<string, unknown>]; | ||
|
|
||
| expect(itemHeader.type).toBe('event'); | ||
| expect(itemBody.message).toBe('flush-marker'); | ||
| }; | ||
|
|
||
| it('instruments sync KV operations on Durable Object storage', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1]; | ||
| const spans = transactionEvent?.spans ?? []; | ||
|
|
||
| expect(transactionEvent).toEqual( | ||
| expect.objectContaining({ | ||
| type: 'transaction', | ||
| transaction: 'GET /', | ||
| }), | ||
| ); | ||
|
|
||
| expect(spans).toHaveLength(4); | ||
| expect(spans).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| description: 'durable_object_storage_kv_put', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| data: expect.objectContaining({ | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'db', | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.cloudflare.durable_object', | ||
| 'db.system.name': 'cloudflare-durable-object-sql', | ||
| 'db.operation.name': 'put', | ||
| }), | ||
| }), | ||
| expect.objectContaining({ | ||
| description: 'durable_object_storage_kv_get', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| data: expect.objectContaining({ | ||
| 'db.system.name': 'cloudflare-durable-object-sql', | ||
| 'db.operation.name': 'get', | ||
| }), | ||
| }), | ||
| expect.objectContaining({ | ||
| description: 'durable_object_storage_kv_list', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| data: expect.objectContaining({ | ||
| 'db.system.name': 'cloudflare-durable-object-sql', | ||
| 'db.operation.name': 'list', | ||
| }), | ||
| }), | ||
| expect.objectContaining({ | ||
| description: 'durable_object_storage_kv_delete', | ||
| op: 'db', | ||
| origin: 'auto.db.cloudflare.durable_object', | ||
| data: expect.objectContaining({ | ||
| 'db.system.name': 'cloudflare-durable-object-sql', | ||
| 'db.operation.name': 'delete', | ||
| }), | ||
| }), | ||
| ]), | ||
| ); | ||
| }) | ||
| .expect(flushMarkerMatcher) | ||
| .start(signal); | ||
|
|
||
| await runner.makeRequest('get', '/'); | ||
| await runner.makeRequest('get', '/flush-marker'); | ||
| await runner.completed(); | ||
| }); | ||
20 changes: 20 additions & 0 deletions
20
...packages/cloudflare-integration-tests/suites/tracing/durableobject-sync-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,20 @@ | ||
| { | ||
| "name": "worker-name", | ||
| "main": "index.ts", | ||
| "compatibility_date": "2025-06-17", | ||
| "migrations": [ | ||
| { | ||
| "new_sqlite_classes": ["TestDurableObject"], | ||
| "tag": "v1", | ||
| }, | ||
| ], | ||
| "durable_objects": { | ||
| "bindings": [ | ||
| { | ||
| "class_name": "TestDurableObject", | ||
| "name": "TEST_DURABLE_OBJECT", | ||
| }, | ||
| ], | ||
| }, | ||
| "compatibility_flags": ["nodejs_als"], | ||
| } |
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
43 changes: 43 additions & 0 deletions
43
packages/cloudflare/src/instrumentations/instrumentDurableObjectSyncKvStorage.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,43 @@ | ||
| import type { SyncKvStorage } from '@cloudflare/workers-types'; | ||
| import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, startSpan } from '@sentry/core'; | ||
|
|
||
| const SYNC_KV_METHODS_TO_INSTRUMENT = ['get', 'put', 'delete', 'list'] as const; | ||
|
|
||
| type SyncKvMethod = (typeof SYNC_KV_METHODS_TO_INSTRUMENT)[number]; | ||
|
|
||
| export function instrumentDurableObjectSyncKvStorage(syncKv: SyncKvStorage): SyncKvStorage { | ||
| return new Proxy(syncKv, { | ||
| get(target, prop, _receiver) { | ||
| const original = Reflect.get(target, prop, target); | ||
|
|
||
| if (typeof original !== 'function') { | ||
| return original; | ||
| } | ||
|
|
||
| const methodName = prop as SyncKvMethod; | ||
|
|
||
| if (!SYNC_KV_METHODS_TO_INSTRUMENT.includes(methodName)) { | ||
| return (original as (...args: unknown[]) => unknown).bind(target); | ||
| } | ||
|
|
||
| return function (this: unknown, ...args: unknown[]) { | ||
| return startSpan( | ||
| { | ||
| name: `durable_object_storage_kv_${methodName}`, | ||
| op: 'db', | ||
| attributes: { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.db.cloudflare.durable_object', | ||
| // keeping the value as close as possible to the Cloudflare Worker KV instrumentation | ||
| // https://github.com/cloudflare/workerd/blob/6b8b11787e2b2a800ab0edd0690bfab3857b0529/src/workerd/api/sync-kv.c%2B%2B#L19 | ||
| 'db.system.name': 'cloudflare-durable-object-sql', | ||
|
JPeer264 marked this conversation as resolved.
|
||
| 'db.operation.name': methodName, | ||
| }, | ||
| }, | ||
| () => { | ||
| return (original as (...args: unknown[]) => unknown).apply(target, args); | ||
| }, | ||
| ); | ||
| }; | ||
| }, | ||
| }); | ||
| } | ||
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
Oops, something went wrong.
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.