errortracking

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2023 License: MIT, BSD-2-Clause Imports: 7 Imported by: 1

Documentation

Overview

Package errortracking abstracts all error reporting features away from services like workhorse or pages

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Capture

func Capture(err error, opts ...CaptureOption)

Capture reports an error to the error reporting service.

Example
package main

import (
	"net/http"

	"gitlab.com/gitlab-org/labkit/errortracking"
)

func main() {
	req, err := http.NewRequest("GET", "http://example.com", nil)
	ctx := req.Context()

	if err != nil {
		// Send the error to the error tracking system
		errortracking.Capture(err,
			errortracking.WithContext(ctx),                          // Extract details such as correlation-id from context
			errortracking.WithRequest(req),                          // Extract additional details from request
			errortracking.WithField("domain", "http://example.com"), // Add additional custom details
			errortracking.WithStackTrace(),                          // Attach the stack trace of up to 10 errors in the chain
		)
	}
}
Output:

func Initialize

func Initialize(opts ...InitializationOption) error

Initialize initializes global error tracking. Call this once on the program start.

func NewHandler

func NewHandler(next http.Handler) http.Handler

NewHandler will recover from panics inside handlers and reports the stacktrace to the errorreporting provider.

Example
package main

import (
	"net/http"

	"gitlab.com/gitlab-org/labkit/errortracking"
)

func main() {
	handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
		panic("oh dear")
	})

	http.ListenAndServe(":1234", errortracking.NewHandler(handler))
}
Output:

Types

type CaptureOption

type CaptureOption func(*captureConfig, *sentry.Event)

CaptureOption will configure how an error is captured.

func WithContext

func WithContext(ctx context.Context) CaptureOption

WithContext will extract information from the context to add to the error.

func WithField

func WithField(key string, value string) CaptureOption

WithField allows to add a custom field to the error.

func WithRequest

func WithRequest(r *http.Request) CaptureOption

WithRequest will capture details of the request along with the error.

func WithStackTrace added in v1.11.0

func WithStackTrace() CaptureOption

WithStackTrace allows to extract the stack trace from an error and report it to Sentry

func WithUserID added in v1.8.0

func WithUserID(userID string) CaptureOption

WithUserID allows to add a user id to the error.

type InitializationOption

type InitializationOption = TrackerOption

type Tracker

type Tracker interface {
	// Capture reports an error to the error reporting service
	Capture(err error, opts ...CaptureOption)
}

Tracker is an abstraction for error tracking.

func DefaultTracker

func DefaultTracker() Tracker

DefaultTracker returns the default global error tracker.

func NewTracker

func NewTracker(opts ...TrackerOption) (Tracker, error)

NewTracker constructs a new Tracker with the provided options.

type TrackerOption

type TrackerOption func(*trackerConfig)

TrackerOption will configure a Tracker.

func WithLoggerName

func WithLoggerName(loggerName string) TrackerOption

WithLoggerName sets the logger name.

func WithSentryDSN

func WithSentryDSN(sentryDSN string) TrackerOption

WithSentryDSN sets the sentry data source name.

func WithSentryEnvironment

func WithSentryEnvironment(sentryEnvironment string) TrackerOption

WithSentryEnvironment sets the sentry environment.

func WithVersion

func WithVersion(version string) TrackerOption

WithVersion is used to configure the version of the service that is currently running.

Jump to

Keyboard shortcuts

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