dlog

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package dlog is shortly for deduplicated logger.

Package dlog provides normalization and bucketing of noisy error strings.

Goals:

  • Strip volatile noise (timestamps, bracketed junk, addr:port).
  • Derive a short, stable reason (e.g. "connection refused").
  • Map reason into a coarse bucket (e.g. "net.refused") for metrics.

Matching prefers the RIGHT-MOST known phrase (not relying on ": "), because the canonical cause often sits at the end of chain-like messages.

IMPORTANT: If no known phrase matches, this package returns the normalized input verbatim as BOTH (reason, bucket). Никаких "other".

Index

Constants

View Source
const (
	Component = "component"
	Event     = "event"
	Scope     = "scope"
)

Variables

View Source
var ErrCh = make(chan error, runtime.GOMAXPROCS(0)*4)

Functions

func Err

func Err(err error)

Err - hot path method for don't affect too much performance on errors.

func Normalize added in v1.6.0

func Normalize(raw string) (reason, bucket string)

Normalize converts raw error text to a canonical (reason, bucket). Behavior:

  • If a known phrase is found → returns that (reason, bucket).
  • If no known phrase is found → returns normalized input as BOTH (reason, bucket).
  • If input is empty → returns ("", "").

func Record added in v1.6.0

func Record(errText string) (bucket, reason string)

Record normalizes errText into (bucket, reason), increments DefaultMeter if present, and returns (bucket, reason) for convenient logging.

Example:

b, r := dlog.Record(err.Error())
log.Error().Str("err_bucket", b).Str("err_reason", r).Msg("upstream call failed")

func RegisterPattern added in v1.6.0

func RegisterPattern(substr, reason, bucket string)

RegisterPattern adds a custom substring pattern to the matcher. Parameters should be lowercase and free of volatile noise; Normalize() lowercases before matching.

Example:

dlog.RegisterPattern("backend overload", "upstream busy", "lb.upstream")

func SetMeter added in v1.6.0

func SetMeter(m Meter)

SetMeter assigns a global meter used by Record().

func StartDedupLogger added in v1.5.0

func StartDedupLogger(ctx context.Context)

Types

type Meter added in v1.6.0

type Meter interface {
	// Inc increments a counter for (bucket, reason).
	Inc(bucket, reason string)
}

Meter is the minimal metrics sink used by this package. Provide your own adapter (e.g., Prometheus/VictoriaMetrics CounterVec{bucket,reason}).

var DefaultMeter Meter

DefaultMeter is an optional global hook. If set, Record() will bump it.

type SimpleMeter added in v1.6.0

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

SimpleMeter is a tiny in-process counter map: (bucket|reason) → u64. Полезно для тестов/локальной отладки; в проде — ваш метрик-бэкенд.

func NewSimpleMeter added in v1.6.0

func NewSimpleMeter() *SimpleMeter

NewSimpleMeter creates a SimpleMeter.

func (*SimpleMeter) Inc added in v1.6.0

func (m *SimpleMeter) Inc(bucket, reason string)

Inc implements Meter. Не подставляет "other": пустые значения — no-op.

func (*SimpleMeter) Snapshot added in v1.6.0

func (m *SimpleMeter) Snapshot() map[string]uint64

Snapshot returns a copy of counters for export/debugging.

Jump to

Keyboard shortcuts

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