metrics

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 17 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 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

View Source
const Namespace = "backd"

Namespace prefixes every metric name.

View Source
const UnmatchedRoute = "unmatched"

UnmatchedRoute is the route label of a request no route matched, so probing random paths can't create series.

Variables

View Source
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.

View Source
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

func JobKind

func JobKind(email, erase, check, image, org, scheduled bool) string

JobKind names a job's kind from what it holds: the label values are this closed list.

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

func New(version, commit string) *Metrics

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

func (m *Metrics) AggregateRan(realm, database, collection, outcome string, took time.Duration)

AggregateRan records an aggregation: its outcome and, when it reached the database, how long it took.

func (*Metrics) DuplicateID added in v0.8.2

func (m *Metrics) DuplicateID(realm, database, collection string)

DuplicateID counts a create that asked for an id that exists.

func (*Metrics) EgressRequest

func (m *Metrics) EgressRequest(outcome string)

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

func (m *Metrics) ExecutorError(kind string)

ExecutorError records a call that failed because the executor couldn't be used.

func (*Metrics) ExecutorFinished

func (m *Metrics) ExecutorFinished(status string, took time.Duration, ran bool)

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

func (m *Metrics) FeedLag(consumer string, lag time.Duration)

FeedLag records how far behind the cluster a consumer is reading.

func (*Metrics) FeedReopened added in v0.8.5

func (m *Metrics) FeedReopened(consumer, reason string)

FeedReopened counts a stream opened again.

func (*Metrics) FeedState added in v0.8.5

func (m *Metrics) FeedState(consumer string, open bool, watched int)

FeedState records whether a change-stream consumer has its stream open and how many collections it watches.

func (*Metrics) FunctionRan

func (m *Metrics) FunctionRan(realm, function, mode, status string, took time.Duration)

FunctionRan records a function run that ended: mode is sync, async or webhook; status is the executor's terminal status.

func (*Metrics) FunctionRefused

func (m *Metrics) FunctionRefused(realm, function, reason string)

FunctionRefused records a call refused before it ran.

func (*Metrics) FunctionReplayed

func (m *Metrics) FunctionReplayed(realm, function string)

FunctionReplayed records a call answered from a stored idempotent result.

func (*Metrics) Handler

func (m *Metrics) Handler(token string) http.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

func (m *Metrics) JobFinished(realm, kind, status string)

JobFinished counts a job a worker finished, by kind and the result's status.

func (*Metrics) JobLeaseExpired

func (m *Metrics) JobLeaseExpired(realm, kind string)

JobLeaseExpired counts a job claimed again after its worker's lease ran out.

func (*Metrics) JobRetried

func (m *Metrics) JobRetried(realm, kind string)

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) PushDeliveries added in v0.8.2

func (m *Metrics) PushDeliveries(realm, status string, n int)

PushDeliveries counts n devices by the status a delivery unit gave them.

func (*Metrics) PushPruned added in v0.8.2

func (m *Metrics) PushPruned(realm, reason string, n int64)

PushPruned counts n devices removed from the registry for a reason.

func (*Metrics) PushUsers added in v0.8.2

func (m *Metrics) PushUsers(realm, outcome string, n int)

PushUsers counts n users by what planning a push did with them.

func (*Metrics) RateLimited

func (m *Metrics) RateLimited(realm, scope string)

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

func (m *Metrics) RequestDone(method, route string, status int, took time.Duration)

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) 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

func (m *Metrics) SessionStarted(realm string)

SessionStarted counts a new session.

func (*Metrics) SessionsEnded

func (m *Metrics) SessionsEnded(realm, reason string, n int64)

SessionsEnded counts n sessions ended for a reason.

func (*Metrics) SetJobs

func (m *Metrics) SetJobs(realm string, stats []JobStat, needsAttention int64)

SetJobs replaces what the job gauges report for realm.

func (*Metrics) SetSerialWaiting added in v0.8.5

func (m *Metrics) SetSerialWaiting(realm string, n int64)

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.

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.

Jump to

Keyboard shortcuts

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