Skip to content

Commit

Permalink
Alerting: new min_interval_seconds options to enforce a minimum eval …
Browse files Browse the repository at this point in the history
…frequency (#21188)

* add min_interval_seconds setting to alerting config

It will let operator enforce a minimum time for the scheduler to enqueue evaluations

* Introduce UI modifications

* Update docs

Co-authored-by: Martin <[email protected]>
  • Loading branch information
papagian and uepoch authored Jan 14, 2020
1 parent 6b3041d commit d135f12
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 2 deletions.
2 changes: 2 additions & 0 deletions conf/defaults.ini
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,8 @@ notification_timeout_seconds = 30
# Default setting for max attempts to sending alert notifications. Default value is 3
max_attempts = 3

# Makes it possible to enforce a minimal interval between evaluations, to reduce load on the backend
min_interval_seconds = 1

#################################### Explore #############################
[explore]
Expand Down
3 changes: 3 additions & 0 deletions conf/sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,9 @@
# Default setting for max attempts to sending alert notifications. Default value is 3
;max_attempts = 3

# Makes it possible to enforce a minimal interval between evaluations, to reduce load on the backend
;min_interval_seconds = 1

#################################### Explore #############################
[explore]
# Enable the Explore section
Expand Down
1 change: 1 addition & 0 deletions docs/sources/alerting/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Currently only the graph panel supports alert rules.
### Name and Evaluation interval

Here you can specify the name of the alert rule and how often the scheduler should evaluate the alert rule.
**Note:** You can set a minimum interval in the `alerting.min_interval_seconds` config field, to set a minimum time between evaluations. Check out the [[configuration]]({{< relref "../installation/configuration.md" >}}#min-interval-seconds) page for more information.

### For

Expand Down
6 changes: 6 additions & 0 deletions docs/sources/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,12 @@ Default setting for alert notification timeout. Default value is `30`

Default setting for max attempts to sending alert notifications. Default value is `3`

### min_interval_seconds

Default setting for minimum interval between rule evaluations. Default value is `1`

> **Note.** This setting has precedence over each individual rule frequency. Therefore, if a rule frequency is lower than this value, this value will be enforced.
## [rendering]

Options to configure a remote HTTP image rendering service, e.g. using https://github.com/grafana/grafana-image-renderer.
Expand Down
1 change: 1 addition & 0 deletions packages/grafana-runtime/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class GrafanaBootConfig {
alertingEnabled = false;
alertingErrorOrTimeout = '';
alertingNoDataOrNullValues = '';
alertingMinInterval = 1;
authProxyEnabled = false;
exploreEnabled = false;
ldapEnabled = false;
Expand Down
1 change: 1 addition & 0 deletions pkg/api/frontendsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *m.ReqContext) (map[string]interf
"alertingEnabled": setting.AlertingEnabled,
"alertingErrorOrTimeout": setting.AlertingErrorOrTimeout,
"alertingNoDataOrNullValues": setting.AlertingNoDataOrNullValues,
"alertingMinInterval": setting.AlertingMinInterval,
"exploreEnabled": setting.ExploreEnabled,
"googleAnalyticsId": setting.GoogleAnalyticsId,
"disableLoginForm": setting.DisableLoginForm,
Expand Down
9 changes: 8 additions & 1 deletion pkg/services/alerting/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/setting"
)

type schedulerImpl struct {
Expand Down Expand Up @@ -61,7 +62,13 @@ func (s *schedulerImpl) Tick(tickTime time.Time, execQueue chan *Job) {
continue
}

if now%job.Rule.Frequency == 0 {
// Check the job frequency against the minimum interval required
interval := job.Rule.Frequency
if interval < setting.AlertingMinInterval {
interval = setting.AlertingMinInterval
}

if now%interval == 0 {
if job.Offset > 0 {
job.OffsetWait = true
} else {
Expand Down
2 changes: 2 additions & 0 deletions pkg/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ var (
AlertingEvaluationTimeout time.Duration
AlertingNotificationTimeout time.Duration
AlertingMaxAttempts int
AlertingMinInterval int64

// Explore UI
ExploreEnabled bool
Expand Down Expand Up @@ -961,6 +962,7 @@ func (cfg *Cfg) Load(args *CommandLineArgs) error {
notificationTimeoutSeconds := alerting.Key("notification_timeout_seconds").MustInt64(30)
AlertingNotificationTimeout = time.Second * time.Duration(notificationTimeoutSeconds)
AlertingMaxAttempts = alerting.Key("max_attempts").MustInt(3)
AlertingMinInterval = alerting.Key("min_interval_seconds").MustInt64(1)

explore := iniFile.Section("explore")
ExploreEnabled = explore.Key("enabled").MustBool(true)
Expand Down
25 changes: 25 additions & 0 deletions public/app/features/alerting/AlertTabCtrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DataQuery, DataSourceApi } from '@grafana/data';
import { PanelModel } from 'app/features/dashboard/state';
import { getDefaultCondition } from './getAlertingValidationMessage';
import { CoreEvents } from 'app/types';
import kbn from 'app/core/utils/kbn';

export class AlertTabCtrl {
panel: PanelModel;
Expand All @@ -31,6 +32,9 @@ export class AlertTabCtrl {
appSubUrl: string;
alertHistory: any;
newAlertRuleTag: any;
alertingMinIntervalSecs: number;
alertingMinInterval: string;
frequencyWarning: any;

/** @ngInject */
constructor(
Expand All @@ -51,6 +55,8 @@ export class AlertTabCtrl {
this.executionErrorModes = alertDef.executionErrorModes;
this.appSubUrl = config.appSubUrl;
this.panelCtrl._enableAlert = this.enable;
this.alertingMinIntervalSecs = config.alertingMinInterval;
this.alertingMinInterval = kbn.secondsToHms(config.alertingMinInterval);
}

$onInit() {
Expand Down Expand Up @@ -178,6 +184,8 @@ export class AlertTabCtrl {
return;
}

this.checkFrequency();

alert.conditions = alert.conditions || [];
if (alert.conditions.length === 0) {
alert.conditions.push(getDefaultCondition());
Expand Down Expand Up @@ -232,6 +240,23 @@ export class AlertTabCtrl {
this.panelCtrl.render();
}

checkFrequency() {
this.frequencyWarning = '';

try {
const frequencySecs = kbn.interval_to_seconds(this.alert.frequency);
if (frequencySecs < this.alertingMinIntervalSecs) {
this.frequencyWarning =
'A minimum evaluation interval of ' +
this.alertingMinInterval +
' have been configured in Grafana and will be used for this alert rule. ' +
'Please contact the administrator to configure a lower interval.';
}
} catch (err) {
this.frequencyWarning = err;
}
}

graphThresholdChanged(evt: any) {
for (const condition of this.alert.conditions) {
if (condition.type === 'query') {
Expand Down
8 changes: 7 additions & 1 deletion public/app/features/alerting/partials/alert_tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h4 class="section-heading">Rule</h4>
</div>
<div class="gf-form">
<span class="gf-form-label width-9">Evaluate every</span>
<input class="gf-form-input max-width-6" type="text" ng-model="ctrl.alert.frequency">
<input class="gf-form-input max-width-6" type="text" ng-model="ctrl.alert.frequency" ng-blur="ctrl.checkFrequency()">
</div>
<div class="gf-form max-width-11">
<label class="gf-form-label width-5">For</label>
Expand All @@ -31,6 +31,12 @@ <h4 class="section-heading">Rule</h4>
</info-popover>
</div>
</div>
<div class="gf-form" ng-if="ctrl.frequencyWarning">
<label class="gf-form-label text-warning">
<i
class="fa fa-warning"></i> {{ctrl.frequencyWarning}}
</label>
</div>
</div>

<div class="gf-form-group">
Expand Down

0 comments on commit d135f12

Please sign in to comment.