Skip to main content

Overview

The sentry middleware integrates with Sentry for error tracking, performance monitoring, and debugging. Use it when you need:
  • Error tracking
  • Performance monitoring
  • Issue management

Installation

Quick Start

Configuration

Options

Examples

Basic Setup

With Environment

Performance Monitoring

Manual Error Capture

Add Context

API Reference

Functions

Technical Details

Architecture

The Sentry middleware is built around a lightweight error tracking system with the following components: Hub: Central management structure that handles event capture, storage, and transport. Each middleware instance creates a Hub that is stored in the request context. Event: Represents an error event with metadata including:
  • Event ID, timestamp, and severity level
  • Exception details with stack traces
  • HTTP request information (URL, method, headers, query strings)
  • User context, tags, and extra data
  • Runtime context (Go version, platform)
Transport: Interface for sending events to Sentry. Supports custom implementations via the Transport interface:
  • MockTransport: For testing, stores events in memory
  • HTTPTransport: Sends events to Sentry via HTTP
  • Custom transports can be implemented by satisfying the Transport interface

Event Capture Flow

  1. Middleware wraps the handler with panic recovery
  2. Hub is stored in request context for the lifetime of the request
  3. Errors returned from handlers are automatically captured
  4. Panics are caught, converted to events, and re-thrown
  5. Events are processed through hooks (BeforeSend) before sending
  6. Events are sent asynchronously via the configured transport

Stack Trace Capture

Stack traces are captured using Go’s runtime.Callers and runtime.CallersFrames:
  • Frames include filename, function name, line number, and absolute path
  • Configurable skip depth to exclude middleware internals
  • Frames are reversed to show most recent call first

Header Filtering

Sensitive headers are automatically filtered from request data:
  • Authorization
  • Cookie
  • X-Api-Key

Sampling

Events can be sampled based on SampleRate (0.0 to 1.0):
  • 1.0 captures all events (default)
  • 0.5 captures 50% of events
  • Sampling uses nanosecond precision for distribution

Context Management

The middleware uses Go’s context to store:
  • Hub instance (via hubKey)
  • User information (via userContextKey)
  • Tags (via tagsContextKey)
  • Extra data (via extraContextKey)

Best Practices

  • Use environment for staging/production
  • Set release for version tracking
  • Add user context for debugging
  • Sample transactions in production

Testing

The Sentry middleware includes comprehensive test coverage for all major features: