tracelog

package
v0.0.0-...-bd7611f Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package tracelog renders request-grouped logs as traces.

A trace is the set of log lines sharing a request ID. Render prints one block per trace: a header summarizing the request, then each line with its offset from the start of the trace:

2026-07-06 12:29:20  req-06FKH...  POST /Identity_Login  500  1ms
  +0s      INFO   http request  agent=curl/8.7.1  remote=[::1]:52161
  +638µs   ERROR  invalid credentials
  +638µs   ERROR  http response  error=invalid credentials  written=34

Collector renders traces live as requests complete, from any stream of slog JSON lines (ParseLine). The same code serves dev (cmd/dev tails the server it starts) and production (the traces CLI reads server log files), so both environments see identical output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Anomalies

func Anomalies(entries []Entry) []string

Anomalies inspects a completed trace for shapes that usually indicate silent failures: successful responses whose side effects never happened. It derives everything from the trace's entries, so detectors work on any log stream, past or live, without app-side support.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration trims a duration to a readable precision for log output, e.g. "12.35ms" rather than "12.345678ms".

func IsTerminal

func IsTerminal(w io.Writer) bool

IsTerminal reports whether w writes to a terminal, for deciding whether to render with color.

func Render

func Render(w io.Writer, id string, entries []Entry)

Render writes one trace block: a header line summarizing the request, then each entry with its offset from the first entry. The method, path, status, and duration attributes of the "http request" and "http response" entries are hoisted into the header.

Types

type Attr

type Attr struct {
	Key   string
	Value string
}

type Collector

type Collector struct {
	Out io.Writer

	// MaxAge bounds how long an unfinished trace is buffered. Zero means
	// 30 seconds.
	MaxAge time.Duration

	// PassThrough prints entries that have no request ID as single lines.
	// When false those entries are dropped.
	PassThrough bool

	// Filter, when set, decides whether a completed trace is rendered.
	Filter func(id string, entries []Entry) bool

	// Color renders anomaly callouts in yellow ANSI color. Enable when Out
	// is a terminal (IsTerminal).
	Color bool
	// contains filtered or unexported fields
}

Collector groups a stream of log entries into traces and renders each trace as soon as its request completes (the "http response" entry). Unfinished traces are flushed after MaxAge so hung or crashed requests still show their logs.

func (*Collector) Add

func (c *Collector) Add(id string, e Entry)

Add feeds one log entry to the collector. Entries with an empty id are printed immediately (or dropped, per PassThrough); others are buffered under their trace until the trace completes.

func (*Collector) Flush

func (c *Collector) Flush()

Flush renders all buffered traces, finished or not. Call it when the input stream ends.

func (*Collector) Sweep

func (c *Collector) Sweep(now time.Time)

Sweep flushes unfinished traces older than MaxAge. Call it periodically when the input stream can go quiet (e.g. a live tail).

type Entry

type Entry struct {
	Time  time.Time
	Level string
	Msg   string
	Attrs []Attr
}

func ParseLine

func ParseLine(line []byte) (id string, e Entry, ok bool)

ParseLine parses one slog JSON log line into its request ID and entry. It returns ok=false for lines that aren't JSON logs.

Source Files

  • anomaly.go
  • collect.go
  • tracelog.go

Jump to

Keyboard shortcuts

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