Package push
Package push provides functions to push metrics to a Pushgateway. The metrics
to push are either collected from a provided registry, or from explicitly
listed collectors.
See the documentation of the Pushgateway to understand the meaning of the
grouping parameters and the differences between push.Registry and
push.Collectors on the one hand and push.AddRegistry and push.AddCollectors
on the other hand: https://github.com/prometheus/pushgateway
- func AddCollectors(job string, grouping map[string]string, url string, collectors ...prometheus.Collector) error
- func AddFromGatherer(job string, grouping map[string]string, url string, g prometheus.Gatherer) error
- func Collectors(job string, grouping map[string]string, url string, collectors ...prometheus.Collector) error
- func FromGatherer(job string, grouping map[string]string, url string, g prometheus.Gatherer) error
- func HostnameGroupingKey() map[string]string
Package files
push.go
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls—perhaps dynamically.
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring func
token.
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
func AddCollectors(job string, grouping map[string]string, url string, collectors ...prometheus.Collector) error
AddCollectors works like AddFromGatherer, but it does not use a Gatherer.
Instead, it collects from the provided collectors directly. It is a
convenient way to push only a few metrics.
func AddFromGatherer(job string, grouping map[string]string, url string, g prometheus.Gatherer) error
AddFromGatherer works like FromGatherer, 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.)
▾ Example
Code:
package push_test
import (
"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) {
return 42, nil
}
func ExampleAddFromGatherer() {
registry := prometheus.NewRegistry()
registry.MustRegister(completionTime, duration, records)
start := time.Now()
n, err := performBackup()
records.Set(float64(n))
duration.Set(time.Since(start).Seconds())
completionTime.SetToCurrentTime()
if err != nil {
fmt.Println("DB backup failed:", err)
} else {
registry.MustRegister(successTime)
successTime.SetToCurrentTime()
}
if err := push.AddFromGatherer(
"db_backup", nil,
"http://pushgateway:9091",
registry,
); err != nil {
fmt.Println("Could not push to Pushgateway:", err)
}
}
func Collectors(job string, grouping map[string]string, url string, collectors ...prometheus.Collector) error
Collectors works like FromGatherer, but it does not use a Gatherer. Instead,
it collects from the provided collectors directly. It is a convenient way to
push only a few metrics.
▾ Example
Code:
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.Collectors(
"db_backup", push.HostnameGroupingKey(),
"http://pushgateway:9091",
completionTime,
); err != nil {
fmt.Println("Could not push completion time to Pushgateway:", err)
}
func FromGatherer(job string, grouping map[string]string, url string, g prometheus.Gatherer) error
FromGatherer triggers a metric collection by the provided Gatherer (which is
usually implemented by a prometheus.Registry) and pushes all gathered metrics
to the Pushgateway specified by url, using the provided job name and the
(optional) further grouping labels (the grouping map may be nil). See the
Pushgateway documentation for detailed implications of the job and other
grouping labels. Neither the job name nor any grouping label value may
contain a "/". The metrics pushed must not contain a job label of their own
nor any of the grouping labels.
You can use just host:port or ip:port as url, in which case 'http://' is
added automatically. You can also include the schema in the URL. However, do
not include the '/metrics/jobs/...' part.
Note that 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.)
func HostnameGroupingKey() map[string]string
HostnameGroupingKey returns a label map with the only entry
{instance="<hostname>"}. This can be conveniently used as the grouping
parameter if metrics should be pushed with the hostname as label. The
returned map is created upon each call so that the caller is free to add more
labels to the map.