grafana/pkg/services/ngalert
Guilherme Caulada a367ad730c
Secrets: Implement basic unified secret store service (#45804)
* wip: Implement kvstore for secrets

* wip: Refactor kvstore for secrets

* wip: Add format key function to secrets kvstore sql

* wip: Add migration for secrets kvstore

* Remove unused Key field from secrets kvstore

* Remove secret values from debug logs

* Integrate unified secrets with datasources

* Fix minor issues and tests for kvstore

* Create test service helper for secret store

* Remove encryption tests from datasources

* Move secret operations after datasources

* Fix datasource proxy tests

* Fix legacy data tests

* Add Name to all delete data source commands

* Implement decryption cache on sql secret store

* Fix minor issue with cache and tests

* Use secret type on secret store datasource operations

* Add comments to make create and update clear

* Rename itemFound variable to isFound

* Improve secret deletion and cache management

* Add base64 encoding to sql secret store

* Move secret retrieval to decrypted values function

* Refactor decrypt secure json data functions

* Fix expr tests

* Fix datasource tests

* Fix plugin proxy tests

* Fix query tests

* Fix metrics api tests

* Remove unused fake secrets service from query tests

* Add rename function to secret store

* Add check for error renaming secret

* Remove bus from tests to fix merge conflicts

* Add background secrets migration to datasources

* Get datasource secure json fields from secrets

* Move migration to secret store

* Revert "Move migration to secret store"

This reverts commit 7c3f872072.

* Add secret service to datasource service on tests

* Fix datasource tests

* Remove merge conflict on wire

* Add ctx to data source http transport on prometheus stats collector

* Add ctx to data source http transport on stats collector test
2022-04-25 13:57:45 -03:00
..
api Secrets: Implement basic unified secret store service (#45804) 2022-04-25 13:57:45 -03:00
eval Alerting: Accurately set value for prom-compatible APIs (#47216) 2022-04-05 19:36:42 +01:00
metrics Alerting: Remove unused features from ticker + metric + tests (#47828) 2022-04-22 15:09:47 -04:00
models Remove redundant queries in GetAlertRules and GetOrgAlertRules and replace with ListAlertRules (#48108) 2022-04-25 11:42:42 +01:00
notifier Indicate whether routes are provisioned when GETting Alertmanager configuration (#47857) 2022-04-22 11:57:56 -05:00
provisioning Indicate whether routes are provisioned when GETting Alertmanager configuration (#47857) 2022-04-22 11:57:56 -05:00
schedule Alerting: Remove unused features from ticker + metric + tests (#47828) 2022-04-22 15:09:47 -04:00
sender Alerting: Remove bridge between log15 and go-kit logger (#43769) 2022-01-07 09:40:09 +01:00
state Remove redundant queries in GetAlertRules and GetOrgAlertRules and replace with ListAlertRules (#48108) 2022-04-25 11:42:42 +01:00
store Remove redundant queries in GetAlertRules and GetOrgAlertRules and replace with ListAlertRules (#48108) 2022-04-25 11:42:42 +01:00
tests Remove redundant queries in GetAlertRules and GetOrgAlertRules and replace with ListAlertRules (#48108) 2022-04-25 11:42:42 +01:00
accesscontrol.go Alerting: Fix nav-links for RBAC and other (#47798) 2022-04-19 11:47:28 -04:00
CHANGELOG.md Alerting: Wrap legacy alerting metrics with legacy_ (#48190) 2022-04-25 17:19:36 +02:00
ngalert.go Indicate whether routes are provisioned when GETting Alertmanager configuration (#47857) 2022-04-22 11:57:56 -05:00
README.md Alerting: README.md for ngalert 2021-11-18 10:26:42 +00:00

Next generation alerting (ngalert) in Grafana 8

Ngalert (Next generation alert) is the next generation of alerting in Grafana 8.

Overview

The ngalert package can be found in pkg/services/ngalert and has the following sub-packages:

- api
- eval
- logging
- metrics
- models
- notifier
- schedule
- sender
- state
- store
- tests

Scheduling and evaluation of alert rules

The scheduling of alert rules happens in the schedule package. This package is responsible for managing the evaluation of alert rules including checking for new alert rules and stopping the evaluation of deleted alert rules.

The scheduler runs at a fixed interval, called its heartbeat, in which it does a number of tasks:

  1. Fetch the alert rules for all organizations (excluding disabled)
  2. Start a goroutine (if this is a new alert rule or the scheduler has just started) to evaluate the alert rule
  3. Send an *evalContext event to the goroutine for each alert rule if its interval has elapsed
  4. Stop the goroutines for all alert rules that have been deleted since the last heartbeat

The function that evaluates each alert rule is called ruleRoutine. It waits for an *evalContext event (sent each interval seconds elapsed and is configurable per alert rule) and then evaluates the alert rule. To ensure that the scheduler is evaluating the latest version of the alert rule it compares its local version of the alert rule with that in the *evalContext event, fetching the latest version of the alert rule from the database if the version numbers mismatch. It then invokes the Evaluator which evaluates any queries, classic conditions or expressions in alert rule and passes the results of this evaluation to the State Manager. An evaluation can return no results in the case of NoData or Error, a single result in the case of classic conditions, or more than one result if the alert rule is multi-dimensional (i.e. one result per label set). In the case of multi-dimensional alert rules the results from an evaluation should never contain more than one per label set.

The State Manager is responsible for determining the current state of the alert rule (normal, pending, firing, etc) by comparing each evaluation result to the previous evaluations of the same label set in the state cache. Given a label set, it updates the state cache with the new current state, the evaluation time of the current evaluation and appends the current evaluation to the slice of previous evaluations. If the alert changes state (i.e. pending to firing) then it also creates an annotation to mark it on the dashboard and panel for this alert rule.

You might have noticed that so far we have avoided using the word "Alert" and instead talked about evaluation results and the current state of an alert rule. The reason for that is at this time in the evaluation of an alert rule the State Manager does not know about alerts, it just knows for each label set the state of an alert rule, the current evaluation and previous evaluations.

Notification of alerts

When an evaluation transitions the state of an alert rule for a given label set from pending to firing or from firing to normal the scheduler creates an alert instance and passes it to Alertmanager. In the case where a label set is transitioning from pending to firing the state of the alert instance is "Firing" and when transitioning from firing to normal the state of the alert instance is "Normal".

Which Alertmanager?

In ngalert it is possible to send alerts to the internal Alertmanager, an external Alertmanager, or both.

The internal Alertmanager is called MultiOrgAlertmanager and creates an Alertmanager for each organization in Grafana to preserve isolation between organizations in Grafana. The MultiOrgAlertmanager receives alerts from the scheduler and then forwards the alert to the correct Alertmanager for the organization.

When Grafana is configured to send alerts to an external Alertmanager it does so via the sender which creates an abstraction over notification of alerts and discovery of external Alertmanagers in Prometheus. The sender receives alerts via the SendAlerts function and then passes them to Prometheus.

How does Alertmanager turn alerts into notifications?

Alertmanager receives alerts via the PutAlerts function. Each alert is validated and its annotations and labels are normalized, then the alerts are put in an in-memory structure. The dispatcher iterates over the alerts and matches it to a route in the configuration as explained here.

The alert is then matched to an alert group depending on the configuration in the route. The alert is then sent through a number of stages including silencing and inhibition and at last the receiver which can include wait, de-duplication, retry.

What are notification channels?

Notification channels receive alerts and turn them into notifications and is often the last callback in the receiver after wait, de-duplication and retry.