Skip to content

Commit

Permalink
Prometheus roundup the trapped time to the interval.
Browse files Browse the repository at this point in the history
In the old way, if the connection is broken less than an interval, the trapped time won't be reported.
With this fix, the prometheus should report the same value as the log.
  • Loading branch information
shizunge committed Jan 28, 2024
1 parent 8131751 commit 0cc52ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ func startSending(maxClients int64, bannerMaxLength int64, records chan<- metric
bytesSent, err := c.Send(bannerMaxLength)
remoteIpAddr := c.RemoteIpAddr()
localPort := c.LocalPort()
millisecondsSpent := c.MillisecondsSinceLast()
if err != nil {
c.Close()
records <- metrics.RecordEntry{
RecordType: metrics.RecordEntryTypeStop,
IpAddr: remoteIpAddr,
LocalPort: localPort,
RecordType: metrics.RecordEntryTypeStop,
IpAddr: remoteIpAddr,
LocalPort: localPort,
MillisecondsSpent: millisecondsSpent,
}
return
}
millisecondsSpent := c.MillisecondsSinceLast()
clients <- c
records <- metrics.RecordEntry{
RecordType: metrics.RecordEntryTypeSend,
IpAddr: remoteIpAddr,
LocalPort: localPort,
BytesSent: bytesSent,
MillisecondsSpent: millisecondsSpent,
BytesSent: bytesSent,
}
}()
}
Expand Down
9 changes: 7 additions & 2 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ type RecordEntry struct {
RecordType int
IpAddr string
LocalPort string
BytesSent int
MillisecondsSpent int64
BytesSent int
}

func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUnseenSeconds int, geoOption geoip.GeoOption) chan RecordEntry {
Expand Down Expand Up @@ -140,10 +140,15 @@ func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUns
clientSeconds.With(prometheus.Labels{
"ip": r.IpAddr,
"local_port": r.LocalPort}).Add(secondsSpent)
totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent))
totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent)
totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent))
pq.Update(r.IpAddr, time.Now())
case RecordEntryTypeStop:
secondsSpent := float64(r.MillisecondsSpent) / 1000
clientSeconds.With(prometheus.Labels{
"ip": r.IpAddr,
"local_port": r.LocalPort}).Add(secondsSpent)
totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent)
totalClientsClosed.With(prometheus.Labels{"local_port": r.LocalPort}).Inc()
pq.Update(r.IpAddr, time.Now())
case RecordEntryTypeClean:
Expand Down

0 comments on commit 0cc52ee

Please sign in to comment.