push

package
v1.1.11 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package push provides functions to push metrics to a Pushgateway. It uses a builder approach. Create a Pusher with New and then add the various options by using its methods, finally calling Add or Push, like this:

// Easy case:
push.New("http://example.org/metrics", "my_job").Gatherer(myRegistry).Push()

// Complex case:
push.New("http://example.org/metrics", "my_job").
    Collector(myCollector1).
    Collector(myCollector2).
    Grouping("zone", "xy").
    Client(&myHTTPClient).
    BasicAuth("top", "secret").
    Add()

See the examples section for more detailed examples.

See the documentation of the Pushgateway to understand the meaning of the grouping key and the differences between Push and Add: https://github.com/prometheus/pushgateway

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTTPDoer

type HTTPDoer interface {
	Do(*http.Request) (*http.Response, error)
}

HTTPDoer is an interface for the one method of http.Client that is used by Pusher

type Pusher

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

Pusher manages a push to the Pushgateway. Use New to create one, configure it with its methods, and finally use the Add or Push method to push.

func New

func New(url, job string) *Pusher

New creates a new Pusher to push to the provided URL with the provided job name (which must not be empty). You can use just host:port or ip:port as url, in which case “http://” is added automatically. Alternatively, include the schema in the URL. However, do not include the “/metrics/jobs/…” part.

func (*Pusher) Add

func (p *Pusher) Add() error

Add works like push, but only previously pushed metrics with the same name (and the same job and other grouping labels) will be replaced. (It uses HTTP method “POST” to push to the Pushgateway.)

func (*Pusher) BasicAuth

func (p *Pusher) BasicAuth(username, password string) *Pusher

BasicAuth configures the Pusher to use HTTP Basic Authentication with the provided username and password. For convenience, this method returns a pointer to the Pusher itself.

func (*Pusher) Client

func (p *Pusher) Client(c HTTPDoer) *Pusher

Client sets a custom HTTP client for the Pusher. For convenience, this method returns a pointer to the Pusher itself. Pusher only needs one method of the custom HTTP client: Do(*http.Request). Thus, rather than requiring a fully fledged http.Client, the provided client only needs to implement the HTTPDoer interface. Since *http.Client naturally implements that interface, it can still be used normally.

func (*Pusher) Collector

func (p *Pusher) Collector(c prometheus.Collector) *Pusher

Collector adds a Collector to the Pusher, from which metrics will be collected to push them to the Pushgateway. The collected metrics must not contain a job label of their own.

For convenience, this method returns a pointer to the Pusher itself.

func (*Pusher) Delete

func (p *Pusher) Delete() error

Delete sends a “DELETE” request to the Pushgateway configured while creating this Pusher, using the configured job name and any added grouping labels as grouping key. Any added Gatherers and Collectors added to this Pusher are ignored by this method.

Delete returns the first error encountered by any method call (including this one) in the lifetime of the Pusher.

func (*Pusher) Format

func (p *Pusher) Format(format expfmt.Format) *Pusher

Format configures the Pusher to use an encoding format given by the provided expfmt.Format. The default format is expfmt.FmtProtoDelim and should be used with the standard Prometheus Pushgateway. Custom implementations may require different formats. For convenience, this method returns a pointer to the Pusher itself.

func (*Pusher) Gatherer

func (p *Pusher) Gatherer(g prometheus.Gatherer) *Pusher

Gatherer adds a Gatherer to the Pusher, from which metrics will be gathered to push them to the Pushgateway. The gathered metrics must not contain a job label of their own.

For convenience, this method returns a pointer to the Pusher itself.

func (*Pusher) Grouping

func (p *Pusher) Grouping(name, value string) *Pusher

Grouping adds a label pair to the grouping key of the Pusher, replacing any previously added label pair with the same label name. Note that setting any labels in the grouping key that are already contained in the metrics to push will lead to an error.

For convenience, this method returns a pointer to the Pusher itself.

func (*Pusher) Push

func (p *Pusher) Push() error

Push collects/gathers all metrics from all Collectors and Gatherers added to this Pusher. Then, it pushes them to the Pushgateway configured while creating this Pusher, using the configured job name and any added grouping labels as grouping key. All previously pushed metrics with the same job and other grouping labels will be replaced with the metrics pushed by this call. (It uses HTTP method “PUT” to push to the Pushgateway.)

Push returns the first error encountered by any method call (including this one) in the lifetime of the Pusher.

Jump to

Keyboard shortcuts

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