Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add namespace to unseal error metric #463

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (c *Controller) unseal(key string) (unsealErr error) {
obj, exists, err := c.informer.GetIndexer().GetByKey(key)
if err != nil {
log.Printf("Error fetching object with key %s from store: %v", key, err)
unsealErrorsTotal.WithLabelValues("fetch").Inc()
unsealErrorsTotal.WithLabelValues("fetch", "").Inc()
return err
}

Expand Down Expand Up @@ -242,7 +242,7 @@ func (c *Controller) unseal(key string) (unsealErr error) {
if err := c.updateSealedSecretStatus(ssecret, unsealErr); err != nil {
// Non-fatal. Log and continue.
log.Printf("Error updating SealedSecret %s status: %v", key, err)
unsealErrorsTotal.WithLabelValues("status").Inc()
unsealErrorsTotal.WithLabelValues("status", ssecret.GetNamespace()).Inc()
} else {
ObserveCondition(ssecret)
}
Expand All @@ -251,7 +251,7 @@ func (c *Controller) unseal(key string) (unsealErr error) {
newSecret, err := c.attemptUnseal(ssecret)
if err != nil {
c.recorder.Eventf(ssecret, corev1.EventTypeWarning, ErrUnsealFailed, "Failed to unseal: %v", err)
unsealErrorsTotal.WithLabelValues("unseal").Inc()
unsealErrorsTotal.WithLabelValues("unseal", ssecret.GetNamespace()).Inc()
return err
}

Expand All @@ -261,14 +261,14 @@ func (c *Controller) unseal(key string) (unsealErr error) {
}
if err != nil {
c.recorder.Event(ssecret, corev1.EventTypeWarning, ErrUpdateFailed, err.Error())
unsealErrorsTotal.WithLabelValues("update").Inc()
unsealErrorsTotal.WithLabelValues("update", ssecret.GetNamespace()).Inc()
return err
}

if !metav1.IsControlledBy(secret, ssecret) && !isAnnotatedToBeManaged(secret) {
msg := fmt.Sprintf("Resource %q already exists and is not managed by SealedSecret", secret.Name)
c.recorder.Event(ssecret, corev1.EventTypeWarning, ErrUpdateFailed, msg)
unsealErrorsTotal.WithLabelValues("unmanaged").Inc()
unsealErrorsTotal.WithLabelValues("unmanaged", ssecret.GetNamespace()).Inc()
return fmt.Errorf("failed update: %s", msg)
}

Expand All @@ -285,7 +285,7 @@ func (c *Controller) unseal(key string) (unsealErr error) {
secret, err = c.sclient.Secrets(ssecret.GetObjectMeta().GetNamespace()).Update(secret)
if err != nil {
c.recorder.Event(ssecret, corev1.EventTypeWarning, ErrUpdateFailed, err.Error())
unsealErrorsTotal.WithLabelValues("update").Inc()
unsealErrorsTotal.WithLabelValues("update", ssecret.GetNamespace()).Inc()
return err
}
}
Expand Down
7 changes: 1 addition & 6 deletions cmd/controller/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var (
Name: "unseal_errors_total",
Help: "Total number of sealed secret unseal errors by reason",
},
[]string{"reason"},
[]string{"reason", "namespace"},
)

conditionInfo = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Expand All @@ -64,11 +64,6 @@ func init() {
prometheus.MustRegister(unsealRequestsTotal)
prometheus.MustRegister(unsealErrorsTotal)
prometheus.MustRegister(conditionInfo)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you have to remove this?

Copy link
Contributor Author

@povilasv povilasv Oct 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

becaue we can't populate it any more, as the label combination is status abd namespace, and you can't init labels with just reason field, you need both.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why it had to be initialized in the first place.

@kskewes I see you added that in #375; is it really necessary these counters are set to 0?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The backstory is here: https://www.robustperception.io/existential-issues-with-metrics

How it relates:

Sealed Secrets Controller behaviour in an error case as I understand it.

  1. Startup
  2. Attempt unseal but there's error. Increase the associated counter metric (with label).
  3. Retry unseal attempt 5 times and then give up.

Prometheus behaviour:

  1. Startup
  2. Detect Sealed Secrets Controller pod (service discovery)
  3. Scrape /metrics endpoint on retry interval (10s or 60s or as configured)
    These scrapes can happen at any point after the controller has started up.

Situation:

  1. Prometheus client libraries initialise counters with no labels to 0 by default.
  2. Prometheus client libraries don't initialise counters with labels by default as it doesn't know what labels will be set.
    So we do this manually.
  3. Above enables easy alerting rules as you can check for an increase in the counter. An increase from null/non-existing metric to X value is not easily alert-able on. There's an absent() function in Prometheus but there are a lot of edge cases we have tried to handle in our other applications with varying degrees of success.

Options (in no specific order):

  1. Add the namespace label per this PR as is - this is likely to result in missed alerts as a error metric for a namespace is likely to go from NaN to 5 within the scrape period interval due to fast retry loop and Prometheus won't catch it. The second increase (or unseal error) will alert via the existing alert.
  2. Add the namespace label per this PR and have a go at rewriting the alert with namespace label and trying to catch absent() metrics with offset etc per the above link.
  3. Add the namespace label and have the controller register the metrics per error reason per namespace. Controller would need to watch Namespaces via Kubernetes API and initialise metric for new namespaces as added. I think cardinality wise this if fine.
  4. Do nothing (We'd like the label, but little bit to do per above).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other options:

  1. List+watch for namespaces via k8s api and initialize metrics based in existing namespaces.

  2. Advise users on how to craft alerts correctly in case of missing values (using or and other trickeries as Brian hinted at in the linked doc)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds reasonable; thanks

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. The retry interval must have changed since I last tested. :)
With 10s scrape interval provided we have retries over at least 2 it might be ok.

It would be good to update the alert unit test in tests.yaml and the alert itself in jsonnet.
Would you like to do in separate PR? Or I can once we pull next release and can validate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Karl, I don't have bandwidth to take care of this, it would be really great if I could delegate that to you

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Happy to pickup after current tasks. :)
Can validate retry versus scrape intervals in the unit test.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we initialize at least "fetch" (which doesn't have a namespace) so the counter exists. See #543

// Initialise known label values
for _, val := range []string{"fetch", "status", "unmanaged", "unseal", "update"} {
unsealErrorsTotal.WithLabelValues(val)
}
}

// ObserveCondition sets a `condition_info` Gauge according to a SealedSecret status.
Expand Down