@@ -17,6 +17,7 @@ package api
17
17
import (
18
18
"bytes"
19
19
"context"
20
+ "fmt"
20
21
"net"
21
22
"net/http"
22
23
"net/url"
@@ -35,11 +36,20 @@ var DefaultRoundTripper http.RoundTripper = &http.Transport{
35
36
TLSHandshakeTimeout : 10 * time .Second ,
36
37
}
37
38
39
+ // DefaultClient is used if no Client is set in Config.
40
+ var DefaultClient http.Client = http.Client {
41
+ Transport : DefaultRoundTripper ,
42
+ }
43
+
38
44
// Config defines configuration parameters for a new client.
39
45
type Config struct {
40
46
// The address of the Prometheus to connect to.
41
47
Address string
42
48
49
+ // Client is used by the Client to drive HTTP requests. If not provided,
50
+ // DefaultClient will be used.
51
+ Client * http.Client
52
+
43
53
// RoundTripper is used by the Client to drive HTTP requests. If not
44
54
// provided, DefaultRoundTripper will be used.
45
55
RoundTripper http.RoundTripper
@@ -52,6 +62,13 @@ func (cfg *Config) roundTripper() http.RoundTripper {
52
62
return cfg .RoundTripper
53
63
}
54
64
65
+ func (cfg * Config ) client () http.Client {
66
+ if cfg .Client == nil {
67
+ return DefaultClient
68
+ }
69
+ return * cfg .Client
70
+ }
71
+
55
72
// Client is the interface for an API client.
56
73
type Client interface {
57
74
URL (ep string , args map [string ]string ) * url.URL
@@ -68,9 +85,13 @@ func NewClient(cfg Config) (Client, error) {
68
85
}
69
86
u .Path = strings .TrimRight (u .Path , "/" )
70
87
88
+ if cfg .Client != nil && cfg .RoundTripper != nil {
89
+ return nil , fmt .Errorf ("both client and roundTripper cannot be configured" )
90
+ }
91
+
71
92
return & httpClient {
72
93
endpoint : u ,
73
- client : http. Client { Transport : cfg .roundTripper ()} ,
94
+ client : cfg .client () ,
74
95
}, nil
75
96
}
76
97
0 commit comments