Monitoring Expert

The Monitoring Expert agent specializes in application monitoring, error tracking, logging, alerting, and observability for production systems.

Expertise Areas#

  • Observability Pillars - Logs, metrics, and traces
  • Error Tracking - Sentry integration and error management
  • Logging Best Practices - Structured logging with Pino
  • Metrics Collection - PostHog and analytics
  • Alerting Configuration - Alert levels and channels
  • Health Checks - System health monitoring
  • Dashboard Design - Metrics visualization

Usage Examples#

Sentry Setup#

Use the monitoring-expert agent to set up Sentry error tracking for a Next.js application.

Response includes:

  • SDK initialization
  • Error boundary components
  • User context setup
  • Breadcrumb tracking

Logging System#

Use the monitoring-expert agent to implement structured logging with Pino.

Response includes:

  • Logger configuration
  • Context-aware logging
  • Request logging middleware
  • Log levels usage

Health Checks#

Use the monitoring-expert agent to implement comprehensive health check endpoints.

Response includes:

  • Health endpoint structure
  • Dependency checking
  • Status aggregation
  • Response formatting

Best Practices Applied#

1. Error Tracking#

  • Capture all exceptions
  • Add user context
  • Include breadcrumbs
  • Filter noise

2. Logging#

  • Structured JSON format
  • Appropriate log levels
  • Context propagation
  • Sensitive data filtering

3. Alerting#

  • Severity-based routing
  • Actionable alerts
  • Alert fatigue prevention
  • Clear runbooks

4. Observability#

  • Request tracing
  • Performance monitoring
  • Business metrics
  • SLO tracking

Common Patterns#

Sentry Configuration#

1// lib/sentry.ts 2import * as Sentry from '@sentry/nextjs'; 3 4Sentry.init({ 5 dsn: process.env.SENTRY_DSN, 6 environment: process.env.NODE_ENV, 7 tracesSampleRate: 1.0, 8 release: process.env.VERCEL_GIT_COMMIT_SHA, 9 10 beforeSend(event) { 11 if (process.env.NODE_ENV === 'development') { 12 return null; 13 } 14 return event; 15 }, 16 17 ignoreErrors: [ 18 'ResizeObserver loop', 19 'Non-Error promise rejection', 20 ], 21}); 22 23export function captureError(error: Error, context?: Record<string, any>) { 24 Sentry.captureException(error, { extra: context }); 25} 26 27export function setUser(user: { id: string; email: string }) { 28 Sentry.setUser(user); 29}

Structured Logging#

1// lib/logger.ts 2import pino from 'pino'; 3 4const logger = pino({ 5 level: process.env.LOG_LEVEL || 'info', 6 transport: process.env.NODE_ENV === 'development' 7 ? { target: 'pino-pretty' } 8 : undefined, 9 formatters: { 10 level: (label) => ({ level: label }), 11 }, 12 base: { 13 env: process.env.NODE_ENV, 14 version: process.env.npm_package_version, 15 }, 16}); 17 18export function createLogger(context: string) { 19 return logger.child({ context }); 20} 21 22// Usage 23const log = createLogger('api'); 24log.info({ userId }, 'User logged in'); 25log.error({ error, stack: error.stack }, 'Request failed');

Health Check Endpoint#

1// app/api/health/route.ts 2import { prisma } from '@/lib/db'; 3import { redis } from '@/lib/redis'; 4 5interface HealthStatus { 6 status: 'healthy' | 'degraded' | 'unhealthy'; 7 checks: Record<string, { status: 'pass' | 'fail'; latency?: number }>; 8 timestamp: string; 9} 10 11async function checkDatabase() { 12 const start = Date.now(); 13 try { 14 await prisma.$queryRaw`SELECT 1`; 15 return { status: 'pass' as const, latency: Date.now() - start }; 16 } catch (error) { 17 return { status: 'fail' as const, message: error.message }; 18 } 19} 20 21async function checkRedis() { 22 const start = Date.now(); 23 try { 24 await redis.ping(); 25 return { status: 'pass' as const, latency: Date.now() - start }; 26 } catch (error) { 27 return { status: 'fail' as const, message: error.message }; 28 } 29} 30 31export async function GET() { 32 const checks = { 33 database: await checkDatabase(), 34 redis: await checkRedis(), 35 }; 36 37 const allPassing = Object.values(checks).every(c => c.status === 'pass'); 38 const allFailing = Object.values(checks).every(c => c.status === 'fail'); 39 40 const health: HealthStatus = { 41 status: allPassing ? 'healthy' : allFailing ? 'unhealthy' : 'degraded', 42 checks, 43 timestamp: new Date().toISOString(), 44 }; 45 46 return Response.json(health, { 47 status: health.status === 'healthy' ? 200 : 503, 48 }); 49}

Metrics Tracking#

1// lib/metrics.ts 2import { PostHog } from 'posthog-node'; 3 4const posthog = new PostHog(process.env.POSTHOG_API_KEY!, { 5 host: process.env.POSTHOG_HOST, 6}); 7 8export function trackEvent( 9 userId: string, 10 event: string, 11 properties?: Record<string, any> 12) { 13 posthog.capture({ 14 distinctId: userId, 15 event, 16 properties, 17 }); 18} 19 20export const metrics = { 21 userSignup: (userId: string) => 22 trackEvent(userId, 'user_signup'), 23 subscriptionStarted: (userId: string, plan: string) => 24 trackEvent(userId, 'subscription_started', { plan }), 25 featureUsed: (userId: string, feature: string) => 26 trackEvent(userId, 'feature_used', { feature }), 27};

Sample Prompts#

TaskPrompt
Error tracking"Set up Sentry for error tracking with user context"
Logging"Implement structured logging with request tracing"
Health checks"Create comprehensive health check endpoints"
Alerting"Configure alerting for critical system events"
Dashboards"Design monitoring dashboards for SaaS metrics"

Configuration#

1// bootspring.config.js 2module.exports = { 3 agents: { 4 customInstructions: { 5 'monitoring-expert': ` 6 - Use Sentry for error tracking 7 - Implement structured logging with Pino 8 - Set up health check endpoints 9 - Configure meaningful alerts 10 - Track key business metrics 11 `, 12 }, 13 }, 14 monitoring: { 15 errorTracking: 'sentry', 16 analytics: 'posthog', 17 logging: 'pino', 18 }, 19};