anchorage v1.0 initial tree
Greenfield Go multi-tenant IPFS Pinning Service wire-compatible with the
IPFS Pinning Services API spec. Paired 1:1 with Kubo over localhost RPC,
clustered via embedded NATS JetStream, Postgres source-of-truth with
RLS-enforced tenancy, Fiber + huma v2 for the HTTP surface, Authentik
OIDC for session login with kid-rotated HS256 JWT API tokens.
Feature-complete against the 22-milestone build plan, including the
ship-it v1.0 gap items:
* admin CLIs: drain/uncordon, maintenance, mint-token, rotate-key,
prune-denylist, rebalance --dry-run, cache-stats, cluster-presences
* TTL leader election via NATS KV, fence tokens, JetStream dedup
* rebalancer (plan/apply split), reconciler, requeue sweeper
* ristretto caches with NATS-backed cross-node invalidation
(placements live-nodes + token denylist)
* maintenance watchdog for stuck cluster-pause flag
* Prometheus /metrics with CIDR ACL, HTTP/pin/scheduler/cache gauges
* rate limiting: session (10/min) + anonymous global (120/min)
* integration tests: rebalance, refcount multi-org, RLS belt
* goreleaser (tar + deb/rpm/apk + Alpine Docker) targeting Gitea
Stack: Cobra/Viper, Fiber v2 + huma v2, embedded NATS JetStream,
pgx/sqlc/golang-migrate, ristretto, TypeID, prometheus/client_golang,
testcontainers-go.
This commit is contained in:
commit
12bf35caf8
112 files changed
+16884
No files matched your search
@@ -0,0 +1,180 @@
|
||||
// Package logging configures anchorage's structured logger.
|
||||
//
|
||||
// The design mirrors the sibling kanrisha project: stdlib log/slog with a
|
||||
// fan-out handler that writes every record to both a rotating JSON file (all
|
||||
// levels ≥ configured level, for machine parsing) and stderr (warn+ only,
|
||||
// human-friendly format, for quick console checks).
|
||||
//
|
||||
// The logger is installed globally via slog.SetDefault so every package
|
||||
// can simply import "log/slog" and call slog.Info / slog.Error without
|
||||
// dependency wiring.
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
lumberjack "gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
// Config holds logging parameters, typically loaded from Viper.
|
||||
//
|
||||
// File is optional: when empty, the file handler is skipped and stderr
|
||||
// receives every record (useful for local dev and short-lived CLI commands).
|
||||
type Config struct {
|
||||
// Level is the minimum level for file output: "debug", "info", "warn", "error".
|
||||
// Empty string is treated as "info".
|
||||
Level string
|
||||
// Format controls stderr rendering: "json" or "text" (default).
|
||||
Format string
|
||||
// Source toggles source file:line attributes on every record.
|
||||
Source bool
|
||||
// File is the rotating log file path. Empty disables file logging.
|
||||
File string
|
||||
// MaxSizeMB is the rotation threshold in megabytes. Ignored when File is empty.
|
||||
MaxSizeMB int
|
||||
// MaxBackups caps the number of rotated files retained.
|
||||
MaxBackups int
|
||||
// MaxAgeDays caps the age (in days) of rotated files before deletion.
|
||||
MaxAgeDays int
|
||||
// Compress gzips rotated files when true.
|
||||
Compress bool
|
||||
}
|
||||
|
||||
// Init installs the process-wide slog.Default handler.
|
||||
//
|
||||
// Call once, after config has been loaded and before any subsystem startup
|
||||
// so that everything that fires off goroutines sees the correct logger.
|
||||
func Init(cfg Config) error {
|
||||
fileLevel, err := parseLevel(cfg.Level)
|
||||
if err != nil {
|
||||
return fmt.Errorf("logging level: %w", err)
|
||||
}
|
||||
|
||||
stderrFormat := strings.ToLower(cfg.Format)
|
||||
if stderrFormat != "" && stderrFormat != "json" && stderrFormat != "text" {
|
||||
return fmt.Errorf("unknown log format %q — must be json or text", cfg.Format)
|
||||
}
|
||||
|
||||
handlers := make([]slog.Handler, 0, 2)
|
||||
|
||||
// File handler: always JSON, respects configured level.
|
||||
if cfg.File != "" {
|
||||
fileWriter := &lumberjack.Logger{
|
||||
Filename: cfg.File,
|
||||
MaxSize: cfg.MaxSizeMB,
|
||||
MaxBackups: cfg.MaxBackups,
|
||||
MaxAge: cfg.MaxAgeDays,
|
||||
Compress: cfg.Compress,
|
||||
}
|
||||
handlers = append(handlers, slog.NewJSONHandler(fileWriter, &slog.HandlerOptions{
|
||||
Level: fileLevel,
|
||||
AddSource: cfg.Source,
|
||||
}))
|
||||
}
|
||||
|
||||
// Stderr handler. When the file handler is present, stderr is noisy-only
|
||||
// (warn+). Without a file handler, stderr must carry every record.
|
||||
stderrLevel := slog.LevelWarn
|
||||
if cfg.File == "" {
|
||||
stderrLevel = fileLevel
|
||||
}
|
||||
stderrOpts := &slog.HandlerOptions{
|
||||
Level: stderrLevel,
|
||||
AddSource: cfg.Source,
|
||||
}
|
||||
var stderrHandler slog.Handler
|
||||
switch stderrFormat {
|
||||
case "json":
|
||||
stderrHandler = slog.NewJSONHandler(os.Stderr, stderrOpts)
|
||||
default: // "text" or ""
|
||||
stderrHandler = slog.NewTextHandler(os.Stderr, stderrOpts)
|
||||
}
|
||||
handlers = append(handlers, stderrHandler)
|
||||
|
||||
slog.SetDefault(slog.New(&fanoutHandler{handlers: handlers}))
|
||||
slog.Debug("logging initialized")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// fanoutHandler dispatches each record to multiple slog.Handlers.
|
||||
//
|
||||
// Mirrors kanrisha's implementation: clones the record per handler so state
|
||||
// (attrs, groups) does not leak between outputs.
|
||||
type fanoutHandler struct {
|
||||
handlers []slog.Handler
|
||||
attrs []slog.Attr
|
||||
groups []string
|
||||
}
|
||||
|
||||
// Enabled reports whether at least one child handler would accept the level.
|
||||
func (h *fanoutHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||
for _, handler := range h.handlers {
|
||||
if handler.Enabled(context.Background(), level) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Handle forwards the record to every child handler that accepts its level.
|
||||
func (h *fanoutHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
for _, handler := range h.handlers {
|
||||
if handler.Enabled(ctx, r.Level) {
|
||||
if err := handler.Handle(ctx, r.Clone()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WithAttrs returns a fresh fanoutHandler that applies the attrs to each child.
|
||||
func (h *fanoutHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
newHandlers := make([]slog.Handler, len(h.handlers))
|
||||
for i, handler := range h.handlers {
|
||||
newHandlers[i] = handler.WithAttrs(attrs)
|
||||
}
|
||||
return &fanoutHandler{
|
||||
handlers: newHandlers,
|
||||
attrs: append(h.attrs, attrs...),
|
||||
groups: h.groups,
|
||||
}
|
||||
}
|
||||
|
||||
// WithGroup returns a fresh fanoutHandler that applies the group to each child.
|
||||
func (h *fanoutHandler) WithGroup(name string) slog.Handler {
|
||||
newHandlers := make([]slog.Handler, len(h.handlers))
|
||||
for i, handler := range h.handlers {
|
||||
newHandlers[i] = handler.WithGroup(name)
|
||||
}
|
||||
return &fanoutHandler{
|
||||
handlers: newHandlers,
|
||||
attrs: h.attrs,
|
||||
groups: append(h.groups, name),
|
||||
}
|
||||
}
|
||||
|
||||
// parseLevel converts a config string to slog.Level.
|
||||
//
|
||||
// Empty string and "info" both map to LevelInfo so zero-valued Configs are
|
||||
// ergonomic. Unknown values return an error (and LevelInfo as a safe default
|
||||
// in case the caller logs the error and carries on).
|
||||
func parseLevel(s string) (slog.Level, error) {
|
||||
switch strings.ToLower(s) {
|
||||
case "debug":
|
||||
return slog.LevelDebug, nil
|
||||
case "info", "":
|
||||
return slog.LevelInfo, nil
|
||||
case "warn", "warning":
|
||||
return slog.LevelWarn, nil
|
||||
case "error":
|
||||
return slog.LevelError, nil
|
||||
default:
|
||||
return slog.LevelInfo, fmt.Errorf("unknown log level %q — must be debug, info, warn, or error", s)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package logging
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseLevel(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want slog.Level
|
||||
err bool
|
||||
}{
|
||||
{"debug", slog.LevelDebug, false},
|
||||
{"info", slog.LevelInfo, false},
|
||||
{"warn", slog.LevelWarn, false},
|
||||
{"warning", slog.LevelWarn, false},
|
||||
{"error", slog.LevelError, false},
|
||||
{"", slog.LevelInfo, false},
|
||||
{"DEBUG", slog.LevelDebug, false},
|
||||
{"INFO", slog.LevelInfo, false},
|
||||
{"WARN", slog.LevelWarn, false},
|
||||
{"ERROR", slog.LevelError, false},
|
||||
{"invalid", slog.LevelInfo, true},
|
||||
{"trace", slog.LevelInfo, true},
|
||||
{"fatal", slog.LevelInfo, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.input, func(t *testing.T) {
|
||||
got, err := parseLevel(tt.input)
|
||||
if tt.err && err == nil {
|
||||
t.Errorf("parseLevel(%q) expected error, got nil", tt.input)
|
||||
}
|
||||
if !tt.err && err != nil {
|
||||
t.Errorf("parseLevel(%q) unexpected error: %v", tt.input, err)
|
||||
}
|
||||
if !tt.err && got != tt.want {
|
||||
t.Errorf("parseLevel(%q) = %v, want %v", tt.input, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// recordingHandler captures log records for testing.
|
||||
type recordingHandler struct {
|
||||
records []slog.Record
|
||||
level slog.Level
|
||||
}
|
||||
|
||||
func (h *recordingHandler) Enabled(_ context.Context, level slog.Level) bool {
|
||||
return level >= h.level
|
||||
}
|
||||
|
||||
func (h *recordingHandler) Handle(_ context.Context, r slog.Record) error {
|
||||
h.records = append(h.records, r)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *recordingHandler) WithAttrs(_ []slog.Attr) slog.Handler { return h }
|
||||
func (h *recordingHandler) WithGroup(_ string) slog.Handler { return h }
|
||||
|
||||
func TestFanoutHandlerDispatchesBothHandlers(t *testing.T) {
|
||||
all := &recordingHandler{level: slog.LevelDebug}
|
||||
errOnly := &recordingHandler{level: slog.LevelWarn}
|
||||
|
||||
handler := &fanoutHandler{handlers: []slog.Handler{all, errOnly}}
|
||||
logger := slog.New(handler)
|
||||
|
||||
logger.Debug("debug msg")
|
||||
logger.Info("info msg")
|
||||
logger.Warn("warn msg")
|
||||
logger.Error("error msg")
|
||||
|
||||
// The all-levels handler should see every record.
|
||||
if len(all.records) != 4 {
|
||||
t.Errorf("all handler got %d records, want 4", len(all.records))
|
||||
}
|
||||
|
||||
// The warn+ handler should see only warn and error.
|
||||
if len(errOnly.records) != 2 {
|
||||
t.Errorf("errOnly handler got %d records, want 2", len(errOnly.records))
|
||||
}
|
||||
|
||||
if len(errOnly.records) >= 2 {
|
||||
if errOnly.records[0].Level != slog.LevelWarn {
|
||||
t.Errorf("first errOnly record level = %v, want WARN", errOnly.records[0].Level)
|
||||
}
|
||||
if errOnly.records[1].Level != slog.LevelError {
|
||||
t.Errorf("second errOnly record level = %v, want ERROR", errOnly.records[1].Level)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFanoutHandlerEnabled(t *testing.T) {
|
||||
debugHandler := &recordingHandler{level: slog.LevelDebug}
|
||||
errorHandler := &recordingHandler{level: slog.LevelError}
|
||||
|
||||
handler := &fanoutHandler{handlers: []slog.Handler{debugHandler, errorHandler}}
|
||||
|
||||
// At least one child accepts debug → fanout accepts debug.
|
||||
if !handler.Enabled(context.Background(), slog.LevelDebug) {
|
||||
t.Error("expected Enabled(Debug) = true")
|
||||
}
|
||||
if !handler.Enabled(context.Background(), slog.LevelInfo) {
|
||||
t.Error("expected Enabled(Info) = true")
|
||||
}
|
||||
if !handler.Enabled(context.Background(), slog.LevelError) {
|
||||
t.Error("expected Enabled(Error) = true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFanoutHandlerEnabledNoneMatch(t *testing.T) {
|
||||
errorOnly := &recordingHandler{level: slog.LevelError}
|
||||
handler := &fanoutHandler{handlers: []slog.Handler{errorOnly}}
|
||||
|
||||
if handler.Enabled(context.Background(), slog.LevelDebug) {
|
||||
t.Error("expected Enabled(Debug) = false with error-only handler")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFanoutWithAttrs(t *testing.T) {
|
||||
h1 := &recordingHandler{level: slog.LevelInfo}
|
||||
h2 := &recordingHandler{level: slog.LevelInfo}
|
||||
handler := &fanoutHandler{handlers: []slog.Handler{h1, h2}}
|
||||
|
||||
newHandler := handler.WithAttrs([]slog.Attr{slog.String("key", "val")})
|
||||
if newHandler == nil {
|
||||
t.Fatal("WithAttrs returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFanoutWithGroup(t *testing.T) {
|
||||
h1 := &recordingHandler{level: slog.LevelInfo}
|
||||
h2 := &recordingHandler{level: slog.LevelInfo}
|
||||
handler := &fanoutHandler{handlers: []slog.Handler{h1, h2}}
|
||||
|
||||
newHandler := handler.WithGroup("test")
|
||||
if newHandler == nil {
|
||||
t.Fatal("WithGroup returned nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestInitStderrOnly verifies that an empty File falls back to stderr-only
|
||||
// logging (no lumberjack file handler) — used by `anchorage version` and
|
||||
// other short-lived commands that don't want to touch the filesystem.
|
||||
func TestInitStderrOnly(t *testing.T) {
|
||||
if err := Init(Config{Level: "debug", Format: "text"}); err != nil {
|
||||
t.Fatalf("Init with empty File returned error: %v", err)
|
||||
}
|
||||
// Should be a fanoutHandler with exactly one child (stderr).
|
||||
h, ok := slog.Default().Handler().(*fanoutHandler)
|
||||
if !ok {
|
||||
t.Fatalf("default handler is not fanoutHandler: %T", slog.Default().Handler())
|
||||
}
|
||||
if len(h.handlers) != 1 {
|
||||
t.Errorf("expected 1 child handler when File is empty, got %d", len(h.handlers))
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitRejectsBadFormat(t *testing.T) {
|
||||
err := Init(Config{Level: "info", Format: "yaml"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown format, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitRejectsBadLevel(t *testing.T) {
|
||||
err := Init(Config{Level: "trace"})
|
||||
if err == nil {
|
||||
t.Fatal("expected error for unknown level, got nil")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user