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 ¶
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 )
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.
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.
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.
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 ¶
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.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is the per-workload ring.
func (*Buffer) Add ¶
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.
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.
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 ¶
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 ¶
NewStreamer builds a streamer. Nothing is read until Run.
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.