2020-02-23 11:35:47 +00:00
|
|
|
package remotewrite
|
|
|
|
|
|
|
|
import (
|
2022-04-06 09:28:54 +00:00
|
|
|
"context"
|
2020-02-23 11:35:47 +00:00
|
|
|
"net"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2020-05-12 12:22:27 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
2020-02-23 11:35:47 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
2023-09-01 07:34:16 +00:00
|
|
|
func statDial(ctx context.Context, _, addr string) (conn net.Conn, err error) {
|
2021-03-16 22:22:57 +00:00
|
|
|
network := netutil.GetTCPNetwork()
|
2024-04-17 18:47:59 +00:00
|
|
|
conn, err = netutil.DialMaybeSRV(ctx, network, addr)
|
2020-02-23 11:35:47 +00:00
|
|
|
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 {
|
2024-02-24 00:44:19 +00:00
|
|
|
closed atomic.Int32
|
2020-02-23 11:35:47 +00:00
|
|
|
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()
|
2024-02-24 00:44:19 +00:00
|
|
|
if sc.closed.Add(1) == 1 {
|
2020-02-23 11:35:47 +00:00
|
|
|
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`)
|
|
|
|
)
|