Skip to content

Commit

Permalink
Fixed possible leak (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam committed Mar 20, 2023
1 parent 0b437c2 commit 0d99636
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/controllers/metrics/pod/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
func (c *Controller) cleanup(podKey types.NamespacedName) {
if labels, ok := c.labelsMap.Load(podKey); ok {
podGaugeVec.Delete(labels.(prometheus.Labels))
c.labelsMap.Delete(podKey)
}
}

Expand Down
11 changes: 7 additions & 4 deletions pkg/controllers/metrics/provisioner/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (c *Controller) cleanup(provisionerKey types.NamespacedName) {
usageGaugeVec.Delete(labels)
usagePctGaugeVec.Delete(labels)
}
c.labelCollection.Delete(provisionerKey)
}
}

Expand Down Expand Up @@ -162,15 +163,17 @@ func (c *Controller) record(ctx context.Context, provisioner *v1alpha5.Provision
func (c *Controller) set(provisioner *v1alpha5.Provisioner, resourceList v1.ResourceList, gaugeVec *prometheus.GaugeVec) error {
provisionerKey := client.ObjectKeyFromObject(provisioner)

var labels []prometheus.Labels
for resourceName, quantity := range resourceList {
resourceTypeName := strings.ReplaceAll(strings.ToLower(string(resourceName)), "-", "_")

labels := c.makeLabels(provisioner, resourceTypeName)
existingLabels, _ := c.labelCollection.LoadOrStore(provisionerKey, []prometheus.Labels{})
c.labelCollection.Store(provisionerKey, append(existingLabels.([]prometheus.Labels), labels))
label := c.makeLabels(provisioner, resourceTypeName)
labels = append(labels, label)
// Store the label we are about to add to the label collection
c.labelCollection.Store(provisionerKey, labels)

// gets existing gauge or gets a new one if it doesn't exist
gauge, err := gaugeVec.GetMetricWith(labels)
gauge, err := gaugeVec.GetMetricWith(label)
if err != nil {
return fmt.Errorf("creating or getting gauge: %w", err)
}
Expand Down

0 comments on commit 0d99636

Please sign in to comment.