-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): Add
sentryAstro
integration (#9218)
Add the `sentryAstro` Astro integration which will be the core component of the Astro SDK. The integration takes care of: * injecting SDK init code into the client and server entry points. * emitting source maps and adding the Sentry Vite plugin for source maps upload The idea is: Basic SDK setup will only require `sentryAstro` to be added by users. Everything else happens automatically. We also support a more custom setup but it will require additional config files. The reason is that we can only inject serialized code. We cannot take e.g. function callbacks (e.g. `beforeSend`) in the options of `sentryAstro`. --------- Co-authored-by: Francesco Novy <francesco.novy@sentry.io>
- Loading branch information
Showing
19 changed files
with
638 additions
and
22 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
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,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 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
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 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 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
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 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.