store

package
v0.0.85-test Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 8, 2023 License: AGPL-3.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const AlertDefinitionMaxTitleLength = 190

AlertDefinitionMaxTitleLength is the maximum length of the alert definition title

View Source
const AlertRuleMaxRuleGroupNameLength = 190

AlertRuleMaxRuleGroupNameLength is the maximum length of the alert rule group name

View Source
const AlertRuleMaxTitleLength = 190

AlertRuleMaxTitleLength is the maximum length of the alert rule title

Variables

View Source
var (
	// ErrNoAlertmanagerConfiguration is an error for when no alertmanager configuration is found.
	ErrNoAlertmanagerConfiguration = fmt.Errorf("could not find an Alertmanager configuration")
	// ErrVersionLockedObjectNotFound is returned when an object is not
	// found using the current hash.
	ErrVersionLockedObjectNotFound = fmt.Errorf("could not find object using provided id and hash")
)
View Source
var (
	// ErrNoAdminConfiguration is an error for when no admin configuration is found.
	ErrNoAdminConfiguration = fmt.Errorf("no admin configuration available")
)
View Source
var GenerateNewAlertRuleUID = func(sess *sqlstore.DBSession, orgID int64, ruleTitle string) (string, error) {
	for i := 0; i < 3; i++ {
		uid := util.GenerateShortUID()

		exists, err := sess.Where("org_id=? AND uid=?", orgID, uid).Get(&ngmodels.AlertRule{})
		if err != nil {
			return "", err
		}

		if !exists {
			return uid, nil
		}
	}

	return "", ngmodels.ErrAlertRuleFailedGenerateUniqueUID
}

GenerateNewAlertRuleUID generates a unique UID for a rule. This is set as a variable so that the tests can override it. The ruleTitle is only used by the mocked functions.

View Source
var TimeNow = time.Now

TimeNow makes it possible to test usage of time

Functions

This section is empty.

Types

type AdminConfigurationStore

type AdminConfigurationStore interface {
	GetAdminConfiguration(orgID int64) (*ngmodels.AdminConfiguration, error)
	GetAdminConfigurations() ([]*ngmodels.AdminConfiguration, error)
	DeleteAdminConfiguration(orgID int64) error
	UpdateAdminConfiguration(UpdateAdminConfigurationCmd) error
}

type AlertingStore

type AlertingStore interface {
	GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error
	GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)
	SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
	SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error
	UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
}

AlertingStore is the database interface used by the Alertmanager service.

type DBstore

type DBstore struct {
	// the base scheduler tick rate; it's used for validating definition interval
	BaseInterval time.Duration
	// default alert definiiton interval
	DefaultInterval time.Duration
	SQLStore        *sqlstore.SQLStore
	Logger          log.Logger
	FolderService   dashboards.FolderService
	AccessControl   accesscontrol.AccessControl
}

DBstore stores the alert definitions and instances in the database.

func (DBstore) DeleteAdminConfiguration

func (st DBstore) DeleteAdminConfiguration(orgID int64) error

func (DBstore) DeleteAlertInstance

func (st DBstore) DeleteAlertInstance(ctx context.Context, orgID int64, ruleUID, labelsHash string) error

func (DBstore) DeleteAlertInstancesByRuleUID

func (st DBstore) DeleteAlertInstancesByRuleUID(ctx context.Context, orgID int64, ruleUID string) error

DeleteAlertInstanceByRuleUID is a handler for deleting alert instances by alert rule UID when a rule has been updated

func (DBstore) DeleteAlertRulesByUID

func (st DBstore) DeleteAlertRulesByUID(ctx context.Context, orgID int64, ruleUID ...string) error

DeleteAlertRulesByUID is a handler for deleting an alert rule.

func (DBstore) DeleteProvenance

func (st DBstore) DeleteProvenance(ctx context.Context, o models.Provisionable, org int64) error

DeleteProvenance deletes the provenance record from the table

func (DBstore) FetchOrgIds

func (st DBstore) FetchOrgIds(ctx context.Context) ([]int64, error)

func (*DBstore) GetAdminConfiguration

func (st *DBstore) GetAdminConfiguration(orgID int64) (*ngmodels.AdminConfiguration, error)

func (DBstore) GetAdminConfigurations

func (st DBstore) GetAdminConfigurations() ([]*ngmodels.AdminConfiguration, error)

func (DBstore) GetAlertInstance

func (st DBstore) GetAlertInstance(ctx context.Context, cmd *models.GetAlertInstanceQuery) error

GetAlertInstance is a handler for retrieving an alert instance based on OrgId, AlertDefintionID, and the hash of the labels.

func (DBstore) GetAlertRuleByUID

func (st DBstore) GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) error

GetAlertRuleByUID is a handler for retrieving an alert rule from that database by its UID and organisation ID. It returns ngmodels.ErrAlertRuleNotFound if no alert rule is found for the provided ID.

func (DBstore) GetAlertRulesForScheduling

func (st DBstore) GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error

GetAlertRulesForScheduling returns alert rule info (identifier, interval, version state) that is useful for it's scheduling.

func (*DBstore) GetAllLatestAlertmanagerConfiguration

func (st *DBstore) GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)

GetAllLatestAlertmanagerConfiguration returns the latest configuration of every organization

func (*DBstore) GetLatestAlertmanagerConfiguration

func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error

GetLatestAlertmanagerConfiguration returns the lastest version of the alertmanager configuration. It returns ErrNoAlertmanagerConfiguration if no configuration is found.

func (DBstore) GetNamespaceByTitle

func (st DBstore) GetNamespaceByTitle(ctx context.Context, namespace string, orgID int64, user *models.SignedInUser, withCanSave bool) (*models.Folder, error)

GetNamespaceByTitle is a handler for retrieving a namespace by its title. Alerting rules follow a Grafana folder-like structure which we call namespaces.

func (DBstore) GetOrgs

func (st DBstore) GetOrgs(ctx context.Context) ([]int64, error)

func (DBstore) GetProvenance

func (st DBstore) GetProvenance(ctx context.Context, o models.Provisionable, org int64) (models.Provenance, error)

GetProvenance gets the provenance status for a provisionable object.

func (DBstore) GetProvenances

func (st DBstore) GetProvenances(ctx context.Context, org int64, resourceType string) (map[string]models.Provenance, error)

GetProvenance gets the provenance status for a provisionable object.

func (DBstore) GetRuleGroups

func (st DBstore) GetRuleGroups(ctx context.Context, query *ngmodels.ListRuleGroupsQuery) error

func (DBstore) GetUserVisibleNamespaces

func (st DBstore) GetUserVisibleNamespaces(ctx context.Context, orgID int64, user *models.SignedInUser) (map[string]*models.Folder, error)

GetNamespaces returns the folders that are visible to the user and have at least one alert in it

func (*DBstore) InTransaction

func (st *DBstore) InTransaction(ctx context.Context, f func(ctx context.Context) error) error

func (DBstore) InsertAlertRules

func (st DBstore) InsertAlertRules(ctx context.Context, rules []ngmodels.AlertRule) error

InsertAlertRules is a handler for creating/updating alert rules.

func (DBstore) ListAlertInstances

func (st DBstore) ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) error

ListAlertInstances is a handler for retrieving alert instances within specific organisation based on various filters.

func (DBstore) ListAlertRules

func (st DBstore) ListAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error

GetOrgAlertRules is a handler for retrieving alert rules of specific organisation.

func (DBstore) SaveAlertInstance

func (st DBstore) SaveAlertInstance(ctx context.Context, cmd *models.SaveAlertInstanceCommand) error

SaveAlertInstance is a handler for saving a new alert instance.

func (DBstore) SaveAlertmanagerConfiguration

func (st DBstore) SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error

SaveAlertmanagerConfiguration creates an alertmanager configuration.

func (DBstore) SaveAlertmanagerConfigurationWithCallback

func (st DBstore) SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error

SaveAlertmanagerConfigurationWithCallback creates an alertmanager configuration version and then executes a callback. If the callback results in error it rolls back the transaction.

func (DBstore) SetProvenance

func (st DBstore) SetProvenance(ctx context.Context, o models.Provisionable, org int64, p models.Provenance) error

SetProvenance changes the provenance status for a provisionable object.

func (DBstore) UpdateAdminConfiguration

func (st DBstore) UpdateAdminConfiguration(cmd UpdateAdminConfigurationCmd) error

func (DBstore) UpdateAlertRules

func (st DBstore) UpdateAlertRules(ctx context.Context, rules []UpdateRule) error

UpdateAlertRules is a handler for creating/updating alert rules.

func (*DBstore) UpdateAlertmanagerConfiguration

func (st *DBstore) UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error

type FakeAdminConfigStore

type FakeAdminConfigStore struct {
	Configs map[int64]*models.AdminConfiguration
	// contains filtered or unexported fields
}

func NewFakeAdminConfigStore

func NewFakeAdminConfigStore(t *testing.T) *FakeAdminConfigStore

func (*FakeAdminConfigStore) DeleteAdminConfiguration

func (f *FakeAdminConfigStore) DeleteAdminConfiguration(orgID int64) error

func (*FakeAdminConfigStore) GetAdminConfiguration

func (f *FakeAdminConfigStore) GetAdminConfiguration(orgID int64) (*models.AdminConfiguration, error)

func (*FakeAdminConfigStore) GetAdminConfigurations

func (f *FakeAdminConfigStore) GetAdminConfigurations() ([]*models.AdminConfiguration, error)

func (*FakeAdminConfigStore) UpdateAdminConfiguration

func (f *FakeAdminConfigStore) UpdateAdminConfiguration(cmd UpdateAdminConfigurationCmd) error

type FakeAnnotationsRepo

type FakeAnnotationsRepo struct {
	Items []*annotations.Item
	// contains filtered or unexported fields
}

func NewFakeAnnotationsRepo

func NewFakeAnnotationsRepo() *FakeAnnotationsRepo

func (*FakeAnnotationsRepo) Delete

func (*FakeAnnotationsRepo) Find

func (*FakeAnnotationsRepo) FindTags

func (*FakeAnnotationsRepo) Len

func (repo *FakeAnnotationsRepo) Len() int

func (*FakeAnnotationsRepo) Save

func (repo *FakeAnnotationsRepo) Save(item *annotations.Item) error

func (*FakeAnnotationsRepo) Update

func (repo *FakeAnnotationsRepo) Update(_ context.Context, item *annotations.Item) error

type FakeExternalAlertmanager

type FakeExternalAlertmanager struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

func NewFakeExternalAlertmanager

func NewFakeExternalAlertmanager(t *testing.T) *FakeExternalAlertmanager

func (*FakeExternalAlertmanager) AlertNamesCompare

func (am *FakeExternalAlertmanager) AlertNamesCompare(expected []string) bool

func (*FakeExternalAlertmanager) Alerts

func (*FakeExternalAlertmanager) AlertsCount

func (am *FakeExternalAlertmanager) AlertsCount() int

func (*FakeExternalAlertmanager) Close

func (am *FakeExternalAlertmanager) Close()

func (*FakeExternalAlertmanager) Handler

func (am *FakeExternalAlertmanager) Handler() func(w http.ResponseWriter, r *http.Request)

func (*FakeExternalAlertmanager) URL

func (am *FakeExternalAlertmanager) URL() string

type FakeInstanceStore

type FakeInstanceStore struct {
	RecordedOps []interface{}
	// contains filtered or unexported fields
}

func (*FakeInstanceStore) DeleteAlertInstance

func (f *FakeInstanceStore) DeleteAlertInstance(_ context.Context, _ int64, _, _ string) error

func (*FakeInstanceStore) FetchOrgIds

func (f *FakeInstanceStore) FetchOrgIds(_ context.Context) ([]int64, error)

func (*FakeInstanceStore) GetAlertInstance

func (*FakeInstanceStore) ListAlertInstances

func (*FakeInstanceStore) SaveAlertInstance

type FakeRuleStore

type FakeRuleStore struct {

	// OrgID -> RuleGroup -> Namespace -> Rules
	Rules       map[int64][]*models.AlertRule
	Hook        func(cmd interface{}) error // use Hook if you need to intercept some query and return an error
	RecordedOps []interface{}
	Folders     map[int64][]*models2.Folder
	// contains filtered or unexported fields
}

FakeRuleStore mocks the RuleStore of the scheduler.

func NewFakeRuleStore

func NewFakeRuleStore(t *testing.T) *FakeRuleStore

func (*FakeRuleStore) DeleteAlertInstancesByRuleUID

func (f *FakeRuleStore) DeleteAlertInstancesByRuleUID(_ context.Context, _ int64, _ string) error

func (*FakeRuleStore) DeleteAlertRulesByUID

func (f *FakeRuleStore) DeleteAlertRulesByUID(_ context.Context, orgID int64, UIDs ...string) error

func (*FakeRuleStore) GetAlertRuleByUID

func (f *FakeRuleStore) GetAlertRuleByUID(_ context.Context, q *models.GetAlertRuleByUIDQuery) error

func (*FakeRuleStore) GetAlertRulesForScheduling

func (f *FakeRuleStore) GetAlertRulesForScheduling(_ context.Context, q *models.ListAlertRulesQuery) error

For now, we're not implementing namespace filtering.

func (*FakeRuleStore) GetNamespaceByTitle

func (f *FakeRuleStore) GetNamespaceByTitle(_ context.Context, title string, orgID int64, _ *models2.SignedInUser, _ bool) (*models2.Folder, error)

func (*FakeRuleStore) GetRecordedCommands

func (f *FakeRuleStore) GetRecordedCommands(predicate func(cmd interface{}) (interface{}, bool)) []interface{}

GetRecordedCommands filters recorded commands using predicate function. Returns the subset of the recorded commands that meet the predicate

func (*FakeRuleStore) GetRuleGroups

func (f *FakeRuleStore) GetRuleGroups(_ context.Context, q *models.ListRuleGroupsQuery) error

func (*FakeRuleStore) GetUserVisibleNamespaces

func (f *FakeRuleStore) GetUserVisibleNamespaces(_ context.Context, orgID int64, _ *models2.SignedInUser) (map[string]*models2.Folder, error)

func (*FakeRuleStore) InTransaction

func (f *FakeRuleStore) InTransaction(ctx context.Context, fn func(c context.Context) error) error

func (*FakeRuleStore) InsertAlertRules

func (f *FakeRuleStore) InsertAlertRules(_ context.Context, q []models.AlertRule) error

func (*FakeRuleStore) ListAlertRules

func (f *FakeRuleStore) ListAlertRules(_ context.Context, q *models.ListAlertRulesQuery) error

func (*FakeRuleStore) PutRule

func (f *FakeRuleStore) PutRule(_ context.Context, rules ...*models.AlertRule)

PutRule puts the rule in the Rules map. If there are existing rule in the same namespace, they will be overwritten

func (*FakeRuleStore) UpdateAlertRules

func (f *FakeRuleStore) UpdateAlertRules(_ context.Context, q []UpdateRule) error

type GenericRecordedQuery

type GenericRecordedQuery struct {
	Name   string
	Params []interface{}
}

type InstanceStore

type InstanceStore interface {
	GetAlertInstance(ctx context.Context, cmd *models.GetAlertInstanceQuery) error
	ListAlertInstances(ctx context.Context, cmd *models.ListAlertInstancesQuery) error
	SaveAlertInstance(ctx context.Context, cmd *models.SaveAlertInstanceCommand) error
	FetchOrgIds(ctx context.Context) ([]int64, error)
	DeleteAlertInstance(ctx context.Context, orgID int64, ruleUID, labelsHash string) error
}

type OrgStore

type OrgStore interface {
	GetOrgs(ctx context.Context) ([]int64, error)
}

type RuleStore

type RuleStore interface {
	DeleteAlertRulesByUID(ctx context.Context, orgID int64, ruleUID ...string) error
	DeleteAlertInstancesByRuleUID(ctx context.Context, orgID int64, ruleUID string) error
	GetAlertRuleByUID(ctx context.Context, query *ngmodels.GetAlertRuleByUIDQuery) error
	GetAlertRulesForScheduling(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error
	ListAlertRules(ctx context.Context, query *ngmodels.ListAlertRulesQuery) error
	// GetRuleGroups returns the unique rule groups across all organizations.
	GetRuleGroups(ctx context.Context, query *ngmodels.ListRuleGroupsQuery) error
	GetUserVisibleNamespaces(context.Context, int64, *models.SignedInUser) (map[string]*models.Folder, error)
	GetNamespaceByTitle(context.Context, string, int64, *models.SignedInUser, bool) (*models.Folder, error)
	InsertAlertRules(ctx context.Context, rule []ngmodels.AlertRule) error
	UpdateAlertRules(ctx context.Context, rule []UpdateRule) error
}

RuleStore is the interface for persisting alert rules and instances

type SaveCallback

type SaveCallback func() error

type UpdateAdminConfigurationCmd

type UpdateAdminConfigurationCmd struct {
	AdminConfiguration *ngmodels.AdminConfiguration
}

type UpdateRule

type UpdateRule struct {
	Existing *ngmodels.AlertRule
	New      ngmodels.AlertRule
}

type UpdateRuleGroupCmd

type UpdateRuleGroupCmd struct {
	OrgID           int64
	NamespaceUID    string
	RuleGroupConfig apimodels.PostableRuleGroupConfig
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL