flightrecorder

package
v1.126.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Box

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

Box holds multiple circular buffers for recording events, one per event type. It also provides a mechanism to swap out active buffers so that a DumpAndReset operation does not block new events.

func NewBox

func NewBox(log *zap.Logger, config Config) *Box

NewBox creates a new Box based on the provided configuration. It initializes buffers for each supported event type.

func (*Box) DumpAndReset

func (b *Box) DumpAndReset(mergeEventDumps bool)

DumpAndReset swaps out active buffers and logs all events from the old buffers. It sorts events by timestamp and uses zap.Logger to output the details.

func (*Box) Enqueue

func (b *Box) Enqueue(eventType EventType, skipCallers int)

Enqueue captures the call stack and enqueues an event for the given event type. The skipCallers parameter allows the caller to skip a number of stack frames.

func (*Box) Reset

func (b *Box) Reset() *[EventTypeCount]*CircularBuffer

Reset replaces the active buffers with new ones and returns the old ones. This ensures DumpAndReset reads from a stable snapshot while new events are recorded.

type CircularBuffer

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

CircularBuffer implements a lock-free ring buffer for storing events. It uses a single atomic "write" counter to determine which slot to use (modulo capacity). The entire fixed buffer is read in Dump, discarding any zero (unused) events.

func NewCircularBuffer

func NewCircularBuffer(capacity int) *CircularBuffer

NewCircularBuffer creates a new CircularBuffer with the given capacity and maximum stack frames per event.

func (*CircularBuffer) Dump

func (cb *CircularBuffer) Dump() []Event

Dump returns a slice of events in the buffer in chronological order (oldest first).

func (*CircularBuffer) DumpTo

func (cb *CircularBuffer) DumpTo(dst []Event) []Event

DumpTo appends events to the provided 'dst' slice in chronological order (oldest first). It iterates over the entire fixed buffer, acquiring each slot to safely read the event, and discards events that are zero (unused).

func (*CircularBuffer) Enqueue

func (cb *CircularBuffer) Enqueue(event Event)

Enqueue adds a new event into the circular buffer in a lock-free manner. It claims the next slot using the atomic write pointer. If the slot is already acquired by another writer, the event is discarded and the function returns false.

type Config

type Config struct {
	Enabled bool `help:"enable flight recorder" default:"false"`

	DBStackFrameCapacity int `help:"capacity of the circular buffer for database stack frame events." default:"1000"`
}

Config holds configuration for the Flight Recorder (Box), including separate buffer configurations for each event type.

type Event

type Event struct {
	Timestamp   uint64
	Stack       [8]uintptr
	Type        EventType
	SkipCallers int
}

Event represents a single recorded event. Timestamp is stored as a uint64 (Unix nanosecond timestamp) to minimize size. Stack is stored as a fixed-size array of uintptr, ensuring constant memory usage.

func NewEvent

func NewEvent(eventType EventType, skipCallers int) Event

NewEvent creates a new event with the given type and stack depth.

func (*Event) FormattedStack

func (e *Event) FormattedStack() string

FormattedStack converts the fixed-size stack array into a human-readable string.

func (*Event) IsZero

func (e *Event) IsZero() bool

IsZero returns true if the event is not set.

type EventType

type EventType int

EventType defines the type of event.

const (
	// EventTypeDB represents a database event.
	EventTypeDB EventType = iota

	// EventTypeCount represents the total number of event types.
	// Always keep this last in the const block.
	EventTypeCount
)

func (EventType) String

func (et EventType) String() string

String returns the string representation of the EventType.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳