metrics

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 7 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 reads CPU and memory usage from whatever source a cluster actually has.

This exists because of the loudest complaint class in the ecosystem. The Lens lineage hardcodes Prometheus query shapes, so pointing those tools at metrics-server or VictoriaMetrics makes values double, zero out, or vanish (Freelens #466, #627, #524, #1670, #964, #1111, #1555, #1883). The design consequence recorded in docs/01-problem.md is that a source is an interface with metrics-server as the default, and that a value which might be wrong is not displayed at all.

Two rules the rest of the product depends on:

  • Never a zero, never a guess. An unavailable source yields no samples, so the UI hides the column rather than rendering 0 and implying idleness.
  • Every sample names its source, so a suspicious number is traceable to the thing that produced it instead of being mysterious.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ByPod

func ByPod(samples []Sample) map[string]Sample

ByPod folds container samples into one reading per pod.

Summing containers is the correct aggregation and also the thing Freelens gets wrong: its pod detail double-counts, which is what #964 and #1111 report. Doing it in one place, with a test, keeps that defect out.

Types

type MetricsServer

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

MetricsServer reads from the metrics.k8s.io aggregated API.

This is the default source because it is the one most clusters actually have, and because assuming Prometheus is the mistake that produces wrong numbers everywhere else in this ecosystem.

func NewMetricsServer

NewMetricsServer probes for the aggregated API and returns a source that reports honestly either way.

A probe failure is not an error: a cluster without metrics-server is normal, and the caller wants a source that says so rather than an error to handle.

func (*MetricsServer) Available

func (m *MetricsServer) Available() bool

func (*MetricsServer) Name

func (m *MetricsServer) Name() string

func (*MetricsServer) PodSamples

func (m *MetricsServer) PodSamples(ctx context.Context, namespace string) ([]Sample, error)

PodSamples returns one sample per container.

Aggregation to pod level is deliberately left to ByPod. Returning per-container readings keeps the raw shape intact, which is what the resolved-config and pod views need later, and confines the summing that competitors get wrong to a single tested function.

func (*MetricsServer) Unavailable

func (m *MetricsServer) Unavailable() string

type None

type None struct {
	// Reason explains what was probed and what was missing.
	Reason string
}

None is the source used when a cluster has no metrics at all.

It is a real implementation rather than a nil check, so every call site handles "no metrics" the same way and nobody has to remember a guard.

func (None) Available

func (None) Available() bool

func (None) Name

func (None) Name() string

func (None) PodSamples

func (None) PodSamples(context.Context, string) ([]Sample, error)

func (None) Unavailable

func (n None) Unavailable() string

type PodMetricsAPI

type PodMetricsAPI interface {
	ListPodMetrics(ctx context.Context, namespace string) (*metricsapi.PodMetricsList, error)
}

PodMetricsAPI is the slice of the metrics API this package uses.

Narrowing the dependency to one method keeps the package testable with a hand-written fake. The generated metrics fake clientset does not track PodMetrics correctly in v0.36, so depending on the full clientset would mean the read path could only be exercised against a live cluster.

func FromClientset

func FromClientset(c metricsv.Interface) PodMetricsAPI

FromClientset wraps a generated metrics clientset for production use.

type Prometheus

type Prometheus struct {
	Endpoint string
}

Prometheus is a placeholder for the source that ships after v1.

It exists now so the probe chain and the UI's empty state are exercised by the real interface rather than by a special case added later. It never claims availability, so it can never contribute a wrong number.

func (Prometheus) Available

func (Prometheus) Available() bool

func (Prometheus) Name

func (Prometheus) Name() string

func (Prometheus) PodSamples

func (Prometheus) PodSamples(context.Context, string) ([]Sample, error)

func (Prometheus) Unavailable

func (p Prometheus) Unavailable() string

type Sample

type Sample struct {
	Namespace string
	Pod       string
	Container string

	// CPUMilli is millicores. MemoryBytes is bytes. Both are raw readings; no
	// unit conversion or rounding happens before the UI, which formats them.
	CPUMilli    int64
	MemoryBytes int64

	// Source names the implementation that produced this reading. It reaches
	// the UI so a doubled or missing value is attributable.
	Source string
	// Timestamp and Window describe when the reading was taken and over what
	// interval. A stale window is worth surfacing rather than hiding.
	Timestamp time.Time
	Window    time.Duration
}

Sample is one container's usage at a point in time.

type Source

type Source interface {
	// Name identifies the implementation, e.g. "metrics-server".
	Name() string
	// Available reports whether this source can answer queries right now.
	Available() bool
	// Unavailable explains why not, phrased so the UI can render it as an
	// empty state naming what to install.
	Unavailable() string
	// PodSamples returns usage for pods in a namespace. Passing an empty
	// namespace means all namespaces the caller may read.
	PodSamples(ctx context.Context, namespace string) ([]Sample, error)
}

Source reads usage for pods in a namespace.

func Probe

func Probe(d discovery.DiscoveryInterface, c PodMetricsAPI, prometheusEndpoint string) Source

Probe picks a source for a cluster.

Order matters and is documented in docs/05-architecture.md: the aggregated API first because it is both the most common and the cheapest to verify, then a configured Prometheus, then none. The result is always a usable Source, never nil.

Jump to

Keyboard shortcuts

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