2020-02-23 12:35:47 +01:00
|
|
|
package promscrape
|
|
|
|
|
|
|
|
import (
|
2020-11-01 22:12:13 +01:00
|
|
|
"context"
|
2020-02-23 12:35:47 +01:00
|
|
|
"net"
|
2020-11-01 22:12:13 +01:00
|
|
|
"sync"
|
2020-02-23 12:35:47 +01:00
|
|
|
"sync/atomic"
|
2020-11-01 22:12:13 +01:00
|
|
|
"time"
|
2020-02-23 12:35:47 +01:00
|
|
|
|
2020-05-05 22:55:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
2020-04-29 15:20:23 +02:00
|
|
|
"github.com/VictoriaMetrics/fasthttp"
|
2020-02-23 12:35:47 +01:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2020-11-01 22:12:13 +01:00
|
|
|
func statStdDial(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
d := getStdDialer()
|
|
|
|
conn, err := d.DialContext(ctx, network, addr)
|
|
|
|
dialsTotal.Inc()
|
|
|
|
if err != nil {
|
|
|
|
dialErrors.Inc()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conns.Inc()
|
|
|
|
sc := &statConn{
|
|
|
|
Conn: conn,
|
|
|
|
}
|
|
|
|
return sc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStdDialer() *net.Dialer {
|
|
|
|
stdDialerOnce.Do(func() {
|
|
|
|
stdDialer = &net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: netutil.TCP6Enabled(),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return stdDialer
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
stdDialer *net.Dialer
|
|
|
|
stdDialerOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2020-05-05 22:55:55 +02:00
|
|
|
func statDial(addr string) (conn net.Conn, err error) {
|
|
|
|
if netutil.TCP6Enabled() {
|
|
|
|
conn, err = fasthttp.DialDualStack(addr)
|
|
|
|
} else {
|
|
|
|
conn, err = fasthttp.Dial(addr)
|
|
|
|
}
|
2020-02-23 12:35:47 +01:00
|
|
|
dialsTotal.Inc()
|
|
|
|
if err != nil {
|
|
|
|
dialErrors.Inc()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
conns.Inc()
|
|
|
|
sc := &statConn{
|
|
|
|
Conn: conn,
|
|
|
|
}
|
|
|
|
return sc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
dialsTotal = metrics.NewCounter(`vm_promscrape_dials_total`)
|
|
|
|
dialErrors = metrics.NewCounter(`vm_promscrape_dial_errors_total`)
|
|
|
|
conns = metrics.NewCounter(`vm_promscrape_conns`)
|
|
|
|
)
|
|
|
|
|
|
|
|
type statConn struct {
|
|
|
|
closed uint64
|
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *statConn) Read(p []byte) (int, error) {
|
|
|
|
n, err := sc.Conn.Read(p)
|
|
|
|
connReadsTotal.Inc()
|
|
|
|
if err != nil {
|
|
|
|
connReadErrors.Inc()
|
|
|
|
}
|
|
|
|
connBytesRead.Add(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *statConn) Write(p []byte) (int, error) {
|
|
|
|
n, err := sc.Conn.Write(p)
|
|
|
|
connWritesTotal.Inc()
|
|
|
|
if err != nil {
|
|
|
|
connWriteErrors.Inc()
|
|
|
|
}
|
|
|
|
connBytesWritten.Add(n)
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sc *statConn) Close() error {
|
|
|
|
err := sc.Conn.Close()
|
|
|
|
if atomic.AddUint64(&sc.closed, 1) == 1 {
|
|
|
|
conns.Dec()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
connReadsTotal = metrics.NewCounter(`vm_promscrape_conn_reads_total`)
|
|
|
|
connWritesTotal = metrics.NewCounter(`vm_promscrape_conn_writes_total`)
|
|
|
|
connReadErrors = metrics.NewCounter(`vm_promscrape_conn_read_errors_total`)
|
|
|
|
connWriteErrors = metrics.NewCounter(`vm_promscrape_conn_write_errors_total`)
|
|
|
|
connBytesRead = metrics.NewCounter(`vm_promscrape_conn_bytes_read_total`)
|
|
|
|
connBytesWritten = metrics.NewCounter(`vm_promscrape_conn_bytes_written_total`)
|
|
|
|
)
|