uptime

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 7 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 uptime computes uptime and latency from heartbeats and the consensus status history, maintains the hourly and daily rollups, compacts old raw data and enforces retention (FA-UPT-01..06).

Uptime % = up / (total − maintenance − unknown) × 100. DEGRADED and PARTIAL count as up (FA-UPT-01). All computations are pure functions of the stored data, so every voter derives the same rollups (FA-UPT-03).

Index

Constants

View Source
const (

	// CompactAfter is how long raw heartbeats are kept in full (FA-UPT-04).
	CompactAfter = 48 * time.Hour
)

Variables

View Source
var DefaultRetention = Retention{Raw: 30 * 24 * time.Hour, Hourly: 365 * 24 * time.Hour}

DefaultRetention follows Pflichtenheft 7.3.

Functions

func Percent

func Percent(r store.Rollup) (float64, bool)

Percent returns up / (up + down) × 100, or false if nothing is known.

Types

type Calculator

type Calculator struct {
	State   store.State
	Metrics store.Metrics
}

Calculator computes buckets from the raw data.

func (*Calculator) Compute

func (c *Calculator) Compute(ctx context.Context, m store.Monitor, start, end int64, opts ComputeOptions) ([]store.Rollup, error)

Compute returns the rollups of [start, end): the consensus view first (if opts.NodeID is ""), then one per node that reported, ordered by node ID. For the consensus alone without latency, heartbeats are not read.

type ComputeOptions

type ComputeOptions struct {
	BucketMs int64
	// NodeID restricts the result to one node; "" includes the consensus.
	NodeID string
	// ConsensusOnly skips per-node rollups.
	ConsensusOnly bool
	// Latency reads heartbeats for latency even if only the consensus is needed.
	Latency bool
}

ComputeOptions select what Compute returns.

type Job

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

Job maintains rollups, compaction and retention of metrics.db. It runs locally on every voter; the results are deterministic (FA-UPT-03).

func NewJob

func NewJob(opts JobOptions) *Job

NewJob creates the job.

func (*Job) Run

func (j *Job) Run(ctx context.Context, interval time.Duration)

Run calls RunOnce every interval until ctx ends.

func (*Job) RunOnce

func (j *Job) RunOnce(ctx context.Context) error

RunOnce rolls up finished hours and days, compacts raw data older than CompactAfter and applies retention once per hour.

type JobOptions

type JobOptions struct {
	State   store.State
	Metrics store.Metrics
	// Vacuum returns freed pages of metrics.db to the file system.
	Vacuum func(ctx context.Context) error
	Logger *slog.Logger
	// Compaction enables FA-UPT-04 (RAW_COMPACTION).
	Compaction bool
	Retention  Retention
	// Now returns the wall clock; tests replace it.
	Now func() time.Time
	// ReplicatedUntil returns the time up to which the heartbeats of all peers
	// are replicated here (FA-REP-03). Hours after it are held back. Nil means
	// no hold-back.
	ReplicatedUntil func() time.Time
}

JobOptions configure a Job.

type Range

type Range string

Range is a reporting period of the API.

const (
	Range24h  Range = "24h"
	Range7d   Range = "7d"
	Range30d  Range = "30d"
	Range365d Range = "365d"
)

Supported ranges. 24h and 7d use hourly buckets, 30d and 365d daily buckets; the last bucket is the current, partial one.

type Retention

type Retention struct {
	Raw    time.Duration // heartbeats and latency minutes
	Hourly time.Duration
	Daily  time.Duration
}

Retention configures how long data is kept (FA-UPT-05). Zero keeps forever.

type Service

type Service struct {
	State   store.State
	Metrics store.Metrics
}

Service answers uptime queries from rollups and computes the buckets that are not rolled up yet from the raw data.

func (*Service) ConsensusTotals

func (s *Service) ConsensusTotals(ctx context.Context, tenantID string, ms []store.Monitor, r Range, now time.Time) (map[string]store.Rollup, error)

ConsensusTotals returns the consensus total of range r for each monitor of the tenant. For every monitor the total equals Stats(ctx, m, "", r, now, false).Total: the same hour selection and the same calculation, but the rollups and the status history of all monitors are loaded with one query each (NFA-PERF-03). Only hourly ranges are supported, and every monitor must belong to tenantID.

func (*Service) Stats

func (s *Service) Stats(ctx context.Context, m store.Monitor, nodeID string, r Range, now time.Time, latency bool) (Stats, error)

Stats returns the uptime of m over r, for one node or the consensus (nodeID ""). Without latency, consensus buckets are computed from the status history alone, which keeps monitor lists fast (NFA-PERF-03).

type Stats

type Stats struct {
	Range   Range
	NodeID  string // "" = consensus
	Total   store.Rollup
	Buckets []store.Rollup
}

Stats is the uptime of a monitor over a range.

Jump to

Keyboard shortcuts

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