feat(push): add format builder option

Allow users to specify which format the pusher should push in.

Signed-off-by: Frank Spitulski <fspituls@ucsd.edu>
This commit is contained in:
Frank Spitulski 2019-02-24 12:51:45 -08:00
parent 4c99dd6630
commit 7b127b3fe5
1 changed files with 12 additions and 1 deletions

View File

@ -64,6 +64,8 @@ type Pusher struct {
client *http.Client client *http.Client
useBasicAuth bool useBasicAuth bool
username, password string username, password string
expfmt expfmt.Format
} }
// New creates a new Pusher to push to the provided URL with the provided job // 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}, gatherers: prometheus.Gatherers{reg},
registerer: reg, registerer: reg,
client: &http.Client{}, client: &http.Client{},
expfmt: expfmt.FmtProtoDelim,
} }
} }
@ -182,6 +185,14 @@ func (p *Pusher) BasicAuth(username, password string) *Pusher {
return p 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 { func (p *Pusher) push(method string) error {
if p.error != nil { if p.error != nil {
return p.error return p.error
@ -197,7 +208,7 @@ func (p *Pusher) push(method string) error {
return err return err
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
enc := expfmt.NewEncoder(buf, expfmt.FmtProtoDelim) enc := expfmt.NewEncoder(buf, p.expfmt)
// Check for pre-existing grouping labels: // Check for pre-existing grouping labels:
for _, mf := range mfs { for _, mf := range mfs {
for _, m := range mf.GetMetric() { for _, m := range mf.GetMetric() {