Integrating Sentry
🎯 Introduction
We have implemented error collection and reporting manually. In production, however, building a receiving service, grouping errors, and configuring notifications adds considerable maintenance. A common alternative is an established monitoring platform such as Sentry. Install its SDK to capture and report errors automatically, inspect errors, affected users, and stack traces in a dashboard, and configure notifications.
By the end of this article, you will be able to:
- Initialize Sentry in a Vue 3 project and report a test error to verify the flow.
- Upload source maps to map minified stack traces back to source locations.
- Configure a basic alert rule to receive timely error notifications.
🧱 What Is Sentry?
Sentry has two parts: an SDK in your application that automatically captures runtime errors, unhandled promise rejections, and more; and a backend service that receives data, groups matching errors into an issue, displays their impact and full stack traces, and provides alerts. Think of a managed security company: you install cameras (the SDK), footage goes to its service, and it organizes recordings and calls you about anomalies (alerts). You do not have to store and review the recordings yourself.
🧱 Setting Up Sentry
Setup has two steps: create a project in the dashboard to get a DSN, then initialize the SDK in code. Sign up or sign in on Sentry's website, create a project, and select Vue as the platform. You will receive a DSN, which combines the reporting endpoint and project identifier, such as https://xxxxx@o123.ingest.sentry.io/456.
Install the SDK and initialize it at the application entry point. This example was verified with Node.js v22 and @sentry/vue v9.x:
pnpm add @sentry/vue
import { createApp } from 'vue';
import * as Sentry from '@sentry/vue';
import App from './App.vue';
const app = createApp(App);
Sentry.init({
app, // Pass the Vue application instance to capture component errors automatically
dsn: 'https://xxxxx@o123.ingest.sentry.io/456', // Replace with your DSN
environment: import.meta.env.MODE, // Distinguish development and production
});
app.mount('#app');
Pass app so the SDK can integrate with Vue's error handling and capture component errors. To verify the flow, temporarily add a button that runs throw new Error('Sentry test error'). After clicking it, check that the error appears in Sentry's Issues list, then remove the button.
💡 Separating Environments and Controlling Volume
Development errors can overwhelm production data if reported together. Two common approaches are tagging events with environment for dashboard filtering, as configured above, and enabling the SDK only in production. Local errors still appear in the console, and you save event quota:
if (import.meta.env.PROD) {
Sentry.init({
app,
dsn: 'https://xxxxx@o123.ingest.sentry.io/456',
environment: import.meta.env.MODE,
});
}
import.meta.env.MODE and import.meta.env.PROD are environment variables provided by Vite. See the Vite course.🧱 Source Maps and Error Locations
Initially, stack traces may show assets/index-Bx3kP9.js:1:18432 rather than source locations: production runs bundled, minified code, so error positions refer to that output. A source map generated during the build maps those positions back to source code. Upload it to Sentry to resolve stack traces automatically. The following upload-plugin example was verified with Node.js v22 and @sentry/vite-plugin v3.x:
pnpm add -D @sentry/vite-plugin
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import { sentryVitePlugin } from '@sentry/vite-plugin';
export default defineConfig({
plugins: [
vue(),
// Place last to upload this build's source maps after the build completes
sentryVitePlugin({
org: 'your-organization',
project: 'your-project',
authToken: process.env.SENTRY_AUTH_TOKEN, // Use an environment variable; do not hardcode
}),
],
build: {
sourcemap: true, // Generate source maps during the Vite build
},
});
Two settings matter: build.sourcemap: true generates the maps the plugin uploads, and authToken authenticates the upload. Generate the token in Sentry and inject it through an environment variable; never commit it. Rebuild and deploy, and newly reported traces should show source locations such as src/views/User.vue:42.
.map files from deployed assets using the plugin's corresponding option, leaving them available only to Sentry.🧱 Alerts and Notifications
Collecting data is only the first step; monitoring also needs to tell you promptly when something fails. Sentry alert rules combine trigger conditions and notification channels. Create a rule on your project's Alerts page:
- Trigger conditions: start with a notification when a new issue appears to catch new errors after launch. You can also trigger when an existing issue's event count exceeds a threshold within a time window.
- Notification channels: email, or integrations with Slack, DingTalk, or Feishu bots to post in team channels.
More alerts are not always better. Notifying the team about every small error quickly causes desensitization, allowing serious problems to be missed. Start with restrained rules for new issues and error spikes, observe them for a week or two, and refine them based on false positives. The next article, Logging and Alerts, explores alert strategy further.
❓ Questions and Answers
Q1: Is an exposed DSN dangerous?
The DSN is already visible in frontend code and is not a secret credential. Others could use it to submit junk data; configuring allowed domains in Sentry helps mitigate this. The source map upload authToken is the credential you must keep secret.
Q2: Do I still need my own window.onerror after adding Sentry?
Usually not. The SDK covers common error scenarios, and additional handlers can create duplicate reports. You still need custom business event tracking and collection logic outside the SDK's scope.
Q3: Why don't I see reports during local development?
Two common causes are initializing Sentry only in production, or a browser ad blocker blocking requests. Temporarily remove the environment condition and inspect the Network panel to check whether requests are sent.
Q4: What if stack traces are still minified?
First confirm that source maps were uploaded. Then check whether build.sourcemap is enabled, whether the plugin's org, project, and authToken are correct, and whether the maps and deployed code come from the same build. Mismatched versions also prevent resolution.
🧪 Exercises
Exercise 1: Complete the entry code to enable Sentry only in production and include the environment tag.
import { createApp } from 'vue';
import * as Sentry from '@sentry/vue';
import App from './App.vue';
const app = createApp(App);
// Write your code here: initialize Sentry only in production and set environment
app.mount('#app');
Exercise 2: A developer sees assets/index-Bx3kP9.js:1:18432 in Sentry and cannot locate the original source. Write the two configuration settings that may be missing and explain each one's purpose:
// Write code or an explanation here: first missing setting
// Write code or an explanation here: second missing setting
Afterward, consider the risks of hardcoding authToken in vite.config.js and committing it to the repository.
🎉 You now know how to integrate Sentry! The final article, Logging and Alerts, completes the course with log levels and alert strategies.
