-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(astro): Add sentryAstro
integration
#9218
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b74d7cc
feat(astro): Add `sentryAstro` integration
Lms24 f8bbf8f
lint
Lms24 65c934b
Update packages/astro/README.md
Lms24 47e7385
extract common init options snippet builder
Lms24 97581c8
remove org from readme (no need with org tokens)
Lms24 b752a2b
new sourcemaps API
Lms24 fd29a63
fix server file import
Lms24 811fd0b
add tests
Lms24 306c97e
build astro integration with server entry point
Lms24 c23cb61
adjust tests
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# The paths in this file are specified so that they align with the file structure in `./build` after this file is copied | ||
# into it by the prepack script `scripts/prepack.ts`. | ||
|
||
* | ||
|
||
!/cjs/**/* | ||
!/esm/**/* | ||
!/types/**/* | ||
!/types-ts3.8/**/* | ||
!/integration/**/* |
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,12 +1,17 @@ | ||
import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'; | ||
|
||
export default makeNPMConfigVariants( | ||
const variants = makeNPMConfigVariants( | ||
makeBaseNPMConfig({ | ||
entrypoints: ['src/index.server.ts', 'src/index.client.ts'], | ||
packageSpecificConfig: { | ||
output: { | ||
dynamicImportInCjs: true, | ||
exports: 'named', | ||
}, | ||
}, | ||
// Astro is Node 18+ no need to add polyfills | ||
addPolyfills: false, | ||
}), | ||
); | ||
|
||
export default variants; |
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 @@ | ||
/* eslint-disable no-console */ | ||
|
||
import * as fse from 'fs-extra'; | ||
import * as path from 'path'; | ||
|
||
const buildDir = path.resolve('build'); | ||
const srcIntegrationDir = path.resolve(path.join('src', 'integration')); | ||
const destIntegrationDir = path.resolve(path.join(buildDir, 'integration')); | ||
|
||
try { | ||
fse.copySync(srcIntegrationDir, destIntegrationDir, { | ||
filter: (src, _) => { | ||
return !src.endsWith('.md'); | ||
}, | ||
}); | ||
console.log('\nCopied Astro integration to ./build/integration\n'); | ||
} catch (e) { | ||
console.error('\nError while copying integration to build dir:'); | ||
console.error(e); | ||
} |
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,81 @@ | ||
/* eslint-disable no-console */ | ||
import { sentryVitePlugin } from '@sentry/vite-plugin'; | ||
import type { AstroIntegration } from 'astro'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets'; | ||
import type { SentryOptions } from './types'; | ||
|
||
const PKG_NAME = '@sentry/astro'; | ||
|
||
export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { | ||
return { | ||
name: PKG_NAME, | ||
hooks: { | ||
'astro:config:setup': async ({ updateConfig, injectScript }) => { | ||
// The third param here enables loading of all env vars, regardless of prefix | ||
// see: https://main.vitejs.dev/config/#using-environment-variables-in-config | ||
|
||
// TODO: Ideally, we want to load the environment with vite like this: | ||
// const env = loadEnv('production', process.cwd(), ''); | ||
// However, this currently throws a build error. | ||
// Will revisit this later. | ||
const env = process.env; | ||
|
||
const uploadOptions = options.sourceMapsUploadOptions || {}; | ||
|
||
const shouldUploadSourcemaps = uploadOptions?.enabled ?? true; | ||
const authToken = uploadOptions.authToken || env.SENTRY_AUTH_TOKEN; | ||
|
||
if (shouldUploadSourcemaps && authToken) { | ||
updateConfig({ | ||
vite: { | ||
build: { | ||
sourcemap: true, | ||
}, | ||
plugins: [ | ||
sentryVitePlugin({ | ||
org: uploadOptions.org ?? env.SENTRY_ORG, | ||
project: uploadOptions.project ?? env.SENTRY_PROJECT, | ||
authToken: uploadOptions.authToken ?? env.SENTRY_AUTH_TOKEN, | ||
telemetry: uploadOptions.telemetry ?? true, | ||
}), | ||
], | ||
}, | ||
}); | ||
} | ||
|
||
const pathToClientInit = options.clientInitPath | ||
? path.resolve(options.clientInitPath) | ||
: findDefaultSdkInitFile('client'); | ||
const pathToServerInit = options.serverInitPath | ||
? path.resolve(options.serverInitPath) | ||
: findDefaultSdkInitFile('server'); | ||
|
||
if (pathToClientInit) { | ||
options.debug && console.log(`[sentry-astro] Using ${pathToClientInit} for client init.`); | ||
injectScript('page', buildSdkInitFileImportSnippet(pathToClientInit)); | ||
} else { | ||
options.debug && console.log('[sentry-astro] Using default client init.'); | ||
injectScript('page', buildClientSnippet(options || {})); | ||
} | ||
|
||
if (pathToServerInit) { | ||
options.debug && console.log(`[sentry-astro] Using ${pathToServerInit} for server init.`); | ||
injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit)); | ||
} else { | ||
options.debug && console.log('[sentry-astro] Using default server init.'); | ||
injectScript('page-ssr', buildServerSnippet(options || {})); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
function findDefaultSdkInitFile(type: 'server' | 'client'): string | undefined { | ||
const fileExtensions = ['ts', 'js', 'tsx', 'jsx', 'mjs', 'cjs', 'mts']; | ||
return fileExtensions | ||
.map(ext => path.resolve(path.join(process.cwd(), `sentry.${type}.config.${ext}`))) | ||
.find(filename => fs.existsSync(filename)); | ||
} |
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,45 @@ | ||
import type { SentryOptions } from './types'; | ||
|
||
/** | ||
* Creates a snippet that imports a Sentry.init file. | ||
*/ | ||
export function buildSdkInitFileImportSnippet(filePath: string): string { | ||
return `import "${filePath}";`; | ||
} | ||
|
||
/** | ||
* Creates a snippet that initializes Sentry on the client by choosing | ||
* default options. | ||
*/ | ||
export function buildClientSnippet(options: SentryOptions): string { | ||
return `import * as Sentry from "@sentry/astro"; | ||
|
||
Sentry.init({ | ||
${buildCommonInitOptions(options)} | ||
integrations: [new Sentry.BrowserTracing(), new Sentry.Replay()], | ||
replaysSessionSampleRate: ${options.replaysSessionSampleRate ?? 0.1}, | ||
replaysOnErrorSampleRate: ${options.replaysOnErrorSampleRate ?? 1.0}, | ||
});`; | ||
} | ||
|
||
/** | ||
* Creates a snippet that initializes Sentry on the server by choosing | ||
* default options. | ||
*/ | ||
export function buildServerSnippet(options: SentryOptions): string { | ||
return `import * as Sentry from "@sentry/astro"; | ||
|
||
Sentry.init({ | ||
${buildCommonInitOptions(options)} | ||
});`; | ||
} | ||
|
||
const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${ | ||
options.dsn ? JSON.stringify(options.dsn) : 'import.meta.env.PUBLIC_SENTRY_DSN' | ||
}, | ||
debug: ${options.debug ? true : false}, | ||
environment: ${options.environment ? JSON.stringify(options.environment) : 'import.meta.env.PUBLIC_VERCEL_ENV'}, | ||
release: ${options.release ? JSON.stringify(options.release) : 'import.meta.env.PUBLIC_VERCEL_GIT_COMMIT_SHA'}, | ||
tracesSampleRate: ${options.tracesSampleRate ?? 1.0},${ | ||
options.sampleRate ? `\n sampleRate: ${options.sampleRate},` : '' | ||
}`; |
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.