mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-25 03:40:10 +01:00
81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package remotewrite
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"strings"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
|
"github.com/VictoriaMetrics/metrics"
|
|
)
|
|
|
|
func statDial(network, addr string) (conn net.Conn, err error) {
|
|
if !strings.HasPrefix(network, "tcp") {
|
|
return nil, fmt.Errorf("unexpected network passed to statDial: %q; it must start from `tcp`", network)
|
|
}
|
|
if !netutil.TCP6Enabled() {
|
|
network = "tcp4"
|
|
}
|
|
conn, err = net.DialTimeout(network, addr, 5*time.Second)
|
|
dialsTotal.Inc()
|
|
if err != nil {
|
|
dialErrors.Inc()
|
|
return nil, err
|
|
}
|
|
conns.Inc()
|
|
sc := &statConn{
|
|
Conn: conn,
|
|
}
|
|
return sc, nil
|
|
}
|
|
|
|
var (
|
|
dialsTotal = metrics.NewCounter(`vmagent_remotewrite_dials_total`)
|
|
dialErrors = metrics.NewCounter(`vmagent_remotewrite_dial_errors_total`)
|
|
conns = metrics.NewCounter(`vmagent_remotewrite_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(`vmagent_remotewrite_conn_reads_total`)
|
|
connWritesTotal = metrics.NewCounter(`vmagent_remotewrite_conn_writes_total`)
|
|
connReadErrors = metrics.NewCounter(`vmagent_remotewrite_conn_read_errors_total`)
|
|
connWriteErrors = metrics.NewCounter(`vmagent_remotewrite_conn_write_errors_total`)
|
|
connBytesRead = metrics.NewCounter(`vmagent_remotewrite_conn_bytes_read_total`)
|
|
connBytesWritten = metrics.NewCounter(`vmagent_remotewrite_conn_bytes_written_total`)
|
|
)
|