Documentation
¶
Overview ¶
Package dbm is database monitoring beyond counters (INF-027, APM-008, APM-024): bind-safe statement text, lock/wait chains and blocking sessions. The functions are pure; the engine modules in ../integrations feed them rows read through a read-only connection.
Index ¶
- Constants
- func BindSafe(q string) string
- func EmitSample(out sink.Sink, system, kind, sev string, payload any, attrs map[string]string, ...)
- func Hash(normalized string) string
- func HashSQL(stmt string, pg bool) (string, string)
- func KeyPattern(k string) string
- func NormalizeSQL(stmt string, pg bool) string
- func ObfuscatePlan(plan string) string
- func PGDialect(system string) bool
- func QueryHash(stmt string, pg bool) string
- func Redact(args []string, keyArgs int) string
- func Report(out sink.Sink, engine string, sessions []Session, now time.Time)
- func Shape(v any) any
- func ShapeText(v any) string
- type Activity
- type Candidate
- type Chain
- type Deltas
- type Sampler
- type Session
- type Statement
- type StatementDelta
Constants ¶
const MaxChains = 20
MaxChains bounds the chains reported per round.
const MaxSampleGroups = 100
MaxSampleGroups bounds the sample records reported per flush.
const MaxStatements = 200
MaxStatements bounds the per-query series reported per round.
Variables ¶
This section is empty.
Functions ¶
func BindSafe ¶
BindSafe replaces every literal (quoted string, number, dollar-quoted body) with `?` so a sample never carries a bound value, then collapses whitespace and bounds the length.
func EmitSample ¶
func EmitSample(out sink.Sink, system, kind, sev string, payload any, attrs map[string]string, now time.Time)
EmitSample writes one sample record (event.name `db.<kind>`): kind is query.sample, query.plan, wait, keyspace, advice or shard. The payload must already be value-free (BindSafe, Shape, Redact).
func Hash ¶
Hash is the canonical `db.query.hash` of normalized text (SQL from NormalizeSQL, or a document shape, Redis command pattern or Cypher text): XXH3-64, seed 0, as a decimal string.
func KeyPattern ¶
KeyPattern turns a key into its pattern: `user:1234:cart` -> `user:*:cart`, `sess:9f8e7d6c5b4a` -> `sess:*`. Segments are split on `:`, `/`, `.`, `-` and `_`; a segment holding a digit becomes `*`.
func NormalizeSQL ¶
NormalizeSQL is the canonical normalization: literals become `?`, comments go, whitespace collapses and `IN (?, ?)` becomes `IN (?+)`. pg is true for postgresql, cockroachdb, redshift.
func ObfuscatePlan ¶
ObfuscatePlan makes an EXPLAIN (FORMAT JSON) document bind-safe: every string value (conditions, filters, outputs) goes through BindSafe, so a plan of a statement run with real literals never carries them. Keys and numbers (costs, rows) are kept. "" if it is not JSON.
func Redact ¶
Redact keeps a command's name and replaces each argument with a key pattern (Redis, Memcached): runs of digits and hex ids become `*`, other arguments `?`. Only the first maxArgs are kept.
func Report ¶
Report emits the blocking gauges and one log record per chain. Statement text is bind-safe.
Types ¶
type Activity ¶
type Activity struct {
Database string
State string
WaitType string // wait_event_type ("" when on CPU)
Wait string // wait_event
QueryID string
Query string // raw text: kept in memory for EXPLAIN only, never reported as is
DurationMs float64
}
Activity is one non-idle session in one snapshot of the engine's activity view.
type Candidate ¶
type Candidate struct{ QueryID, Query, Raw string }
Candidate is a sampled statement the caller may EXPLAIN: Raw is the text as the engine ran it (with literals), Query its bind-safe form.
type Chain ¶
type Chain struct {
Root Session `json:"root"`
Waiters []Session `json:"waiters"`
Depth int `json:"depth"`
}
Chain is one blocking tree: Root blocks everything in Waiters (transitively, nearest first).
func BuildChains ¶
BuildChains turns sessions with their blockers into one chain per root blocker (a session that blocks others and is not itself blocked), longest first. A cycle (a deadlock the engine has not yet broken) has no root and is reported by its lowest pid with Depth counting the members. Chains are capped at MaxChains.
type Deltas ¶
type Deltas struct {
// contains filtered or unexported fields
}
Deltas turns cumulative statement rows into per-round deltas. Not safe for concurrent use.
func (*Deltas) Observe ¶
func (d *Deltas) Observe(rows []Statement, limit int) []StatementDelta
Observe takes one round of rows and returns the statements that ran since the previous round, most total time first, at most limit. A statement seen for the first time sets its baseline only (its history is not this round's work). A counter that went down was reset, so its current value is the delta.
type Sampler ¶
type Sampler struct {
// contains filtered or unexported fields
}
Sampler aggregates activity snapshots (taken every second) into groups of database, state, wait event and statement, so the agent ships one record per group per flush instead of one per session per second. Not safe for concurrent use.
type Session ¶
type Session struct {
PID int `json:"pid"`
BlockedBy []int `json:"blocked_by,omitempty"`
State string `json:"state,omitempty"`
WaitEvent string `json:"wait_event,omitempty"`
Query string `json:"query,omitempty"`
AgeMs float64 `json:"age_ms,omitempty"`
Locks []string `json:"locks,omitempty"`
}
Session is one database session, as the engine's activity view reports it.
type Statement ¶
type Statement struct {
Key string // e.g. database + "/" + queryid
QueryID string
Database string
Query string
Calls int64
TotalMs float64
Rows int64
BlksHit int64
BlksRead int64
}
Statement is one row of the engine's statement statistics (pg_stat_statements), cumulative since the engine last reset them.
type StatementDelta ¶
type StatementDelta struct {
Statement // running totals
MeanMs float64 // of the calls in this round
Delta Statement // this round only
}
StatementDelta is one statement's activity since the previous round plus the agent's own running totals of it, which keep counting across an engine reset or eviction.