state

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 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 state owns everything Inobi persists.

Index

Constants

View Source
const (
	LargeValueSize = 1 << 20

	DefaultFlushInterval = time.Second
)
View Source
const (
	NSKV               = "h"
	NSComposite        = "c"
	NSTimeout          = "t"
	NSRisk             = "r"
	NSRiskAlertedLevel = "rl"
	NSSource           = "s"
	NSMeta             = "m"
)

Key layout: - h:<rule id>:<kv key> - c:<rule id>:<group> - t:<rule id>:<group> - r:<entity type>:<entity value> - rl:<entity type>:<entity value> - s:<source id> - m:<name>

Every part except the last is escaped.

View Source
const FormatVersion = 1

FormatVersion must be bumped whenever the key layout or a value encoding changes incompatibly.

Variables

View Source
var (
	ErrFormatVersion = errors.New("unsupported state format version")
	ErrNotExist      = errors.New("no state database")
	ErrInUse         = errors.New("state directory in use, ensure inobi is not running")
)
View Source
var ErrBadType = errors.New("bad type")

Functions

func AppendGroupKey

func AppendGroupKey(dst []byte, data map[string]any, fields [][]string) ([]byte, bool)

func EscapePart

func EscapePart(s string) string

Example: "a:b\c" -> "a\:b\\c"

func GroupKey

func GroupKey(data map[string]any, fields [][]string) (string, bool)

Example: data={"user": "a:b", "port": 22}, fields=[[user] [port]] -> "a\:b:22"

Types

type CompositeItem

type CompositeItem struct {
	Timestamp time.Time      `json:"t"`
	Event     map[string]any `json:"e"`
	Seq       int64          `json:"s,omitempty"`
}

type CompositeUpdate

type CompositeUpdate int
const (
	CompositeKeep CompositeUpdate = iota
	CompositeWrite
	CompositeClear
)

type Composites

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

func (Composites) Update

func (c Composites) Update(rid ruleset.RuleID, group string, ttl time.Duration,
	fn func(items *[]CompositeItem) CompositeUpdate) error

func (Composites) UpdateET

func (c Composites) UpdateET(rid ruleset.RuleID, group string, ttl time.Duration,
	fn func(st *ETCompositeState) CompositeUpdate) error

type DB

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

func Open

func Open(opts Options) (*DB, error)

func (*DB) Close

func (db *DB) Close() error

func (*DB) Composites

func (db *DB) Composites() Composites

func (*DB) DeleteEntries

func (db *DB) DeleteEntries(filter Filter) (int, error)

func (*DB) Entries

func (db *DB) Entries(filter Filter) ([]Entry, error)

func (*DB) Flush

func (db *DB) Flush() error

Flush writes all pending changes to Badger in one batch.

func (*DB) KV

func (db *DB) KV(rid ruleset.RuleID) KV

func (*DB) Risk

func (db *DB) Risk() Risk

func (*DB) RunGC

func (db *DB) RunGC(ctx context.Context)

func (*DB) Sources

func (db *DB) Sources() Sources

func (*DB) Timeouts

func (db *DB) Timeouts() Timeouts

func (*DB) Usage

func (db *DB) Usage(filter Filter) ([]Usage, error)

type ETCompositeState

type ETCompositeState struct {
	MaxEventTS time.Time         `json:"ts"`
	Parts      [][]CompositeItem `json:"p"`
	Chains     [][]CompositeItem `json:"c,omitempty"`
}

ETCompositeState holds each part's events sorted by time. Events older than MaxEventTS minus the allowed lateness are settled: no new event can precede them, so they are compacted (see engine.compactETComposite) into at most one item per part, or for ordered rules into Chains, where Chains[i] is the settled sequence of parts 0..i with the latest start.

type Entry

type Entry struct {
	Kind      Kind
	RuleID    ruleset.RuleID
	Key       string
	ExpiresAt time.Time // zero if the entry does not expire
	Size      int64
	Value     []byte
}

type Filter

type Filter struct {
	// Empty means every kind, or every rule-scoped kind when RuleID is set.
	Kinds  []Kind
	RuleID *ruleset.RuleID
}

type KV

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

func (KV) Add

func (kv KV) Add(k string, val any, ttl time.Duration) (float64, error)

func (KV) Delete

func (kv KV) Delete(k string) error

func (KV) Exists

func (kv KV) Exists(k string) (bool, error)

func (KV) Get

func (kv KV) Get(k string) (any, error)

func (KV) Set

func (kv KV) Set(k string, value any, ttl time.Duration) error

func (KV) Swap

func (kv KV) Swap(k string, value any, ttl time.Duration) (any, error)

type Kind

type Kind string
const (
	KindKV        Kind = "kv"
	KindComposite Kind = "composite"
	KindTimeout   Kind = "timeout"
	KindRisk      Kind = "risk"
	KindSource    Kind = "source"
)

func (Kind) RuleScoped

func (k Kind) RuleScoped() bool

type Options

type Options struct {
	Dir           string
	MustExist     bool
	InMemory      bool
	SyncWrites    bool
	FlushInterval time.Duration
	Logger        *slog.Logger
	Operations    *prometheus.CounterVec // Has "operation" and "status" labels.
	LargeValues   *prometheus.CounterVec // Has a "rule_id" label.
}

type Risk

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

func (Risk) AlertedLevel

func (r Risk) AlertedLevel(entityType, entityValue string) (int, bool, error)

func (Risk) Contributions

func (r Risk) Contributions(entityType, entityValue string) ([]RiskContribution, error)

func (Risk) Delete

func (r Risk) Delete(entityType, entityValue string) (bool, error)

func (Risk) DeleteAlertedLevel

func (r Risk) DeleteAlertedLevel(entityType, entityValue string) error

func (Risk) Entities

func (r Risk) Entities() ([]RiskEntity, error)

func (Risk) SetAlertedLevel

func (r Risk) SetAlertedLevel(entityType, entityValue string, rank int, ttl time.Duration) error

func (Risk) UpdateContributions

func (r Risk) UpdateContributions(entityType, entityValue string,
	fn func(entries *[]RiskContribution) time.Duration) error

type RiskContribution

type RiskContribution struct {
	RuleID      ruleset.RuleID `json:"r"`
	EntityIndex int            `json:"i"`
	Score       float64        `json:"s"`
	Scaling     float64        `json:"sc"`
	TTLSeconds  int64          `json:"ttl"`
	Decay       string         `json:"d"`
	CreatedAt   int64          `json:"t"`
}

type RiskEntity

type RiskEntity struct {
	Type          string
	Value         string
	Contributions []RiskContribution
	AlertedLevel  int
	Alerted       bool
}

type SourcePosition

type SourcePosition struct {
	ID    string
	Value []byte
}

type Sources

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

func (Sources) Delete

func (s Sources) Delete(id string) (bool, error)

Delete reports whether a position was saved for id.

func (Sources) List

func (s Sources) List() ([]SourcePosition, error)

func (Sources) Load

func (s Sources) Load(id string, v any) (bool, error)

Load reports false if nothing is saved for id or the saved value does not decode.

func (Sources) Save

func (s Sources) Save(id string, v any) error

type Timeouts

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

func (Timeouts) Check

func (t Timeouts) Check(rid ruleset.RuleID, group string, timeout time.Duration) (bool, error)

Check starts a new suppression period when the alert is not suppressed.

type Usage

type Usage struct {
	Kind   Kind
	RuleID ruleset.RuleID
	Keys   int64
	Bytes  int64
}

Jump to

Keyboard shortcuts

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