metrics

package
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 17 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 metrics is phase 2 of the estimate (vision docs/specs/2026-10-10-savings-estimate-design.md §5): each eligible workload's requests per minute over 14 days, and the measured uptime of EC2 instances, ECS services and companion databases, read from CloudWatch.

This file ports manager packages/estimate/src/requests.ts: each workload's target group RequestCount (CloudWatch keeps one-minute data 15 days), five metrics per GetMetricData call (20,160 values each, under the 100,800 a call returns), every page, by each value's timestamp. CloudWatch publishes nothing for a minute without request: an empty series is a workload no one called; a metric it could not read is unreadable, never silence. Kept per account and region the same UTC day.

Index

Constants

View Source
const (
	ScaleSame         = "same"
	ScaleEC2PerMinute = "ec2PerMinute" // an EC2 5-minute sum, a minute
	ScaleRDSPerMinute = "rdsPerMinute" // RDS bytes a second, a minute
)

How a signal's queries make one value a point: each series the sum of its queries, scaled to the signal's unit; the largest series is the signal's value (the router's measured).

View Source
const (
	PhaseRequests = "requests"
	PhaseUptime   = "uptime"
)

Phases of the progress events this package emits.

View Source
const Days = 14

Days is the period the estimate reads by default (--days; CloudWatch keeps one-minute data 15 days).

View Source
const PhaseStandalone = "standalone"

PhaseStandalone is the Phase of the events the standalone signals' read emits.

View Source
const ScanByAscending = "TimestampAscending"

ScanByAscending asks the oldest values first (Node passes it on every call).

Variables

View Source
var DefaultKinds = UptimeKinds{EC2: true}

DefaultKinds is what Node measures when no kinds are given: the instances.

Functions

func DatabaseKey

func DatabaseKey(d discover.Database) string

DatabaseKey is a companion database's key in the uptime: its kind and identifier.

func Measured

func Measured(w discover.Workload, kinds UptimeKinds) bool

Measured says whether the option measures the workload's uptime.

func Unreadable added in v0.16.0

func Unreadable(err error) bool

Unreadable tells whether err is a metric CloudWatch could not read.

Types

type ActivityBackend added in v0.16.0

type ActivityBackend struct {
	Kind      string `json:"kind"`
	ID        string `json:"id"`
	Companion bool   `json:"companion,omitempty"`
}

ActivityBackend is a workload's backend as the plan reads it: its kind and id; Companion a server's companion database.

type ActivityPlanOut added in v0.16.0

type ActivityPlanOut struct {
	Signals []PlannedSignal `json:"signals"`
	Queries []ActivityQuery `json:"queries"`
}

ActivityPlanOut is the plan: the signals and the queries they read.

func ActivityPlan added in v0.16.0

func ActivityPlan(backends []ActivityBackend, settings ActivitySettings) ActivityPlanOut

ActivityPlan is the signals of a workload's resolved settings and their queries (the router's plan): CPU and network are its own backends' (its database's when its resource is the database), the metric's ${resource} its own id.

func StandalonePlan added in v0.16.0

StandalonePlan is a standalone resource's default signals as the activity's plan: each signal a query of its own, its series that query alone.

type ActivityPoints added in v0.16.0

type ActivityPoints struct {
	From   string                  `json:"from"`
	To     string                  `json:"to"`
	Points map[string][][2]float64 `json:"points"`
}

ActivityPoints are the points read: by query id, each point's minute from From and its value.

func ReadActivity added in v0.16.0

func ReadActivity(ctx context.Context, cw CloudWatchPort, queries []ActivityQuery, days int, now time.Time) (ActivityPoints, error)

ReadActivity reads the queries' 5-minute points over the days before now: the window ends at now's 5-minute boundary 10 minutes behind and starts days before; every page read.

type ActivityQuery added in v0.16.0

type ActivityQuery struct {
	ID         string      `json:"id"`
	Namespace  string      `json:"namespace"`
	Name       string      `json:"name"`
	Dimensions []Dimension `json:"dimensions"`
	Stat       string      `json:"stat"`
}

ActivityQuery is one CloudWatch query of the plan.

type ActivitySettings added in v0.16.0

type ActivitySettings struct {
	IdleCPUPercent   *float64
	IdleNetworkBytes *float64
	IdleMetric       *IdleMetric
}

ActivitySettings are a workload's resolved idle signals: a threshold, or nil for "off" (absent is off).

func (*ActivitySettings) UnmarshalJSON added in v0.16.0

func (s *ActivitySettings) UnmarshalJSON(b []byte) error

UnmarshalJSON reads each signal's number or object, or "off".

type CloudWatchOf

type CloudWatchOf func(ctx context.Context, account, region string) (CloudWatchPort, error)

CloudWatchOf gives an account's CloudWatch in a region (Node's cloudwatchOf).

type CloudWatchPort

type CloudWatchPort interface {
	GetMetricData(ctx context.Context, in GetMetricDataInput) (GetMetricDataOutput, error)
}

CloudWatchPort is the metrics API.

type Dimension

type Dimension = discover.Dimension

The query shapes are the scan's (discovery reads the same GetMetricData).

type DimensionSet added in v0.16.0

type DimensionSet []Dimension

DimensionSet is a custom metric's dimensions, in the order they are given (a JSON object, its keys in order).

func (DimensionSet) MarshalJSON added in v0.16.0

func (d DimensionSet) MarshalJSON() ([]byte, error)

MarshalJSON writes the object in order.

func (*DimensionSet) UnmarshalJSON added in v0.16.0

func (d *DimensionSet) UnmarshalJSON(b []byte) error

UnmarshalJSON reads an object, keeping its key order.

type GetMetricDataInput

type GetMetricDataInput struct {
	StartTime         time.Time
	EndTime           time.Time
	MetricDataQueries []MetricDataQuery
	ScanBy            string
	NextToken         string
}

GetMetricDataInput is a GetMetricData call. Its times are written as Node's toISOString does (2026-10-12T08:00:00.000Z).

func (GetMetricDataInput) MarshalJSON

func (in GetMetricDataInput) MarshalJSON() ([]byte, error)

MarshalJSON writes the input with its times as ISO strings.

type GetMetricDataOutput

type GetMetricDataOutput struct {
	MetricDataResults []MetricDataResult `json:"MetricDataResults,omitempty"`
	NextToken         string             `json:"NextToken,omitempty"`
}

GetMetricDataOutput is a page of results; NextToken says there is another.

type IdleMetric added in v0.16.0

type IdleMetric struct {
	Namespace  string
	Name       string
	Dimensions []DimensionSet
	List       bool
	Statistic  string
	Below      float64
}

IdleMetric is a workload's custom idle metric: idle under Below. Its dimensions are one set (List false: the query metric) or a list of sets (List true: a query each, metric_<i>); a value's ${resource} is the workload's own id.

func (IdleMetric) MarshalJSON added in v0.16.0

func (m IdleMetric) MarshalJSON() ([]byte, error)

MarshalJSON writes the metric as the API says it.

func (*IdleMetric) UnmarshalJSON added in v0.16.0

func (m *IdleMetric) UnmarshalJSON(b []byte) error

UnmarshalJSON reads the metric, its dimensions an object or a list of objects.

type Message

type Message struct {
	Code  string `json:"Code,omitempty"`
	Value string `json:"Value,omitempty"`
}

Message is a message of a result (why CloudWatch could not read a metric).

type Metric

type Metric = discover.Metric

The query shapes are the scan's (discovery reads the same GetMetricData).

type MetricDataQuery

type MetricDataQuery = discover.MetricDataQuery

The query shapes are the scan's (discovery reads the same GetMetricData).

type MetricDataResult

type MetricDataResult struct {
	ID         string      `json:"Id,omitempty"`
	Timestamps []time.Time `json:"Timestamps,omitempty"`
	Values     []float64   `json:"Values,omitempty"`
	StatusCode string      `json:"StatusCode,omitempty"`
	Messages   []Message   `json:"Messages,omitempty"`
}

MetricDataResult is one query's values: Timestamps and Values side by side.

type MetricStat

type MetricStat = discover.MetricStat

The query shapes are the scan's (discovery reads the same GetMetricData).

type PlannedSignal added in v0.16.0

type PlannedSignal struct {
	Signal  string   `json:"signal"`
	Below   *float64 `json:"below"`
	Applies bool     `json:"applies"`
	Reason  string   `json:"reason,omitempty"`
	Series  []Series `json:"series"`
}

PlannedSignal is a signal of the plan: cpu, network or metric, idle under Below (nil: off); one that does not apply says why.

type RegionRequests

type RegionRequests struct {
	Account    string                  `json:"account"`
	Region     string                  `json:"region"`
	From       string                  `json:"from"`
	To         string                  `json:"to"`
	Minutes    map[string][][2]float64 `json:"minutes"`
	Unreadable map[string]string       `json:"unreadable"`
}

RegionRequests is a region's requests: by target group ARN, [minute, count] pairs (minutes from From, ascending; only minutes with a request) or why its metric could not be read. JSON names as Node's (the cache and the report).

func Requests

func Requests(ctx context.Context, cw CloudWatchPort, workloads []discover.Workload, days int, now time.Time) (RegionRequests, error)

Requests reads the requests per minute of the workloads' target groups over the days before now (to the minute): one metric per target group, a managed workload's waker too, added up to its own (R10). An error is a call that failed, whole.

func RequestsAll

func RequestsAll(ctx context.Context, o RequestsOptions) ([]RegionRequests, error)

RequestsAll reads the requests of every region with workloads and no error, --parallel regions at a time, in the regions' order. A region it cannot read at all is a RegionRequests whose workloads are unreadable with AWS's reason; the error is the context's.

type RegionSignals added in v0.16.0

type RegionSignals struct {
	Account    string                             `json:"account"`
	Region     string                             `json:"region"`
	From       string                             `json:"from"`
	To         string                             `json:"to"`
	Points     map[string]map[string][][2]float64 `json:"points"`
	Unreadable map[string]string                  `json:"unreadable"`
	// With --ecs-uptime: each ECS service's task-minutes over the window (its LiveTaskCount), as the HTTP services' are
	// counted; none when CloudWatch has none.
	Tasks map[string]float64 `json:"tasks,omitempty"`
	// Counted: the tasks were asked (a read kept without them is read again by a run that counts them).
	Counted bool `json:"counted,omitempty"`
}

RegionSignals are a region's standalone resources' points (by resource ARN, then by query id: minutes from From and values), or why their metrics could not be read.

func StandaloneAll added in v0.16.0

func StandaloneAll(ctx context.Context, o StandaloneOptions) ([]RegionSignals, error)

StandaloneAll reads the signals of every region with standalone resources, --parallel regions at a time, in the regions' order; the error is the context's.

func (RegionSignals) Of added in v0.16.0

Of is a resource's points as the activity reads them.

type RegionUptime

type RegionUptime struct {
	Account    string              `json:"account"`
	Region     string              `json:"region"`
	From       string              `json:"from"`
	To         string              `json:"to"`
	Running    map[string][][2]int `json:"running"`
	Unreadable map[string]string   `json:"unreadable"`
	// Go only: each ECS service's task-minutes over the period (its LiveTaskCount); none when CloudWatch has none.
	// Counted: the tasks were read (an uptime kept from before is read again).
	Tasks   map[string]float64 `json:"tasks,omitempty"`
	Counted bool               `json:"counted,omitempty"`
}

RegionUptime is a region's measured uptime: by instance id, service ARN or database key, when it ran ([start, end) in minutes from From, sorted and disjoint), or why it could not be read. JSON names as Node's.

func UptimeAll

func UptimeAll(ctx context.Context, o UptimeOptions) ([]RegionUptime, error)

UptimeAll measures the regions with something to measure, over their requests period, --parallel regions at a time, in the regions' order. A region it cannot read at all is a RegionUptime whose ids are unreadable with AWS's reason; the error is the context's.

type RequestsOptions

type RequestsOptions struct {
	Regions      []discover.RegionDiscovery
	CloudWatchOf CloudWatchOf
	Cache        *cache.Cache
	// Refresh is "discovery" or "metrics" (read again) or "".
	Refresh string
	Now     func() time.Time
	// Progress, OnPlan and OnStart are called one at a time (a mutex), though
	// regions run at once. OnPlan comes first, before any read.
	Progress func(discover.ProgressEvent)
	// Concurrency is the regions read at once (--parallel; 5 when < 1).
	Concurrency int
	// OnPlan says how many regions are read; OnStart says when each starts.
	OnPlan  func(count int)
	OnStart func(account, region string)
	// Days is the days of requests read before the run (--days; Days when < 1).
	Days int
}

RequestsOptions are the inputs of RequestsAll.

type Series added in v0.16.0

type Series struct {
	IDs   []string `json:"ids"`
	Scale string   `json:"scale"`
}

Series is a signal's series: the sum of its queries, scaled.

type SignalValue added in v0.16.0

type SignalValue struct {
	Signal string     `json:"signal"`
	Values []*float64 `json:"values"`
}

SignalValue is an applying signal's value at each 5-minute point from From (nil: missing).

func SignalValues added in v0.16.0

func SignalValues(signals []PlannedSignal, read ActivityPoints) []SignalValue

SignalValues are each applying signal's value at each 5-minute point, from From to To: each series the sum of its queries, scaled; the largest series; nil when one of its queries has no point.

type StandaloneOptions added in v0.16.0

type StandaloneOptions struct {
	Regions      []discover.StandaloneRegion
	CloudWatchOf CloudWatchOf
	Cache        *cache.Cache
	Refresh      string
	Now          func() time.Time
	Progress     func(discover.ProgressEvent)
	Concurrency  int
	OnPlan       func(count int)
	OnStart      func(account, region string)
	Days         int
	// Tasks (--ecs-uptime): each ECS service's tasks are counted too (its LiveTaskCount), as an HTTP service's are.
	Tasks bool
}

StandaloneOptions are StandaloneAll's inputs (RequestsOptions' for the standalone resources).

type UnreadableError added in v0.16.0

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

UnreadableError is a metric CloudWatch could not read: an error, never an idle day.

func (*UnreadableError) Error added in v0.16.0

func (e *UnreadableError) Error() string

type UptimeKinds

type UptimeKinds struct{ EC2, ECS, RDS bool }

UptimeKinds says what each option measures: EC2 instances without an Auto Scaling group (EC2), ECS services (ECS), never what Dormeto manages; and the companion databases a nap could stop (RDS).

type UptimeOptions

type UptimeOptions struct {
	Regions []discover.RegionDiscovery
	// Requests are the regions' requests: each region is measured over its period.
	Requests     []RegionRequests
	CloudWatchOf CloudWatchOf
	Cache        *cache.Cache
	// Refresh is "discovery" or "metrics" (read again) or "".
	Refresh  string
	Now      func() time.Time
	Progress func(discover.ProgressEvent)
	// Kinds is what is measured; nil: DefaultKinds (Node's default argument).
	Kinds *UptimeKinds
	// Concurrency is the regions read at once (--parallel; 5 when < 1).
	Concurrency int
	// OnPlan says how many regions are measured; OnStart when each starts.
	OnPlan  func(count int)
	OnStart func(account, region string)
}

UptimeOptions are the inputs of UptimeAll.

type UptimeRead

type UptimeRead struct {
	Running    map[string][][2]int
	Unreadable map[string]string
	Tasks      map[string]float64
}

UptimeRead is what Uptime reads of a list of ids: when each ran, and each ECS service's task-minutes.

func Uptime

func Uptime(ctx context.Context, cw CloudWatchPort, ids []string, from, to string) (UptimeRead, error)

Uptime reads the running periods of the ids over [from, to] (ISO strings, the region's requests period): each datapoint is running its 5 minutes, consecutive ones joined. An error is a call that failed, whole.

Jump to

Keyboard shortcuts

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