-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cloudflare): Read SENTRY_RELEASE
from env
#16201
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
AbhiPrasad
merged 6 commits into
develop
from
lms/feat-cloudflare-sentry-release-from-env
May 7, 2025
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fd592c6
feat(cloudflare): Read `SENTRY_RELEASE` from `env`
Lms24 49f8f76
ref and extract options merging helper, add to durable objects
Lms24 e49219f
simplify getFinalOptions
Lms24 858134d
lint
Lms24 04a1c54
Merge branch 'develop' into lms/feat-cloudflare-sentry-release-from-env
AbhiPrasad e0c18f6
Update packages/cloudflare/tsconfig.json
Lms24 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
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
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
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 @@ | ||
import type { CloudflareOptions } from './client'; | ||
|
||
/** | ||
* Merges the options passed in from the user with the options we read from | ||
* the Cloudflare `env` environment variable object. | ||
* | ||
* @param userOptions - The options passed in from the user. | ||
* @param env - The environment variables. | ||
* | ||
* @returns The final options. | ||
*/ | ||
export function getFinalOptions(userOptions: CloudflareOptions, env: unknown): CloudflareOptions { | ||
if (typeof env !== 'object' || env === null) { | ||
return userOptions; | ||
} | ||
|
||
const release = 'SENTRY_RELEASE' in env && typeof env.SENTRY_RELEASE === 'string' ? env.SENTRY_RELEASE : undefined; | ||
|
||
return { release, ...userOptions }; | ||
} |
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 |
---|---|---|
|
@@ -10,6 +10,7 @@ import { withSentry } from '../src/handler'; | |
|
||
const MOCK_ENV = { | ||
SENTRY_DSN: 'https://[email protected]/1337', | ||
SENTRY_RELEASE: '1.1.1', | ||
}; | ||
|
||
describe('withSentry', () => { | ||
|
@@ -51,6 +52,65 @@ describe('withSentry', () => { | |
|
||
expect(result).toBe(response); | ||
}); | ||
|
||
test('merges options from env and callback', async () => { | ||
const handler = { | ||
fetch(_request, _env, _context) { | ||
throw new Error('test'); | ||
}, | ||
} satisfies ExportedHandler<typeof MOCK_ENV>; | ||
|
||
let sentryEvent: Event = {}; | ||
|
||
const wrappedHandler = withSentry( | ||
env => ({ | ||
dsn: env.SENTRY_DSN, | ||
beforeSend(event) { | ||
sentryEvent = event; | ||
return null; | ||
}, | ||
}), | ||
handler, | ||
); | ||
|
||
try { | ||
await wrappedHandler.fetch(new Request('https://example.com'), MOCK_ENV, createMockExecutionContext()); | ||
} catch { | ||
// ignore | ||
} | ||
|
||
expect(sentryEvent.release).toEqual('1.1.1'); | ||
}); | ||
|
||
test('callback options take precedence over env options', async () => { | ||
const handler = { | ||
fetch(_request, _env, _context) { | ||
throw new Error('test'); | ||
}, | ||
} satisfies ExportedHandler<typeof MOCK_ENV>; | ||
|
||
let sentryEvent: Event = {}; | ||
|
||
const wrappedHandler = withSentry( | ||
env => ({ | ||
dsn: env.SENTRY_DSN, | ||
release: '2.0.0', | ||
beforeSend(event) { | ||
sentryEvent = event; | ||
return null; | ||
}, | ||
}), | ||
handler, | ||
); | ||
|
||
try { | ||
await wrappedHandler.fetch(new Request('https://example.com'), MOCK_ENV, createMockExecutionContext()); | ||
} catch { | ||
// ignore | ||
} | ||
|
||
expect(sentryEvent.release).toEqual('2.0.0'); | ||
}); | ||
}); | ||
|
||
describe('scheduled handler', () => { | ||
|
@@ -70,6 +130,55 @@ describe('withSentry', () => { | |
expect(optionsCallback).toHaveBeenLastCalledWith(MOCK_ENV); | ||
}); | ||
|
||
test('merges options from env and callback', async () => { | ||
const handler = { | ||
scheduled(_controller, _env, _context) { | ||
SentryCore.captureMessage('cloud_resource'); | ||
return; | ||
}, | ||
} satisfies ExportedHandler<typeof MOCK_ENV>; | ||
|
||
let sentryEvent: Event = {}; | ||
const wrappedHandler = withSentry( | ||
env => ({ | ||
dsn: env.SENTRY_DSN, | ||
beforeSend(event) { | ||
sentryEvent = event; | ||
return null; | ||
}, | ||
}), | ||
handler, | ||
); | ||
await wrappedHandler.scheduled(createMockScheduledController(), MOCK_ENV, createMockExecutionContext()); | ||
|
||
expect(sentryEvent.release).toBe('1.1.1'); | ||
}); | ||
|
||
test('callback options take precedence over env options', async () => { | ||
const handler = { | ||
scheduled(_controller, _env, _context) { | ||
SentryCore.captureMessage('cloud_resource'); | ||
return; | ||
}, | ||
} satisfies ExportedHandler<typeof MOCK_ENV>; | ||
|
||
let sentryEvent: Event = {}; | ||
const wrappedHandler = withSentry( | ||
env => ({ | ||
dsn: env.SENTRY_DSN, | ||
release: '2.0.0', | ||
beforeSend(event) { | ||
sentryEvent = event; | ||
return null; | ||
}, | ||
}), | ||
handler, | ||
); | ||
await wrappedHandler.scheduled(createMockScheduledController(), MOCK_ENV, createMockExecutionContext()); | ||
|
||
expect(sentryEvent.release).toEqual('2.0.0'); | ||
}); | ||
|
||
test('flushes the event after the handler is done using the cloudflare context.waitUntil', async () => { | ||
const handler = { | ||
scheduled(_controller, _env, _context) { | ||
|
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 { describe, expect, it } from 'vitest'; | ||
import { getFinalOptions } from '../src/options'; | ||
|
||
describe('getFinalOptions', () => { | ||
it('returns user options when env is not an object', () => { | ||
const userOptions = { dsn: 'test-dsn', release: 'test-release' }; | ||
const env = 'not-an-object'; | ||
|
||
const result = getFinalOptions(userOptions, env); | ||
|
||
expect(result).toEqual(userOptions); | ||
}); | ||
|
||
it('returns user options when env is null', () => { | ||
const userOptions = { dsn: 'test-dsn', release: 'test-release' }; | ||
const env = null; | ||
|
||
const result = getFinalOptions(userOptions, env); | ||
|
||
expect(result).toEqual(userOptions); | ||
}); | ||
|
||
it('merges options from env with user options', () => { | ||
const userOptions = { dsn: 'test-dsn', release: 'user-release' }; | ||
const env = { SENTRY_RELEASE: 'env-release' }; | ||
|
||
const result = getFinalOptions(userOptions, env); | ||
|
||
expect(result).toEqual({ dsn: 'test-dsn', release: 'user-release' }); | ||
}); | ||
|
||
it('uses user options when SENTRY_RELEASE exists but is not a string', () => { | ||
const userOptions = { dsn: 'test-dsn', release: 'user-release' }; | ||
const env = { SENTRY_RELEASE: 123 }; | ||
|
||
const result = getFinalOptions(userOptions, env); | ||
|
||
expect(result).toEqual(userOptions); | ||
}); | ||
|
||
it('uses user options when SENTRY_RELEASE does not exist', () => { | ||
const userOptions = { dsn: 'test-dsn', release: 'user-release' }; | ||
const env = { OTHER_VAR: 'some-value' }; | ||
|
||
const result = getFinalOptions(userOptions, env); | ||
|
||
expect(result).toEqual(userOptions); | ||
}); | ||
|
||
it('takes user options over env options', () => { | ||
const userOptions = { dsn: 'test-dsn', release: 'user-release' }; | ||
const env = { SENTRY_RELEASE: 'env-release' }; | ||
|
||
const result = getFinalOptions(userOptions, env); | ||
|
||
expect(result).toEqual(userOptions); | ||
}); | ||
}); |
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.
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.