Skip to content

Commit

Permalink
Merge pull request alibaba-archive#9 from rambohe-ch/add-metrics
Browse files Browse the repository at this point in the history
add metrics for anp
  • Loading branch information
zyjhtangtang authored May 18, 2021
2 parents a24da18 + 7f5c874 commit 48fbcdd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions pkg/agent/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ func (a *Client) Serve() {
start := time.Now()
conn, err := net.Dial(dialReq.Protocol, dialReq.Address)
if err != nil {
klog.ErrorS(err, "could not dial", "protocol", dialReq.Protocol, "address", dialReq.Address)
resp.GetDialResponse().Error = err.Error()
if err := a.Send(resp); err != nil {
klog.ErrorS(err, "could not send stream")
Expand Down
45 changes: 40 additions & 5 deletions pkg/server/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ var (

// ServerMetrics includes all the metrics of the proxy server.
type ServerMetrics struct {
latencies *prometheus.HistogramVec
latencies *prometheus.HistogramVec
registeredAgents *prometheus.GaugeVec
registeredAgentsCounter prometheus.Gauge
}

// newServerMetrics create a new ServerMetrics, configured with default metric names.
Expand All @@ -52,10 +54,31 @@ func newServerMetrics() *ServerMetrics {
Help: "Latency of dial to the remote endpoint in seconds",
Buckets: latencyBuckets,
},
[]string{},
[]string{"agent"},
)
registeredAgents := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "registered_agents",
Help: "specified agentID is registered or not",
},
[]string{"agentID"})
registeredAgentsCounter := prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: "total_registered_agents",
Help: "total of registered agents in server",
})
prometheus.MustRegister(latencies)
return &ServerMetrics{latencies: latencies}
prometheus.MustRegister(registeredAgents)
prometheus.MustRegister(registeredAgentsCounter)
return &ServerMetrics{
latencies: latencies,
registeredAgents: registeredAgents,
registeredAgentsCounter: registeredAgentsCounter,
}
}

// Reset resets the metrics.
Expand All @@ -64,6 +87,18 @@ func (a *ServerMetrics) Reset() {
}

// ObserveDialLatency records the latency of dial to the remote endpoint.
func (a *ServerMetrics) ObserveDialLatency(elapsed time.Duration) {
a.latencies.WithLabelValues().Observe(elapsed.Seconds())
func (a *ServerMetrics) ObserveDialLatency(agent string, elapsed time.Duration) {
a.latencies.WithLabelValues(agent).Observe(elapsed.Seconds())
}

// IncRegisteredAgent add one agent in registration
func (a *ServerMetrics) IncRegisteredAgent(agentID string) {
a.registeredAgents.WithLabelValues(agentID).Set(float64(1))
a.registeredAgentsCounter.Inc()
}

// DecRegisteredAgent dec one agent in registration
func (a *ServerMetrics) DecRegisteredAgent(agentID string) {
a.registeredAgents.WithLabelValues(agentID).Set(float64(0))
a.registeredAgentsCounter.Dec()
}
4 changes: 3 additions & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ func (s *ProxyServer) addBackend(agentID string, conn agent.AgentService_Connect
backend = s.BackendManagers[i].AddBackend(agentID, pkgagent.UID, conn)
}
}
metrics.Metrics.IncRegisteredAgent(agentID)
return
}

Expand Down Expand Up @@ -261,6 +262,7 @@ func (s *ProxyServer) removeBackend(agentID string, conn agent.AgentService_Conn
bm.RemoveBackend(agentID, pkgagent.UID, conn)
}
}
metrics.Metrics.DecRegisteredAgent(agentID)
}

func (s *ProxyServer) addFrontend(agentID string, connID int64, p *ProxyClientConnection) {
Expand Down Expand Up @@ -708,7 +710,7 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
frontend.agentID = agentID
s.addFrontend(agentID, resp.ConnectID, frontend)
close(frontend.connected)
metrics.Metrics.ObserveDialLatency(time.Since(frontend.start))
metrics.Metrics.ObserveDialLatency(agentID, time.Since(frontend.start))
}

case client.PacketType_DATA:
Expand Down

0 comments on commit 48fbcdd

Please sign in to comment.