Movatterモバイル変換


[0]ホーム

URL:


push

package
v1.23.2Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2025 License:Apache-2.0Imports:12Imported by:926

Details

Repository

github.com/prometheus/client_golang

Links

Documentation

Overview

Package push provides functions to push metrics to a Pushgateway. It uses abuilder approach. Create a Pusher with New and then add the various optionsby 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 ofthe grouping key and the differences between Push and Add:https://github.com/prometheus/pushgateway

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

typeHTTPDoeradded inv0.9.3

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

typePusheradded inv0.9.0

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

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

funcNewadded inv0.9.0

func New(url, jobstring) *Pusher

New creates a new Pusher to push to the provided URL with the provided jobname (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 theschema in the URL. However, do not include the “/metrics/jobs/…” part.

func (*Pusher)Addadded inv0.9.0

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 HTTPmethod “POST” to push to the Pushgateway.)

Example
package mainimport ("fmt""time""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/push")var (completionTime = prometheus.NewGauge(prometheus.GaugeOpts{Name: "db_backup_last_completion_timestamp_seconds",Help: "The timestamp of the last completion of a DB backup, successful or not.",})successTime = prometheus.NewGauge(prometheus.GaugeOpts{Name: "db_backup_last_success_timestamp_seconds",Help: "The timestamp of the last successful completion of a DB backup.",})duration = prometheus.NewGauge(prometheus.GaugeOpts{Name: "db_backup_duration_seconds",Help: "The duration of the last DB backup in seconds.",})records = prometheus.NewGauge(prometheus.GaugeOpts{Name: "db_backup_records_processed",Help: "The number of records processed in the last DB backup.",}))func performBackup() (int, error) {// Perform the backup and return the number of backed up records and any// applicable error.// ...return 42, nil}func main() {// We use a registry here to benefit from the consistency checks that// happen during registration.registry := prometheus.NewRegistry()registry.MustRegister(completionTime, duration, records)// Note that successTime is not registered.pusher := push.New("http://pushgateway:9091", "db_backup").Gatherer(registry)start := time.Now()n, err := performBackup()records.Set(float64(n))// Note that time.Since only uses a monotonic clock in Go1.9+.duration.Set(time.Since(start).Seconds())completionTime.SetToCurrentTime()if err != nil {fmt.Println("DB backup failed:", err)} else {// Add successTime to pusher only in case of success.// We could as well register it with the registry.// This example, however, demonstrates that you can// mix Gatherers and Collectors when handling a Pusher.pusher.Collector(successTime)successTime.SetToCurrentTime()}// Add is used here rather than Push to not delete a previously pushed// success timestamp in case of a failure of this backup.if err := pusher.Add(); err != nil {fmt.Println("Could not push to Pushgateway:", err)}}

func (*Pusher)AddContextadded inv1.13.0

func (p *Pusher) AddContext(ctxcontext.Context)error

AddContext is like Add but includes a context.

If the context expires before HTTP request is complete, an error is returned.

func (*Pusher)BasicAuthadded inv0.9.0

func (p *Pusher) BasicAuth(username, passwordstring) *Pusher

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

func (*Pusher)Clientadded inv0.9.0

func (p *Pusher) Client(cHTTPDoer) *Pusher

Client sets a custom HTTP client for the Pusher. For convenience, this methodreturns 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)Collectoradded inv0.9.0

func (p *Pusher) Collector(cprometheus.Collector) *Pusher

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

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

func (*Pusher)Deleteadded inv0.12.1

func (p *Pusher) Delete()error

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

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

func (*Pusher)Erroradded inv1.13.0

func (p *Pusher) Error()error

Error returns the error that was encountered.

func (*Pusher)Formatadded inv0.9.3

func (p *Pusher) Format(formatexpfmt.Format) *Pusher

Format configures the Pusher to use an encoding format given by theprovided expfmt.Format. The default format is expfmt.FmtProtoDelim andshould be used with the standard Prometheus Pushgateway. Customimplementations may require different formats. For convenience, thismethod returns a pointer to the Pusher itself.

func (*Pusher)Gathereradded inv0.9.0

func (p *Pusher) Gatherer(gprometheus.Gatherer) *Pusher

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

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

func (*Pusher)Groupingadded inv0.9.0

func (p *Pusher) Grouping(name, valuestring) *Pusher

Grouping adds a label pair to the grouping key of the Pusher, replacing anypreviously added label pair with the same label name. Note that setting anylabels in the grouping key that are already contained in the metrics to pushwill lead to an error.

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

func (*Pusher)Headeradded inv1.15.0

func (p *Pusher) Header(headerhttp.Header) *Pusher

Header sets a custom HTTP header for the Pusher's client. For convenience, this methodreturns a pointer to the Pusher itself.

func (*Pusher)Pushadded inv0.9.0

func (p *Pusher) Push()error

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

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

Example
package mainimport ("fmt""github.com/prometheus/client_golang/prometheus""github.com/prometheus/client_golang/prometheus/push")func main() {completionTime := prometheus.NewGauge(prometheus.GaugeOpts{Name: "db_backup_last_completion_timestamp_seconds",Help: "The timestamp of the last successful completion of a DB backup.",})completionTime.SetToCurrentTime()if err := push.New("http://pushgateway:9091", "db_backup").Collector(completionTime).Grouping("db", "customers").Push(); err != nil {fmt.Println("Could not push completion time to Pushgateway:", err)}}

func (*Pusher)PushContextadded inv1.13.0

func (p *Pusher) PushContext(ctxcontext.Context)error

PushContext is like Push but includes a context.

If the context expires before HTTP request is complete, an error is returned.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp