Documentation
¶
Overview ¶
Package prometheus reads container metrics history from a Prometheus server that scrapes containerd's built-in metrics (#176, ADR-0023). It is a small read-only client of the Prometheus HTTP API: queries are built from fixed templates, never taken from callers.
Index ¶
- Constants
- Variables
- func Queries(namespace, id string, window time.Duration) (map[string]string, error)
- type Client
- func (c *Client) Check(ctx context.Context) (version string, containers int, err error)
- func (c *Client) History(ctx context.Context, namespace, id string, r Range, end time.Time) (*History, error)
- func (c *Client) Query(ctx context.Context, query string) ([]float64, error)
- func (c *Client) QueryRange(ctx context.Context, query string, start, end time.Time, step time.Duration) ([]Series, error)
- func (c *Client) Version(ctx context.Context) string
- type Config
- type History
- type Range
- type Sample
- type Series
Constants ¶
const ( AuthNone = "none" AuthBasic = "basic" AuthBearer = "bearer" )
Authentication modes of the Prometheus server.
const ( MetricCPU = "container_cpu_usage_usec_microseconds" MetricMemory = "container_memory_usage_bytes" MetricInactiveFile = "container_memory_inactive_file_bytes" MetricMemoryLimit = "container_memory_usage_limit_bytes" MetricIORead = "container_io_rbytes_bytes" MetricIOWrite = "container_io_wbytes_bytes" MetricPIDs = "container_pids_current" )
Metric names of containerd's cgroup v2 task metrics ("container" namespace, unit suffix added by docker/go-metrics; labels container_id and namespace). Verified against a real containerd by TestIntegrationContainerdMetrics.
Variables ¶
var Ranges = []Range{ {Name: "1h", Duration: time.Hour, Step: 15 * time.Second, Window: 2 * time.Minute}, {Name: "6h", Duration: 6 * time.Hour, Step: time.Minute, Window: 4 * time.Minute}, {Name: "24h", Duration: 24 * time.Hour, Step: 5 * time.Minute, Window: 20 * time.Minute}, {Name: "7d", Duration: 7 * 24 * time.Hour, Step: 30 * time.Minute, Window: 2 * time.Hour}, }
Ranges are the supported periods, about 240–360 points each.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client queries one Prometheus server.
func New ¶
New returns a client for cfg (validated by the caller). A nil hc uses a client with a request timeout.
func (*Client) Check ¶
Check verifies the connection and whether containerd task metrics are scraped: it returns the server version ("" if unknown) and the number of containers with CPU metrics.
func (*Client) History ¶
func (c *Client) History(ctx context.Context, namespace, id string, r Range, end time.Time) (*History, error)
History queries the metrics of a container over r, ending at end.
func (*Client) Query ¶
Query runs an instant query (GET /api/v1/query) and returns the values of a vector.
type Config ¶
type Config struct {
// URL is the base URL of the server, e.g. http://127.0.0.1:9090 or
// https://example.org/prometheus (without /api/v1).
URL string `json:"url"`
// Auth is AuthNone, AuthBasic or AuthBearer.
Auth string `json:"auth"`
Username string `json:"username,omitempty"`
// Secret is the password (basic) or the token (bearer). It is never returned by the API.
Secret string `json:"secret,omitempty"`
}
Config is the connection to a Prometheus server.
type History ¶
type History struct {
Range string `json:"range"`
// Step is the distance between points in seconds.
Step int64 `json:"step"`
Times []int64 `json:"times"`
// CPU in percent of one core, like nerdctl stats.
CPU []*float64 `json:"cpu"`
// Memory without inactive file cache (like nerdctl stats), and its limit (null when
// unlimited).
Memory []*float64 `json:"memory"`
MemoryLimit []*float64 `json:"memoryLimit"`
// Block I/O rates in bytes per second.
BlockRead []*float64 `json:"blockRead"`
BlockWrite []*float64 `json:"blockWrite"`
PIDs []*float64 `json:"pids"`
}
History is the metrics history of one container on a common time axis; a nil value is a gap (no data). Keep in sync with api/openapi.yaml (StatsHistory).
type Range ¶
type Range struct {
Name string
Duration time.Duration
// Step is the resolution; Window the rate() window, at least two scrapes long.
Step time.Duration
Window time.Duration
}
Range is a history period offered by the UI.
func RangeByName ¶
RangeByName returns the range named name.