Skip to content

Commit

Permalink
Added IP Usage metrics at Rest server.
Browse files Browse the repository at this point in the history
  • Loading branch information
rsagasthya committed May 2, 2023
1 parent 12b29ac commit bf71542
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
6 changes: 2 additions & 4 deletions cns/restserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ func (service *HTTPRestService) reserveIPAddress(w http.ResponseWriter, r *http.

if resp.ReturnCode == 0 {
// If Response is success i.e. code 0, then publish metrics.
state := service.buildIPState()
publishIPStateMetrics(state)
publishIPStateMetrics(service.buildIPState())
}

reserveResp := &cns.ReserveIPAddressResponse{Response: resp, IPAddress: address}
Expand Down Expand Up @@ -483,8 +482,7 @@ func (service *HTTPRestService) releaseIPAddress(w http.ResponseWriter, r *http.

if resp.ReturnCode == 0 {
// If Response is success i.e. code 0, then publish metrics.
state := service.buildIPState()
publishIPStateMetrics(state)
publishIPStateMetrics(service.buildIPState())
}

err = service.Listener.Encode(w, &resp)
Expand Down
3 changes: 1 addition & 2 deletions cns/restserver/internalapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
if returnCode == 0 {
logNCSnapshot(*req)

state := service.buildIPState()
publishIPStateMetrics(state)
publishIPStateMetrics(service.buildIPState())
} else {
logger.Errorf(returnMessage)
}
Expand Down
5 changes: 3 additions & 2 deletions cns/restserver/ipusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ipState struct {
releasingIPs int64
}

func (service *HTTPRestService) buildIPState() ipState {
func (service *HTTPRestService) buildIPState() *ipState {
service.Lock()
defer service.Unlock()

Expand All @@ -28,6 +28,7 @@ func (service *HTTPRestService) buildIPState() ipState {
availableIPs: 0,
}

//nolint:gocritic // This has to iterate over the IP Config state to get the counts.
for _, ipConfig := range service.PodIPConfigState {
state.allocatedIPs++
if ipConfig.GetState() == types.Assigned {
Expand All @@ -45,5 +46,5 @@ func (service *HTTPRestService) buildIPState() ipState {
}

logger.Printf("[IP Usage] allocated IPs: %d, assigned IPs: %d, available IPs: %d", state.allocatedIPs, state.assignedIPs, state.availableIPs)
return state
return &state
}
24 changes: 12 additions & 12 deletions cns/restserver/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
subnetLabel = "subnet"
subnetCIDRLabel = "subnet_cidr"
podnetARMIDLabel = "podnet_arm_id"
cnsReturnCode = "Cns-Return-Code"
cnsReturnCode = "cns_return_code"
customerMetricLabel = "customer_metric"
customerMetricLabelValue = "customer metric"
)
Expand Down Expand Up @@ -64,43 +64,43 @@ var (
)
allocatedIPCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cx_allocated_ips",
Name: "cx_allocated_ips_v2",
Help: "Count of IPs CNS has Allocated",
ConstLabels: prometheus.Labels{customerMetricLabel: customerMetricLabelValue},
},
[]string{subnetLabel, subnetCIDRLabel, podnetARMIDLabel},
[]string{},
)
assignedIPCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cx_assigned_ips",
Name: "cx_assigned_ips_v2",
Help: "Count of IPs CNS has Assigned to Pods",
ConstLabels: prometheus.Labels{customerMetricLabel: customerMetricLabelValue},
},
[]string{subnetLabel, subnetCIDRLabel, podnetARMIDLabel},
[]string{},
)
availableIPCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cx_available_ips",
Name: "cx_available_ips_v2",
Help: "Count of IPs Available",
ConstLabels: prometheus.Labels{customerMetricLabel: customerMetricLabelValue},
},
[]string{subnetLabel, subnetCIDRLabel, podnetARMIDLabel},
[]string{},
)
pendingProgrammingIPCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cx_pending_programming_ips",
Name: "cx_pending_programming_ips_v2",
Help: "Count of IPs in Pending Programming State",
ConstLabels: prometheus.Labels{customerMetricLabel: customerMetricLabelValue},
},
[]string{subnetLabel, subnetCIDRLabel, podnetARMIDLabel},
[]string{},
)
pendingReleaseIPCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "cx_pending_release_ips",
Name: "cx_pending_release_ips_v2",
Help: "Count of IPs in Pending Release State",
ConstLabels: prometheus.Labels{customerMetricLabel: customerMetricLabelValue},
},
[]string{subnetLabel, subnetCIDRLabel, podnetARMIDLabel},
[]string{},
)
)

Expand Down Expand Up @@ -142,7 +142,7 @@ func stateTransitionMiddleware(i *cns.IPConfigurationStatus, s types.IPState) {
ipConfigStatusStateTransitionTime.WithLabelValues(string(i.GetState()), string(s)).Observe(time.Since(i.LastStateTransition).Seconds())
}

func publishIPStateMetrics(state ipState) {
func publishIPStateMetrics(state *ipState) {
labels := []string{} // TODO. ragasthya Add dimensions to the IP Usage metrics.
allocatedIPCount.WithLabelValues(labels...).Set(float64(state.allocatedIPs))
assignedIPCount.WithLabelValues(labels...).Set(float64(state.assignedIPs))
Expand Down

0 comments on commit bf71542

Please sign in to comment.