loki

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 9 Imported by: ? 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 loki ships Beacon logs to a Grafana Loki instance via /loki/api/v1/push. Loki is the most common self-hosted target in homelab deployments — easy to compose, low ceremony, plays nicely with Grafana for ad-hoc querying.

Design notes:

  • Async, batched, lossy on shutdown. Logs are NOT critical-path: dropping a few records on a network blip is preferable to blocking app code on a remote write. The batcher accumulates up to MaxBatchSize records or MaxBatchInterval seconds (whichever first) and flushes in one POST.
  • One stream per service+level. Loki indexes "labels" (the keys you can filter on cheaply); attribute values go in the unindexed log line as JSON. We intentionally keep the label set tiny — `service` and `level` — to avoid the high- cardinality footgun where every (hash, episode) combination creates its own stream.
  • Auth: optional X-Scope-OrgID header for multi-tenant setups, plus optional Basic auth user/pass for managed Loki services.

Operator config (via env vars in the host service's main.go):

BEACON_LOG_LOKI_URL       — push endpoint base URL (e.g. http://loki:3100)
BEACON_LOG_LOKI_TENANT    — optional X-Scope-OrgID
BEACON_LOG_LOKI_USER      — optional basic auth user
BEACON_LOG_LOKI_PASS      — optional basic auth password

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Service string
	URL     string

	// Auth (all optional)
	TenantID  string
	BasicUser string
	BasicPass string

	// Batcher tuning. Defaults are reasonable for steady-state
	// homelab traffic; bump MaxBatchSize for very chatty services.
	MaxBatchSize     int           // default 200
	MaxBatchInterval time.Duration // default 2s
	Timeout          time.Duration // default 5s per push

	// Client overrides for testing.
	HTTPClient *http.Client
}

Config controls the Loki plugin. Service is required so log lines are tagged correctly in Loki's label index.

type Plugin

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

Plugin implements log.Plugin. Constructed by New, registered via system.Add(plugin).

func New

func New(cfg Config) (*Plugin, error)

New validates config and returns a started plugin. Returns an error when URL is empty (so the caller can decide whether to crash startup or just log a warning and continue without Loki).

func (*Plugin) Close

func (p *Plugin) Close(ctx context.Context) error

Close drains pending records up to a timeout. Returns the context error if the deadline is exceeded — the host service can decide whether to surface that to the operator.

func (*Plugin) Handler

func (p *Plugin) Handler() slog.Handler

Handler returns the slog.Handler that captures records into the async batcher.

func (*Plugin) Name

func (p *Plugin) Name() string

Name returns "loki".

Jump to

Keyboard shortcuts

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