-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use compile time environment variables for Sentry
The dev/prod division belongs in `environment`, we will bake the cli/action distinction as `entrypoint`
- Loading branch information
Showing
5 changed files
with
105 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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,47 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import chalk from 'chalk'; | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { filterBreadcrumb, filterErrorEvent } from './errorMonitoring'; | ||
|
||
const redError = chalk.red('error'); | ||
const blueMessage = chalk.blue('message'); | ||
|
||
describe('filterErrorEvent', () => { | ||
it('removes ANSI from error.message', () => { | ||
const actual = filterErrorEvent({ message: redError } as Sentry.ErrorEvent); | ||
expect(actual.message).toBe('error'); | ||
}); | ||
|
||
it('remove ANSI from exceptions', () => { | ||
const actual = filterErrorEvent({ | ||
exception: { values: [{ value: redError }, { value: redError }] }, | ||
} as Sentry.ErrorEvent); | ||
expect(actual.exception.values[0].value).toBe('error'); | ||
expect(actual.exception.values[1].value).toBe('error'); | ||
}); | ||
}); | ||
|
||
describe('filterBreadcrumb', () => { | ||
it('does nothing with non-console breadcrumbs', () => { | ||
const breadcrumb = { category: 'http', message: blueMessage } as Sentry.Breadcrumb; | ||
const actual = filterBreadcrumb(breadcrumb); | ||
expect(actual).toBe(breadcrumb); | ||
}); | ||
|
||
it('removes ANSI from console breadcrumb messages', () => { | ||
const actual = filterBreadcrumb({ | ||
category: 'console', | ||
message: blueMessage, | ||
} as Sentry.Breadcrumb); | ||
expect(actual.message).toBe('message'); | ||
}); | ||
|
||
it('returns null for empty console breadcrumbs', () => { | ||
const actual = filterBreadcrumb({ | ||
category: 'console', | ||
message: '', | ||
} as Sentry.Breadcrumb); | ||
expect(actual).toBeNull(); | ||
}); | ||
}); |
This file contains 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,15 +1,62 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import stripAnsi from 'strip-ansi'; | ||
|
||
/** | ||
* Remove ANSI escape codes from Sentry ErrorEvents. | ||
* | ||
* @param event An event containing a captured exception. | ||
* | ||
* @returns The modified event. | ||
*/ | ||
export function filterErrorEvent(event: Sentry.ErrorEvent) { | ||
// Remove ANSI escape codes from error messages | ||
if (event?.message) { | ||
event.message = stripAnsi(event.message); | ||
} | ||
// And from exception messages | ||
if (event?.exception?.values) { | ||
for (const [index, exception] of event.exception.values.entries()) { | ||
event.exception.values[index].value = stripAnsi(exception.value); | ||
} | ||
} | ||
return event; | ||
} | ||
|
||
/** | ||
* Remove ANSI escape codes from console breadcrumbs, and skip breadcrumbs for empty lines. | ||
* | ||
* @param breadcrumb A breadcrumb captured by Sentry. | ||
* | ||
* @returns The modified breadcrumb, or `null` when it is not desired. | ||
*/ | ||
export function filterBreadcrumb(breadcrumb: Sentry.Breadcrumb) { | ||
if (breadcrumb?.category === 'console') { | ||
// Do not send breadcrumbs for console newlines | ||
if (breadcrumb.message === '') { | ||
// eslint-disable-next-line unicorn/no-null | ||
return null; | ||
} | ||
// Otherwise remove ANSI escape codes | ||
if (breadcrumb?.message) { | ||
breadcrumb.message = stripAnsi(breadcrumb.message); | ||
} | ||
} | ||
return breadcrumb; | ||
} | ||
|
||
Sentry.init({ | ||
dsn: 'https://4fa173db2ef3fb073b8ea153a5466d28@o4504181686599680.ingest.us.sentry.io/4507930289373184', | ||
sampleRate: 1, | ||
environment: process.env.CI && process.env.GITHUB_RUN_ID ? 'action' : 'cli', | ||
environment: process.env.SENTRY_ENVIRONMENT, | ||
enabled: process.env.DISABLE_ERROR_MONITORING !== 'true', | ||
enableTracing: false, | ||
integrations: [], | ||
initialScope: { | ||
tags: { | ||
entrypoint: process.env.CI && process.env.GITHUB_RUN_ID ? 'action' : 'cli', | ||
version: process.env.npm_package_version, | ||
}, | ||
}, | ||
beforeSend: filterErrorEvent, | ||
beforeBreadcrumb: filterBreadcrumb, | ||
}); |
This file contains 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 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 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