Skip to content

Commit

Permalink
fix the proxy server backend metric error
Browse files Browse the repository at this point in the history
  • Loading branch information
YRXING committed Nov 24, 2021
1 parent 5944ae0 commit 27e897b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 0 additions & 3 deletions pkg/server/backend_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"k8s.io/klog/v2"
client "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/proto/client"
pkgagent "sigs.k8s.io/apiserver-network-proxy/pkg/agent"
"sigs.k8s.io/apiserver-network-proxy/pkg/server/metrics"
"sigs.k8s.io/apiserver-network-proxy/proto/agent"
)

Expand Down Expand Up @@ -204,7 +203,6 @@ func (s *DefaultBackendStorage) AddBackend(identifier string, idType pkgagent.Id
return addedBackend
}
s.backends[identifier] = []*backend{addedBackend}
metrics.Metrics.SetBackendCount(len(s.backends))
s.agentIDs = append(s.agentIDs, identifier)
if idType == pkgagent.DefaultRoute {
s.defaultRouteAgentIDs = append(s.defaultRouteAgentIDs, identifier)
Expand Down Expand Up @@ -257,7 +255,6 @@ func (s *DefaultBackendStorage) RemoveBackend(identifier string, idType pkgagent
if !found {
klog.V(1).InfoS("Could not find connection matching identifier to remove", "connection", conn, "identifier", identifier)
}
metrics.Metrics.SetBackendCount(len(s.backends))
}

// NumBackends resturns the number of available backends
Expand Down
15 changes: 14 additions & 1 deletion pkg/server/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ func newServerMetrics() *ServerMetrics {
Name: "ready_backend_connections",
Help: "Number of konnectivity agent connected to the proxy server",
},
[]string{},
[]string{
"manager",
"idType",
},
)
pendingDials := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Expand Down Expand Up @@ -164,6 +167,16 @@ func (a *ServerMetrics) SetBackendCount(count int) {
a.backend.WithLabelValues().Set(float64(count))
}

// BackendCountInc increments a new backend connection.
func (a *ServerMetrics) BackendCountInc(manager string, idType string) {
a.backend.WithLabelValues(manager, idType).Inc()
}

// BackendCountInc decrements a finished backend connection.
func (a *ServerMetrics) BackendCountDec(manager string, idType string) {
a.backend.WithLabelValues(manager, idType).Desc()
}

// SetPendingDialCount sets the number of pending dials.
func (a *ServerMetrics) SetPendingDialCount(count int) {
a.pendingDials.WithLabelValues().Set(float64(count))
Expand Down
10 changes: 10 additions & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,17 @@ func (s *ProxyServer) addBackend(agentID string, conn agent.AgentService_Connect
for _, ipv4 := range agentIdentifiers.IPv4 {
klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", ipv4)
s.BackendManagers[i].AddBackend(ipv4, pkgagent.IPv4, conn)
metrics.Metrics.BackendCountInc("DestHostBackendManager", "ipv4")
}
for _, ipv6 := range agentIdentifiers.IPv6 {
klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", ipv6)
s.BackendManagers[i].AddBackend(ipv6, pkgagent.IPv6, conn)
metrics.Metrics.BackendCountInc("DestHostBackendManager", "ipv6")
}
for _, host := range agentIdentifiers.Host {
klog.V(5).InfoS("Add the agent to DestHostBackendManager", "agent address", host)
s.BackendManagers[i].AddBackend(host, pkgagent.Host, conn)
metrics.Metrics.BackendCountInc("DestHostBackendManager", "host")
}
case *DefaultRouteBackendManager:
agentIdentifiers, err := getAgentIdentifiers(conn)
Expand All @@ -218,10 +221,12 @@ func (s *ProxyServer) addBackend(agentID string, conn agent.AgentService_Connect
if agentIdentifiers.DefaultRoute {
klog.V(5).InfoS("Add the agent to DefaultRouteBackendManager", "agentID", agentID)
backend = s.BackendManagers[i].AddBackend(agentID, pkgagent.DefaultRoute, conn)
metrics.Metrics.BackendCountInc("DefaultRouteBackendManager", "default-route")
}
default:
klog.V(5).InfoS("Add the agent to DefaultBackendManager", "agentID", agentID)
backend = s.BackendManagers[i].AddBackend(agentID, pkgagent.UID, conn)
metrics.Metrics.BackendCountInc("DefaultBackendManager", "uid")
}
}
return
Expand All @@ -239,14 +244,17 @@ func (s *ProxyServer) removeBackend(agentID string, conn agent.AgentService_Conn
for _, ipv4 := range agentIdentifiers.IPv4 {
klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", ipv4)
bm.RemoveBackend(ipv4, pkgagent.IPv4, conn)
metrics.Metrics.BackendCountDec("DestHostBackendManager", "ipv4")
}
for _, ipv6 := range agentIdentifiers.IPv6 {
klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", ipv6)
bm.RemoveBackend(ipv6, pkgagent.IPv6, conn)
metrics.Metrics.BackendCountDec("DestHostBackendManager", "ipv6")
}
for _, host := range agentIdentifiers.Host {
klog.V(5).InfoS("Remove the agent from the DestHostBackendManager", "agentHost", host)
bm.RemoveBackend(host, pkgagent.Host, conn)
metrics.Metrics.BackendCountDec("DestHostBackendManager", "host")
}
case *DefaultRouteBackendManager:
agentIdentifiers, err := getAgentIdentifiers(conn)
Expand All @@ -257,10 +265,12 @@ func (s *ProxyServer) removeBackend(agentID string, conn agent.AgentService_Conn
if agentIdentifiers.DefaultRoute {
klog.V(5).InfoS("Remove the agent from the DefaultRouteBackendManager", "agentID", agentID)
bm.RemoveBackend(agentID, pkgagent.DefaultRoute, conn)
metrics.Metrics.BackendCountDec("DefaultRouteBackendManager", "default-route")
}
default:
klog.V(5).InfoS("Remove the agent from the DefaultBackendManager", "agentID", agentID)
bm.RemoveBackend(agentID, pkgagent.UID, conn)
metrics.Metrics.BackendCountDec("DefaultBackendManager", "uid")
}
}
}
Expand Down

0 comments on commit 27e897b

Please sign in to comment.