logs

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2026 License: Apache-2.0 Imports: 10 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 logs merges the output of every replica of one workload.

The unit is the workload, not the pod. A developer debugging a deployment does not care which of six replicas served the failing request, and making them open one tab per replica is the complaint this screen exists to answer. Per-pod is a filter applied to the merged stream, never the way in.

Everything here stays in memory. The ring buffer is capped per workload and counts what it dropped, because a buffer that quietly loses lines makes a chatty workload look calm.

Index

Constants

View Source
const (
	// DefaultBufferLines is the ring buffer size per workload, from
	// docs/05-architecture.md.
	DefaultBufferLines = 10_000

	// MaxLineBytes caps one line. Applications emit megabyte JSON blobs; the
	// stream must survive them, so the line is truncated and marked rather
	// than dropped or allowed to consume the buffer.
	MaxLineBytes = 64 * 1024

	// ReorderWindow is how far back a newly arrived line may be inserted once a
	// stream is live. Replicas do not arrive in lockstep, and a merged view
	// that ignores that reads out of order. Beyond the window the reader has
	// already moved on, so a line lands at the end and says it arrived late.
	ReorderWindow = 5 * time.Second

	// SettleWindow is how long after a subscription every line merges strictly
	// by timestamp, however old.
	//
	// Opening a workload replays each replica's retained scrollback at once,
	// and that backlog can span hours. Judging those lines by the live reorder
	// window would order the first screen by which kubelet answered first,
	// which is exactly the wrong answer for the screen a developer is looking
	// at when they arrive.
	SettleWindow = 3 * time.Second
)
View Source
const (
	// EdgeHorizon marks where a stream begins. Earlier output existed; the
	// kubelet no longer has it.
	EdgeHorizon = "horizon"
	// EdgeRestart marks previous-instance output, which reaches exactly one
	// restart back.
	EdgeRestart = "restart"
	// EdgeGone marks a pod that left the workload. Its logs went with it.
	EdgeGone = "gone"
	// EdgeError marks a stream that could not be read, with the reason.
	EdgeError = "error"
	// EdgeEnded marks a stream that finished, which is a container that
	// stopped rather than a gap in what was retained.
	EdgeEnded = "ended"
)

Edge kinds. Log availability has edges, and naming them is what keeps a crash loop from reading as a quiet period.

View Source
const DefaultRefresh = 3 * time.Second

DefaultRefresh is how often the pod set is re-read, so a rollout's replicas join within a few seconds without watching every pod in the cluster.

View Source
const DefaultReopen = 2 * time.Second

DefaultReopen is how long a stream waits before reconnecting after its container stopped writing. Long enough not to spin against a container that is genuinely finished, short enough that a restart is not a visible gap.

View Source
const DefaultTail int64 = 2000

DefaultTail is how much scrollback a stream opens with. Enough to see what just happened, short enough that opening a week-old pod does not pull the kubelet's entire retained buffer across the wire.

Variables

This section is empty.

Functions

func HiddenByDefault

func HiddenByDefault(container string) bool

HiddenByDefault reports whether a container is infrastructure rather than application output. It matches exact names only: "envoy-config-reloader" is somebody's application and stays visible.

func ParseLine

func ParseLine(raw string) (time.Time, string, bool)

ParseLine splits the RFC3339Nano stamp the kubelet prefixes when logs are requested with timestamps. A line without one keeps its whole text: guessing a time would place it wrongly in the merge, and the caller can decide.

func Scan

func Scan(r io.Reader, meta Meta, emit func(Line)) error

Scan reads a log stream and emits one Line per record.

It reads incrementally rather than buffering the stream, so follow mode delivers a line the moment the kubelet does.

Types

type Buffer

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

Buffer is the per-workload ring.

func NewBuffer

func NewBuffer(n int) *Buffer

NewBuffer makes a ring holding at most n lines.

func (*Buffer) Add

func (b *Buffer) Add(l Line) Line

Add places a line in time order and returns it as stored.

A line stamped inside the reorder window is inserted where it belongs. One stamped further back is appended and marked late: the reader has already scrolled past that moment, and quietly rewriting history behind them is worse than admitting the line arrived out of order.

func (*Buffer) Seq

func (b *Buffer) Seq() int64

Seq is the sequence of the most recently added line.

func (*Buffer) Since

func (b *Buffer) Since(seq int64) ([]Line, int)

Since returns the lines added after seq, which is how a follower catches up without re-reading the window it already has.

func (*Buffer) Snapshot

func (b *Buffer) Snapshot() ([]Line, int)

Snapshot returns everything held, oldest first, and how many lines the ring has dropped since it started.

type Edge

type Edge struct {
	Kind      string
	Pod       string
	Container string
	Time      time.Time
	Reason    string
}

Edge is one boundary in what is knowable.

type KubeSource

type KubeSource struct {
	Client    kubernetes.Interface
	Namespace string
	PodNames  func(ctx context.Context) ([]string, error)
}

KubeSource reads logs from a cluster.

The pod set comes from a caller-supplied function rather than a label selector, because the grouping engine already decides which pods belong to an app, and a selector would be a second, disagreeing answer.

func (KubeSource) Open

func (k KubeSource) Open(ctx context.Context, t Target, opts OpenOptions) (io.ReadCloser, error)

Open starts one container's stream. Timestamps are always requested: without them there is no way to merge replicas into a single true order.

func (KubeSource) Targets

func (k KubeSource) Targets(ctx context.Context) ([]Target, error)

Targets lists every container of every pod currently backing the workload.

type Line

type Line struct {
	Time      time.Time
	Pod       string
	Container string
	Text      string
	Previous  bool
	// Truncated marks a line cut at MaxLineBytes.
	Truncated bool
	// Late marks a line that arrived too long after its timestamp to be
	// placed in order. Saying so beats silently misplacing it.
	Late bool
	// Seq is the buffer's monotonic sequence, so a client can ask for
	// everything after what it already has.
	Seq int64
}

Line is one log line, merged.

type Meta

type Meta struct {
	Pod       string
	Container string
	// Previous marks output from the instance before the last restart. The
	// kubelet keeps exactly one back, never two.
	Previous bool
}

Meta is what every line from one stream carries.

type OpenOptions

type OpenOptions struct {
	Follow    bool
	Previous  bool
	TailLines int64
	SinceTime *time.Time
}

OpenOptions is what a stream is opened with.

type Options

type Options struct {
	// Buffer is the ring size. Zero takes DefaultBufferLines.
	Buffer int
	// Containers, when set, restricts to these container names.
	Containers []string
	// Pods, when set, restricts to these pods. Per-pod is a filter on the
	// merged stream, not a different way in.
	Pods []string
	// IncludeSidecars reveals mesh proxies, hidden by default so their access
	// lines do not bury application output.
	IncludeSidecars bool
	// IncludeInit reveals init containers.
	IncludeInit bool
	// Previous reads the instance before the last restart.
	Previous bool
	// Tail is how many lines to ask for on open. Zero asks for the source's
	// default.
	Tail int64
	// Since bounds the window, for binding logs to a timeline selection.
	Since *time.Time
	// Refresh is how often the target list is re-read.
	Refresh time.Duration
	// Reopen is how long to wait before reconnecting a stream that ended. A
	// container that restarts ends its stream; treating that as the end of the
	// story would render a crash loop as a quiet period.
	Reopen time.Duration

	OnLine func(Line)
	OnEdge func(Edge)
}

Options configures a Streamer.

type Source

type Source interface {
	// Targets lists the container instances currently backing the workload.
	// It is called again on every refresh, which is how a rollout's new
	// replicas join the stream.
	Targets(ctx context.Context) ([]Target, error)
	Open(ctx context.Context, t Target, opts OpenOptions) (io.ReadCloser, error)
}

Source is the cluster side of a log stream, kept an interface so the merge logic is tested without an apiserver.

type Streamer

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

Streamer merges every replica of one workload into one buffer.

func NewStreamer

func NewStreamer(src Source, opts Options) *Streamer

NewStreamer builds a streamer. Nothing is read until Run.

func (*Streamer) Buffer

func (s *Streamer) Buffer() *Buffer

Buffer is the merged ring.

func (*Streamer) Edges

func (s *Streamer) Edges() []Edge

Edges are the availability boundaries recorded so far.

func (*Streamer) Run

func (s *Streamer) Run(ctx context.Context)

Run reads until ctx is cancelled.

type Target

type Target struct {
	Namespace string
	Pod       string
	Container string
	// Init marks an init container, which is behind an explicit toggle: its
	// output is usually setup noise, until the setup is what broke.
	Init bool
	// Previous asks for the instance before the last restart.
	Previous bool
}

Target is one container instance to read.

Jump to

Keyboard shortcuts

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