Skip to content

Commit

Permalink
controller: improve absent metric PromRule naming
Browse files Browse the repository at this point in the history
  • Loading branch information
talal committed Aug 12, 2020
1 parent c9268bb commit 7a64dcc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ labels:
severity: info
annotations:
summary: missing foo_bar
description: The metric 'foo_bar' is missing
description: The metric 'foo_bar' is missing. Alerts using it may not fire as intended.
```

## Installation
Expand Down Expand Up @@ -121,8 +121,8 @@ For example, if a namespace has alert rules defined across several
`Infra`. The absent metric alerts for this namespace would be aggregated in two
new `PrometheusRule` resources called:

- `openstack-absent-metrics-alert-rules`
- `infra-absent-metrics-alert-rules`
- `openstack-absent-metric-alert-rules`
- `infra-absent-metric-alert-rules`

### Template

Expand All @@ -138,7 +138,7 @@ labels:
severity: info
annotations:
summary: missing $metric
description: The metric '$metric' is missing
description: The metric '$metric' is missing. Alerts using it may not fire as intended.
```

Consider the metric `limes_successful_scrapes:rate5m` with tier `os` and
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func (c *Controller) processNextWorkItem() bool {
}

// syncHandler gets a PrometheusRule from the queue and updates the
// corresponding absent metrics alert PrometheusRule for it.
// corresponding absent metric alert PrometheusRule for it.
func (c *Controller) syncHandler(key string) error {
// Convert the namespace/name string into a distinct namespace and name.
namespace, name, err := cache.SplitMetaNamespaceKey(key)
Expand All @@ -228,7 +228,7 @@ func (c *Controller) syncHandler(key string) error {
case apierrors.IsNotFound(err):
// The resource may no longer exist, in which case we clean up any
// orphaned absent alert rules.
c.logger.Info("msg", "PrometheusRule no longer exists in work queue", "key", key)
c.logger.Info("msg", "PrometheusRule no longer exists", "key", key)
return c.deleteAbsentAlertRulesNamespace(namespace, name)
default:
// Requeue object for later processing.
Expand All @@ -243,9 +243,9 @@ func (c *Controller) syncHandler(key string) error {
return nil
}

// Get the PrometheusRule resource that defines the absent metrics alert
// Get the PrometheusRule resource that defines the absent metric alert
// rules for this namespace.
absentPromRuleName := fmt.Sprintf("%s-absent-metrics-alert-rules", promServerName)
absentPromRuleName := fmt.Sprintf("%s-absent-metric-alert-rules", promServerName)
absentPromRule, err := c.promClientset.MonitoringV1().PrometheusRules(namespace).
Get(context.Background(), absentPromRuleName, metav1.GetOptions{})

Expand Down
26 changes: 14 additions & 12 deletions internal/controller/prometheusrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ func (c *Controller) createAbsentPrometheusRule(namespace, name, promServerName
},
}

_, err := c.promClientset.MonitoringV1().PrometheusRules(namespace).
Create(context.Background(), pr, metav1.CreateOptions{})
_, err := c.promClientset.MonitoringV1().PrometheusRules(namespace).Create(context.Background(), pr, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "could not create new absent PrometheusRule")
}
Expand Down Expand Up @@ -87,13 +86,12 @@ func (c *Controller) updateAbsentPrometheusRule(
}
pr.Spec.Groups = new

_, err := c.promClientset.MonitoringV1().PrometheusRules(namespace).
Update(context.Background(), pr, metav1.UpdateOptions{})
_, err := c.promClientset.MonitoringV1().PrometheusRules(namespace).Update(context.Background(), pr, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "could not update absent PrometheusRule")
}

c.logger.Info("msg", "successfully updated absent alert rules",
c.logger.Info("msg", "successfully updated absent metric alert rules",
"key", fmt.Sprintf("%s/%s", namespace, pr.Name))
return nil
}
Expand Down Expand Up @@ -136,17 +134,21 @@ func (c *Controller) deleteAbsentAlertRules(namespace, promRuleName string, abse

var err error
if len(pr.Spec.Groups) == 0 {
err = c.promClientset.MonitoringV1().PrometheusRules(namespace).
Delete(context.Background(), pr.Name, metav1.DeleteOptions{})
err = c.promClientset.MonitoringV1().PrometheusRules(namespace).Delete(context.Background(), pr.Name, metav1.DeleteOptions{})
if err == nil {
c.logger.Info("msg", "successfully deleted orphaned absent PrometheusRule",
"key", fmt.Sprintf("%s/%s", namespace, pr.Name))
}
} else {
_, err = c.promClientset.MonitoringV1().PrometheusRules(namespace).
Update(context.Background(), pr, metav1.UpdateOptions{})
_, err = c.promClientset.MonitoringV1().PrometheusRules(namespace).Update(context.Background(), pr, metav1.UpdateOptions{})
if err == nil {
c.logger.Info("msg", "successfully cleaned up orphaned absent metric alert rules",
"key", fmt.Sprintf("%s/%s", namespace, pr.Name))
}
}
if err != nil {
return errors.Wrap(err, "could not clean up orphaned absent alert rules")
return errors.Wrap(err, "could not clean up orphaned absent metric alert rules")
}

c.logger.Info("msg", "successfully cleaned up orphaned absent alert rules",
"key", fmt.Sprintf("%s/%s", namespace, pr.Name))
return nil
}
2 changes: 1 addition & 1 deletion test/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ var _ = Describe("Controller", func() {
})

It("should delete orphaned absent metric alert rule from "+fixtures.OSAbsentPromRuleName+" in resmgmt namespace", func() {
// Since "openstack-absent-metrics-alert-rules" in "resmgmt"
// Since "openstack-absent-metric-alert-rules" in "resmgmt"
// namespace holds the aggregate absent metric alert rules for
// the "openstack-limes-api.alerts" and
// "openstack-limes-roleassign.alerts", deleting one resource
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ package fixtures

// Common constants for reusability.
const (
K8sAbsentPromRuleName = "kubernetes-absent-metrics-alert-rules"
OSAbsentPromRuleName = "openstack-absent-metrics-alert-rules"
K8sAbsentPromRuleName = "kubernetes-absent-metric-alert-rules"
OSAbsentPromRuleName = "openstack-absent-metric-alert-rules"
)

0 comments on commit 7a64dcc

Please sign in to comment.