prometheus

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 15 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 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

View Source
const (
	AuthNone   = "none"
	AuthBasic  = "basic"
	AuthBearer = "bearer"
)

Authentication modes of the Prometheus server.

View Source
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

View Source
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

func Queries

func Queries(namespace, id string, window time.Duration) (map[string]string, error)

Queries returns the PromQL of each history column for a container. id and namespace are validated containerd identifiers and quoted, so they cannot change the query.

Types

type Client

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

Client queries one Prometheus server.

func New

func New(cfg Config, hc *http.Client) *Client

New returns a client for cfg (validated by the caller). A nil hc uses a client with a request timeout.

func (*Client) Check

func (c *Client) Check(ctx context.Context) (version string, containers int, err error)

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

func (c *Client) Query(ctx context.Context, query string) ([]float64, error)

Query runs an instant query (GET /api/v1/query) and returns the values of a vector.

func (*Client) QueryRange

func (c *Client) QueryRange(ctx context.Context, query string, start, end time.Time, step time.Duration) ([]Series, error)

QueryRange runs a range query (GET /api/v1/query_range) and returns a matrix.

func (*Client) Version

func (c *Client) Version(ctx context.Context) string

Version returns the server version from /api/v1/status/buildinfo ("" when unavailable, e.g. on Prometheus-compatible servers that do not implement it).

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.

func (*Config) Validate

func (c *Config) Validate() error

Validate checks the configuration and normalizes the URL (no trailing slash).

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

func RangeByName(name string) (Range, bool)

RangeByName returns the range named name.

type Sample

type Sample struct {
	T float64
	V float64
}

Sample is one point of a series: unix seconds and value.

type Series

type Series struct {
	Labels  map[string]string
	Samples []Sample
}

Series is one result series of a query.

Jump to

Keyboard shortcuts

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