-
Notifications
You must be signed in to change notification settings - Fork 16
feat(explorers): add support for environment in Sentry (AG-2031) #3943
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
Draft
sagely1
wants to merge
8
commits into
Sage-Bionetworks:main
Choose a base branch
from
sagely1:sentry-environment
base: main
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e08e0e3
remove config.json
sagely1 67e159a
add support for creating config.json
sagely1 2c64db8
adding support for SENTRY_ENVIRONMENT
sagely1 a5741b8
Merge branch 'main' into sentry-environment
sagely1 442ead7
fix unit tests
sagely1 ff8c988
Merge branch 'main' into sentry-environment
sagely1 04b5348
add fallback for sentry environment
sagely1 075e865
fix unit tests
sagely1 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,13 @@ | |
| "cwd": "{projectRoot}" | ||
| } | ||
| }, | ||
| "create-config-json": { | ||
| "executor": "nx:run-commands", | ||
| "options": { | ||
| "command": "node ../../../tools/create-config-json.js", | ||
| "cwd": "{projectRoot}" | ||
| } | ||
| }, | ||
|
Comment on lines
+17
to
+23
Contributor
Author
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. Adds support to generate a config.json with |
||
| "build": { | ||
| "executor": "@nx/angular:application", | ||
| "outputs": ["{options.outputPath}"], | ||
|
|
||
This file was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| { | ||
| "apiDocsUrl": "${API_DOCS_URL}", | ||
| "appVersion": "${APP_VERSION}", | ||
| "commitSha": "${COMMIT_SHA}", | ||
| "csrApiUrl": "${CSR_API_URL}", | ||
| "ssrApiUrl": "${SSR_API_URL}", | ||
| "googleTagManagerId": "${GOOGLE_TAG_MANAGER_ID}", | ||
| "sentryRelease": "${SENTRY_RELEASE}" | ||
| "apiDocsUrl": "${API_DOCS_URL:-http://localhost:8000/api-docs}", | ||
| "appVersion": "${APP_VERSION:-local}", | ||
| "commitSha": "${COMMIT_SHA:-}", | ||
| "csrApiUrl": "${CSR_API_URL:-http://localhost:8000/api/v1}", | ||
| "ssrApiUrl": "${SSR_API_URL:-http://agora-api:3333/v1}", | ||
| "googleTagManagerId": "${GOOGLE_TAG_MANAGER_ID:-}", | ||
| "sentryDSN": "${SENTRY_DSN:-}", | ||
| "sentryEnvironment": "${SENTRY_ENVIRONMENT:-localhost}", | ||
| "sentryRelease": "${SENTRY_RELEASE:-}" | ||
| } |
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| { | ||
| "apiDocsUrl": "${API_DOCS_URL}", | ||
| "appVersion": "${APP_VERSION}", | ||
| "commitSha": "${COMMIT_SHA}", | ||
| "csrApiUrl": "${CSR_API_URL}", | ||
| "googleTagManagerId": "${GOOGLE_TAG_MANAGER_ID}", | ||
| "sentryRelease": "${SENTRY_RELEASE}", | ||
| "ssrApiUrl": "${SSR_API_URL}" | ||
| "apiDocsUrl": "${API_DOCS_URL:-http://localhost:8000/api-docs}", | ||
| "appVersion": "${APP_VERSION:-local}", | ||
| "commitSha": "${COMMIT_SHA:-}", | ||
| "csrApiUrl": "${CSR_API_URL:-http://localhost:8000/api/v1}", | ||
| "ssrApiUrl": "${SSR_API_URL:-http://model-ad-api:3333/v1}", | ||
| "googleTagManagerId": "${GOOGLE_TAG_MANAGER_ID:-}", | ||
| "sentryDSN": "${SENTRY_DSN:-}", | ||
| "sentryEnvironment": "${SENTRY_ENVIRONMENT:-localhost}", | ||
| "sentryRelease": "${SENTRY_RELEASE:-}" | ||
| } |
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
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 |
|---|---|---|
| @@ -1,72 +1,73 @@ | ||
| import * as Sentry from '@sentry/angular'; | ||
| import { getSentryEnvironment, initSentry, SentryConfig } from './init-sentry'; | ||
| import { initSentry, SentryConfig } from './init-sentry'; | ||
|
|
||
| jest.mock('@sentry/angular', () => ({ | ||
| init: jest.fn(), | ||
| })); | ||
|
|
||
| const hostEnvironmentMap: Record<string, string> = { | ||
| 'app-dev.example.com': 'dev', | ||
| 'app-stage.example.com': 'stage', | ||
| 'app.example.com': 'prod', | ||
| }; | ||
|
|
||
| const mockConfig: SentryConfig = { | ||
| dsn: 'https://test-dsn@sentry.io/123', | ||
| hostEnvironmentMap, | ||
| }; | ||
|
|
||
| describe('getSentryEnvironment', () => { | ||
| // The "server" (SSR) path cannot be tested in jsdom since window is always defined, | ||
| // and passing undefined explicitly triggers the default parameter which reads window.location.hostname. | ||
|
|
||
| it('should return "localhost" for localhost', () => { | ||
| expect(getSentryEnvironment(hostEnvironmentMap, 'localhost')).toBe('localhost'); | ||
| describe('initSentry', () => { | ||
| afterEach(() => { | ||
| (Sentry.init as jest.Mock).mockClear(); | ||
| }); | ||
|
|
||
| it('should return "localhost" for 127.0.0.1', () => { | ||
| expect(getSentryEnvironment(hostEnvironmentMap, '127.0.0.1')).toBe('localhost'); | ||
| }); | ||
| it('should initialize Sentry with environment and release', () => { | ||
| initSentry({ ...mockConfig, environment: 'prod', release: '1.0.0+abc1234' }); | ||
|
|
||
| it('should resolve a mapped hostname to its environment', () => { | ||
| expect(getSentryEnvironment(hostEnvironmentMap, 'app-dev.example.com')).toBe('dev'); | ||
| expect(getSentryEnvironment(hostEnvironmentMap, 'app-stage.example.com')).toBe('stage'); | ||
| expect(getSentryEnvironment(hostEnvironmentMap, 'app.example.com')).toBe('prod'); | ||
| expect(Sentry.init).toHaveBeenCalledWith({ | ||
| dsn: mockConfig.dsn, | ||
| environment: 'prod', | ||
| release: '1.0.0+abc1234', | ||
| sendDefaultPii: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('should fall back to the raw hostname when not in the map', () => { | ||
| expect(getSentryEnvironment(hostEnvironmentMap, 'unknown-host.example.com')).toBe( | ||
| 'unknown-host.example.com', | ||
| ); | ||
| }); | ||
| }); | ||
| it('should fallback to window.location.hostname when environment not provided', () => { | ||
| initSentry(mockConfig); | ||
|
|
||
| describe('initSentry', () => { | ||
| afterEach(() => { | ||
| (Sentry.init as jest.Mock).mockClear(); | ||
| expect(Sentry.init).toHaveBeenCalledWith( | ||
| expect.objectContaining({ environment: window.location.hostname }), | ||
| ); | ||
| }); | ||
|
|
||
| it('should initialize Sentry with release from config', () => { | ||
| initSentry({ ...mockConfig, release: '1.0.0+abc1234' }); | ||
| it('should fallback to window.location.hostname when environment is empty string', () => { | ||
| initSentry({ ...mockConfig, environment: '' }); | ||
|
|
||
| expect(Sentry.init).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| dsn: mockConfig.dsn, | ||
| release: '1.0.0+abc1234', | ||
| sendDefaultPii: false, | ||
| }), | ||
| expect.objectContaining({ environment: window.location.hostname }), | ||
| ); | ||
| }); | ||
|
|
||
| it('should initialize Sentry without release when release is not provided', () => { | ||
| it('should initialize Sentry without release when not provided', () => { | ||
| initSentry(mockConfig); | ||
|
|
||
| expect(Sentry.init).toHaveBeenCalledWith(expect.objectContaining({ release: undefined })); | ||
| }); | ||
|
|
||
| it('should initialize Sentry without release when release is empty string', () => { | ||
| it('should initialize Sentry without release when empty string', () => { | ||
| initSentry({ ...mockConfig, release: '' }); | ||
|
|
||
| expect(Sentry.init).toHaveBeenCalledWith(expect.objectContaining({ release: undefined })); | ||
| }); | ||
|
|
||
| it('should warn and not initialize Sentry when dsn is not provided', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(); | ||
| initSentry({}); | ||
|
|
||
| expect(console.warn).toHaveBeenCalledWith('Sentry DSN is not configured.'); | ||
| expect(Sentry.init).not.toHaveBeenCalled(); | ||
| warnSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('should warn and not initialize Sentry when dsn is empty string', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(); | ||
| initSentry({ dsn: '' }); | ||
|
|
||
| expect(console.warn).toHaveBeenCalledWith('Sentry DSN is not configured.'); | ||
| expect(Sentry.init).not.toHaveBeenCalled(); | ||
| warnSpy.mockRestore(); | ||
| }); | ||
| }); |
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
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,32 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Generates config.json from config.json.template by substituting environment variables. | ||
| * | ||
| * Supports POSIX shell parameter expansion syntax: ${VAR:-default} | ||
| * - If VAR is set, uses its value | ||
| * - If VAR is unset or empty, uses the default value | ||
| * | ||
| * Usage: node tools/create-config-json.js | ||
| * (Run from project root, e.g., apps/agora/app) | ||
| */ | ||
|
|
||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
|
|
||
| const templatePath = path.join(process.cwd(), 'src/config/config.json.template'); | ||
| const outputPath = path.join(process.cwd(), 'src/config/config.json'); | ||
|
|
||
| if (!fs.existsSync(templatePath)) { | ||
| console.error(`Template not found: ${templatePath}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const template = fs.readFileSync(templatePath, 'utf8'); | ||
|
|
||
| const result = template.replace(/\${([^}]+)}/g, (_, expr) => { | ||
| const [varName, defaultVal] = expr.split(':-'); | ||
| return process.env[varName] || defaultVal || ''; | ||
| }); | ||
|
|
||
| fs.writeFileSync(outputPath, result); | ||
| console.log(`Created ${outputPath}`); |
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.
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.
Adding config.json to .gitignore to follow best practices