Skip to content

Commit

Permalink
fix: take family metrics out of defer
Browse files Browse the repository at this point in the history
Deferring these will end up making the end times match for both families
as the variables aren't tracked separately. Since these are the same
metrics, it should be safe to emit them at time of generation.
  • Loading branch information
aauren committed Oct 7, 2023
1 parent 301e856 commit 68a7d03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 30 deletions.
36 changes: 16 additions & 20 deletions pkg/controllers/netpol/network_policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,17 +257,15 @@ func (npc *NetworkPolicyController) fullPolicySync() {
saveStart := time.Now()
err := iptablesSaveRestore.SaveInto("filter", npc.filterTableRules[ipFamily])
saveEndTime := time.Since(saveStart)
defer func() {
if npc.MetricsEnabled {
switch ipFamily {
case v1core.IPv4Protocol:
metrics.ControllerIptablesV4SaveTime.Observe(saveEndTime.Seconds())
case v1core.IPv6Protocol:
metrics.ControllerIptablesV6SaveTime.Observe(saveEndTime.Seconds())
}
if npc.MetricsEnabled {
switch ipFamily {
case v1core.IPv4Protocol:
metrics.ControllerIptablesV4SaveTime.Observe(saveEndTime.Seconds())
case v1core.IPv6Protocol:
metrics.ControllerIptablesV6SaveTime.Observe(saveEndTime.Seconds())
}
klog.V(2).Infof("Saving %v iptables rules took %v", ipFamily, saveEndTime)
}()
}
klog.V(2).Infof("Saving %v iptables rules took %v", ipFamily, saveEndTime)

if err != nil {
klog.Errorf("Aborting sync. Failed to run iptables-save: %v", err.Error())
Expand Down Expand Up @@ -298,17 +296,15 @@ func (npc *NetworkPolicyController) fullPolicySync() {
restoreStart := time.Now()
err := iptablesSaveRestore.Restore("filter", npc.filterTableRules[ipFamily].Bytes())
restoreEndTime := time.Since(restoreStart)
defer func() {
if npc.MetricsEnabled {
switch ipFamily {
case v1core.IPv4Protocol:
metrics.ControllerIptablesV4RestoreTime.Observe(restoreEndTime.Seconds())
case v1core.IPv6Protocol:
metrics.ControllerIptablesV6RestoreTime.Observe(restoreEndTime.Seconds())
}
if npc.MetricsEnabled {
switch ipFamily {
case v1core.IPv4Protocol:
metrics.ControllerIptablesV4RestoreTime.Observe(restoreEndTime.Seconds())
case v1core.IPv6Protocol:
metrics.ControllerIptablesV6RestoreTime.Observe(restoreEndTime.Seconds())
}
klog.V(2).Infof("Restoring %v iptables rules took %v", ipFamily, restoreEndTime)
}()
}
klog.V(2).Infof("Restoring %v iptables rules took %v", ipFamily, restoreEndTime)

if err != nil {
klog.Errorf("Aborting sync. Failed to run iptables-restore: %v\n%s",
Expand Down
18 changes: 8 additions & 10 deletions pkg/controllers/netpol/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,15 @@ func (npc *NetworkPolicyController) syncNetworkPolicyChains(networkPoliciesInfo
err := ipset.Restore()
restoreEndTime := time.Since(restoreStart)

defer func() {
if npc.MetricsEnabled {
switch ipFamily {
case api.IPv4Protocol:
metrics.ControllerPolicyIpsetV4RestoreTime.Observe(restoreEndTime.Seconds())
case api.IPv6Protocol:
metrics.ControllerPolicyIpsetV6RestoreTime.Observe(restoreEndTime.Seconds())
}
if npc.MetricsEnabled {
switch ipFamily {
case api.IPv4Protocol:
metrics.ControllerPolicyIpsetV4RestoreTime.Observe(restoreEndTime.Seconds())
case api.IPv6Protocol:
metrics.ControllerPolicyIpsetV6RestoreTime.Observe(restoreEndTime.Seconds())
}
klog.V(2).Infof("Restoring %v ipset took %v", ipFamily, restoreEndTime)
}()
}
klog.V(2).Infof("Restoring %v ipset took %v", ipFamily, restoreEndTime)

if err != nil {
return nil, nil, fmt.Errorf("failed to perform ipset restore: %w", err)
Expand Down

0 comments on commit 68a7d03

Please sign in to comment.