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
- Variables
- func Bytes(v float64) string
- func Color(name string) string
- func Count(v float64) string
- func Run(ctx context.Context, st Store, collect Collect, log logrus.FieldLogger)
- func Serve(ctx context.Context, addr string, page http.Handler, extra ...Extra) error
- func SetVersionLookup(f func(cipher.PubKey) string)
- type Board
- type Chart
- type Collect
- type Content
- type Extra
- type Frame
- type Kind
- type Link
- type NoteError
- type Page
- type Range
- type Sample
- type Section
- type Series
- type ServiceStats
- func (s *ServiceStats) Charts(f *Frame, from, to time.Time) []Chart
- func (s *ServiceStats) Collect(v map[string]float64)
- func (s *ServiceStats) CountDmsg(c *dmsg.Client)
- func (s *ServiceStats) Handler(next http.Handler) http.Handler
- func (s *ServiceStats) NamePort(port uint16, name string)
- func (s *ServiceStats) Wrap(collect Collect) Collect
- type Start
- type Store
- type Table
Constants ¶
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 )
const Interval = 5 * time.Minute
Interval is how often a service samples its counts.
Variables ¶
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 Run ¶
Run records this start of the service, then samples on every Interval boundary until ctx ends.
func Serve ¶
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
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.
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.
type Collect ¶
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 Frame ¶
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 ¶
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 ¶
Group returns one series per key under prefix, named without the prefix. Past the top keys the rest are summed into "other".
type NoteError ¶
type NoteError string
NoteError is a section's render error that the board shows in its place.
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.
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.
type Sample ¶
Sample is one observation of every series at one instant.
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.
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 ¶
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 ¶
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.