stacktrace

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Package stacktrace uses the go runtime to capture stack trace data.

Index

Constants

This section is empty.

Variables

View Source
var Disabled atomic.Bool

Disabled disables stacktrace collection in Wrap when set to true.

Functions

func Wrap

func Wrap(err error) error

Wrap extends err by attaching a StackTrace captured at the call site. If err is nil or Disabled is true, err is returned unchanged. If err already carries a StackTrace, it is not wrapped again.

Types

type Frame

type Frame struct {
	// File is the source file path of the frame.
	File string `json:"source"`
	// LineNumber is the line number within File where the call was made.
	LineNumber int `json:"line"`
	// Function is the fully-qualified function name of the frame.
	Function string `json:"func"`
}

Frame represents human-readable information about a single frame in a stack trace.

type StackTrace

type StackTrace []Frame

StackTrace represents a program stack trace as a series of frames.

func Extract

func Extract(err error) StackTrace

Extract returns the StackTrace attached to err, or nil if none is present or err is nil.

func GetStack

func GetStack(skipFrames int, skipRuntime bool) StackTrace

GetStack captures the current program stack trace and returns it as a StackTrace. skipFrames controls how many frames to skip: passing 1 makes GetStack itself the first captured frame. When skipRuntime is true, frames from the Go runtime (e.g. runtime.main, runtime.panic) and the testing package are omitted from the result.

func (StackTrace) LogValue

func (st StackTrace) LogValue() slog.Value

LogValue implements slog.LogValuer. It returns a group containing a single "stacktrace" attr whose value is an array of frame objects, each with "func", "line", and "source" keys.

Each frame is represented as map[string]any rather than slog.GroupValue because slog handlers only resolve slog.LogValuer at the top level of an attribute value — they do not recursively resolve slog.Value elements nested inside a []slog.Value wrapped in slog.AnyValue. JSON and text handlers would encode those as empty objects ({}). map[string]any is handled correctly by encoding/json and produces the expected key-value output.

Jump to

Keyboard shortcuts

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