metrics

package
v0.9.6 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package metrics scrapes an app's own metrics endpoint and parses the text exposition format into capped, validated samples.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBodyTooLarge fails a scrape whose body is over the limit; nothing is stored.
	ErrBodyTooLarge = errors.New("metrics: response body exceeds the size limit")
	// ErrRedirect fails a scrape that answered with a redirect.
	ErrRedirect = errors.New("metrics: redirects are refused")
	// ErrBadTarget fails a target outside the app's private network.
	ErrBadTarget = errors.New("metrics: target is not an app network address")
)

Functions

func ErrorMessage

func ErrorMessage(err error) string

ErrorMessage maps a scrape failure to a fixed message, so stored and shown errors never carry the container address or raw transport text.

func NewClient

func NewClient(timeout time.Duration) *http.Client

NewClient returns a direct HTTP client: no proxy, no redirects, a hard timeout.

func ParseText

func ParseText(r io.Reader, lim Limits) ([]Sample, Stats)

ParseText parses exposition text, dropping invalid lines and stopping at MaxSeries.

func Scrape

func Scrape(ctx context.Context, client *http.Client, target string, lim Limits) ([]Sample, Stats, error)

Scrape fetches target and parses the body within lim.

func TargetURL

func TargetURL(ip string, port int, path string) (string, error)

TargetURL builds the scrape URL from the container's own network address only. A non-private address is refused so a bad lookup can never reach the outside.

func ValidName

func ValidName(s string) bool

ValidName reports whether s is a valid metric name.

func ValidatePath

func ValidatePath(p string) error

ValidatePath accepts a plain absolute path with no query, host or traversal parts.

func ValidatePort

func ValidatePort(port int) error

ValidatePort accepts a TCP port number.

Types

type Limits

type Limits struct {
	Timeout      time.Duration
	MaxBodyBytes int64
	MaxSeries    int
	MaxLabels    int
	// MaxLabelBytes caps the canonical label text of one whole scrape.
	MaxLabelBytes int
}

Limits bound one scrape; every field comes from runtime settings.

type Sample

type Sample struct {
	Name   string
	Labels string
	Value  float64
}

Sample is one parsed series value. Labels is the canonical sorted form.

type Stats

type Stats struct {
	Kept      int
	Dropped   int
	Truncated bool
}

Stats reports what a parse kept and dropped.

type StatusError

type StatusError struct{ Code int }

StatusError is a scrape that answered something other than 200.

func (StatusError) Error

func (e StatusError) Error() string

Jump to

Keyboard shortcuts

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