logs

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 28 Imported by: 1 module, 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 collects system logs: file tails (with offsets kept per inode and multiline joining), journald, the Windows Event Log and a syslog receiver, all forwarded as OTLP logs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KafkaRecord

func KafkaRecord(out sink.Sink, r *kgo.Record)

KafkaRecord writes one record as a log line.

func Run

func Run(ctx context.Context, raw json.RawMessage, stateDir string, out sink.Sink) error

Run starts every configured source and blocks until ctx ends or one fails.

func RunDocker

func RunDocker(ctx context.Context, c DockerLogs, stateDir string, out sink.Sink) error

RunDocker follows container logs until ctx ends.

func RunJournald

func RunJournald(ctx context.Context, path, stateDir string, out sink.Sink) error

RunJournald follows the journal with `journalctl -o json -f`. It keeps the __CURSOR of the last record in <stateDir>/journald.cursor (written atomically, at most every 5 s and when the process ends) and resumes with --after-cursor, so a restart neither loses the downtime nor replays more than a few seconds. The binary path is local config only. Off Linux it waits.

func RunKafka

func RunKafka(ctx context.Context, c KafkaLogs, out sink.Sink) error

RunKafka consumes until ctx ends.

func RunKafkaBatches added in v0.3.1

func RunKafkaBatches(ctx context.Context, c KafkaLogs, handle func(context.Context, []*kgo.Record) error) error

RunKafkaBatches consumes until ctx ends and hands each fetched batch to handle; offsets are committed only after handle returns nil (a handle error ends the consumer, nothing committed).

func RunSyslog

func RunSyslog(ctx context.Context, c SyslogConf, out sink.Sink) error

RunSyslog listens until ctx ends.

func RunTail

func RunTail(ctx context.Context, specs []TailSpec, stateDir string, out sink.Sink) error

RunTail polls the files until ctx ends.

func RunWinEvents

func RunWinEvents(ctx context.Context, _ WinEvent, _ sink.Sink) error

RunWinEvents has nothing to read off Windows.

func Severity

func Severity(body string) string

Severity guesses a level from the first line of a record; it defaults to INFO.

Types

type Config

type Config struct {
	MatchRules             // include_at_match / exclude_at_match for every input
	Enabled    *bool       `json:"enabled"`
	Files      []TailSpec  `json:"files"`
	Journal    *Journal    `json:"journald"`
	WinEvt     *WinEvent   `json:"windows_events"`
	Syslog     *SyslogConf `json:"syslog"`
	Docker     *DockerLogs `json:"docker"`
	Kafka      *KafkaLogs  `json:"kafka"`
}

Config is the logs module's config. Only Enabled can be set remotely (Interval has no meaning here); every path, command and listen address is local-file only.

type DockerLogs

type DockerLogs struct {
	MatchRules                   // include_at_match / exclude_at_match on top of the global ones
	Host       string            `json:"host"`   // unix:///var/run/docker.sock (default) or http(s)://
	Labels     map[string]string `json:"labels"` // only containers carrying every label (server-side filter)
	// RequireLabels makes empty Labels follow nothing instead of every container (shared hosts).
	RequireLabels bool `json:"require_labels"`
	// MaxRecordsPerSec caps records per container; the excess is dropped and reported as a WARN
	// record. 0 (default) means no cap, so a busy workload never loses logs unless asked.
	MaxRecordsPerSec int `json:"max_records_per_second"`
}

DockerLogs selects the engine and containers.

type Entry

type Entry struct {
	Off   int64 `json:"off"`
	Size  int64 `json:"size"`  // size when last read
	MTime int64 `json:"mtime"` // mtime (unix ns) when last read
	Seen  int64 `json:"seen"`  // unix seconds the file was last matched
}

Entry is what is remembered per file identity.

type Journal

type Journal struct {
	Path string `json:"journalctl_path"` // default "journalctl"
}

Journal enables the journald reader.

type KafkaLogs

type KafkaLogs struct {
	Brokers []string `json:"brokers"`
	Topics  []string `json:"topics"`
	Group   string   `json:"group"` // default wt-agent-logs
	TLS     bool     `json:"tls"`
	SASL    *struct {
		Username string `json:"username"`
		Password string `json:"password"`
	} `json:"sasl_plain"`
}

KafkaLogs is the consumer's configuration. The SASL password may be a secret reference.

type MatchRules added in v0.3.1

type MatchRules struct {
	Include []string `json:"include_at_match"`
	Exclude []string `json:"exclude_at_match"`
}

MatchRules drops log records on the host, before they are batched or sent: `include_at_match` keeps only records matching at least one of the regexes, `exclude_at_match` drops records matching any of them (exclusion wins). It sits on the global logs config, on a file input and on the docker input; the regexes see the whole record body (a joined multiline record included). Empty: everything passes.

func (MatchRules) Wrap added in v0.3.1

func (m MatchRules) Wrap(out sink.Sink) (sink.Sink, error)

Wrap returns out with the rules applied to its log records (out itself when there are none).

type Multiline

type Multiline struct {
	StartPattern string `json:"start_pattern"`
	MaxLines     int    `json:"max_lines"` // default 500
	MaxWait      string `json:"max_wait"`  // default 2s: a pending record is flushed after this much quiet
	// Auto picks the start pattern itself (timestamp shapes, log levels, JSON objects) from the
	// first lines of each file when no start_pattern is set; see detectStart.
	Auto bool `json:"auto"`
}

Multiline joins the lines of one record (a stack trace) per file input: `multiline: {start_pattern, max_lines, max_wait}`. A line matching StartPattern begins a new record. It supersedes the older flat `multiline_start` key, which still works.

type Record

type Record struct {
	Severity string // DEBUG, INFO, WARN or ERROR
	Body     string
	At       time.Time
	Attrs    map[string]string
	Cursor   string // journald __CURSOR
}

Record is one log record before export.

func ParseEventXML

func ParseEventXML(x string) (Record, error)

ParseEventXML converts a rendered event (EvtRenderEventXml) into a record. The body is the provider, the event id and the event data; the localized message text is not resolved.

func ParseJournalLine

func ParseJournalLine(line []byte) (Record, error)

ParseJournalLine parses one line of `journalctl -o json`.

func ParseSyslog

func ParseSyslog(msg string, now time.Time) (Record, error)

ParseSyslog parses an RFC 5424 or RFC 3164 message. year/location defaults for the old format (which carries neither) come from now.

type State

type State struct {
	Off map[string]Entry `json:"offsets"`
	// contains filtered or unexported fields
}

State persists, per file identity (device+inode, or volume+index on Windows), the offset up to which records were delivered, so a restart resumes exactly there and a rename keeps its place. Size and mtime expose a reused inode: a "new" file that is smaller or older than what was last read under that identity starts at 0. It is written (fsync) only when something changed.

func LoadState

func LoadState(dir string) *State

LoadState reads <dir>/tail-offsets.json; a missing or torn file starts empty.

func MemState

func MemState() *State

MemState keeps offsets in memory only: after a restart, existing files are followed from their end again.

func (*State) Save

func (s *State) Save() error

Save writes the state atomically (temp file, fsync, rename) if it changed.

type SyslogConf

type SyslogConf struct {
	UDP string `json:"udp"` // e.g. "127.0.0.1:5514"
	TCP string `json:"tcp"`
}

SyslogConf makes the agent a syslog receiver. Addresses come from the local file only. The listener is unauthenticated: bind it to loopback or a trusted interface, and use a port above 1024 for an unprivileged agent.

type TailSpec

type TailSpec struct {
	MatchRules        // include_at_match / exclude_at_match for this input
	Path       string `json:"path"` // a filepath.Glob pattern
	// MultilineStart is a regex that matches the first line of a record; lines that do not match
	// are joined to the previous record (stack traces). Empty: one line, one record.
	MultilineStart string `json:"multiline_start"`
	// Multiline is the fuller form (pattern, line cap, quiet period); see multiline.go.
	Multiline *Multiline `json:"multiline"`
	// FromBeginning reads files found at startup from their first byte (default: from the end).
	// Files that appear later are always read whole.
	FromBeginning bool              `json:"from_beginning"`
	Labels        map[string]string `json:"labels"`
	// MaxFiles raises the per-pattern file cap (default 200, at most 5000); the Kubernetes node
	// agent needs it for nodes with many containers.
	MaxFiles int `json:"max_files"`
}

TailSpec is one file pattern to follow. All of it comes from the agent's own config file.

type Tailer

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

Tailer follows files.

func NewTailer

func NewTailer(specs []TailSpec, st *State, out sink.Sink) (*Tailer, error)

NewTailer compiles the specs.

func (*Tailer) Poll

func (t *Tailer) Poll(now time.Time)

Poll reads what each file gained since the last poll, delivers complete records and persists the offsets.

type WinEvent

type WinEvent struct {
	Channels []string `json:"channels"` // default System and Application
	Query    string   `json:"query"`    // XPath filter, default "*"
}

WinEvent configures the Windows Event Log subscription (Windows only; elsewhere it waits).

Jump to

Keyboard shortcuts

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