From 7b127b3fe5913993c4125afada8a5e0aa203c5b5 Mon Sep 17 00:00:00 2001 From: Frank Spitulski Date: Sun, 24 Feb 2019 12:51:45 -0800 Subject: [PATCH 1/2] feat(push): add format builder option Allow users to specify which format the pusher should push in. Signed-off-by: Frank Spitulski --- prometheus/push/push.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/prometheus/push/push.go b/prometheus/push/push.go index 3721ff1..ccfca3a 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -64,6 +64,8 @@ type Pusher struct { client *http.Client useBasicAuth bool username, password string + + expfmt expfmt.Format } // New creates a new Pusher to push to the provided URL with the provided job @@ -96,6 +98,7 @@ func New(url, job string) *Pusher { gatherers: prometheus.Gatherers{reg}, registerer: reg, client: &http.Client{}, + expfmt: expfmt.FmtProtoDelim, } } @@ -182,6 +185,14 @@ func (p *Pusher) BasicAuth(username, password string) *Pusher { return p } +// Format configures the Pusher to use an encoding format given by the +// provided expfmt.Format. For convenience, this method returns a +// pointer to the Pusher itself. +func (p *Pusher) Format(format expfmt.Format) *Pusher { + p.expfmt = format + return p +} + func (p *Pusher) push(method string) error { if p.error != nil { return p.error @@ -197,7 +208,7 @@ func (p *Pusher) push(method string) error { return err } buf := &bytes.Buffer{} - enc := expfmt.NewEncoder(buf, expfmt.FmtProtoDelim) + enc := expfmt.NewEncoder(buf, p.expfmt) // Check for pre-existing grouping labels: for _, mf := range mfs { for _, m := range mf.GetMetric() { From 10e34c668faa0b614e94e466777585a24bdf5270 Mon Sep 17 00:00:00 2001 From: Frank Spitulski Date: Sun, 24 Feb 2019 15:15:35 -0800 Subject: [PATCH 2/2] fix(push): incorporate changes from review Signed-off-by: Frank Spitulski --- prometheus/push/push.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/prometheus/push/push.go b/prometheus/push/push.go index ccfca3a..6636ffc 100644 --- a/prometheus/push/push.go +++ b/prometheus/push/push.go @@ -186,8 +186,10 @@ func (p *Pusher) BasicAuth(username, password string) *Pusher { } // Format configures the Pusher to use an encoding format given by the -// provided expfmt.Format. For convenience, this method returns a -// pointer to the Pusher itself. +// 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 (p *Pusher) Format(format expfmt.Format) *Pusher { p.expfmt = format return p @@ -233,7 +235,7 @@ func (p *Pusher) push(method string) error { if p.useBasicAuth { req.SetBasicAuth(p.username, p.password) } - req.Header.Set(contentTypeHeader, string(expfmt.FmtProtoDelim)) + req.Header.Set(contentTypeHeader, string(p.expfmt)) resp, err := p.client.Do(req) if err != nil { return err