2017-04-20 00:24:14 +03:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-26 00:15:57 +03:00
|
|
|
// Package api provides clients for the HTTP APIs.
|
2017-04-20 00:24:14 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-01-25 13:16:10 +03:00
|
|
|
"bytes"
|
2017-04-20 15:57:46 +03:00
|
|
|
"context"
|
2022-04-29 17:02:44 +03:00
|
|
|
"errors"
|
2024-10-18 16:13:33 +03:00
|
|
|
"io"
|
2017-04-20 00:24:14 +03:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-04-20 16:31:18 +03:00
|
|
|
// DefaultRoundTripper is used if no RoundTripper is set in Config.
|
|
|
|
var DefaultRoundTripper http.RoundTripper = &http.Transport{
|
2017-04-20 00:24:14 +03:00
|
|
|
Proxy: http.ProxyFromEnvironment,
|
2018-06-27 13:02:52 +03:00
|
|
|
DialContext: (&net.Dialer{
|
2017-04-20 00:24:14 +03:00
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
2018-06-27 13:02:52 +03:00
|
|
|
}).DialContext,
|
2017-04-20 00:24:14 +03:00
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config defines configuration parameters for a new client.
|
|
|
|
type Config struct {
|
|
|
|
// The address of the Prometheus to connect to.
|
|
|
|
Address string
|
|
|
|
|
2022-04-29 17:02:44 +03:00
|
|
|
// Client is used by the Client to drive HTTP requests. If not provided,
|
2023-12-22 14:16:00 +03:00
|
|
|
// a new one based on the provided RoundTripper (or DefaultRoundTripper) will be used.
|
|
|
|
Client *http.Client
|
2022-04-29 17:02:44 +03:00
|
|
|
|
2017-04-20 16:31:18 +03:00
|
|
|
// RoundTripper is used by the Client to drive HTTP requests. If not
|
|
|
|
// provided, DefaultRoundTripper will be used.
|
|
|
|
RoundTripper http.RoundTripper
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:31:18 +03:00
|
|
|
func (cfg *Config) roundTripper() http.RoundTripper {
|
|
|
|
if cfg.RoundTripper == nil {
|
|
|
|
return DefaultRoundTripper
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
2017-04-20 16:31:18 +03:00
|
|
|
return cfg.RoundTripper
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
|
2023-12-22 14:16:00 +03:00
|
|
|
func (cfg *Config) client() http.Client {
|
2022-04-29 17:02:44 +03:00
|
|
|
if cfg.Client == nil {
|
2023-12-22 14:16:00 +03:00
|
|
|
return http.Client{
|
2022-04-29 17:02:44 +03:00
|
|
|
Transport: cfg.roundTripper(),
|
|
|
|
}
|
|
|
|
}
|
2023-12-22 14:16:00 +03:00
|
|
|
return *cfg.Client
|
2022-04-29 17:02:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *Config) validate() error {
|
|
|
|
if cfg.Client != nil && cfg.RoundTripper != nil {
|
|
|
|
return errors.New("api.Config.RoundTripper and api.Config.Client are mutually exclusive")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
// Client is the interface for an API client.
|
|
|
|
type Client interface {
|
|
|
|
URL(ep string, args map[string]string) *url.URL
|
2019-12-10 23:16:44 +03:00
|
|
|
Do(context.Context, *http.Request) (*http.Response, []byte, error)
|
2019-04-30 10:13:48 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 18:00:55 +03:00
|
|
|
type CloseIdler interface {
|
|
|
|
CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2017-04-25 17:37:13 +03:00
|
|
|
// NewClient returns a new Client.
|
2017-04-20 00:24:14 +03:00
|
|
|
//
|
|
|
|
// It is safe to use the returned Client from multiple goroutines.
|
2017-04-25 17:37:13 +03:00
|
|
|
func NewClient(cfg Config) (Client, error) {
|
2017-04-20 00:24:14 +03:00
|
|
|
u, err := url.Parse(cfg.Address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
u.Path = strings.TrimRight(u.Path, "/")
|
|
|
|
|
2022-04-29 17:02:44 +03:00
|
|
|
if err := cfg.validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:24:14 +03:00
|
|
|
return &httpClient{
|
2017-04-20 16:31:18 +03:00
|
|
|
endpoint: u,
|
2022-04-29 17:02:44 +03:00
|
|
|
client: cfg.client(),
|
2017-04-20 00:24:14 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpClient struct {
|
2017-04-20 16:31:18 +03:00
|
|
|
endpoint *url.URL
|
2023-12-22 14:16:00 +03:00
|
|
|
client http.Client
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
|
|
|
|
p := path.Join(c.endpoint.Path, ep)
|
|
|
|
|
|
|
|
for arg, val := range args {
|
|
|
|
arg = ":" + arg
|
2022-08-03 07:30:51 +03:00
|
|
|
p = strings.ReplaceAll(p, arg, val)
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
u := *c.endpoint
|
|
|
|
u.Path = p
|
|
|
|
|
|
|
|
return &u
|
|
|
|
}
|
|
|
|
|
2024-09-04 18:00:55 +03:00
|
|
|
func (c *httpClient) CloseIdleConnections() {
|
|
|
|
c.client.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:16:44 +03:00
|
|
|
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
|
2017-04-20 16:31:18 +03:00
|
|
|
if ctx != nil {
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
}
|
|
|
|
resp, err := c.client.Do(req)
|
2017-04-20 00:24:14 +03:00
|
|
|
defer func() {
|
|
|
|
if resp != nil {
|
2024-10-18 16:13:33 +03:00
|
|
|
_, _ = io.Copy(io.Discard, resp.Body)
|
|
|
|
_ = resp.Body.Close()
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil {
|
2019-12-10 23:16:44 +03:00
|
|
|
return nil, nil, err
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var body []byte
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
2022-01-25 13:16:10 +03:00
|
|
|
var buf bytes.Buffer
|
2024-10-18 16:13:33 +03:00
|
|
|
// TODO(bwplotka): Add LimitReader for too long err messages (e.g. limit by 1KB)
|
2022-01-25 13:16:10 +03:00
|
|
|
_, err = buf.ReadFrom(resp.Body)
|
|
|
|
body = buf.Bytes()
|
2017-04-20 00:24:14 +03:00
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
<-done
|
2019-01-28 11:53:16 +03:00
|
|
|
err = resp.Body.Close()
|
2017-04-20 00:24:14 +03:00
|
|
|
if err == nil {
|
|
|
|
err = ctx.Err()
|
|
|
|
}
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
|
2019-12-10 23:16:44 +03:00
|
|
|
return resp, body, err
|
2017-04-20 00:24:14 +03:00
|
|
|
}
|