-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(cloudflare): Add allow list to enableRpcTracePropagation #23363
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { | |
| } from '../../utils/isBinding'; | ||
| import { instrumentD1 } from './instrumentD1'; | ||
| import { appendRpcMeta } from '../../utils/rpcMeta'; | ||
| import { createRpcPropagationResolver } from '../../utils/rpcPropagation'; | ||
| import { instrumentDurableObjectNamespace, STUB_NON_RPC_METHODS } from '../instrumentDurableObjectNamespace'; | ||
| import { instrumentFetcher } from './instrumentFetcher'; | ||
| import { instrumentQueueProducer } from './instrumentQueueProducer'; | ||
|
|
@@ -44,6 +45,8 @@ export function instrumentEnv<Env extends Record<string, unknown>>(env: Env, opt | |
| return env; | ||
| } | ||
|
|
||
| const shouldPropagateRpcTrace = createRpcPropagationResolver(options); | ||
|
|
||
| return new Proxy(env, { | ||
| get(target, prop, receiver) { | ||
| const item = Reflect.get(target, prop, receiver); | ||
|
|
@@ -91,7 +94,7 @@ export function instrumentEnv<Env extends Record<string, unknown>>(env: Env, opt | |
| return instrumented; | ||
| } | ||
|
|
||
| if (!options?.enableRpcTracePropagation) { | ||
| if (!shouldPropagateRpcTrace(String(prop))) { | ||
| return item; | ||
| } | ||
|
|
||
|
Comment on lines
94
to
100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The module-level Suggested FixThe order of operations should be changed. The Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { stringMatchesSomePattern } from '@sentry/core'; | ||
| import type { CloudflareOptions } from '../client'; | ||
|
|
||
| const PROPAGATE_TO_NONE = () => false; | ||
| const PROPAGATE_TO_ALL = () => true; | ||
|
|
||
| /** | ||
| * Builds the per-binding predicate that decides whether a binding takes part in RPC trace | ||
| * propagation. | ||
| */ | ||
| export function createRpcPropagationResolver(options: CloudflareOptions | undefined): (bindingName: string) => boolean { | ||
| const value: CloudflareOptions['enableRpcTracePropagation'] | undefined = options?.enableRpcTracePropagation; | ||
|
|
||
| if (value === true) { | ||
| return PROPAGATE_TO_ALL; | ||
| } | ||
|
|
||
| if (!Array.isArray(value) || !value.length) { | ||
| return PROPAGATE_TO_NONE; | ||
| } | ||
|
|
||
| // Strings must match a binding name exactly, without this, an entry of `DB` would also enable | ||
| // propagation for a binding named `MY_DB`. Regular expressions still give pattern matching. | ||
| return (bindingName: string) => stringMatchesSomePattern(bindingName, value, true); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createRpcPropagationResolver } from '../../src/utils/rpcPropagation'; | ||
|
|
||
| describe('createRpcPropagationResolver', () => { | ||
| it('propagates to nothing when no options are available', () => { | ||
| const shouldPropagate = createRpcPropagationResolver(undefined); | ||
|
|
||
| expect(shouldPropagate('MY_DO')).toBe(false); | ||
| }); | ||
|
|
||
| it('propagates to nothing when the option is unset', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: undefined }); | ||
|
|
||
| expect(shouldPropagate('MY_DO')).toBe(false); | ||
| expect(shouldPropagate('EXTERNAL')).toBe(false); | ||
| }); | ||
|
|
||
| it('propagates to nothing when the option is `false`', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: false }); | ||
|
|
||
| expect(shouldPropagate('MY_DO')).toBe(false); | ||
| }); | ||
|
|
||
| it('propagates to every binding when the option is `true`', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: true }); | ||
|
|
||
| expect(shouldPropagate('MY_DO')).toBe(true); | ||
| expect(shouldPropagate('EXTERNAL')).toBe(true); | ||
| }); | ||
|
|
||
| it('propagates only to allowlisted binding names', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: ['MY_DO', 'EXTERNAL'] }); | ||
|
|
||
| expect(shouldPropagate('MY_DO')).toBe(true); | ||
| expect(shouldPropagate('EXTERNAL')).toBe(true); | ||
| expect(shouldPropagate('OTHER')).toBe(false); | ||
| }); | ||
|
|
||
| it('propagates to nothing for an empty allowlist', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: [] }); | ||
|
|
||
| expect(shouldPropagate('MY_DO')).toBe(false); | ||
| }); | ||
|
|
||
| it('matches binding names exactly, never as a substring', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: ['DB'] }); | ||
|
|
||
| expect(shouldPropagate('DB')).toBe(true); | ||
| expect(shouldPropagate('MY_DB')).toBe(false); | ||
| expect(shouldPropagate('DB_REPLICA')).toBe(false); | ||
| }); | ||
|
|
||
| it('supports regular expressions for pattern matching', () => { | ||
| const shouldPropagate = createRpcPropagationResolver({ enableRpcTracePropagation: [/^SVC_/] }); | ||
|
|
||
| expect(shouldPropagate('SVC_ORDERS')).toBe(true); | ||
| expect(shouldPropagate('SVC_USERS')).toBe(true); | ||
| expect(shouldPropagate('ORDERS')).toBe(false); | ||
| expect(shouldPropagate('PREFIXED_SVC_ORDERS')).toBe(false); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The integration test for RPC trace propagation incorrectly includes
'SUB_WORKER_NO_PROPAGATION'in theenableRpcTracePropagationallowlist, contradicting the test's goal of verifying behavior for a disabled binding.Severity: LOW
Suggested Fix
To align the test's behavior with its stated purpose, remove
'SUB_WORKER_NO_PROPAGATION'from theenableRpcTracePropagationallowlist within the test's configuration. This will ensure the test correctly validates that trace metadata is not injected when propagation is disabled.Prompt for AI Agent