report

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package report turns an app's unexpected errors and panics, on the server and in the browser, into reports grouped by cause, and sends them to an error tracker such as Sentry:

reporter := report.New(report.Options{Sinks: []report.Sink{sentry.New(sentry.Options{})}})
server, err := ternApp(kit.WithReporter(reporter))

kit reports what it would log as an unexpected error: errors that loads, actions and endpoints return that aren't answers (NotFound, Fail, redirects), panics, and, with the client's help, uncaught errors in the browser. Each report has the error, its stack where there is one, the request, the route, the trace and the user, if a hook added one to the logging context (c.LogWith(slog.String("user", id))).

Reports with the same cause share a group: the same error type and message, numbers and IDs aside, at the same route, or for panics, the same place in the code. Each group is sent a few times a minute at most, and counted. Under tern dev, the log view lists the groups.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Errorf

func Errorf(format string, args ...any) error

Errorf is fmt.Errorf with the stack, as WithStack adds it.

func Fingerprint

func Fingerprint(rep Report) string

Fingerprint is a report's group: for a panic, the error and the innermost frames of the app's code; otherwise the type, the normalized message and the route.

func Normalize

func Normalize(msg string) string

Normalize is a message with what changes between occurrences of the same problem taken out: quoted values, UUIDs, hex and numbers.

func Permanent

func Permanent(err error) error

Permanent marks a sink's error as one retrying won't fix.

func SetDefault

func SetDefault(r *Reporter)

SetDefault makes r the reporter for code outside requests, such as jobs.

func WithStack

func WithStack(err error) error

WithStack wraps err with the stack where WithStack is called, so its report shows where it came from. Errors without one are reported without a stack.

Types

type Frame

type Frame struct {
	Function string `json:"function"`
	File     string `json:"file"`
	Line     int    `json:"line"`
	// InApp is set for the app's own code, not Go's or a dependency's.
	InApp bool `json:"inApp,omitempty"`
}

Frame is a place in the code.

type Group

type Group struct {
	ID        string    `json:"id"`
	Type      string    `json:"type"`
	Message   string    `json:"message"`
	Route     string    `json:"route,omitempty"`
	Source    string    `json:"source"`
	Count     int       `json:"count"`
	FirstSeen time.Time `json:"firstSeen"`
	LastSeen  time.Time `json:"lastSeen"`
	// Last is the latest report.
	Last Report `json:"last"`
	// contains filtered or unexported fields
}

Group is a cause and how often it happened.

type Options

type Options struct {
	// Sinks get the reports.
	Sinks []Sink
	// Release names the build, such as a version or commit. The default is
	// TERN_RELEASE's, or the VCS revision Go stamped into the binary.
	Release string
	// Environment is "production", "staging"… The default is TERN_ENV's.
	Environment string
	// PerMinute is how many reports of one group are sent each minute;
	// the rest are counted. The default is 10.
	PerMinute int
	// Batch says how reports are batched and retried.
	Batch batch.Options
}

Options configures a reporter.

type Report

type Report struct {
	ID   string    `json:"id"` // 32 hex digits
	Time time.Time `json:"time"`
	// Source is "server", "browser" or "job".
	Source string `json:"source"`
	// Type is the error's Go type, "panic", or the JavaScript error's
	// name; Message is its text.
	Type    string  `json:"type"`
	Message string  `json:"message"`
	Panic   bool    `json:"panic,omitempty"`
	Stack   []Frame `json:"stack,omitempty"` // innermost first
	// Group identifies the cause: reports with the same group are the
	// same problem.
	Group string `json:"group"`

	Method string `json:"method,omitempty"`
	URL    string `json:"url,omitempty"`
	Route  string `json:"route,omitempty"`
	// UserAgent is the browser's, for browser errors.
	UserAgent string `json:"userAgent,omitempty"`

	RequestID string `json:"requestId,omitempty"`
	TraceID   string `json:"traceId,omitempty"`
	SpanID    string `json:"spanId,omitempty"`
	User      string `json:"user,omitempty"`
	// Attrs are the other attributes of the request's logging context.
	Attrs map[string]any `json:"attrs,omitempty"`

	Release     string `json:"release,omitempty"`
	Environment string `json:"environment,omitempty"`
}

Report is one unexpected error.

type Reporter

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

Reporter groups reports and sends them. Make one with New.

func Default

func Default() *Reporter

Default returns the default reporter, or nil.

func DevOnly

func DevOnly() *Reporter

DevOnly returns a reporter that only feeds tern dev's log view, or nil outside tern dev. kit uses it when the app has no reporter of its own.

func New

func New(o Options) *Reporter

New returns a reporter. Under tern dev, it also sends reports to the dev server's log view.

func (*Reporter) Flush

func (r *Reporter) Flush(ctx context.Context) error

Flush waits until the reports so far have been sent, or ctx ends.

func (*Reporter) Groups

func (r *Reporter) Groups() []Group

Groups returns the groups seen since the process started, most recent first.

func (*Reporter) Report

func (r *Reporter) Report(ctx context.Context, err error, fill ...func(*Report))

Report records err as an unexpected error, with what ctx carries: the request, trace and user. fill can set the report's other fields.

func (*Reporter) Send

func (r *Reporter) Send(rep Report)

Send groups and sends a report made elsewhere, such as from a browser.

type Sink

type Sink interface {
	Send(ctx context.Context, reports []Report) error
}

A Sink sends reports to an error tracker, in batches, from a goroutine.

Directories

Path Synopsis
Package sentry is a report sink for Sentry, and for the services that take its protocol, such as GlitchTip and Bugsink:
Package sentry is a report sink for Sentry, and for the services that take its protocol, such as GlitchTip and Bugsink:

Jump to

Keyboard shortcuts

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