TUN-7477: Add UDP/TCP session metrics

New gauge metrics are exposed in the prometheus endpoint to
capture the current and total TCP and UDP sessions that
cloudflared has proxied.
This commit is contained in:
Devin Carr 2023-06-16 17:07:56 -07:00
parent 20e36c5bf3
commit a3bcf25fae
4 changed files with 73 additions and 2 deletions

View File

@ -118,6 +118,7 @@ func (m *manager) registerSession(ctx context.Context, registration *registerSes
session := m.newSession(registration.sessionID, registration.originProxy) session := m.newSession(registration.sessionID, registration.originProxy)
m.sessions[registration.sessionID] = session m.sessions[registration.sessionID] = session
registration.resultChan <- session registration.resultChan <- session
incrementUDPSessions()
} }
func (m *manager) newSession(id uuid.UUID, dstConn io.ReadWriteCloser) *Session { func (m *manager) newSession(id uuid.UUID, dstConn io.ReadWriteCloser) *Session {
@ -163,6 +164,7 @@ func (m *manager) unregisterSession(unregistration *unregisterSessionEvent) {
if ok { if ok {
delete(m.sessions, unregistration.sessionID) delete(m.sessions, unregistration.sessionID)
session.close(unregistration.err) session.close(unregistration.err)
decrementUDPActiveSessions()
} }
} }

View File

@ -0,0 +1,40 @@
package datagramsession
import (
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "cloudflared"
)
var (
activeUDPSessions = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "udp",
Name: "active_sessions",
Help: "Concurrent count of UDP sessions that are being proxied to any origin",
})
totalUDPSessions = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "udp",
Name: "total_sessions",
Help: "Total count of UDP sessions that have been proxied to any origin",
})
)
func init() {
prometheus.MustRegister(
activeUDPSessions,
totalUDPSessions,
)
}
func incrementUDPSessions() {
totalUDPSessions.Inc()
activeUDPSessions.Inc()
}
func decrementUDPActiveSessions() {
activeUDPSessions.Dec()
}

View File

@ -43,6 +43,22 @@ var (
Help: "Count of error proxying to origin", Help: "Count of error proxying to origin",
}, },
) )
activeTCPSessions = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: connection.MetricsNamespace,
Subsystem: "tcp",
Name: "active_sessions",
Help: "Concurrent count of TCP sessions that are being proxied to any origin",
},
)
totalTCPSessions = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: connection.MetricsNamespace,
Subsystem: "tcp",
Name: "total_sessions",
Help: "Total count of TCP sessions that have been proxied to any origin",
},
)
) )
func init() { func init() {
@ -51,6 +67,8 @@ func init() {
concurrentRequests, concurrentRequests,
responseByCode, responseByCode,
requestErrors, requestErrors,
activeTCPSessions,
totalTCPSessions,
) )
} }
@ -62,3 +80,14 @@ func incrementRequests() {
func decrementConcurrentRequests() { func decrementConcurrentRequests() {
concurrentRequests.Dec() concurrentRequests.Dec()
} }
func incrementTCPRequests() {
incrementRequests()
totalTCPSessions.Inc()
activeTCPSessions.Inc()
}
func decrementTCPConcurrentRequests() {
decrementConcurrentRequests()
activeTCPSessions.Dec()
}

View File

@ -158,8 +158,8 @@ func (p *Proxy) ProxyTCP(
rwa connection.ReadWriteAcker, rwa connection.ReadWriteAcker,
req *connection.TCPRequest, req *connection.TCPRequest,
) error { ) error {
incrementRequests() incrementTCPRequests()
defer decrementConcurrentRequests() defer decrementTCPConcurrentRequests()
if p.warpRouting == nil { if p.warpRouting == nil {
err := errors.New(`cloudflared received a request from WARP client, but your configuration has disabled ingress from WARP clients. To enable this, set "warp-routing:\n\t enabled: true" in your config.yaml`) err := errors.New(`cloudflared received a request from WARP client, but your configuration has disabled ingress from WARP clients. To enable this, set "warp-routing:\n\t enabled: true" in your config.yaml`)