Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push: Add PushContext and AddContext to Pusher #1028

Merged
merged 1 commit into from Apr 12, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions prometheus/push/push.go
Expand Up @@ -36,6 +36,7 @@ package push

import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
Expand Down Expand Up @@ -123,14 +124,28 @@ func New(url, job string) *Pusher {
// Push returns the first error encountered by any method call (including this
// one) in the lifetime of the Pusher.
func (p *Pusher) Push() error {
return p.push(http.MethodPut)
return p.push(context.Background(), http.MethodPut)
}

// PushContext is like Push but includes a context.
//
// If the context expires before HTTP request is complete, an error is returned.
func (p *Pusher) PushContext(ctx context.Context) error {
return p.push(ctx, http.MethodPut)
}

// Add works like push, but only previously pushed metrics with the same name
// (and the same job and other grouping labels) will be replaced. (It uses HTTP
// method “POST” to push to the Pushgateway.)
func (p *Pusher) Add() error {
return p.push(http.MethodPost)
return p.push(context.Background(), http.MethodPost)
}

// AddContext is like Add but includes a context.
//
// If the context expires before HTTP request is complete, an error is returned.
func (p *Pusher) AddContext(ctx context.Context) error {
return p.push(ctx, http.MethodPost)
}

// Gatherer adds a Gatherer to the Pusher, from which metrics will be gathered
Expand Down Expand Up @@ -233,7 +248,7 @@ func (p *Pusher) Delete() error {
return nil
}

func (p *Pusher) push(method string) error {
func (p *Pusher) push(ctx context.Context, method string) error {
if p.error != nil {
return p.error
}
Expand All @@ -260,7 +275,7 @@ func (p *Pusher) push(method string) error {
}
enc.Encode(mf)
}
req, err := http.NewRequest(method, p.fullURL(), buf)
req, err := http.NewRequestWithContext(ctx, method, p.fullURL(), buf)
if err != nil {
return err
}
Expand Down