add local_port label to all metrics

This commit is contained in:
Shizun Ge 2024-01-17 17:48:33 -08:00
parent 7da8e5e9ab
commit 37d428cdf5

View file

@ -19,7 +19,6 @@ package metrics
import ( import (
"endlessh-go/geoip" "endlessh-go/geoip"
"net/http" "net/http"
"sync/atomic"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -27,50 +26,38 @@ import (
) )
var ( var (
numTotalClients int64 totalClients *prometheus.CounterVec
numTotalClientsClosed int64 totalClientsClosed *prometheus.CounterVec
numTotalBytes int64 totalBytes *prometheus.CounterVec
numTotalMilliseconds int64 totalSeconds *prometheus.CounterVec
totalClients prometheus.CounterFunc clientIP *prometheus.CounterVec
totalClientsClosed prometheus.CounterFunc clientSeconds *prometheus.CounterVec
totalBytes prometheus.CounterFunc
totalSeconds prometheus.CounterFunc
clientIP *prometheus.CounterVec
clientSeconds *prometheus.CounterVec
) )
func InitPrometheus(prometheusHost, prometheusPort, prometheusEntry string) { func InitPrometheus(prometheusHost, prometheusPort, prometheusEntry string) {
totalClients = prometheus.NewCounterFunc( totalClients = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: "endlessh_client_open_count_total", Name: "endlessh_client_open_count_total",
Help: "Total number of clients that tried to connect to this host.", Help: "Total number of clients that tried to connect to this host.",
}, func() float64 { }, []string{"local_port"},
return float64(numTotalClients)
},
) )
totalClientsClosed = prometheus.NewCounterFunc( totalClientsClosed = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: "endlessh_client_closed_count_total", Name: "endlessh_client_closed_count_total",
Help: "Total number of clients that stopped connecting to this host.", Help: "Total number of clients that stopped connecting to this host.",
}, func() float64 { }, []string{"local_port"},
return float64(numTotalClientsClosed)
},
) )
totalBytes = prometheus.NewCounterFunc( totalBytes = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: "endlessh_sent_bytes_total", Name: "endlessh_sent_bytes_total",
Help: "Total bytes sent to clients that tried to connect to this host.", Help: "Total bytes sent to clients that tried to connect to this host.",
}, func() float64 { }, []string{"local_port"},
return float64(numTotalBytes)
},
) )
totalSeconds = prometheus.NewCounterFunc( totalSeconds = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
Name: "endlessh_trapped_time_seconds_total", Name: "endlessh_trapped_time_seconds_total",
Help: "Total seconds clients spent on endlessh.", Help: "Total seconds clients spent on endlessh.",
}, func() float64 { }, []string{"local_port"},
return float64(numTotalMilliseconds) / 1000
},
) )
clientIP = prometheus.NewCounterVec( clientIP = prometheus.NewCounterVec(
prometheus.CounterOpts{ prometheus.CounterOpts{
@ -138,15 +125,16 @@ func StartRecording(maxClients int64, prometheusEnabled bool, geoOption geoip.Ge
"geohash": geohash, "geohash": geohash,
"country": country, "country": country,
"location": location}).Inc() "location": location}).Inc()
atomic.AddInt64(&numTotalClients, 1) totalClients.With(prometheus.Labels{"local_port": r.LocalPort}).Inc()
case RecordEntryTypeSend: case RecordEntryTypeSend:
secondsSpent := float64(r.MillisecondsSpent) / 1000
clientSeconds.With(prometheus.Labels{ clientSeconds.With(prometheus.Labels{
"ip": r.IpAddr, "ip": r.IpAddr,
"local_port": r.LocalPort}).Add(float64(r.MillisecondsSpent) / 1000) "local_port": r.LocalPort}).Add(secondsSpent)
atomic.AddInt64(&numTotalBytes, int64(r.BytesSent)) totalBytes.With(prometheus.Labels{"local_port": r.LocalPort}).Add(float64(r.BytesSent))
atomic.AddInt64(&numTotalMilliseconds, r.MillisecondsSpent) totalSeconds.With(prometheus.Labels{"local_port": r.LocalPort}).Add(secondsSpent)
case RecordEntryTypeStop: case RecordEntryTypeStop:
atomic.AddInt64(&numTotalClientsClosed, 1) totalClientsClosed.With(prometheus.Labels{"local_port": r.LocalPort}).Inc()
} }
} }
}() }()