* feat(observability): OpenTelemetry logs+traces with secret redaction Part of #265. Adds opt-in OpenTelemetry (logs + traces) alongside the existing stderr + opslog pipeline, plus secret redaction on all sinks. Default-off: with no OTEL_* / SILO_OTEL_ENABLED config, behavior is unchanged. Bootstrap (internal/telemetry): - Setup() builds one shared resource, a TracerProvider (parent-based trace-id ratio sampler), a LoggerProvider, and the W3C TraceContext+Baggage propagator from env. It installs NO MeterProvider — metrics stay on Prometheus, and the built-in no-op global MeterProvider keeps the trace instrumentation libs from double-emitting. Shutdown is deferred with a flush timeout. - Logs are bridged via otelslog fan-out (slog.MultiHandler), level-gated by the shared LevelVar and best-effort so a failing collector can't break the console or DB branches. stderr + opslog stay untouched. Secret redaction (internal/logredact): - A slog.Handler masks secret-keyed attributes (password, token, api_key, authorization, cookie, ...) — including .With-bound attrs, nested groups, secret-keyed group subtrees, and values behind a LogValuer — on the console and OTLP sinks, with a no-op fast path when a record has no secret keys. opslog.shouldRedact delegates to logredact.SecretKey so all sinks share one marker list. Rotation is infra-managed (no custom file sink): container runtime for stderr, collector/backend for OTLP, opslog partition-pruning for the DB. Documented in docs/architecture/observability.md. Verification: go build ./..., go vet, gofmt -l — clean; go test ./internal/telemetry/ ./internal/logredact/ -race pass. AI-use disclosure: implemented with AI assistance (Claude Code), including adversarial reviews that hardened the bootstrap and fixed two redaction leak paths; reviewed by the author. * refactor(observability): slog context+component sweep, sloglint gate (phase 3) Part of #265. Builds on the OTel bootstrap + redaction commit. Standardizes every log call site onto the context-carrying slog variants so records correlate with the active OpenTelemetry trace, and locks the standard in with a machine gate so future code (human- or AI-authored) can't drift back. - Call-site sweep: converted the remaining slog.<Level>(...) calls to the slog.<Level>Context(ctx, ...) form wherever a context.Context is in scope (background/init calls with no ctx are left as-is), across 183 files. Applied via a type-aware AST codemod. Log levels and message strings are preserved verbatim; a component attr (canonical per-package name) is added to direct package-level slog calls. Bound-logger calls keep their existing .With bindings. The main.go and telemetry package conversions rode with their file in the previous commit to keep each file within a single commit. - Enforcement (.golangci.yml): enable sloglint with context=scope, static-msg, key-naming-case=snake, no-mixed-args. After the sweep all four report zero violations repo-wide (tests included), so make lint / CI now blocks any regression to the non-context form. The gate ships with the sweep because it cannot be green until the legacy sites are converted. Metrics remain on Prometheus; no behavior change to /metrics or Grafana. Verification: go build ./..., go vet ./..., gofmt -l — clean; sloglint (all 4 rules) 0 violations repo-wide; log levels verified unchanged. AI-use disclosure: implemented with AI assistance (Claude Code), including the codemod; reviewed by the author. * fix(observability): honor per-signal OTLP protocol and secret WithGroup names Two Codex review findings on PR #290: - telemetry: OTEL_EXPORTER_OTLP_{TRACES,LOGS}_PROTOCOL now override the generic OTEL_EXPORTER_OTLP_PROTOCOL per signal, so mixed collector setups (e.g. HTTP logs + gRPC traces) build the right exporter. - logredact: entering a group whose name is secret-bearing (e.g. WithGroup("authorization")) now masks every leaf in that subtree, matching how slog.Group("authorization", ...) is masked as a whole. * fix(observability): address review feedback on telemetry bootstrap - Telemetry setup failure no longer kills boot: Setup returns usable no-op providers alongside the error and main logs and continues with telemetry disabled, honoring the best-effort contract. - Honor OTEL_TRACES_SAMPLER (always_on/off, traceidratio, parentbased_* variants); unsupported values fall back to parentbased_traceidratio. - Attach node identity as semconv service.instance.id instead of the non-semconv node.name. - Rename opslog retention-scope log attrs to target_component/target_level so they no longer collide with the canonical component routing key, and tag those lines with component=opslog. - Fix stale levelGated comment casing; use WarnContext in the telemetry shutdown defer; document the LogValuer double-resolve on the redaction slow path. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Quick <31828688+Quick104@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
224 lines
6.1 KiB
Go
224 lines
6.1 KiB
Go
package opslog
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/redis/go-redis/v9"
|
|
|
|
"github.com/Silo-Server/silo-server/internal/logstream"
|
|
)
|
|
|
|
var popBatchScript = redis.NewScript(`
|
|
local key = KEYS[1]
|
|
local count = tonumber(ARGV[1])
|
|
local items = redis.call('LRANGE', key, 0, count - 1)
|
|
if #items > 0 then
|
|
redis.call('LTRIM', key, count, -1)
|
|
end
|
|
return items
|
|
`)
|
|
|
|
type Consumer struct {
|
|
pool *pgxpool.Pool
|
|
redis *redis.Client
|
|
batchSize int
|
|
interval time.Duration
|
|
maxRetries int
|
|
streamHub *logstream.Hub
|
|
}
|
|
|
|
func NewConsumer(pool *pgxpool.Pool, redisClient *redis.Client, streamHub *logstream.Hub) *Consumer {
|
|
return &Consumer{
|
|
pool: pool,
|
|
redis: redisClient,
|
|
batchSize: 100,
|
|
interval: 2 * time.Second,
|
|
maxRetries: 3,
|
|
streamHub: streamHub,
|
|
}
|
|
}
|
|
|
|
func (c *Consumer) RunRedis(ctx context.Context) {
|
|
ticker := time.NewTicker(c.interval)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
c.drainRedis(context.Background())
|
|
return
|
|
case <-ticker.C:
|
|
c.drainRedis(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Consumer) drainRedis(ctx context.Context) {
|
|
for {
|
|
entries, err := c.popRedisBatch(ctx)
|
|
if err != nil {
|
|
slog.WarnContext(ctx, "opslog: Redis pop error", "component", "opslog", "error", err)
|
|
return
|
|
}
|
|
if len(entries) == 0 {
|
|
return
|
|
}
|
|
if err := c.insertBatchWithRetry(ctx, entries); err != nil {
|
|
slog.ErrorContext(ctx, "opslog: batch insert failed after retries, dropping batch", "component", "opslog", "error", err, "count", len(entries))
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Consumer) popRedisBatch(ctx context.Context) ([]Entry, error) {
|
|
result, err := popBatchScript.Run(ctx, c.redis, []string{redisKey}, c.batchSize).StringSlice()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("pop batch script: %w", err)
|
|
}
|
|
|
|
entries := make([]Entry, 0, len(result))
|
|
for _, raw := range result {
|
|
var entry Entry
|
|
if err := json.Unmarshal([]byte(raw), &entry); err != nil {
|
|
slog.WarnContext(ctx, "opslog: skipping malformed entry", "component", "opslog", "error", err)
|
|
continue
|
|
}
|
|
entries = append(entries, entry)
|
|
}
|
|
return entries, nil
|
|
}
|
|
|
|
func (c *Consumer) insertBatchWithRetry(ctx context.Context, entries []Entry) error {
|
|
var lastErr error
|
|
for attempt := 0; attempt < c.maxRetries; attempt++ {
|
|
if err := c.insertBatch(ctx, entries); err != nil {
|
|
lastErr = err
|
|
slog.WarnContext(ctx, "opslog: batch insert attempt failed", "component", "opslog", "attempt", attempt+1, "error", err)
|
|
time.Sleep(time.Duration(attempt+1) * time.Second)
|
|
continue
|
|
}
|
|
return nil
|
|
}
|
|
return lastErr
|
|
}
|
|
|
|
func (c *Consumer) RunMemory(ctx context.Context, ch <-chan Entry) {
|
|
ticker := time.NewTicker(c.interval)
|
|
defer ticker.Stop()
|
|
|
|
var batch []Entry
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
if len(batch) > 0 {
|
|
if err := c.insertBatch(context.Background(), batch); err != nil {
|
|
slog.WarnContext(ctx, "opslog batch insert failed", "component", "opslog", "entries", len(batch), "error", err)
|
|
}
|
|
}
|
|
return
|
|
case entry, ok := <-ch:
|
|
if !ok {
|
|
if len(batch) > 0 {
|
|
if err := c.insertBatch(context.Background(), batch); err != nil {
|
|
slog.WarnContext(ctx, "opslog batch insert failed", "component", "opslog", "entries", len(batch), "error", err)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
batch = append(batch, entry)
|
|
if len(batch) >= c.batchSize {
|
|
if err := c.insertBatch(ctx, batch); err != nil {
|
|
slog.WarnContext(ctx, "opslog batch insert failed", "component", "opslog", "entries", len(batch), "error", err)
|
|
}
|
|
batch = batch[:0]
|
|
}
|
|
case <-ticker.C:
|
|
if len(batch) > 0 {
|
|
if err := c.insertBatch(ctx, batch); err != nil {
|
|
slog.WarnContext(ctx, "opslog batch insert failed", "component", "opslog", "entries", len(batch), "error", err)
|
|
}
|
|
batch = batch[:0]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (c *Consumer) insertBatch(ctx context.Context, entries []Entry) error {
|
|
if len(entries) == 0 {
|
|
return nil
|
|
}
|
|
|
|
var b strings.Builder
|
|
b.WriteString("INSERT INTO operational_logs (timestamp, level, component, message, request_id, user_id, session_id, playback_session_id, client_ip, node_id, attrs) VALUES ")
|
|
|
|
args := make([]any, 0, len(entries)*11)
|
|
for i, e := range entries {
|
|
if i > 0 {
|
|
b.WriteString(", ")
|
|
}
|
|
base := i * 11
|
|
fmt.Fprintf(&b, "($%d, $%d, $%d, $%d, $%d, $%d, $%d, $%d, NULLIF($%d, '')::inet, $%d, $%d::jsonb)",
|
|
base+1, base+2, base+3, base+4, base+5, base+6, base+7, base+8, base+9, base+10, base+11)
|
|
attrsJSON, err := json.Marshal(e.Attrs)
|
|
if err != nil {
|
|
attrsJSON = []byte(`{}`)
|
|
}
|
|
args = append(args, e.Timestamp, e.Level, e.Component, e.Message, e.RequestID, e.UserID, e.SessionID, e.PlaybackSessionID, e.ClientIP, e.NodeID, string(attrsJSON))
|
|
}
|
|
b.WriteString(" RETURNING id, timestamp, level, component, message, COALESCE(request_id, ''), user_id, COALESCE(session_id, ''), COALESCE(playback_session_id, ''), COALESCE(client_ip::text, ''), COALESCE(node_id, ''), attrs")
|
|
|
|
rows, err := c.pool.Query(ctx, b.String(), args...)
|
|
if err != nil {
|
|
return fmt.Errorf("batch insert: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
inserted := make([]EntryRow, 0, len(entries))
|
|
for rows.Next() {
|
|
var entry EntryRow
|
|
var attrsJSON []byte
|
|
if err := rows.Scan(
|
|
&entry.ID,
|
|
&entry.Timestamp,
|
|
&entry.Level,
|
|
&entry.Component,
|
|
&entry.Message,
|
|
&entry.RequestID,
|
|
&entry.UserID,
|
|
&entry.SessionID,
|
|
&entry.PlaybackSessionID,
|
|
&entry.ClientIP,
|
|
&entry.NodeID,
|
|
&attrsJSON,
|
|
); err != nil {
|
|
return fmt.Errorf("scan inserted operational log row: %w", err)
|
|
}
|
|
if len(attrsJSON) > 0 {
|
|
if err := json.Unmarshal(attrsJSON, &entry.Attrs); err != nil {
|
|
return fmt.Errorf("decode inserted operational log attrs: %w", err)
|
|
}
|
|
}
|
|
inserted = append(inserted, entry)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return fmt.Errorf("iterate inserted operational log rows: %w", err)
|
|
}
|
|
|
|
for _, entry := range inserted {
|
|
if err := c.streamHub.PublishAppend(ctx, logstream.StreamApp, entry); err != nil {
|
|
slog.WarnContext(ctx, "opslog: failed to publish log stream append", "component", "opslog", "error", err, "id", entry.ID)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|