Documentation
¶
Overview ¶
Package metrics is backd's Prometheus instrumentation (design/metrics.md).
A process builds one *Metrics and passes it to what it measures. Every method accepts a nil receiver and does nothing, so code that is measured never asks whether metrics are on. Label values come from closed sets or from the configuration (route patterns, realm, database and function names), never from what a caller sent: no ids, emails, addresses, tokens, key names or free text.
Index ¶
- Constants
- Variables
- func JobKind(email, erase, check, image, org, scheduled bool) string
- type JobSource
- type JobStat
- type Metrics
- func (m *Metrics) AggregateRan(realm, database, collection, outcome string, took time.Duration)
- func (m *Metrics) DuplicateID(realm, database, collection string)
- func (m *Metrics) EgressRequest(outcome string)
- func (m *Metrics) ExecutorError(kind string)
- func (m *Metrics) ExecutorFinished(status string, took time.Duration, ran bool)
- func (m *Metrics) ExecutorStarted()
- func (m *Metrics) FeedEvent(consumer string, op changefeed.Op, system bool)
- func (m *Metrics) FeedLag(consumer string, lag time.Duration)
- func (m *Metrics) FeedReopened(consumer, reason string)
- func (m *Metrics) FeedState(consumer string, open bool, watched int)
- func (m *Metrics) FunctionRan(realm, function, mode, status string, took time.Duration)
- func (m *Metrics) FunctionRefused(realm, function, reason string)
- func (m *Metrics) FunctionReplayed(realm, function string)
- func (m *Metrics) Handler(token string) http.Handler
- func (m *Metrics) JobFinished(realm, kind, status string)
- func (m *Metrics) JobLeaseExpired(realm, kind string)
- func (m *Metrics) JobRetried(realm, kind string)
- func (m *Metrics) MongoMonitor() *event.CommandMonitor
- func (m *Metrics) PasskeyEvent(realm, event string)
- func (m *Metrics) PushDeliveries(realm, status string, n int)
- func (m *Metrics) PushPruned(realm, reason string, n int64)
- func (m *Metrics) PushUsers(realm, outcome string, n int)
- func (m *Metrics) RateLimited(realm, scope string)
- func (m *Metrics) Realtime() *RealtimeObserver
- func (m *Metrics) Register(c ...prometheus.Collector)
- func (m *Metrics) RequestDone(method, route string, status int, took time.Duration)
- func (m *Metrics) RequestStarted()
- func (m *Metrics) SchemaBelowCurrent(realm, collection string, n int64)
- func (m *Metrics) SchemaMigrated(realm, collection, result string, n int64)
- func (m *Metrics) Serve(ctx context.Context, addr, token string, shutdown time.Duration, ...) error
- func (m *Metrics) SessionStarted(realm string)
- func (m *Metrics) SessionsEnded(realm, reason string, n int64)
- func (m *Metrics) SetJobs(realm string, stats []JobStat, needsAttention int64)
- func (m *Metrics) SetSerialWaiting(realm string, n int64)
- func (m *Metrics) TriggerObserver() *TriggerObserver
- func (m *Metrics) WatchJobs(ctx context.Context, interval time.Duration, ...)
- func (m *Metrics) WatchMongo(ctx context.Context, ping func(context.Context) error, interval time.Duration)
- func (m *Metrics) WatchRateLimiter(size func() int, overflowed func() int64)
- type RealmJobs
- type RealtimeObserver
- func (o *RealtimeObserver) Checks(realm, kind string, n int)
- func (o *RealtimeObserver) Event(realm, event string)
- func (o *RealtimeObserver) Refused(realm, reason string)
- func (o *RealtimeObserver) Reset(realm, reason string)
- func (o *RealtimeObserver) StreamClosed(realm string)
- func (o *RealtimeObserver) StreamOpened(realm string)
- type TriggerObserver
- func (o *TriggerObserver) Backlog(realm string, n int)
- func (o *TriggerObserver) Dropped(realm, collection, trigger, reason string)
- func (o *TriggerObserver) Enqueued(realm string, created, duplicates int)
- func (o *TriggerObserver) Event(realm, collection, trigger, outcome string)
- func (o *TriggerObserver) Gap(realm, reason string)
- func (o *TriggerObserver) Lag(d time.Duration)
- func (o *TriggerObserver) Lease(held bool, epoch int64)
- func (o *TriggerObserver) Watermark(at time.Time)
Constants ¶
const Namespace = "backd"
Namespace prefixes every metric name.
const UnmatchedRoute = "unmatched"
UnmatchedRoute is the route label of a request no route matched, so probing random paths can't create series.
Variables ¶
var DurationBuckets = []float64{.001, .0025, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 30, 60}
DurationBuckets are the buckets of request and function durations, in seconds.
var MongoBuckets = []float64{.0005, .001, .0025, .005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
MongoBuckets are the buckets of MongoDB command durations, in seconds.
Functions ¶
Types ¶
type JobSource ¶
type JobSource interface {
JobStats(ctx context.Context, now time.Time) ([]JobStat, error)
EraseNeedsAttention(ctx context.Context) (int64, error)
}
JobSource is a store that can summarize its job queue; the MongoDB auth store is one.
type JobStat ¶
type JobStat struct {
Kind string // function, schedule, email, erase or check
// Queued jobs wait for a worker, Running ones hold a lease, Waiting ones
// wait out the delay of a retry.
Queued, Running, Waiting int64
// OldestQueued is when the longest-waiting queued job was created; zero
// without queued jobs.
OldestQueued time.Time
}
JobStat is the queue of one kind of job in one realm.
type Metrics ¶
type Metrics struct {
Registry *prometheus.Registry
// contains filtered or unexported fields
}
Metrics holds a process's collectors and the registry they are in. The registry is its own (not Prometheus' global one).
func New ¶
New builds the registry with the Go runtime and process collectors, the build information and the collectors of the HTTP API.
func (*Metrics) AggregateRan ¶ added in v0.8.4
AggregateRan records an aggregation: its outcome and, when it reached the database, how long it took.
func (*Metrics) DuplicateID ¶ added in v0.8.2
DuplicateID counts a create that asked for an id that exists.
func (*Metrics) EgressRequest ¶
EgressRequest counts a request to the egress proxy by what it decided: allowed, denied_credentials, denied_host or denied_address. Host names are never labels.
func (*Metrics) ExecutorError ¶
ExecutorError records a call that failed because the executor couldn't be used.
func (*Metrics) ExecutorFinished ¶
ExecutorFinished records a function process that ended (or a call the executor refused as busy), with the status it ended with.
func (*Metrics) ExecutorStarted ¶
func (m *Metrics) ExecutorStarted()
ExecutorStarted and ExecutorFinished bracket one function process.
func (*Metrics) FeedEvent ¶ added in v0.8.5
func (m *Metrics) FeedEvent(consumer string, op changefeed.Op, system bool)
FeedEvent counts a change a consumer read.
func (*Metrics) FeedLag ¶ added in v0.8.5
FeedLag records how far behind the cluster a consumer is reading.
func (*Metrics) FeedReopened ¶ added in v0.8.5
FeedReopened counts a stream opened again.
func (*Metrics) FeedState ¶ added in v0.8.5
FeedState records whether a change-stream consumer has its stream open and how many collections it watches.
func (*Metrics) FunctionRan ¶
FunctionRan records a function run that ended: mode is sync, async or webhook; status is the executor's terminal status.
func (*Metrics) FunctionRefused ¶
FunctionRefused records a call refused before it ran.
func (*Metrics) FunctionReplayed ¶
FunctionReplayed records a call answered from a stored idempotent result.
func (*Metrics) Handler ¶
Handler serves the metrics at /metrics and nothing else. With a token, every request must carry it as a bearer token.
func (*Metrics) JobFinished ¶
JobFinished counts a job a worker finished, by kind and the result's status.
func (*Metrics) JobLeaseExpired ¶
JobLeaseExpired counts a job claimed again after its worker's lease ran out.
func (*Metrics) JobRetried ¶
JobRetried counts a failed attempt that was queued again.
func (*Metrics) MongoMonitor ¶
func (m *Metrics) MongoMonitor() *event.CommandMonitor
MongoMonitor times every command the driver sends. Hand it to mongodb.Connect: one hook measures the storage, the auth stores, the jobs and everything else, without a wrapper per call. Nil when metrics are off.
func (*Metrics) PasskeyEvent ¶ added in v0.9.10
PasskeyEvent counts a passkey event: registered, removed or removed_by_admin.
func (*Metrics) PushDeliveries ¶ added in v0.8.2
PushDeliveries counts n devices by the status a delivery unit gave them.
func (*Metrics) PushPruned ¶ added in v0.8.2
PushPruned counts n devices removed from the registry for a reason.
func (*Metrics) PushUsers ¶ added in v0.8.2
PushUsers counts n users by what planning a push did with them.
func (*Metrics) RateLimited ¶
RateLimited counts a refusal by a limit.
func (*Metrics) Realtime ¶ added in v0.8.5
func (m *Metrics) Realtime() *RealtimeObserver
Realtime returns the observer for the realtime hub; nil on a nil Metrics.
func (*Metrics) Register ¶
func (m *Metrics) Register(c ...prometheus.Collector)
Register adds collectors a package defines for itself.
func (*Metrics) RequestDone ¶
RequestDone records a finished request. route is the router's pattern ("" for a request no route matched).
func (*Metrics) RequestStarted ¶
func (m *Metrics) RequestStarted()
RequestStarted and RequestDone bracket a request being answered.
func (*Metrics) SchemaBelowCurrent ¶ added in v0.9.11
SchemaBelowCurrent sets how many documents of the collection are below its current schema version.
func (*Metrics) SchemaMigrated ¶ added in v0.9.11
SchemaMigrated counts documents a schema migration job handled, by result.
func (*Metrics) Serve ¶
func (m *Metrics) Serve(ctx context.Context, addr, token string, shutdown time.Duration, log *slog.Logger) error
Serve answers /metrics on addr until ctx ends. An empty addr means metrics are off and it returns at once. The listener is its own server: whatever happens to it never touches the API.
func (*Metrics) SessionStarted ¶
SessionStarted counts a new session.
func (*Metrics) SessionsEnded ¶
SessionsEnded counts n sessions ended for a reason.
func (*Metrics) SetSerialWaiting ¶ added in v0.8.5
SetSerialWaiting records how many ordered jobs of a realm wait behind another.
func (*Metrics) TriggerObserver ¶ added in v0.8.5
func (m *Metrics) TriggerObserver() *TriggerObserver
TriggerObserver returns the observer for the trigger dispatcher; nil when metrics are off.
func (*Metrics) WatchJobs ¶
func (m *Metrics) WatchJobs(ctx context.Context, interval time.Duration, refresh func(context.Context) (map[string]RealmJobs, error))
WatchJobs refreshes the job gauges every interval until ctx ends. refresh reads the realms' queues (one realm failing must not hide the others: it returns what it could read). Errors are counted, and the last values stay.
func (*Metrics) WatchMongo ¶
func (m *Metrics) WatchMongo(ctx context.Context, ping func(context.Context) error, interval time.Duration)
WatchMongo pings MongoDB every interval until ctx ends and keeps backd_mongodb_up current. It does nothing when metrics are off.
func (*Metrics) WatchRateLimiter ¶ added in v0.9.12
WatchRateLimiter exposes the request limiter's table: how many buckets it holds and how many requests shared an overflow bucket because it was full. Registering twice keeps the first.
type RealmJobs ¶
type RealmJobs struct {
Stats []JobStat
NeedsAttention int64
// SerialWaiting counts ordered jobs waiting behind another of their key.
SerialWaiting int64
}
RealmJobs is what one realm's refresh found.
type RealtimeObserver ¶ added in v0.8.5
type RealtimeObserver struct {
// contains filtered or unexported fields
}
RealtimeObserver reports realtime streams.
func (*RealtimeObserver) Checks ¶ added in v0.8.5
func (o *RealtimeObserver) Checks(realm, kind string, n int)
func (*RealtimeObserver) Event ¶ added in v0.8.5
func (o *RealtimeObserver) Event(realm, event string)
func (*RealtimeObserver) Refused ¶ added in v0.8.5
func (o *RealtimeObserver) Refused(realm, reason string)
func (*RealtimeObserver) Reset ¶ added in v0.8.5
func (o *RealtimeObserver) Reset(realm, reason string)
func (*RealtimeObserver) StreamClosed ¶ added in v0.8.5
func (o *RealtimeObserver) StreamClosed(realm string)
func (*RealtimeObserver) StreamOpened ¶ added in v0.8.5
func (o *RealtimeObserver) StreamOpened(realm string)
type TriggerObserver ¶ added in v0.8.5
type TriggerObserver struct {
// contains filtered or unexported fields
}
TriggerObserver is the dispatcher's view of the metrics (triggers.Observer). A nil one does nothing.
func (*TriggerObserver) Backlog ¶ added in v0.8.5
func (o *TriggerObserver) Backlog(realm string, n int)
Backlog records a realm's unfinished trigger jobs.
func (*TriggerObserver) Dropped ¶ added in v0.8.5
func (o *TriggerObserver) Dropped(realm, collection, trigger, reason string)
Dropped counts an event that matched and was not queued.
func (*TriggerObserver) Enqueued ¶ added in v0.8.5
func (o *TriggerObserver) Enqueued(realm string, created, duplicates int)
Enqueued counts the jobs queued and the replays collapsed.
func (*TriggerObserver) Event ¶ added in v0.8.5
func (o *TriggerObserver) Event(realm, collection, trigger, outcome string)
Event counts a change a trigger considered.
func (*TriggerObserver) Gap ¶ added in v0.8.5
func (o *TriggerObserver) Gap(realm, reason string)
Gap counts a gap recorded.
func (*TriggerObserver) Lag ¶ added in v0.8.5
func (o *TriggerObserver) Lag(d time.Duration)
Lag records the time from a change to its jobs being queued.
func (*TriggerObserver) Lease ¶ added in v0.8.5
func (o *TriggerObserver) Lease(held bool, epoch int64)
Lease records whether this instance holds the lease.
func (*TriggerObserver) Watermark ¶ added in v0.8.5
func (o *TriggerObserver) Watermark(at time.Time)
Watermark records when the position was last stored.