diff --git a/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go b/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go index da74c511a..5db31fdc2 100644 --- a/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go +++ b/documentation/examples/remote_storage/remote_storage_adapter/opentsdb/client.go @@ -27,7 +27,6 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/common/model" - "golang.org/x/net/context/ctxhttp" ) const ( @@ -106,7 +105,12 @@ func (c *Client) Write(samples model.Samples) error { ctx, cancel := context.WithTimeout(context.Background(), c.timeout) defer cancel() - resp, err := ctxhttp.Post(ctx, http.DefaultClient, u.String(), contentTypeJSON, bytes.NewBuffer(buf)) + req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(buf)) + if err != nil { + return err + } + req.Header.Set("Content-Type", contentTypeJSON) + resp, err := http.DefaultClient.Do(req.WithContext(ctx)) if err != nil { return err } diff --git a/notifier/notifier.go b/notifier/notifier.go index 8a8b97303..8604fe28d 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -33,8 +33,6 @@ import ( "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" - old_ctx "golang.org/x/net/context" - "golang.org/x/net/context/ctxhttp" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -124,7 +122,7 @@ type Options struct { ExternalLabels model.LabelSet RelabelConfigs []*config.RelabelConfig // Used for sending HTTP requests to the Alertmanager. - Do func(ctx old_ctx.Context, client *http.Client, req *http.Request) (*http.Response, error) + Do func(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) Registerer prometheus.Registerer } @@ -206,12 +204,19 @@ func newAlertMetrics(r prometheus.Registerer, queueCap int, queueLen, alertmanag return m } +func do(ctx context.Context, client *http.Client, req *http.Request) (*http.Response, error) { + if client == nil { + client = http.DefaultClient + } + return client.Do(req.WithContext(ctx)) +} + // NewManager is the manager constructor. func NewManager(o *Options, logger log.Logger) *Manager { ctx, cancel := context.WithCancel(context.Background()) if o.Do == nil { - o.Do = ctxhttp.Do + o.Do = do } if logger == nil { logger = log.NewNopLogger() diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index 3d68d3ac1..a98d7d714 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -25,7 +25,6 @@ import ( "testing" "time" - old_ctx "golang.org/x/net/context" yaml "gopkg.in/yaml.v2" config_util "github.com/prometheus/common/config" @@ -212,7 +211,7 @@ func TestCustomDo(t *testing.T) { var received bool h := NewManager(&Options{ - Do: func(ctx old_ctx.Context, client *http.Client, req *http.Request) (*http.Response, error) { + Do: func(_ context.Context, client *http.Client, req *http.Request) (*http.Response, error) { received = true body, err := ioutil.ReadAll(req.Body) diff --git a/scrape/scrape.go b/scrape/scrape.go index 3060c34a5..5b40bf905 100644 --- a/scrape/scrape.go +++ b/scrape/scrape.go @@ -32,7 +32,6 @@ import ( config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" - "golang.org/x/net/context/ctxhttp" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -469,7 +468,7 @@ func (s *targetScraper) scrape(ctx context.Context, w io.Writer) (string, error) s.req = req } - resp, err := ctxhttp.Do(ctx, s.client, s.req) + resp, err := s.client.Do(s.req.WithContext(ctx)) if err != nil { return "", err } diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index fa64c3010..3d6afdbb9 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -1252,8 +1252,11 @@ func TestTargetScrapeScrapeCancel(t *testing.T) { }() go func() { - if _, err := ts.scrape(ctx, ioutil.Discard); err != context.Canceled { - errc <- fmt.Errorf("Expected context cancelation error but got: %s", err) + _, err := ts.scrape(ctx, ioutil.Discard) + if err == nil { + errc <- fmt.Errorf("Expected error but got nil") + } else if ctx.Err() != context.Canceled { + errc <- fmt.Errorf("Expected context cancelation error but got: %s", ctx.Err()) } close(errc) }() diff --git a/storage/remote/client.go b/storage/remote/client.go index 92fb67266..6e1f8f0fc 100644 --- a/storage/remote/client.go +++ b/storage/remote/client.go @@ -26,7 +26,6 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" "github.com/prometheus/common/model" - "golang.org/x/net/context/ctxhttp" config_util "github.com/prometheus/common/config" "github.com/prometheus/prometheus/prompb" @@ -90,7 +89,7 @@ func (c *Client) Store(ctx context.Context, req *prompb.WriteRequest) error { ctx, cancel := context.WithTimeout(context.Background(), c.timeout) defer cancel() - httpResp, err := ctxhttp.Do(ctx, c.client, httpReq) + httpResp, err := c.client.Do(httpReq.WithContext(ctx)) if err != nil { // Errors from client.Do are from (for example) network errors, so are // recoverable. @@ -144,7 +143,7 @@ func (c *Client) Read(ctx context.Context, query *prompb.Query) (*prompb.QueryRe ctx, cancel := context.WithTimeout(ctx, c.timeout) defer cancel() - httpResp, err := ctxhttp.Do(ctx, c.client, httpReq) + httpResp, err := c.client.Do(httpReq.WithContext(ctx)) if err != nil { return nil, fmt.Errorf("error sending request: %v", err) } diff --git a/vendor/modules.txt b/vendor/modules.txt index 0a180ded1..7a0e7cdff 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -212,12 +212,12 @@ golang.org/x/crypto/ed25519 golang.org/x/crypto/ed25519/internal/edwards25519 golang.org/x/crypto/ssh/terminal # golang.org/x/net v0.0.0-20180320002117-6078986fec03 -golang.org/x/net/context/ctxhttp golang.org/x/net/context golang.org/x/net/netutil golang.org/x/net/trace golang.org/x/net/ipv4 golang.org/x/net/ipv6 +golang.org/x/net/context/ctxhttp golang.org/x/net/http2 golang.org/x/net/http2/hpack golang.org/x/net/internal/timeseries