cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package cache implements the collect-on-an-interval / serve-from-cache model: a background scheduler gathers metrics every interval into an atomically-swapped snapshot, and the HTTP handler serves that snapshot. Hardware is therefore never read on a scrape — only on a tick.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache holds the latest Snapshot for lock-free reads.

func (*Cache) Gather

func (c *Cache) Gather(now func() time.Time) ([]*dto.MetricFamily, error)

Gather serves the cached families plus freshly-computed staleness meta-metrics (age is computed at call time, so it stays truthful between ticks). It never triggers a collection. Satisfies prometheus.GathererFunc when wrapped.

func (*Cache) Load

func (c *Cache) Load() *Snapshot

Load returns the current snapshot, or nil before the first collection.

func (*Cache) Store

func (c *Cache) Store(s *Snapshot)

Store atomically replaces the cached snapshot.

type Gatherer

type Gatherer interface {
	Gather() ([]*dto.MetricFamily, error)
}

Gatherer is the subset of prometheus.Gatherer the cache needs. Declared here at the point of consumption; a *prometheus.Registry satisfies it.

type Scheduler

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

Scheduler gathers from src into a Cache on an interval.

func NewScheduler

func NewScheduler(src Gatherer, cache *Cache, now func() time.Time, logger *slog.Logger) *Scheduler

NewScheduler wires a scheduler. now is injected for testability.

func (*Scheduler) CollectOnce

func (s *Scheduler) CollectOnce() error

CollectOnce gathers once and stores the snapshot. A gather error is logged but any partial families returned are still stored. Returns the gather error (used by the eager startup collection to surface problems).

func (*Scheduler) Run

func (s *Scheduler) Run(ctx context.Context, interval time.Duration)

Run collects every interval until ctx is cancelled.

type Snapshot

type Snapshot struct {
	Families    []*dto.MetricFamily
	CollectedAt time.Time
	Duration    time.Duration
}

Snapshot is one immutable collection result.

Jump to

Keyboard shortcuts

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