Merge pull request #540 from FrankSpitulski/feat/push/add-format-parameter

feat(push): add format builder option
This commit is contained in:
Björn Rabenstein 2019-02-25 13:35:56 +01:00 committed by GitHub
commit 2daed26f63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 2 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,16 @@ 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. 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
}
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 +210,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() {
@ -222,7 +235,7 @@ func (p *Pusher) push(method string) error {
if p.useBasicAuth { if p.useBasicAuth {
req.SetBasicAuth(p.username, p.password) 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) resp, err := p.client.Do(req)
if err != nil { if err != nil {
return err return err