スキル一覧に戻る
imehr

error-tracking

by imehr

0🍴 0📅 2026年1月15日
GitHubで見るManusで実行

SKILL.md


name: error-tracking description: Sentry error tracking and monitoring patterns version: 1.0.0 triggers:

  • sentry
  • error tracking
  • error monitoring
  • exception handling
  • captureException

Error Tracking Guidelines (Sentry)

Overview

This skill provides patterns for integrating Sentry error tracking into React/Node.js applications.

Quick Reference

Use CaseMethodExample
Catch exceptionscaptureExceptionSentry.captureException(error)
Log messagescaptureMessageSentry.captureMessage('Event')
Add contextsetContextSentry.setContext('user', {...})
Add tagssetTagSentry.setTag('feature', 'checkout')

Project Configuration

SettingDefaultYour Value
DSNenv.SENTRY_DSNCHANGE_ME
Environmentenv.NODE_ENVCHANGE_ME
Sample Rate1.0 (100%)CHANGE_ME
Traces Sample0.1 (10%)CHANGE_ME

Core Patterns

Pattern 1: Backend Setup (Node.js/Express)

// src/lib/sentry.ts
import * as Sentry from '@sentry/node';
import { Express } from 'express';

export function initSentry(app: Express) {
  Sentry.init({
    dsn: process.env.SENTRY_DSN,
    environment: process.env.NODE_ENV,
    release: process.env.APP_VERSION,

    // Performance monitoring
    tracesSampleRate: process.env.NODE_ENV === 'production' ? 0.1 : 1.0,

    // Filter out noisy errors
    ignoreErrors: [
      'ECONNREFUSED',
      'ECONNRESET',
    ],

    beforeSend(event, hint) {
      // Don't send errors in development
      if (process.env.NODE_ENV === 'development') {
        console.error('Sentry event (dev):', event);
        return null;
      }

      // Filter sensitive data
      if (event.request?.headers) {
        delete event.request.headers.authorization;
        delete event.request.headers.cookie;
      }

      return event;
    },
  });

  // Request handler creates a separate execution context
  app.use(Sentry.Handlers.requestHandler());

  // TracingHandler creates a trace for every incoming request
  app.use(Sentry.Handlers.tracingHandler());
}

export function initSentryErrorHandler(app: Express) {
  // Error handler must be before other error middleware
  app.use(Sentry.Handlers.errorHandler());
}

Pattern 2: Frontend Setup (React)

// src/lib/sentry.ts
import * as Sentry from '@sentry/react';

export function initSentry() {
  Sentry.init({
    dsn: import.meta.env.VITE_SENTRY_DSN,
    environment: import.meta.env.MODE,
    release: import.meta.env.VITE_APP_VERSION,

    // Performance
    tracesSampleRate: import.meta.env.PROD ? 0.1 : 1.0,

    // Session replay (optional)
    replaysSessionSampleRate: 0.1,
    replaysOnErrorSampleRate: 1.0,

    integrations: [
      Sentry.browserTracingIntegration(),
      Sentry.replayIntegration(),
    ],

    beforeSend(event) {
      // Filter user data
      if (event.user) {
        delete event.user.ip_address;
      }
      return event;
    },
  });
}

// Wrap App with Sentry error boundary
export const SentryErrorBoundary = Sentry.ErrorBoundary;

Pattern 3: Capturing Errors with Context

// ✅ CORRECT: Rich context for debugging
async function createOrder(userId: string, items: CartItem[]) {
  try {
    return await orderService.create({ userId, items });
  } catch (error) {
    Sentry.withScope((scope) => {
      // Add contextual data
      scope.setTag('feature', 'checkout');
      scope.setTag('userId', userId);

      scope.setContext('order', {
        itemCount: items.length,
        totalAmount: items.reduce((sum, i) => sum + i.price, 0),
      });

      scope.setLevel('error');

      Sentry.captureException(error);
    });

    throw error;
  }
}

Pattern 4: User Context

// Set user context after authentication
function setUserContext(user: User) {
  Sentry.setUser({
    id: user.id,
    email: user.email,
    username: user.name,
  });
}

// Clear on logout
function clearUserContext() {
  Sentry.setUser(null);
}

Pattern 5: Performance Tracking

// Manual transaction
async function processPayment(orderId: string) {
  const transaction = Sentry.startTransaction({
    name: 'Process Payment',
    op: 'payment',
  });

  try {
    const span = transaction.startChild({
      op: 'payment.validate',
      description: 'Validate payment details',
    });
    await validatePayment(orderId);
    span.finish();

    const chargeSpan = transaction.startChild({
      op: 'payment.charge',
      description: 'Charge payment',
    });
    const result = await chargePayment(orderId);
    chargeSpan.finish();

    transaction.setStatus('ok');
    return result;
  } catch (error) {
    transaction.setStatus('internal_error');
    throw error;
  } finally {
    transaction.finish();
  }
}

Anti-patterns Summary

  1. Logging Everything - Too many events → Sample strategically
  2. Missing Context - Bare exceptions → Add tags and context
  3. Sensitive Data - Passwords, tokens in events → Filter in beforeSend
  4. No User Context - Can't identify affected users → Set user on login

Resources

TopicWhen to ReadLink
SetupInitial integration[mdc:resources/sentry-setup.md]
ContextAdding debugging info[mdc:resources/context-enrichment.md]
AlertsSetting up notifications[mdc:resources/alerts.md]
  • error-handling - For error class patterns
  • backend-dev-guidelines - For middleware integration
  • frontend-dev-guidelines - For error boundary integration

スコア

総合スコア

50/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です