diag

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package diag provides a lightweight, always-on runtime watchdog that samples process resource metrics (goroutine count, heap usage) on an interval and records a diagnostic when a threshold is crossed. It exists to catch runaway resource growth — leaked goroutines or unbounded memory — early, and to leave a durable, actionable artifact (including a full goroutine stack dump) even when structured logging via GRUT_LOG is not enabled.

The watchdog is cheap: one runtime.ReadMemStats and runtime.NumGoroutine per interval (default 60s), so it is safe to run for the entire session.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alert

type Alert struct {
	Timestamp      time.Time
	Kind           string // kindGoroutines or kindHeap
	Message        string
	Goroutines     int
	Baseline       int
	HeapInuseBytes uint64
	NumGC          uint32
}

Alert describes a threshold breach detected by the watchdog.

type Thresholds

type Thresholds struct {
	// GoroutineFloor is an absolute goroutine count that always triggers an
	// alert regardless of the startup baseline.
	GoroutineFloor int
	// GoroutineGrowth triggers an alert when the live goroutine count exceeds
	// GoroutineGrowth * baseline (and is at least GoroutineGrowthMin).
	GoroutineGrowth float64
	// GoroutineGrowthMin guards the growth check so a tiny baseline cannot
	// produce alerts at harmless goroutine counts.
	GoroutineGrowthMin int
	// HeapInuseBytes triggers an alert when heap-in-use exceeds this many bytes.
	HeapInuseBytes uint64
	// Cooldown is the minimum time between alerts of the same kind.
	Cooldown time.Duration
}

Thresholds configures when the watchdog reports.

func DefaultThresholds

func DefaultThresholds() Thresholds

DefaultThresholds returns the built-in threshold configuration.

type Watchdog

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

Watchdog samples runtime metrics and reports threshold breaches.

func New

func New() *Watchdog

New creates a Watchdog with default cadence, thresholds, and reporter. The default reporter logs via slog and appends a durable diagnostic record (with a goroutine stack dump for goroutine alerts) under the app data directory.

func (*Watchdog) Run

func (w *Watchdog) Run(ctx context.Context)

Run samples on the configured interval until ctx is cancelled. It takes an immediate baseline sample so the growth check has a reference point, then reports any breaches through the configured reporter.

Jump to

Keyboard shortcuts

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