AgentSkillsCN

error-tracking-patterns

用于错误追踪与性能监控的模式。关键词:错误追踪、监控、模式。

SKILL.md
--- frontmatter
name: error-tracking-patterns
description: "Patterns for error tracking and performance monitoring. Keywords: error tracking, monitoring, patterns."

Error Tracking & Monitoring

This skill provides reusable patterns for integrating error tracking and performance monitoring into services, jobs, and scripts.

While examples may mention Sentry-style APIs, the principles apply to any monitoring backend.


Purpose

  • Ensure failures are observable (no silent failures).
  • Capture enough context to debug (without leaking secrets).
  • Make performance issues diagnosable (traces/spans, slow queries, hot paths).

When to Use

Use this skill when:

  • Adding new endpoints, controllers, or middleware
  • Adding background jobs / scheduled tasks
  • Handling errors from external systems (DB, APIs, queues)
  • Refactoring error handling and logging
  • Adding performance spans or profiling

Core rules

  1. Capture exceptions to a monitoring backend (not only console.error).
  2. Attach context: service name, environment, operation name, request correlation id (if available), and user identity (if safe).
  3. Initialize early: monitoring SDK should be initialized before application logic.
  4. Do not leak secrets: redact tokens, passwords, raw PII, and full request bodies unless explicitly approved.
  5. Prefer structured logs: logs should be queryable and correlated with traces.

Patterns

Controller / request handler

ts
try {
  // handler logic
} catch (err) {
  monitor.captureException(err, {
    tags: { layer: "controller", route: "your/<route>", method: "POST" },
  });
  throw err;
}

Background jobs

  • Initialize monitoring at process start.
  • Wrap the job in a single top-level span/transaction.
  • Ensure non-zero exit codes on failure (so infra can detect it).

Performance spans

ts
return monitor.startSpan({ name: "db.query", op: "db" }, async () => {
  return await doDatabaseOperation();
});

Checklist (for new code)

  • Monitoring initialized early
  • Exceptions captured with useful tags/context
  • Errors are re-thrown or converted into explicit failure responses (no swallowing)
  • Sensitive fields are redacted
  • Key operations include spans/traces
  • A minimal test exists to verify capture behavior (unit/integration)

Related

  • /.system/skills/ssot/backend/backend-dev-guidelines/SKILL.md
  • /.system/skills/ssot/frontend/frontend-dev-guidelines/SKILL.md