From 779cbe15b24f52a8d62c04506dbb16427ffec3db Mon Sep 17 00:00:00 2001 From: beorn7 Date: Wed, 14 Feb 2018 17:19:52 +0100 Subject: [PATCH] Remove HostnameGrouping method --- prometheus/push/deprecated.go | 7 +++++-- prometheus/push/examples_test.go | 2 +- prometheus/push/push.go | 16 ---------------- prometheus/push/push_test.go | 10 +--------- 4 files changed, 7 insertions(+), 28 deletions(-) diff --git a/prometheus/push/deprecated.go b/prometheus/push/deprecated.go index 8ffea7c..af48f5d 100644 --- a/prometheus/push/deprecated.go +++ b/prometheus/push/deprecated.go @@ -164,8 +164,11 @@ func pushCollectors(job string, grouping map[string]string, url, method string, // returned map is created upon each call so that the caller is free to add more // labels to the map. // -// Deprecated: It is recommended to use a Pusher created with New, which has a -// method HostnameGrouping. +// Deprecated: Usually, metrics pushed to the Pushgateway should not be +// host-centric. (You would use https://github.com/prometheus/node_exporter in +// that case.) If you have the need to add the hostname to the grouping key, you +// are probably doing something wrong. See +// https://prometheus.io/docs/practices/pushing/ for details. func HostnameGroupingKey() map[string]string { hostname, err := os.Hostname() if err != nil { diff --git a/prometheus/push/examples_test.go b/prometheus/push/examples_test.go index e447849..fa5549a 100644 --- a/prometheus/push/examples_test.go +++ b/prometheus/push/examples_test.go @@ -28,7 +28,7 @@ func ExamplePusher_Push() { completionTime.SetToCurrentTime() if err := push.New("http://pushgateway:9091", "db_backup"). Collector(completionTime). - HostnameGrouping(). + Grouping("db", "customers"). Push(); err != nil { fmt.Println("Could not push completion time to Pushgateway:", err) } diff --git a/prometheus/push/push.go b/prometheus/push/push.go index 0917445..360a58d 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -41,7 +41,6 @@ import ( "io/ioutil" "net/http" "net/url" - "os" "strings" "github.com/prometheus/common/expfmt" @@ -167,21 +166,6 @@ func (p *Pusher) Grouping(name, value string) *Pusher { return p } -// HostnameGrouping adds a label pair with “instance” as the name and the -// current hostname as the value to the grouping key of the Pusher. If the -// hostname cannot be determined, “unknown” is used as the label value. For -// further implications of setting a grouping key, see the Grouping method. -// -// For convenience, this method returns a pointer to the Pusher itself. -func (p *Pusher) HostnameGrouping() *Pusher { - hostname, err := os.Hostname() - if err != nil { - hostname = "unknown" - } - p.grouping["instance"] = hostname - return p -} - // Client sets a custom HTTP client for the Pusher. For convenience, this method // returns a pointer to the Pusher itself. func (p *Pusher) Client(c *http.Client) *Pusher { diff --git a/prometheus/push/push_test.go b/prometheus/push/push_test.go index 116df63..670dc9e 100644 --- a/prometheus/push/push_test.go +++ b/prometheus/push/push_test.go @@ -24,7 +24,6 @@ import ( "io/ioutil" "net/http" "net/http/httptest" - "os" "testing" "github.com/prometheus/common/expfmt" @@ -40,11 +39,6 @@ func TestPush(t *testing.T) { lastPath string ) - host, err := os.Hostname() - if err != nil { - t.Error(err) - } - // Fake a Pushgateway that always responds with 202. pgwOK := httptest.NewServer( http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -100,7 +94,6 @@ func TestPush(t *testing.T) { // Push some Collectors, all good. if err := New(pgwOK.URL, "testjob"). - HostnameGrouping(). Collector(metric1). Collector(metric2). Push(); err != nil { @@ -112,7 +105,7 @@ func TestPush(t *testing.T) { if bytes.Compare(lastBody, wantBody) != 0 { t.Errorf("got body %v, want %v", lastBody, wantBody) } - if lastPath != "/metrics/job/testjob/instance/"+host { + if lastPath != "/metrics/job/testjob" { t.Error("unexpected path:", lastPath) } @@ -176,7 +169,6 @@ func TestPush(t *testing.T) { // Push registry, all good. if err := New(pgwOK.URL, "testjob"). - HostnameGrouping(). Gatherer(reg). Push(); err != nil { t.Fatal(err)