feat: add proxy_status metric

We want to be able to tell when a proxy goes offline.
Added a proxy_status metric that is either 1 (online) or
0 (offline).
pull/4864/head
Melvin Hulsmans 2025-07-02 10:35:26 +02:00
parent 61330d4d79
commit 458a8bad52
6 changed files with 43 additions and 0 deletions

View File

@ -68,6 +68,12 @@ func (m *serverMetrics) CloseProxy(name string, proxyType string) {
}
}
func (m *serverMetrics) ProxyStatus(name string, proxyType string, online bool) {
for _, v := range m.ms {
v.ProxyStatus(name, proxyType, online)
}
}
func (m *serverMetrics) OpenConnection(name string, proxyType string) {
for _, v := range m.ms {
v.OpenConnection(name, proxyType)

View File

@ -133,6 +133,20 @@ func (m *serverMetrics) CloseProxy(name string, proxyType string) {
}
}
func (m *serverMetrics) ProxyStatus(name string, proxyType string, online bool) {
m.mu.Lock()
defer m.mu.Unlock()
proxyStats, ok := m.info.ProxyStatistics[name]
if ok {
if online {
proxyStats.Status = "online"
} else {
proxyStats.Status = "offline"
}
m.info.ProxyStatistics[name] = proxyStats
}
}
func (m *serverMetrics) OpenConnection(name string, _ string) {
m.info.CurConns.Inc(1)
@ -217,6 +231,7 @@ func (m *serverMetrics) GetProxiesByType(proxyType string) []*ProxyStats {
TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
CurConns: int64(proxyStats.CurConns.Count()),
Status: proxyStats.Status,
}
if !proxyStats.LastStartTime.IsZero() {
ps.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")
@ -248,6 +263,7 @@ func (m *serverMetrics) GetProxiesByTypeAndName(proxyType string, proxyName stri
TodayTrafficIn: proxyStats.TrafficIn.TodayCount(),
TodayTrafficOut: proxyStats.TrafficOut.TodayCount(),
CurConns: int64(proxyStats.CurConns.Count()),
Status: proxyStats.Status,
}
if !proxyStats.LastStartTime.IsZero() {
res.LastStartTime = proxyStats.LastStartTime.Format("01-02 15:04:05")

View File

@ -40,6 +40,7 @@ type ProxyStats struct {
LastStartTime string
LastCloseTime string
CurConns int64
Status string
}
type ProxyTrafficInfo struct {
@ -56,6 +57,7 @@ type ProxyStatistics struct {
CurConns metric.Counter
LastStartTime time.Time
LastCloseTime time.Time
Status string
}
type ServerStatistics struct {

View File

@ -16,6 +16,7 @@ var ServerMetrics metrics.ServerMetrics = newServerMetrics()
type serverMetrics struct {
clientCount prometheus.Gauge
proxyCount *prometheus.GaugeVec
proxyStatus *prometheus.GaugeVec
connectionCount *prometheus.GaugeVec
trafficIn *prometheus.CounterVec
trafficOut *prometheus.CounterVec
@ -37,6 +38,13 @@ func (m *serverMetrics) CloseProxy(_ string, proxyType string) {
m.proxyCount.WithLabelValues(proxyType).Dec()
}
func (m *serverMetrics) ProxyStatus(name string, proxyType string, online bool) {
if online {
m.proxyStatus.WithLabelValues(name, proxyType).Set(1)
}
m.proxyStatus.WithLabelValues(name, proxyType).Set(0)
}
func (m *serverMetrics) OpenConnection(name string, proxyType string) {
m.connectionCount.WithLabelValues(name, proxyType).Inc()
}
@ -67,6 +75,12 @@ func newServerMetrics() *serverMetrics {
Name: "proxy_counts",
Help: "The current proxy counts",
}, []string{"type"}),
proxyStatus: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: serverSubsystem,
Name: "proxy_status",
Help: "The current proxy status",
}, []string{"name", "type"}),
connectionCount: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: serverSubsystem,

View File

@ -17,6 +17,7 @@ package server
import (
"cmp"
"encoding/json"
"github.com/fatedier/frp/server/metrics"
"net/http"
"slices"
@ -253,11 +254,13 @@ func (svr *Service) getProxyStatsByType(proxyType string) (proxyInfos []*ProxySt
continue
}
proxyInfo.Status = "online"
metrics.Server.ProxyStatus(pxy.GetName(), pxy.GetConfigurer().GetBaseConfig().Type, true)
if pxy.GetLoginMsg() != nil {
proxyInfo.ClientVersion = pxy.GetLoginMsg().Version
}
} else {
proxyInfo.Status = "offline"
metrics.Server.ProxyStatus(pxy.GetName(), pxy.GetConfigurer().GetBaseConfig().Type, false)
}
proxyInfo.Name = ps.Name
proxyInfo.TodayTrafficIn = ps.TodayTrafficIn

View File

@ -9,6 +9,7 @@ type ServerMetrics interface {
CloseClient()
NewProxy(name string, proxyType string)
CloseProxy(name string, proxyType string)
ProxyStatus(name string, proxyType string, online bool)
OpenConnection(name string, proxyType string)
CloseConnection(name string, proxyType string)
AddTrafficIn(name string, proxyType string, trafficBytes int64)
@ -31,6 +32,7 @@ func (noopServerMetrics) NewClient() {}
func (noopServerMetrics) CloseClient() {}
func (noopServerMetrics) NewProxy(string, string) {}
func (noopServerMetrics) CloseProxy(string, string) {}
func (noopServerMetrics) ProxyStatus(string, string, bool) {}
func (noopServerMetrics) OpenConnection(string, string) {}
func (noopServerMetrics) CloseConnection(string, string) {}
func (noopServerMetrics) AddTrafficIn(string, string, int64) {}