Prometheus roundup the trapped time to the interval.
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.
This commit is contained in:
11
main.go
11
main.go
@@ -42,23 +42,24 @@ func startSending(maxClients int64, bannerMaxLength int64, records chan<- metric
|
|||||||
bytesSent, err := c.Send(bannerMaxLength)
|
bytesSent, err := c.Send(bannerMaxLength)
|
||||||
remoteIpAddr := c.RemoteIpAddr()
|
remoteIpAddr := c.RemoteIpAddr()
|
||||||
localPort := c.LocalPort()
|
localPort := c.LocalPort()
|
||||||
|
millisecondsSpent := c.MillisecondsSinceLast()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Close()
|
c.Close()
|
||||||
records <- metrics.RecordEntry{
|
records <- metrics.RecordEntry{
|
||||||
RecordType: metrics.RecordEntryTypeStop,
|
RecordType: metrics.RecordEntryTypeStop,
|
||||||
IpAddr: remoteIpAddr,
|
IpAddr: remoteIpAddr,
|
||||||
LocalPort: localPort,
|
LocalPort: localPort,
|
||||||
|
MillisecondsSpent: millisecondsSpent,
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
millisecondsSpent := c.MillisecondsSinceLast()
|
|
||||||
clients <- c
|
clients <- c
|
||||||
records <- metrics.RecordEntry{
|
records <- metrics.RecordEntry{
|
||||||
RecordType: metrics.RecordEntryTypeSend,
|
RecordType: metrics.RecordEntryTypeSend,
|
||||||
IpAddr: remoteIpAddr,
|
IpAddr: remoteIpAddr,
|
||||||
LocalPort: localPort,
|
LocalPort: localPort,
|
||||||
BytesSent: bytesSent,
|
|
||||||
MillisecondsSpent: millisecondsSpent,
|
MillisecondsSpent: millisecondsSpent,
|
||||||
|
BytesSent: bytesSent,
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -106,8 +106,8 @@ type RecordEntry struct {
|
|||||||
RecordType int
|
RecordType int
|
||||||
IpAddr string
|
IpAddr string
|
||||||
LocalPort string
|
LocalPort string
|
||||||
BytesSent int
|
|
||||||
MillisecondsSpent int64
|
MillisecondsSpent int64
|
||||||
|
BytesSent int
|
||||||
}
|
}
|
||||||
|
|
||||||
func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUnseenSeconds int, geoOption geoip.GeoOption) chan RecordEntry {
|
func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUnseenSeconds int, geoOption geoip.GeoOption) chan RecordEntry {
|
||||||
@@ -140,10 +140,15 @@ func StartRecording(maxClients int64, prometheusEnabled bool, prometheusCleanUns
|
|||||||
clientSeconds.With(prometheus.Labels{
|
clientSeconds.With(prometheus.Labels{
|
||||||
"ip": r.IpAddr,
|
"ip": r.IpAddr,
|
||||||
"local_port": r.LocalPort}).Add(secondsSpent)
|
"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)
|
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())
|
pq.Update(r.IpAddr, time.Now())
|
||||||
case RecordEntryTypeStop:
|
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()
|
totalClientsClosed.With(prometheus.Labels{"local_port": r.LocalPort}).Inc()
|
||||||
pq.Update(r.IpAddr, time.Now())
|
pq.Update(r.IpAddr, time.Now())
|
||||||
case RecordEntryTypeClean:
|
case RecordEntryTypeClean:
|
||||||
|
|||||||
Reference in New Issue
Block a user