all: Add Prometheus-style metrics to expose some internal performance counters (fixes #5175) (#9003)
This commit is contained in:
@@ -10,8 +10,9 @@ import (
|
||||
|
||||
type countingReader struct {
|
||||
io.Reader
|
||||
tot atomic.Int64 // bytes
|
||||
last atomic.Int64 // unix nanos
|
||||
idString string
|
||||
tot atomic.Int64 // bytes
|
||||
last atomic.Int64 // unix nanos
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -24,6 +25,7 @@ func (c *countingReader) Read(bs []byte) (int, error) {
|
||||
c.tot.Add(int64(n))
|
||||
totalIncoming.Add(int64(n))
|
||||
c.last.Store(time.Now().UnixNano())
|
||||
metricDeviceRecvBytes.WithLabelValues(c.idString).Add(float64(n))
|
||||
return n, err
|
||||
}
|
||||
|
||||
@@ -35,8 +37,9 @@ func (c *countingReader) Last() time.Time {
|
||||
|
||||
type countingWriter struct {
|
||||
io.Writer
|
||||
tot atomic.Int64 // bytes
|
||||
last atomic.Int64 // unix nanos
|
||||
idString string
|
||||
tot atomic.Int64 // bytes
|
||||
last atomic.Int64 // unix nanos
|
||||
}
|
||||
|
||||
func (c *countingWriter) Write(bs []byte) (int, error) {
|
||||
@@ -44,6 +47,7 @@ func (c *countingWriter) Write(bs []byte) (int, error) {
|
||||
c.tot.Add(int64(n))
|
||||
totalOutgoing.Add(int64(n))
|
||||
c.last.Store(time.Now().UnixNano())
|
||||
metricDeviceSentBytes.WithLabelValues(c.idString).Add(float64(n))
|
||||
return n, err
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// Copyright (C) 2023 The Syncthing Authors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
var (
|
||||
metricDeviceSentBytes = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "syncthing",
|
||||
Subsystem: "protocol",
|
||||
Name: "sent_bytes_total",
|
||||
Help: "Total amount of data sent, per device",
|
||||
}, []string{"device"})
|
||||
metricDeviceSentUncompressedBytes = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "syncthing",
|
||||
Subsystem: "protocol",
|
||||
Name: "sent_uncompressed_bytes_total",
|
||||
Help: "Total amount of data sent, before compression, per device",
|
||||
}, []string{"device"})
|
||||
metricDeviceSentMessages = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "syncthing",
|
||||
Subsystem: "protocol",
|
||||
Name: "sent_messages_total",
|
||||
Help: "Total number of messages sent, per device",
|
||||
}, []string{"device"})
|
||||
|
||||
metricDeviceRecvBytes = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "syncthing",
|
||||
Subsystem: "protocol",
|
||||
Name: "recv_bytes_total",
|
||||
Help: "Total amount of data received, per device",
|
||||
}, []string{"device"})
|
||||
metricDeviceRecvDecompressedBytes = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "syncthing",
|
||||
Subsystem: "protocol",
|
||||
Name: "recv_decompressed_bytes_total",
|
||||
Help: "Total amount of data received, after decompression, per device",
|
||||
}, []string{"device"})
|
||||
metricDeviceRecvMessages = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "syncthing",
|
||||
Subsystem: "protocol",
|
||||
Name: "recv_messages_total",
|
||||
Help: "Total number of messages received, per device",
|
||||
}, []string{"device"})
|
||||
)
|
||||
|
||||
func registerDeviceMetrics(deviceID string) {
|
||||
// Register metrics for this device, so that counters are present even
|
||||
// when zero.
|
||||
metricDeviceSentBytes.WithLabelValues(deviceID)
|
||||
metricDeviceSentUncompressedBytes.WithLabelValues(deviceID)
|
||||
metricDeviceSentMessages.WithLabelValues(deviceID)
|
||||
metricDeviceRecvBytes.WithLabelValues(deviceID)
|
||||
metricDeviceRecvMessages.WithLabelValues(deviceID)
|
||||
}
|
||||
@@ -183,6 +183,7 @@ type rawConnection struct {
|
||||
ConnectionInfo
|
||||
|
||||
deviceID DeviceID
|
||||
idString string
|
||||
model contextLessModel
|
||||
startTime time.Time
|
||||
|
||||
@@ -263,12 +264,15 @@ func NewConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, closer
|
||||
}
|
||||
|
||||
func newRawConnection(deviceID DeviceID, reader io.Reader, writer io.Writer, closer io.Closer, receiver contextLessModel, connInfo ConnectionInfo, compress Compression) *rawConnection {
|
||||
cr := &countingReader{Reader: reader}
|
||||
cw := &countingWriter{Writer: writer}
|
||||
idString := deviceID.String()
|
||||
cr := &countingReader{Reader: reader, idString: idString}
|
||||
cw := &countingWriter{Writer: writer, idString: idString}
|
||||
registerDeviceMetrics(idString)
|
||||
|
||||
return &rawConnection{
|
||||
ConnectionInfo: connInfo,
|
||||
deviceID: deviceID,
|
||||
idString: deviceID.String(),
|
||||
model: receiver,
|
||||
cr: cr,
|
||||
cw: cw,
|
||||
@@ -445,6 +449,8 @@ func (c *rawConnection) dispatcherLoop() (err error) {
|
||||
return ErrClosed
|
||||
}
|
||||
|
||||
metricDeviceRecvMessages.WithLabelValues(c.idString).Inc()
|
||||
|
||||
msgContext, err := messageContext(msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("protocol error: %w", err)
|
||||
@@ -553,6 +559,8 @@ func (c *rawConnection) readMessageAfterHeader(hdr Header, fourByteBuf []byte) (
|
||||
|
||||
// ... and is then unmarshalled
|
||||
|
||||
metricDeviceRecvDecompressedBytes.WithLabelValues(c.idString).Add(float64(4 + len(buf)))
|
||||
|
||||
msg, err := newMessage(hdr.Type)
|
||||
if err != nil {
|
||||
BufferPool.Put(buf)
|
||||
@@ -593,6 +601,8 @@ func (c *rawConnection) readHeader(fourByteBuf []byte) (Header, error) {
|
||||
return Header{}, fmt.Errorf("unmarshalling header: %w", err)
|
||||
}
|
||||
|
||||
metricDeviceRecvDecompressedBytes.WithLabelValues(c.idString).Add(float64(2 + len(buf)))
|
||||
|
||||
return hdr, nil
|
||||
}
|
||||
|
||||
@@ -758,6 +768,10 @@ func (c *rawConnection) writeMessage(msg message) error {
|
||||
msgContext, _ := messageContext(msg)
|
||||
l.Debugf("Writing %v", msgContext)
|
||||
|
||||
defer func() {
|
||||
metricDeviceSentMessages.WithLabelValues(c.idString).Inc()
|
||||
}()
|
||||
|
||||
size := msg.ProtoSize()
|
||||
hdr := Header{
|
||||
Type: typeOf(msg),
|
||||
@@ -784,6 +798,8 @@ func (c *rawConnection) writeMessage(msg message) error {
|
||||
}
|
||||
}
|
||||
|
||||
metricDeviceSentUncompressedBytes.WithLabelValues(c.idString).Add(float64(totSize))
|
||||
|
||||
// Header length
|
||||
binary.BigEndian.PutUint16(buf, uint16(hdrSize))
|
||||
// Header
|
||||
@@ -817,6 +833,9 @@ func (c *rawConnection) writeCompressedMessage(msg message, marshaled []byte) (o
|
||||
}
|
||||
|
||||
cOverhead := 2 + hdrSize + 4
|
||||
|
||||
metricDeviceSentUncompressedBytes.WithLabelValues(c.idString).Add(float64(cOverhead + len(marshaled)))
|
||||
|
||||
// The compressed size may be at most n-n/32 = .96875*n bytes,
|
||||
// I.e., if we can't save at least 3.125% bandwidth, we forgo compression.
|
||||
// This number is arbitrary but cheap to compute.
|
||||
|
||||
Reference in New Issue
Block a user