Help us shape the future of Go! Take the 2026 Go Developer Survey

charts

package
v1.3.104 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Overview

Package charts pkg/deployment/charts/service_stats.go c4-net-discovery

Package charts records a deployment service's own counts over time and renders them as a self-contained HTML page of SVG charts.

Index

Constants

View Source
const (
	// RawRetention is how long full-resolution samples are kept.
	RawRetention = 8 * 24 * time.Hour
	// HourlyRetention is how long the hourly means are kept.
	HourlyRetention = 400 * 24 * time.Hour
)
View Source
const Interval = 5 * time.Minute

Interval is how often a service samples its counts.

Variables

View Source
var Ranges = []Range{
	{Name: "24h", Span: 24 * time.Hour, Bucket: Interval},
	{Name: "7d", Span: 7 * 24 * time.Hour, Bucket: 30 * time.Minute},
	{Name: "30d", Span: 30 * 24 * time.Hour, Hourly: true, Bucket: 2 * time.Hour},
	{Name: "1y", Span: 365 * 24 * time.Hour, Hourly: true, Bucket: 24 * time.Hour},
}

Ranges are the windows every page offers, the first being the default.

Functions

func Bytes

func Bytes(v float64) string

Bytes formats a byte count with binary units.

func Color

func Color(name string) string

Color returns a stable color for a series name.

func Count

func Count(v float64) string

Count formats a count compactly: 950, 9.5k, 1.2M.

func Run

func Run(ctx context.Context, st Store, collect Collect, log logrus.FieldLogger)

Run records this start of the service, then samples on every Interval boundary until ctx ends.

func Serve

func Serve(ctx context.Context, addr string, page http.Handler, extra ...Extra) error

Serve answers plain HTTP on addr with the page at /, any extra pages at their paths, and 404 for every other path, until ctx ends.

func SetVersionLookup added in v1.3.101

func SetVersionLookup(f func(cipher.PubKey) string)

SetVersionLookup makes f the lookup of a caller's version by its key, used for requests that do not carry one. Transport discovery sets it.

Types

type Board added in v1.3.101

type Board struct {
	Title string
	About string
	// Sections is read on every render, so it can follow services that
	// start and stop.
	Sections func() []Section
	// Summary, when set, is a table shown before the sections.
	Summary func() *Table
	Log     logrus.FieldLogger
	// Separate keeps each section's process charts in it, for sections that
	// are separate processes, such as the visors of a hypervisor.
	Separate bool
	// contains filtered or unexported fields
}

Board serves the charts of several services as one page, a section each, such as every service a deployment host runs.

func (*Board) Extras

func (bd *Board) Extras() http.Handler

Extras serves the Extra pages of the board's sections at their paths and 404 for any other path. Mount it at "/" beside the board.

func (*Board) ServeHTTP added in v1.3.101

func (bd *Board) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP renders the board for ?range=, from a copy at most a minute old.

type Chart

type Chart struct {
	Title  string
	Note   string
	Kind   Kind
	From   time.Time
	To     time.Time
	Times  []time.Time
	Series []Series
	// Format renders a value. Count is used when it is nil.
	Format func(float64) string
	// Dates labels the x axis and readout with dates only.
	Dates bool
	// Binary rounds the y axis to powers of 1024, for byte counts.
	Binary bool
	// Legend, when set, draws the legend as a table with these column headers.
	// The first column is the series name and the last is its latest value.
	Legend []string
	// Process marks a chart of the whole process rather than one service. A
	// Board draws these once, not in each section of a service sharing it.
	Process bool
	// Starts are the service's starts, drawn as markers where they fall in the chart's window.
	Starts []Start
	// contains filtered or unexported fields
}

Chart is one chart on a page.

func (*Chart) SVG

func (c *Chart) SVG(id string) string

SVG renders the chart, followed by its legend and the readout data.

type Collect

type Collect func(ctx context.Context) (map[string]float64, error)

Collect reads the current value of every series. It returns an error when the counts cannot be trusted yet, and that sample is skipped rather than drawn as a dip.

type Content

type Content struct {
	Charts []Chart
	Tables []Table
}

Content is what a page shows for one range.

type Extra

type Extra struct {
	Path    string
	Handler http.Handler
}

Extra is another page a service serves next to its charts.

type Frame

type Frame struct {
	Times []time.Time
	// contains filtered or unexported fields
}

Frame lines samples up for charting. A row is nil where the series was not being recorded, so a chart breaks there instead of bridging the gap.

func NewFrame

func NewFrame(samples []Sample, step time.Duration) *Frame

NewFrame builds a frame from samples spaced step apart. A gap longer than two and a half steps gets an empty row.

func (*Frame) Group

func (f *Frame) Group(prefix string, top int) []Series

Group returns one series per key under prefix, named without the prefix. Past the top keys the rest are summed into "other".

func (*Frame) Keys

func (f *Frame) Keys(prefix string) []string

Keys returns the keys under prefix, largest latest value first.

func (*Frame) Latest

func (f *Frame) Latest(key string) (float64, bool)

Latest is key's value in the newest sample that carries it.

func (*Frame) Values

func (f *Frame) Values(key string, missingZero bool) []float64

Values returns key's series. A sample without key reads as zero when missingZero is set, which suits counts of things that can drop to none.

type Kind

type Kind int

Kind is how a chart draws its series.

const (
	// Lines draws each series as its own line.
	Lines Kind = iota
	// Stacked draws the series as stacked areas, so the top edge is the total.
	Stacked
)
type Link struct {
	Name string
	Href string
}

Link points to another page of the service.

type NoteError

type NoteError string

NoteError is a section's render error that the board shows in its place.

func (NoteError) Error

func (e NoteError) Error() string

type Page

type Page struct {
	Title string
	// About is a line under the title, such as the service's public key.
	About string
	// Links are other pages of the service, shown under the title.
	Links []Link
	// Extra are the pages Links point to, so a Board that shows this page
	// can serve them too.
	Extra []Extra
	Build func(ctx context.Context, r Range, now time.Time) (Content, error)
	// Stats, when set, adds the service's process and traffic charts after
	// Build's, read from Store.
	Stats *ServiceStats
	Store Store
	// Log, when set, records why a render failed; the client only sees a 503.
	Log logrus.FieldLogger
	// contains filtered or unexported fields
}

Page serves a service's charts as one self-contained HTML document.

func (*Page) ServeHTTP

func (p *Page) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP renders the page for ?range=, from a copy at most a minute old.

type Range

type Range struct {
	Name string
	Span time.Duration
	// Hourly reads the hourly means instead of the raw samples.
	Hourly bool
	// Bucket averages the samples down to this width.
	Bucket time.Duration
}

Range is one of the windows a page can show.

func (Range) Frame

func (r Range) Frame(ctx context.Context, st Store, now time.Time) (*Frame, time.Time, error)

Frame reads r's window from st, ending at now. The window starts at the first sample when the history is shorter than the range.

type Sample

type Sample struct {
	At time.Time          `json:"-"`
	V  map[string]float64 `json:"v"`
}

Sample is one observation of every series at one instant.

func Bucket

func Bucket(samples []Sample, d time.Duration) []Sample

Bucket averages samples into buckets of width d, one sample per bucket.

func Mean

func Mean(at time.Time, samples []Sample) Sample

Mean averages samples into one sample at at. A series missing from some samples is averaged over the samples that carry it.

type Section added in v1.3.101

type Section struct {
	Title string
	About string
	// Page is the service's charts. Nil shows the title, About and Note only.
	Page *Page
	// Note is shown under the title, such as why there are no charts.
	Note string
}

Section is one service on a Board.

type Series

type Series struct {
	Name string
	// Title is shown on hover over the legend entry, such as a full key.
	Title string
	// Cells fill the columns named by the chart's Legend.
	Cells []string
	Vals  []float64
}

Series is one named series aligned with its chart's Times. NaN is a gap.

type ServiceStats

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

ServiceStats samples what a service's process does and what its listeners carry, for the service's status page: CPU, memory, goroutines and open files of the process, bytes and streams per dmsg listening port (its HTTP API and each CXO feed), and HTTP requests by endpoint and status.

func NewServiceStats

func NewServiceStats(name string) *ServiceStats

NewServiceStats returns an empty ServiceStats for the named service.

func NewStatsView

func NewStatsView(name string) *ServiceStats

NewStatsView draws the samples another process's ServiceStats took, such as a visor's on its hypervisor's page. It collects nothing.

func (*ServiceStats) Charts

func (s *ServiceStats) Charts(f *Frame, from, to time.Time) []Chart

Charts draws the figures Collect sampled.

func (*ServiceStats) Collect

func (s *ServiceStats) Collect(v map[string]float64)

Collect adds this interval's figures to v: process use, the bytes and streams each listening port took since the last call, and the HTTP requests since then.

func (*ServiceStats) CountDmsg

func (s *ServiceStats) CountDmsg(c *dmsg.Client)

CountDmsg counts the traffic c accepts on its listening ports.

func (*ServiceStats) Handler

func (s *ServiceStats) Handler(next http.Handler) http.Handler

Handler counts next's requests by route pattern, status class, bytes sent and time taken. An httputil.Router reports its pattern; anything else is counted by its first path segment.

func (*ServiceStats) NamePort

func (s *ServiceStats) NamePort(port uint16, name string)

NamePort names a dmsg listening port on the page. Unnamed ports show by number.

func (*ServiceStats) Wrap

func (s *ServiceStats) Wrap(collect Collect) Collect

Wrap adds this interval's figures to every sample collect takes.

type Start

type Start struct {
	At      time.Time `json:"-"`
	Version string    `json:"version"`
	Commit  string    `json:"commit,omitempty"`
}

Start is one start of the service and the build it ran, drawn as a marker on every chart so a change in a series can be matched to a deploy.

func (Start) Label

func (s Start) Label() string

Label names the build, adding the short commit when the version lacks it.

type Store

type Store interface {
	Add(ctx context.Context, s Sample) error
	// Range returns samples in [from, to), oldest first, from the hourly
	// series when hourly is set.
	Range(ctx context.Context, from, to time.Time, hourly bool) ([]Sample, error)
	// AddStart records a start of the service, and Starts returns those in [from, to).
	AddStart(ctx context.Context, s Start) error
	Starts(ctx context.Context, from, to time.Time) ([]Start, error)
}

Store keeps raw samples and their hourly means.

func NewBboltStore

func NewBboltStore(db *bbolt.DB, name string) (Store, error)

NewBboltStore returns a Store kept in the bucket named name of db, which the caller owns and closes.

func NewMemoryStore

func NewMemoryStore() Store

NewMemoryStore returns a Store that lives in process memory.

func NewRedisStore

func NewRedisStore(url, password, prefix string) (Store, error)

NewRedisStore returns a Store kept under prefix in the redis at url.

type Table

type Table struct {
	Title string
	Note  string
	Head  []string
	Rows  [][]string
	// Marks, when set, gives a row a color to stand out in. Empty leaves it plain.
	Marks []string
	// Tall keeps a long table to a scrolling box with its header in view.
	Tall bool
}

Table is a plain table shown after the charts.

Jump to

Keyboard shortcuts

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