2020-04-29 16:27:08 +02:00
package discoveryutils
import (
2020-05-04 19:48:02 +02:00
"crypto/tls"
2020-05-19 16:35:47 +02:00
"flag"
2020-05-04 19:48:02 +02:00
"fmt"
"net"
2020-04-29 16:27:08 +02:00
"net/http"
2020-05-04 19:48:02 +02:00
"strings"
2020-05-19 16:35:47 +02:00
"sync"
2020-04-29 16:27:08 +02:00
"time"
2020-05-04 19:48:02 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth"
2020-05-19 16:35:47 +02:00
"github.com/VictoriaMetrics/VictoriaMetrics/lib/timerpool"
2020-05-04 19:48:02 +02:00
"github.com/VictoriaMetrics/fasthttp"
2020-04-29 16:27:08 +02:00
)
2020-05-19 16:35:47 +02:00
var (
2020-06-20 16:52:49 +02:00
maxConcurrency = flag . Int ( "promscrape.discovery.concurrency" , 100 , "The maximum number of concurrent requests to Prometheus autodiscovery API (Consul, Kubernetes, etc.)" )
2020-05-19 16:35:47 +02:00
maxWaitTime = flag . Duration ( "promscrape.discovery.concurrentWaitTime" , time . Minute , "The maximum duration for waiting to perform API requests " +
"if more than -promscrape.discovery.concurrency requests are simultaneously performed" )
)
2020-04-29 16:27:08 +02:00
var defaultClient = & http . Client {
Timeout : 30 * time . Second ,
}
// GetHTTPClient returns default client for http API requests.
func GetHTTPClient ( ) * http . Client {
return defaultClient
}
2020-05-04 19:48:02 +02:00
// Client is http client, which talks to the given apiServer.
type Client struct {
2020-12-03 18:50:50 +01:00
// hc is used for short requests.
2020-12-03 18:47:40 +01:00
hc * fasthttp . HostClient
2020-12-03 18:50:50 +01:00
// blockingClient is used for long-polling requests.
2020-12-03 18:47:40 +01:00
blockingClient * fasthttp . HostClient
2020-12-03 18:50:50 +01:00
ac * promauth . Config
apiServer string
hostPort string
2020-05-04 19:48:02 +02:00
}
// NewClient returns new Client for the given apiServer and the given ac.
func NewClient ( apiServer string , ac * promauth . Config ) ( * Client , error ) {
2020-10-12 12:38:21 +02:00
var (
dialFunc fasthttp . DialFunc
tlsCfg * tls . Config
u fasthttp . URI
)
2020-05-04 19:48:02 +02:00
u . Update ( apiServer )
2020-10-12 12:38:21 +02:00
// special case for unix socket connection
if string ( u . Scheme ( ) ) == "unix" {
dialAddr := string ( u . Path ( ) )
apiServer = "http://"
dialFunc = func ( _ string ) ( net . Conn , error ) {
return net . Dial ( "unix" , dialAddr )
}
}
2020-05-04 19:48:02 +02:00
hostPort := string ( u . Host ( ) )
isTLS := string ( u . Scheme ( ) ) == "https"
if isTLS && ac != nil {
tlsCfg = ac . NewTLSConfig ( )
}
if ! strings . Contains ( hostPort , ":" ) {
port := "80"
if isTLS {
port = "443"
}
hostPort = net . JoinHostPort ( hostPort , port )
}
hc := & fasthttp . HostClient {
Addr : hostPort ,
Name : "vm_promscrape/discovery" ,
DialDualStack : netutil . TCP6Enabled ( ) ,
IsTLS : isTLS ,
TLSConfig : tlsCfg ,
ReadTimeout : time . Minute ,
WriteTimeout : 10 * time . Second ,
MaxResponseBodySize : 300 * 1024 * 1024 ,
2020-06-20 16:52:49 +02:00
MaxConns : 2 * * maxConcurrency ,
2020-10-12 12:38:21 +02:00
Dial : dialFunc ,
2020-05-04 19:48:02 +02:00
}
2020-12-03 18:50:50 +01:00
blockingClient := & fasthttp . HostClient {
2020-12-03 18:47:40 +01:00
Addr : hostPort ,
Name : "vm_promscrape/discovery" ,
DialDualStack : netutil . TCP6Enabled ( ) ,
IsTLS : isTLS ,
TLSConfig : tlsCfg ,
ReadTimeout : time . Minute * 3 ,
WriteTimeout : 10 * time . Second ,
MaxResponseBodySize : 300 * 1024 * 1024 ,
2020-12-05 11:13:57 +01:00
MaxConns : 64 * 1024 ,
2020-12-03 18:47:40 +01:00
Dial : dialFunc ,
}
2020-05-04 19:48:02 +02:00
return & Client {
2020-12-03 18:47:40 +01:00
hc : hc ,
2020-12-03 18:50:50 +01:00
blockingClient : blockingClient ,
2020-12-03 18:47:40 +01:00
ac : ac ,
apiServer : apiServer ,
hostPort : hostPort ,
2020-05-04 19:48:02 +02:00
} , nil
}
2020-05-19 16:35:47 +02:00
var (
concurrencyLimitCh chan struct { }
concurrencyLimitChOnce sync . Once
)
func concurrencyLimitChInit ( ) {
concurrencyLimitCh = make ( chan struct { } , * maxConcurrency )
}
2020-12-03 18:50:50 +01:00
// Addr returns the address the client connects to.
func ( c * Client ) Addr ( ) string {
return c . hc . Addr
2020-12-03 18:47:40 +01:00
}
2020-05-04 19:48:02 +02:00
// GetAPIResponse returns response for the given absolute path.
func ( c * Client ) GetAPIResponse ( path string ) ( [ ] byte , error ) {
2020-05-19 16:35:47 +02:00
// Limit the number of concurrent API requests.
concurrencyLimitChOnce . Do ( concurrencyLimitChInit )
t := timerpool . Get ( * maxWaitTime )
select {
case concurrencyLimitCh <- struct { } { } :
timerpool . Put ( t )
case <- t . C :
timerpool . Put ( t )
return nil , fmt . Errorf ( "too many outstanding requests to %q; try increasing -promscrape.discovery.concurrentWaitTime=%s or -promscrape.discovery.concurrency=%d" ,
c . apiServer , * maxWaitTime , * maxConcurrency )
}
defer func ( ) { <- concurrencyLimitCh } ( )
2020-12-03 18:47:40 +01:00
return c . getAPIResponseWithParamsAndClient ( c . hc , path , nil )
}
2020-05-19 16:35:47 +02:00
2020-12-03 18:47:40 +01:00
// GetBlockingAPIResponse returns response for given absolute path with blocking client and optional callback for api response,
// inspectResponse - should never reference data from response.
func ( c * Client ) GetBlockingAPIResponse ( path string , inspectResponse func ( resp * fasthttp . Response ) ) ( [ ] byte , error ) {
return c . getAPIResponseWithParamsAndClient ( c . blockingClient , path , inspectResponse )
}
// getAPIResponseWithParamsAndClient returns response for the given absolute path with optional callback for response.
func ( c * Client ) getAPIResponseWithParamsAndClient ( client * fasthttp . HostClient , path string , inspectResponse func ( resp * fasthttp . Response ) ) ( [ ] byte , error ) {
2020-05-04 19:48:02 +02:00
requestURL := c . apiServer + path
var u fasthttp . URI
u . Update ( requestURL )
var req fasthttp . Request
req . SetRequestURIBytes ( u . RequestURI ( ) )
req . SetHost ( c . hostPort )
req . Header . Set ( "Accept-Encoding" , "gzip" )
if c . ac != nil && c . ac . Authorization != "" {
req . Header . Set ( "Authorization" , c . ac . Authorization )
}
2020-12-03 18:47:40 +01:00
2020-05-04 19:48:02 +02:00
var resp fasthttp . Response
2020-12-03 18:47:40 +01:00
deadline := time . Now ( ) . Add ( client . ReadTimeout )
if err := doRequestWithPossibleRetry ( client , & req , & resp , deadline ) ; err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot fetch %q: %w" , requestURL , err )
2020-05-04 19:48:02 +02:00
}
var data [ ] byte
if ce := resp . Header . Peek ( "Content-Encoding" ) ; string ( ce ) == "gzip" {
dst , err := fasthttp . AppendGunzipBytes ( nil , resp . Body ( ) )
if err != nil {
2020-06-30 21:58:18 +02:00
return nil , fmt . Errorf ( "cannot ungzip response from %q: %w" , requestURL , err )
2020-05-04 19:48:02 +02:00
}
data = dst
} else {
data = append ( data [ : 0 ] , resp . Body ( ) ... )
}
2020-12-03 18:47:40 +01:00
if inspectResponse != nil {
inspectResponse ( & resp )
}
2020-05-04 19:48:02 +02:00
statusCode := resp . StatusCode ( )
if statusCode != fasthttp . StatusOK {
return nil , fmt . Errorf ( "unexpected status code returned from %q: %d; expecting %d; response body: %q" ,
requestURL , statusCode , fasthttp . StatusOK , data )
}
return data , nil
}
2020-08-13 21:31:42 +02:00
func doRequestWithPossibleRetry ( hc * fasthttp . HostClient , req * fasthttp . Request , resp * fasthttp . Response , deadline time . Time ) error {
for {
// Use DoDeadline instead of Do even if hc.ReadTimeout is already set in order to guarantee the given deadline
// across multiple retries.
err := hc . DoDeadline ( req , resp , deadline )
if err == nil {
return nil
}
if err != fasthttp . ErrConnectionClosed {
return err
}
// Retry request if the server closes the keep-alive connection unless deadline exceeds.
if time . Since ( deadline ) >= 0 {
return fmt . Errorf ( "the server closes all the connection attempts: %w" , err )
}
}
}