logcount

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package logcount counts the log lines a miren process prints, by level and by source, so the error rate can be shipped as a metric and alerted on instead of read out of a journal nobody collects.

Counting happens in a slog.Handler that wraps the process's root handler, so it sees exactly the records that get printed. Output relayed from child processes (vmagent, etcd, buildkit and friends) passes through the same handler, but it is counted under the child's name and at the child's own level, which the relay may have clamped for display. Without that split an etcd error would count as a miren info line, and a burst of vmagent failures would be invisible.

Index

Constants

View Source
const NumSources = int(numSources)

NumSources is the number of distinct Source values, for iterating Counts.

Variables

View Source
var Default = &Counts{}

Default is the process-wide set that the CLI's root logger counts into and the metrics collectors read from.

View Source
var Levels = [numLevels]string{"debug", "info", "warn", "error"}

Levels are the label values for the four buckets a counted line lands in.

Functions

func CountAt

func CountAt(ctx context.Context, level slog.Level) context.Context

CountAt marks ctx so that a record logged with it is counted as miren's own line at level, whatever level it prints at. It's for lines that log loud only so they always print, like a log level change, and would otherwise count as errors.

func MirenLevel added in v0.18.0

func MirenLevel(ctx context.Context, printed slog.Level) slog.Level

MirenLevel returns the level a miren line logged with ctx is counted at: the one CountAt set, or printed when it set none. Relayed children keep their printed level, since a clamp lowers it on purpose.

func Relayed

func Relayed(ctx context.Context, source Source, level slog.Level) context.Context

Relayed marks ctx so that a record logged with it is counted under source at level, rather than as miren's own line at whatever level it was logged. Relays use it to count a child's line at the child's level even when they clamp the level they print.

Types

type Counts

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

Counts holds cumulative line counts for one process.

func (*Counts) Load

func (c *Counts) Load(s Source, level int) uint64

Load returns the count for one source at one of the four Levels.

type Handler

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

Handler counts every record it handles into Counts, then passes it on.

func NewHandler

func NewHandler(inner slog.Handler, counts *Counts) *Handler

NewHandler wraps inner so that its records are counted into counts.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled defers to the wrapped handler, so records below its level are never built and never counted. The counter reports what was printed.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

type Source

type Source int

Source is who wrote a counted line: miren itself, or a relayed child.

const (
	SourceMiren Source = iota
	SourceBuildkit
	SourceContainerd
	SourceEtcd
	SourceEtcdutl
	SourceVictoriaLogs
	SourceVictoriaMetrics
	SourceVMAgent
	SourceOther
)

The relayed sources are a fixed set, so the metric's source label stays bounded no matter what a caller passes to SourceFor.

func SourceFor

func SourceFor(name string) Source

SourceFor maps a relayed child's name to its Source. Names outside the known set become SourceOther rather than a new label value.

func (Source) String

func (s Source) String() string

Jump to

Keyboard shortcuts

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