Documentation
¶
Overview ¶
Package metrics provides Fendix's internal, opt-in product metrics collection (v0.20 — Establish Baselines). It records structural facts about a scan — durations, finding counts, memory — so engineers can track product health release-over-release. It is off by default.
PRIVACY: this package must never log source code, finding content, file paths, hostnames, secrets, or any user data — only structural counts and timings. MetricEvent has no field that can carry such data; keep it that way. Anything richer belongs in a debug bundle, not here.
Index ¶
Constants ¶
const DefaultPath = "metrics/events.jsonl"
DefaultPath is where the append-only event log lives, relative to the working directory. It is gitignored — it is local-only telemetry.
const EnvFlag = "FENDIX_METRICS"
EnvFlag is the environment variable that turns metrics collection on. Any of "1", "true", "yes" (case-insensitive) enables it; everything else — including unset — disables it. Opt-in is mandatory.
const EnvPathFlag = "FENDIX_METRICS_PATH"
EnvPathFlag optionally overrides where the event log is written. Useful for tests and for sandboxes where the default relative path isn't writable. Ignored unless EnvFlag is enabled.
Variables ¶
This section is empty.
Functions ¶
func MemoryMB ¶
MemoryMB converts a byte count (e.g. runtime.MemStats.HeapAlloc) to MiB for the MemoryMB field, so callers don't repeat the unit math.
func ResolvePath ¶
func ResolvePath() string
ResolvePath returns the path the collector actually writes to: the FENDIX_METRICS_PATH override if set, else DefaultPath. The reader commands (metrics show/export/clear) use this so they read what FromEnv wrote — without it a user who sets FENDIX_METRICS_PATH would see an empty log.
Types ¶
type Collector ¶
type Collector interface {
// Record buffers an event. It must add negligible overhead.
Record(event MetricEvent) error
// Flush persists any buffered events. Safe to call multiple times.
Flush() error
}
Collector records metric events. Record must be cheap (it is called on the scan hot path); durable I/O is deferred to Flush.
type CommandSummary ¶
type CommandSummary struct {
Total int
Success int
SuccessRate float64 // Success/Total in [0,1]; 0 when Total == 0
// ByErrorClass counts FAILURES grouped by coarse class ("usage",
// "scan-error", …). Successes are not included.
ByErrorClass map[string]int
}
CommandSummary is the aggregate view of CLI-invocation events (phase "cli") behind the "metrics show" success-rate line. Unlike Summarize it does NOT filter to scans — it counts every recorded invocation so the rate reflects real first-try success across all commands (the v0.25 metric).
func SummarizeCommands ¶
func SummarizeCommands(events []MetricEvent, lastN int) CommandSummary
SummarizeCommands aggregates the most recent lastN CLI-invocation events (lastN <= 0 means all). It reads phase "cli" events only — never scans, so a scan that also emits a "scan" event is counted once here.
type FileCollector ¶
type FileCollector struct {
// contains filtered or unexported fields
}
FileCollector buffers events in memory and appends them as newline-delimited JSON on Flush. It is safe for concurrent use.
func NewFileCollector ¶
func NewFileCollector(path string) *FileCollector
NewFileCollector returns a FileCollector writing to path with the default size cap. The file and its parent directory are created lazily on the first Flush.
func (*FileCollector) Flush ¶
func (c *FileCollector) Flush() error
Flush appends all buffered events to the log as NDJSON, then clears the buffer. If the existing log already exceeds the size cap it is rotated to <path>.1 (one generation) before writing, bounding disk use.
func (*FileCollector) Record ¶
func (c *FileCollector) Record(event MetricEvent) error
Record buffers event in memory. It performs no I/O, so its cost is a slice append under a mutex — well under the < 1ms-per-scan budget.
type MetricEvent ¶
type MetricEvent struct {
Version string `json:"version"`
Phase string `json:"phase"` // "scan" (a scan) or "cli" (one invocation)
DurationMs int64 `json:"duration_ms"`
FindingCount int `json:"finding_count"`
MemoryMB float64 `json:"memory_mb"`
Timestamp time.Time `json:"timestamp"`
// --- v0.25 CLI-success-rate instrumentation (phase "cli") -----------
// Command is the subcommand name only ("scan", "version", …) — never the
// args. ErrorClass is a coarse bucket ("usage", "scan-findings",
// "scan-error", …), never the error text. All omitempty so scan-phase
// events are byte-identical to pre-v0.25.
Command string `json:"command,omitempty"`
ExitCode int `json:"exit_code,omitempty"`
Success bool `json:"success,omitempty"`
ErrorClass string `json:"error_class,omitempty"`
}
MetricEvent is one structural observation about a scan phase or CLI invocation. Every field is a count, a duration, a version string, a command name, or an error CLASS — never user data (no paths, args, hosts, or finding content). PRIVACY: keep it that way.
func LoadEvents ¶
func LoadEvents(path string) ([]MetricEvent, error)
LoadEvents reads all NDJSON metric events from path. A missing file is not an error — it returns an empty slice (metrics are opt-in, so "no log yet" is the normal first-run state). Malformed lines are skipped rather than aborting the whole read, so one bad append can't hide the history.
type NoopCollector ¶
type NoopCollector struct{}
NoopCollector discards everything. It is the default when metrics are disabled, so instrumented code can call Record/Flush unconditionally with zero overhead and zero I/O.
func (NoopCollector) Record ¶
func (NoopCollector) Record(MetricEvent) error
Record discards the event.
type Summary ¶
type Summary struct {
Count int
AvgDurationMs float64
AvgFindings float64
AvgMemoryMB float64
LastRun time.Time
// DurationTrend is "improving" (getting faster), "worsening" (slower),
// or "flat", computed by comparing the older vs newer half of the window.
DurationTrend string
}
Summary is the aggregate view of recent metric events for `metrics show`.
func Summarize ¶
func Summarize(events []MetricEvent, lastN int) Summary
Summarize aggregates the most recent lastN scan-phase events (lastN <= 0 means all). It ignores non-"scan" phases so the headline numbers describe whole scans, not sub-phases.