eventlog

package
v0.9.9-dev.1 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package eventlog is the durable record of what Kivgraph did: when it indexed and for how long, which tool a client called and whether it answered, and when a server came up or went down.

It exists because none of that survived the process. The per-tool latency in internal/metrics is atomics in a map, minted fresh by every serve and discarded when it exits; the stage durations of a rebuild are printed to stderr and dropped. A reader that runs as its own process -- which is what `kivgraph logs` and `kivgraph tool-stats` are -- could observe neither.

The store is one append-only JSON-lines file. That choice is what makes it safe for the several processes that write it at once: a record is a single write of well under a pipe buffer to a file opened O_APPEND, which POSIX keeps whole and unmixed. Nothing here holds a lock across processes, and nothing here reads before writing.

Index

Constants

View Source
const (
	// StatusOK marks work that finished as asked.
	StatusOK = "ok"
	// StatusError marks work that failed.
	StatusError = "error"
)

Status is the outcome of a completed unit of work.

View Source
const DefaultMaxBytes = 8 << 20

DefaultMaxBytes is the size at which the live file is rotated. One rotation is kept, so the store costs at most twice this on disk.

Variables

This section is empty.

Functions

func Kinds added in v0.3.6

func Kinds() []string

Kinds is the whole vocabulary, in the order a reader meets it. A caller that validates or completes a --kind reads it here rather than restating the three constants, which is how the CLI and this package stay in step.

Types

type Event

type Event struct {
	Time    time.Time `json:"ts"`
	Level   Level     `json:"level"`
	Kind    Kind      `json:"kind"`
	Message string    `json:"msg"`

	// PID names the process that emitted the event, so a machine running
	// three servers can be told apart from one running a single server.
	PID int `json:"pid,omitempty"`

	// Tool, Status and DurationMS describe a completed call or pass. A
	// duration is a pointer because zero milliseconds is a real answer and
	// "not timed" is a different one.
	Tool       string   `json:"tool,omitempty"`
	Status     string   `json:"status,omitempty"`
	DurationMS *float64 `json:"duration_ms,omitempty"`

	Stage      string `json:"stage,omitempty"`
	Repository string `json:"repository,omitempty"`
	Generation string `json:"generation,omitempty"`

	// Results is what a tool returned, Symbols what a pass produced.
	Results *int64 `json:"results,omitempty"`
	Symbols *int64 `json:"symbols,omitempty"`

	// Error carries the rendered failure. Tool failures arrive as
	// "CODE: message", so the stable code survives without a second field.
	Error string `json:"error,omitempty"`
}

Event is one record. Its fields are a closed set rather than a free-form attribute bag: every producer is in this repository, and a fixed shape is what lets a reader render columns in a stable order and aggregate without guessing at types.

func Read

func Read(path string, options ReadOptions) ([]Event, error)

Read answers the matching events in chronological order. A store that does not exist yet is not an error: it is a machine that has not run anything.

A line that does not parse is skipped rather than failing the read. The file is written by several processes at once and may hold a record from a version that knew fields this one does not; refusing to show the rest would turn a harmless unknown into an outage of the only view a reader has.

func (Event) Duration

func (event Event) Duration() (time.Duration, bool)

Duration answers the observed duration and whether one was recorded.

func (Event) ErrorCode added in v0.9.2

func (event Event) ErrorCode() string

ErrorCode is the stable code a failed tool call carries.

It is read back out of Error rather than kept in a field of its own, which is the shape the writer chose on purpose: a tool failure is rendered as "CODE: message", so the classification already survives in the file. That makes it readable from logs written before anything knew to classify them, which is the only reason the five-day measurement in LUQUE-2235 can be checked against the code that answers it.

func (Event) Failed

func (event Event) Failed() bool

Failed reports whether the event describes work that did not answer.

A refusal is one of these: it returns no rows, so it takes the error path and is recorded here as one. Telling the two apart is the reader's job and needs the tool vocabulary, which this package does not have -- see ErrorCode and Summarize.

func (Event) WithDuration

func (event Event) WithDuration(elapsed time.Duration) Event

WithDuration returns the event carrying elapsed, in milliseconds.

func (Event) WithResults

func (event Event) WithResults(count int) Event

WithResults returns the event carrying a returned-row count.

func (Event) WithSymbols

func (event Event) WithSymbols(count int64) Event

WithSymbols returns the event carrying a produced-symbol count.

type Kind

type Kind string

Kind is the subsystem an event came from. It is what a reader filters on and what decides the badge a rendered line carries.

const (
	// KindServe is the lifecycle of a long-running process.
	KindServe Kind = "serve"
	// KindTool is one completed MCP tool call.
	KindTool Kind = "tool"
	// KindIndex is an indexing pass or one of its stages.
	KindIndex Kind = "index"
)

type Level

type Level string

Level is the severity of an event, kept as a string so the file stays readable without this package.

const (
	// LevelInfo reports progress.
	LevelInfo Level = "info"
	// LevelWarn reports a degraded result that still answered.
	LevelWarn Level = "warn"
	// LevelError reports a failure.
	LevelError Level = "error"
)

type ReadOptions

type ReadOptions struct {
	// Since drops events older than this instant.
	Since time.Time
	// Kinds keeps only these kinds. Empty keeps every kind.
	Kinds []Kind
	// Tool keeps only events naming this tool. Empty keeps every tool.
	Tool string
	// FailuresOnly keeps only events that did not answer.
	FailuresOnly bool
	// Limit keeps at most this many of the newest matching events. Zero or
	// less keeps all of them.
	Limit int
}

ReadOptions narrows a read. The zero value reads everything the store holds.

type Summary

type Summary struct {
	Tools   []ToolSummary `json:"tools"`
	Calls   int           `json:"calls"`
	OK      int           `json:"ok"`
	Refused int           `json:"refused"`
	Failed  int           `json:"failed"`
	First   time.Time     `json:"first,omitempty"`
	Last    time.Time     `json:"last,omitempty"`
}

Summary is the whole aggregation, ordered so a reader sees the busiest tool first and so two runs over the same events render identically.

func Summarize

func Summarize(events []Event, refusals ...string) Summary

Summarize aggregates the tool events among events. Events of another kind are ignored, so a caller may hand it an unfiltered read.

refusals are the error codes that mean the call declined by design rather than failed. They are a parameter because the vocabulary belongs to the tool surface and this package is below it; a caller that passes none gets the count that sums the two, which is what every caller got before the split and is the number LUQUE-2235 exists to stop publishing.

type ToolSummary

type ToolSummary struct {
	Tool  string `json:"tool"`
	Calls int    `json:"calls"`
	OK    int    `json:"ok"`
	// Refused is the count of calls that declined by design. It is its own
	// column and not a subtraction from Failed, because the two do not mean
	// the same thing in either direction: a refusal that rises is people
	// asking about common names, which is the tool working.
	Refused  int           `json:"refused"`
	Failed   int           `json:"failed"`
	Mean     time.Duration `json:"mean_ns"`
	Median   time.Duration `json:"median_ns"`
	P95      time.Duration `json:"p95_ns"`
	Max      time.Duration `json:"max_ns"`
	Last     time.Time     `json:"last"`
	LastFail string        `json:"last_failure,omitempty"`
}

ToolSummary is what one tool did over the events it was summarised from.

Percentiles are here because the store keeps every individual call rather than a running sum: internal/metrics deliberately retains latency as count, total and maximum to avoid an unbounded sample list in a live process, which buys a mean and a worst case and nothing in between. A reader over the file pays for the sample once, so it can answer the question a mean hides -- a tool whose median is fast and whose tail is not.

func (ToolSummary) SuccessRate

func (summary ToolSummary) SuccessRate() (float64, bool)

SuccessRate answers the share of calls that answered, in the range 0..1. A tool with no calls has no rate, so it answers 0 alongside false.

type Writer

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

Writer appends events to the store. A nil *Writer is a working sink that discards, so a caller that could not open the file needs no branch.

func Open

func Open(path string) (*Writer, error)

Open answers a Writer appending to path, creating the directory when it is missing.

func OpenWithLimit

func OpenWithLimit(path string, maxBytes int64) (*Writer, error)

OpenWithLimit is Open with an explicit rotation threshold.

func (*Writer) Append

func (writer *Writer) Append(event Event)

Append writes one event. It never returns an error and never blocks on anything but its own mutex: a log that cannot be written must not be able to fail the work it describes. A record that does not fit is dropped, not truncated, so every line in the file stays parseable.

func (*Writer) Close

func (writer *Writer) Close() error

Close releases the file.

func (*Writer) Path

func (writer *Writer) Path() string

Path answers the file the Writer appends to, or the empty string when it discards.

Jump to

Keyboard shortcuts

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