2023-12-01 10:47:19 -06:00
|
|
|
package ngalert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RuleUsageReader interface {
|
|
|
|
Count(ctx context.Context, orgID int64) (int64, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterQuotas(cfg *setting.Cfg, qs quota.Service, rules RuleUsageReader) error {
|
|
|
|
defaultLimits, err := readQuotaConfig(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return qs.RegisterQuotaReporter("a.NewUsageReporter{
|
|
|
|
TargetSrv: models.QuotaTargetSrv,
|
|
|
|
DefaultLimits: defaultLimits,
|
|
|
|
Reporter: UsageReporter(rules),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsageReporter(rules RuleUsageReader) quota.UsageReporterFunc {
|
|
|
|
return func(ctx context.Context, scopeParams *quota.ScopeParameters) (*quota.Map, error) {
|
|
|
|
u := "a.Map{}
|
|
|
|
|
|
|
|
var orgID int64 = 0
|
|
|
|
if scopeParams != nil {
|
|
|
|
orgID = scopeParams.OrgID
|
|
|
|
}
|
|
|
|
|
|
|
|
if orgUsage, err := rules.Count(ctx, orgID); err != nil {
|
|
|
|
return u, err
|
|
|
|
} else {
|
|
|
|
tag, err := quota.NewTag(models.QuotaTargetSrv, models.QuotaTarget, quota.OrgScope)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
u.Set(tag, orgUsage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalUsage, err := rules.Count(ctx, 0); err != nil {
|
|
|
|
return u, err
|
|
|
|
} else {
|
|
|
|
tag, err := quota.NewTag(models.QuotaTargetSrv, models.QuotaTarget, quota.GlobalScope)
|
|
|
|
if err != nil {
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
u.Set(tag, globalUsage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func readQuotaConfig(cfg *setting.Cfg) (*quota.Map, error) {
|
|
|
|
limits := "a.Map{}
|
|
|
|
|
|
|
|
if cfg == nil {
|
|
|
|
return limits, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
globalQuotaTag, err := quota.NewTag(models.QuotaTargetSrv, models.QuotaTarget, quota.GlobalScope)
|
|
|
|
if err != nil {
|
|
|
|
return limits, err
|
|
|
|
}
|
|
|
|
orgQuotaTag, err := quota.NewTag(models.QuotaTargetSrv, models.QuotaTarget, quota.OrgScope)
|
|
|
|
if err != nil {
|
|
|
|
return limits, err
|
|
|
|
}
|
|
|
|
|
2024-01-05 17:19:12 -06:00
|
|
|
limits.Set(globalQuotaTag, cfg.Quota.Global.AlertRule)
|
|
|
|
limits.Set(orgQuotaTag, cfg.Quota.Org.AlertRule)
|
2023-12-01 10:47:19 -06:00
|
|
|
return limits, nil
|
|
|
|
}
|