errortracking

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2025 License: MIT Imports: 5 Imported by: 0

README

Error Tracking

This package provides error tracking integration for ResolveSpec, with built-in support for Sentry.

Features

  • Provider Interface: Flexible design supporting multiple error tracking backends
  • Sentry Integration: Full-featured Sentry support with automatic error, warning, and panic tracking
  • Automatic Logger Integration: All logger.Error() and logger.Warn() calls are automatically sent to the error tracker
  • Panic Tracking: Automatic panic capture with stack traces
  • NoOp Provider: Zero-overhead when error tracking is disabled

Configuration

Add error tracking configuration to your config file:

error_tracking:
  enabled: true
  provider: "sentry"  # Currently supports: "sentry" or "noop"
  dsn: "https://your-sentry-dsn@sentry.io/project-id"
  environment: "production"  # e.g., production, staging, development
  release: "v1.0.0"  # Your application version
  debug: false
  sample_rate: 1.0  # Error sample rate (0.0-1.0)
  traces_sample_rate: 0.1  # Traces sample rate (0.0-1.0)

Usage

Initialization

Initialize error tracking in your application startup:

package main

import (
    "github.com/bitechdev/ResolveSpec/pkg/config"
    "github.com/bitechdev/ResolveSpec/pkg/errortracking"
    "github.com/bitechdev/ResolveSpec/pkg/logger"
)

func main() {
    // Load your configuration
    cfg := config.Config{
        ErrorTracking: config.ErrorTrackingConfig{
            Enabled:     true,
            Provider:    "sentry",
            DSN:         "https://your-sentry-dsn@sentry.io/project-id",
            Environment: "production",
            Release:     "v1.0.0",
            SampleRate:  1.0,
        },
    }

    // Initialize logger
    logger.Init(false)

    // Initialize error tracking
    provider, err := errortracking.NewProviderFromConfig(cfg.ErrorTracking)
    if err != nil {
        logger.Error("Failed to initialize error tracking: %v", err)
    } else {
        logger.InitErrorTracking(provider)
    }

    // Your application code...

    // Cleanup on shutdown
    defer logger.CloseErrorTracking()
}
Automatic Tracking

Once initialized, all logger errors and warnings are automatically sent to the error tracker:

// This will be logged AND sent to Sentry
logger.Error("Database connection failed: %v", err)

// This will also be logged AND sent to Sentry
logger.Warn("Cache miss for key: %s", key)
Panic Tracking

Panics are automatically captured when using the logger's panic handlers:

// Using CatchPanic
defer logger.CatchPanic("MyFunction")()

// Using CatchPanicCallback
defer logger.CatchPanicCallback("MyFunction", func(err any) {
    // Custom cleanup
})()

// Using HandlePanic
defer func() {
    if r := recover(); r != nil {
        err = logger.HandlePanic("MyMethod", r)
    }
}()
Manual Tracking

You can also use the provider directly for custom error tracking:

import (
    "context"
    "github.com/bitechdev/ResolveSpec/pkg/errortracking"
    "github.com/bitechdev/ResolveSpec/pkg/logger"
)

func someFunction() {
    tracker := logger.GetErrorTracker()
    if tracker != nil {
        // Capture an error
        tracker.CaptureError(context.Background(), err, errortracking.SeverityError, map[string]interface{}{
            "user_id": userID,
            "request_id": requestID,
        })

        // Capture a message
        tracker.CaptureMessage(context.Background(), "Important event occurred", errortracking.SeverityInfo, map[string]interface{}{
            "event_type": "user_signup",
        })

        // Capture a panic
        tracker.CapturePanic(context.Background(), recovered, stackTrace, map[string]interface{}{
            "context": "background_job",
        })
    }
}

Severity Levels

The package supports the following severity levels:

  • SeverityError: For errors that should be tracked and investigated
  • SeverityWarning: For warnings that may indicate potential issues
  • SeverityInfo: For informational messages
  • SeverityDebug: For debug-level information

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NoOpProvider

type NoOpProvider struct{}

NoOpProvider is a no-op implementation of the Provider interface Used when error tracking is disabled

func NewNoOpProvider

func NewNoOpProvider() *NoOpProvider

NewNoOpProvider creates a new NoOp provider

func (*NoOpProvider) CaptureError

func (n *NoOpProvider) CaptureError(ctx context.Context, err error, severity Severity, extra map[string]interface{})

CaptureError does nothing

func (*NoOpProvider) CaptureMessage

func (n *NoOpProvider) CaptureMessage(ctx context.Context, message string, severity Severity, extra map[string]interface{})

CaptureMessage does nothing

func (*NoOpProvider) CapturePanic

func (n *NoOpProvider) CapturePanic(ctx context.Context, recovered interface{}, stackTrace []byte, extra map[string]interface{})

CapturePanic does nothing

func (*NoOpProvider) Close

func (n *NoOpProvider) Close() error

Close does nothing

func (*NoOpProvider) Flush

func (n *NoOpProvider) Flush(timeout int) bool

Flush does nothing and returns true

type Provider

type Provider interface {
	// CaptureError captures an error with the given severity and additional context
	CaptureError(ctx context.Context, err error, severity Severity, extra map[string]interface{})

	// CaptureMessage captures a message with the given severity and additional context
	CaptureMessage(ctx context.Context, message string, severity Severity, extra map[string]interface{})

	// CapturePanic captures a panic with stack trace
	CapturePanic(ctx context.Context, recovered interface{}, stackTrace []byte, extra map[string]interface{})

	// Flush waits for all events to be sent (useful for graceful shutdown)
	Flush(timeout int) bool

	// Close closes the provider and releases resources
	Close() error
}

Provider defines the interface for error tracking providers

func NewProviderFromConfig

func NewProviderFromConfig(cfg config.ErrorTrackingConfig) (Provider, error)

NewProviderFromConfig creates an error tracking provider based on the configuration

type SentryConfig

type SentryConfig struct {
	DSN              string
	Environment      string
	Release          string
	Debug            bool
	SampleRate       float64
	TracesSampleRate float64
}

SentryConfig holds the configuration for Sentry

type SentryProvider

type SentryProvider struct {
	// contains filtered or unexported fields
}

SentryProvider implements the Provider interface using Sentry

func NewSentryProvider

func NewSentryProvider(config SentryConfig) (*SentryProvider, error)

NewSentryProvider creates a new Sentry provider

func (*SentryProvider) CaptureError

func (s *SentryProvider) CaptureError(ctx context.Context, err error, severity Severity, extra map[string]interface{})

CaptureError captures an error with the given severity and additional context

func (*SentryProvider) CaptureMessage

func (s *SentryProvider) CaptureMessage(ctx context.Context, message string, severity Severity, extra map[string]interface{})

CaptureMessage captures a message with the given severity and additional context

func (*SentryProvider) CapturePanic

func (s *SentryProvider) CapturePanic(ctx context.Context, recovered interface{}, stackTrace []byte, extra map[string]interface{})

CapturePanic captures a panic with stack trace

func (*SentryProvider) Close

func (s *SentryProvider) Close() error

Close closes the provider and releases resources

func (*SentryProvider) Flush

func (s *SentryProvider) Flush(timeout int) bool

Flush waits for all events to be sent (useful for graceful shutdown)

type Severity

type Severity string

Severity represents the severity level of an error

const (
	SeverityError   Severity = "error"
	SeverityWarning Severity = "warning"
	SeverityInfo    Severity = "info"
	SeverityDebug   Severity = "debug"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL