Skip to content

Commit 86cef81

Browse files
author
dvilaverde
committed
simple metrics to capture packet data size both compressed & uncompressed
1 parent 8d4de0f commit 86cef81

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packet/conn.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ type Conn struct {
6767
compressedHeader [7]byte
6868

6969
compressedReader io.Reader
70+
71+
Stats *Stats
7072
}
7173

7274
func NewConn(conn net.Conn) *Conn {
@@ -274,6 +276,9 @@ func (c *Conn) ReadPacketTo(w io.Writer) error {
274276
// will modify data inplace
275277
func (c *Conn) WritePacket(data []byte) error {
276278
length := len(data) - 4
279+
if c.Stats != nil {
280+
c.Stats.AddTxUncompressedSize(uint64(length))
281+
}
277282

278283
for length >= MaxPayloadLen {
279284
data[0] = 0xff
@@ -378,6 +383,9 @@ func (c *Conn) writeCompressed(data []byte) (n int, err error) {
378383
return 0, err
379384
}
380385

386+
if c.Stats != nil {
387+
c.Stats.AddTxCompressedSize(uint64(compressedPacket.Len()))
388+
}
381389
_, err = c.Write(compressedPacket.Bytes())
382390
if err != nil {
383391
return 0, err

packet/metrics.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package packet
2+
3+
import "sync/atomic"
4+
5+
type Stats struct {
6+
packetTxCompressedSize atomic.Uint64
7+
packetTxUncompressedSize atomic.Uint64
8+
}
9+
10+
func (p *Stats) AddTxCompressedSize(size uint64) {
11+
p.packetTxCompressedSize.Add(size)
12+
}
13+
14+
func (p *Stats) AddTxUncompressedSize(size uint64) {
15+
p.packetTxUncompressedSize.Add(size)
16+
}
17+
18+
func (p *Stats) GetTxCompressedSize() uint64 {
19+
return p.packetTxCompressedSize.Load()
20+
}
21+
22+
func (p *Stats) GetTxUncompressedSize() uint64 {
23+
return p.packetTxUncompressedSize.Load()
24+
}

0 commit comments

Comments
 (0)