Mario Brusarosco

sentry multi environment setup

In the ground since Sun Oct 26 2025

Last watered inSun Oct 26 2025

Related Topics

Referece Links

Sentry Multi-Environment Setup

Use one Sentry project to monitor multiple environments (demo, staging, production) through environment tagging. Unified monitoring with clear separation.

Benefits

Unified Dashboard - All errors in one place
Easy Comparison - Compare error rates across environments
Cost Effective - Save on Sentry quota
Simplified Management - One project to configure
Environment Filtering - Filter by environment in Sentry UI

Architecture Overview

1┌─────────────────────────────────────────────┐
2│ Sentry Project: "api-best-shot" │
3├─────────────────────────────────────────────┤
4│ ┌────────┐ ┌────────┐ ┌────────────┐ │
5│ │ demo │ │staging │ │ production │ │
6│ └────────┘ └────────┘ └────────────┘ │
7│ │
8│ Same DSN, Different Environment Tags │
9└─────────────────────────────────────────────┘

Quick Setup

1. Create Sentry Project

  1. Visit Sentry.io
  2. Create new project → Choose Node.js
  3. Name: api-best-shot-non-production
  4. Copy the DSN

2. Configure Environment Variables

.env.demo

1NODE_ENV=demo
2SENTRY_DSN=https://your-key@o123456.ingest.sentry.io/789012

.env.staging

1NODE_ENV=staging
2SENTRY_DSN=https://your-key@o123456.ingest.sentry.io/789012

3. Initialize Sentry

1// src/services/profiling/sentry-instrument.ts
2import * as Sentry from "@sentry/node";
3
4Sentry.init({
5 dsn: process.env.SENTRY_DSN,
6 environment: process.env.NODE_ENV, // 'demo', 'staging', or 'production'
7 tracesSampleRate: 1.0,
8});

Filtering by Environment

In Sentry UI

  1. Navigate to Issues or Performance
  2. Use environment dropdown filter
  3. Select: demo, staging, or production
  4. Compare data across environments

Environment-Specific Alerts

1Alert Rule: High Error Rate in Staging
2Conditions:
3 - Environment: staging
4 - Error count > 10 in 5 minutes
5Actions:
6 - Send Slack notification

Deployment Configuration

GitHub Actions

1# .github/workflows/deploy-demo.yml
2- name: Deploy to Demo
3 env:
4 NODE_ENV: demo
5 SENTRY_DSN: ${{ secrets.SENTRY_DSN_NON_PROD }}

AWS Lambda

1aws lambda update-function-configuration \
2 --function-name your-demo-function \
3 --environment Variables='{
4 "NODE_ENV":"demo",
5 "SENTRY_DSN":"https://your-key@o123456.ingest.sentry.io/789012"
6 }'

Best Practices

Separate Production

  • Non-Production Project: demo + staging
  • Production Project: production only

Environment-Specific Sample Rates

Adjust sample rates based on traffic volume:

1const tracesSampleRate =
2 ENV === "production"
3 ? 0.1 // 10% in production
4 : ENV === "staging"
5 ? 0.5 // 50% in staging
6 : 1.0; // 100% in demo
7
8Sentry.init({
9 dsn: process.env.SENTRY_DSN,
10 environment: ENV,
11 tracesSampleRate,
12});

Release Tracking

Track deployed versions for better debugging:

1Sentry.init({
2 dsn: process.env.SENTRY_DSN,
3 environment: ENV,
4 release: `api-best-shot@${process.env.VERSION}`,
5});

Additional Context Tags

1Sentry.setTag("deployment_region", "us-east-1");
2Sentry.setTag("service_version", "1.2.3");

Troubleshooting

Errors Not Appearing

Check:

  1. SENTRY_DSN is set correctly
  2. NODE_ENV matches expected value
  3. ✅ Sentry initialization happens before app code
  4. ✅ Network connectivity to Sentry

Debug:

1// Add to initialization
2console.log("Sentry Environment:", process.env.NODE_ENV);
3console.log("Sentry DSN:", process.env.SENTRY_DSN ? "Set" : "Missing");

Wrong Environment Tag

Verify environment variable is passed correctly:

1# Test locally
2NODE_ENV=demo node your-app.js

Too Many Events

1Sentry.init({
2 dsn: process.env.SENTRY_DSN,
3 environment: ENV,
4 beforeSend(event) {
5 // Filter out connection errors
6 if (event.message?.includes("ECONNREFUSED")) {
7 return null; // Don't send to Sentry
8 }
9 return event;
10 },
11});

Key Takeaways

  • Use one DSN for multiple environments
  • Set environment tag via NODE_ENV
  • Filter by environment in Sentry UI
  • Create environment-specific alerts
  • Consider separate project for production
  • Adjust sample rates per environment