stat

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package stat collects per-operation success/error counts so a summary can be printed at the end of a run. It is opt-in: until InitStat is called (root --stat flag), Add and Collect are cheap no-ops, so callers can report unconditionally without worrying about whether stats are enabled.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.0.6

func Add(op string, success bool)

Add records one completed operation. It is a no-op until InitStat has been called. The log package calls it for every operation-attributed Info (success) and Error (failure) message, so enabling --stat requires no per-command wiring.

func Collect

func Collect(op string, err *error) func()

Collect returns a closure intended for `defer` at the top of an Action. When the deferred call runs, if *err is nil the operation's success counter is incremented, and the total counter is always incremented (provided stat collection is enabled). The closure is a no-op when stat collection is disabled, so it is safe to defer unconditionally.

func InitStat

func InitStat()

InitStat enables statistics collection. It must be called before any command runs (the root PersistentPreRunE does this when --stat is set); otherwise Add and Collect are no-ops.

Types

type Stat

type Stat struct {
	Operation string `json:"operation"`
	Success   int64  `json:"success"`
	Error     int64  `json:"error"`
}

Stat is a single operation's statistics.

type Stats

type Stats []Stat

Stats is a slice of Stat that implements log.Message.

func Statistics

func Statistics() Stats

Statistics returns the stats collected so far, sorted by operation name so the summary table is deterministic. Returns an empty slice if stat collection is disabled. The maps are snapshotted under their mutexes so Statistics is safe to call while workers are still adding.

func (Stats) JSON

func (s Stats) JSON() string

JSON renders Stats as one JSON object per line.

func (Stats) String

func (s Stats) String() string

String renders Stats as a right-aligned table.

Jump to

Keyboard shortcuts

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