From 7b127b3fe5913993c4125afada8a5e0aa203c5b5 Mon Sep 17 00:00:00 2001 From: Frank Spitulski Date: Sun, 24 Feb 2019 12:51:45 -0800 Subject: [PATCH] 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() {