meter

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 4 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 meter counts what a unit of the loop's work costs at its boundaries. A unit is a Scope carried by a context; adapters add a Sample per boundary call, and a Registry on the context keeps totals per scope since start.

Index

Constants

View Source
const Untagged = "untagged"

Untagged names the registry's totals of samples added outside any scope.

Variables

View Source
var Resources = []Resource{SQL, Tx, Workspace, GitHub, Git, Agent, Work}

Resources is every Resource in display order.

Functions

func Add

func Add(ctx context.Context, x Sample)

Add records x in ctx's scope chain and registry; without a scope it goes to the registry under Untagged; without either it is dropped.

func Job

func Job(name string) string

Job is the scope name of a daemon job.

func Loop

func Loop(name string) string

Loop is the scope name of a long-running loop.

func Route

func Route(pattern string) string

Route is the scope name of an HTTP route: the ServeMux pattern, never the URL path.

func Spend

func Spend(ctx context.Context, res Resource, op string, points int64)

Spend adds points to Key{res, op} without counting a call: the cost a response reports for a request its transport already counted.

func Trace

func Trace(ctx context.Context) context.Context

Trace makes every scope opened under ctx keep its samples, and adapters fill Detail. For tests; production never traces.

func Tracing

func Tracing(ctx context.Context) bool

Tracing reports whether ctx traces.

func WithRegistry

func WithRegistry(ctx context.Context, r *Registry) context.Context

WithRegistry returns ctx carrying r; a nil r returns ctx.

Types

type Key

type Key struct {
	Res Resource
	Op  string // "" in a per-resource roll-up
}

Key names one operation of one resource.

type Registry

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

Registry keeps totals per scope full name since it was made. Safe for concurrent use.

func NewRegistry

func NewRegistry(now func() time.Time) *Registry

NewRegistry makes a registry whose clock is now; a nil now is time.Now.

func (*Registry) Add added in v0.3.0

func (r *Registry) Add(name string, x Sample, spend bool)

Add records x in r under the scope full name name, with no scope open: a sample that reaches the host after the scope it was made under ended.

func (*Registry) Recent

func (r *Registry) Recent(d time.Duration) (time.Time, []Total)

Recent is what each scope spent since the newest mark at least d old, and that mark's time; before one exists, since the registry was made. Runs, Wall and Tally are the window's; Last and its fields are the totals'.

func (*Registry) Since

func (r *Registry) Since() time.Time

Since is when the registry was made.

func (*Registry) Totals

func (r *Registry) Totals() []Total

Totals is one entry per scope full name that got a sample or ended, sorted by Name. Tallies are copies.

type Resource

type Resource string

Resource is the boundary a sample crossed, named by port.

const (
	SQL       Resource = "sql"       // one statement; Op "query" or "exec"; Rows for a query
	Tx        Resource = "tx"        // one transaction, Dur from begin to commit or rollback; Op "read" (no exec ran) or "write"
	Workspace Resource = "workspace" // one workspace-manager call; Op its subcommand or method, such as "worktree ps"
	GitHub    Resource = "github"    // one GitHub API request; Op "graphql", "rest" or "gh <subcommand>"
	Git       Resource = "git"       // one git command; Op its subcommand
	Agent     Resource = "agent"     // one headless agent run; Op "ask" or "shell"
	Work      Resource = "work"      // one in-process unit its package names, such as "derive rows"
)

type Sample

type Sample struct {
	Res Resource
	// Op is bounded: a subcommand, a method or a statement class; never an
	// id, a ref, a path or a URL.
	Op    string
	Spawn bool // the call started a process
	Dur   time.Duration
	// Err is a failure other than the caller's cancellation or deadline:
	// adapters set it only when err != nil && ctx.Err() == nil.
	Err         bool
	NotModified bool  // a conditional request answered 304
	Points      int64 // GitHub rate-limit points the call spent, when the response says
	Bytes       int64 // response bytes read
	Rows        int64 // rows a query returned
	// Detail is the statement text (whitespace collapsed) or the argv, cut
	// to 300 bytes. Adapters fill it only when Tracing(ctx).
	Detail string
}

Sample is one call across a boundary.

type Scope

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

Scope is one unit of work. Safe for concurrent Add.

func ScopeOf added in v0.3.0

func ScopeOf(ctx context.Context) *Scope

ScopeOf is the scope ctx carries; nil when it carries none.

func Start

func Start(ctx context.Context, name string) (context.Context, *Scope)

Start opens a scope under ctx's scope, if any. Its full name is name, or "<parent full name>/<name>" when nested. Samples added under the returned context reach this scope and every enclosing one, and reach the registry on ctx, if any, once, under this scope's full name.

func (*Scope) Add added in v0.3.0

func (s *Scope) Add(x Sample, spend bool)

Add records x in s, its enclosing scopes and the registry s reports to, as Add does under a context that carries s. Spend says x only adds its Points, the cost a response reports for a request already counted.

func (*Scope) End

func (s *Scope) End() Tally

End freezes the tally and counts one run of the scope's name in the registry, with its wall time and tally as the name's Last. A second End returns the same tally. Samples after End still reach the registry, not the tally.

func (*Scope) Name

func (s *Scope) Name() string

Name is the scope's full name.

func (*Scope) Samples

func (s *Scope) Samples() []Sample

Samples are the samples in arrival order when the scope traces, nil otherwise.

func (*Scope) Tally

func (s *Scope) Tally() Tally

Tally is the scope's tally so far.

type Stat

type Stat struct {
	Calls, Spawns, Errors, NotModified int64
	Points, Bytes, Rows                int64
	Dur, Max                           time.Duration
}

Stat aggregates samples of one Key. Calls counts samples; Spend adds Points without a call.

func (*Stat) Add

func (s *Stat) Add(x Sample)

Add counts x as one call.

func (Stat) Merge

func (s Stat) Merge(o Stat) Stat

Merge is the sum of s and o.

type Tally

type Tally map[Key]Stat

Tally is stats by Key: a value, safe to keep and compare.

func (Tally) ByResource

func (t Tally) ByResource() Tally

ByResource folds every op into Op "".

func (Tally) Keys

func (t Tally) Keys() []Key

Keys is the tally's keys in Resources order, then by Op.

func (Tally) Merge

func (t Tally) Merge(o Tally) Tally

Merge is a new tally holding t and o.

func (Tally) Sub

func (t Tally) Sub(o Tally) Tally

Sub is t less o, key by key, without the keys left with nothing; Max stays t's.

func (Tally) Total

func (t Tally) Total(res Resource) Stat

Total is every op of res.

type Total

type Total struct {
	Name     string
	Runs     int64         // scopes of this name ended
	Wall     time.Duration // summed over ended scopes
	Tally    Tally         // every sample since Since, ended scopes or not
	Last     Tally         // the last ended scope's tally; nil before one ends
	LastAt   time.Time
	LastWall time.Duration
}

Total is a registry's totals for one scope full name.

Directories

Path Synopsis
Package metertest asserts what a unit of work cost, in the manner of assertNumQueries: a failure lists the samples so an N+1 shows.
Package metertest asserts what a unit of work cost, in the manner of assertNumQueries: a failure lists the samples so an N+1 shows.

Jump to

Keyboard shortcuts

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