Documentation ¶
Index ¶
- Constants
- Variables
- func AlertRuleGen(mutators ...func(*AlertRule)) func() *AlertRule
- func PatchPartialAlertRule(existingRule *AlertRule, ruleToPatch *AlertRule)
- func ValidateAlertInstance(alertInstance *AlertInstance) error
- type AdminConfiguration
- type AlertConfiguration
- type AlertInstance
- type AlertQuery
- func (aq *AlertQuery) GetDatasource() (string, error)
- func (aq *AlertQuery) GetIntervalDuration() (time.Duration, error)
- func (aq *AlertQuery) GetMaxDatapoints() (int64, error)
- func (aq *AlertQuery) GetModel() ([]byte, error)
- func (aq *AlertQuery) GetQuery() (string, error)
- func (aq *AlertQuery) IsExpression() (bool, error)
- func (aq *AlertQuery) PreSave() error
- type AlertRule
- func (alertRule *AlertRule) Diff(rule *AlertRule, ignore ...string) cmputil.DiffReport
- func (alertRule *AlertRule) GetKey() AlertRuleKey
- func (alertRule *AlertRule) GetLabels(opts ...LabelOption) map[string]string
- func (alertRule *AlertRule) PreSave(timeNow func() time.Time) error
- func (alertRule *AlertRule) ResourceID() string
- func (alertRule *AlertRule) ResourceOrgID() int64
- func (alertRule *AlertRule) ResourceType() string
- type AlertRuleKey
- type AlertRuleVersion
- type AlertmanagersChoice
- type Condition
- type Duration
- type EvalAlertConditionCommand
- type ExecutionErrorState
- type GetAlertInstanceQuery
- type GetAlertRuleByUIDQuery
- type GetLatestAlertmanagerConfigurationQuery
- type InstanceLabels
- type InstanceStateType
- type LabelOption
- type ListAlertInstancesQuery
- type ListAlertInstancesQueryResult
- type ListAlertRulesQuery
- type ListNamespaceAlertRulesQuery
- type ListOrgRuleGroupsQuery
- type ListRuleGroupsQuery
- type NoDataState
- type Provenance
- type Provisionable
- type RelativeTimeRange
- type SaveAlertInstanceCommand
- type SaveAlertmanagerConfigurationCmd
Constants ¶
const ( RuleUIDLabel = "__alert_rule_uid__" NamespaceUIDLabel = "__alert_rule_namespace_uid__" // Annotations are actually a set of labels, so technically this is the label name of an annotation. DashboardUIDAnnotation = "__dashboardUid__" PanelIDAnnotation = "__panelId__" )
const AlertConfigurationVersion = 1
Variables ¶
var ( // ErrAlertRuleNotFound is an error for an unknown alert rule. ErrAlertRuleNotFound = fmt.Errorf("could not find alert rule") // ErrAlertRuleFailedGenerateUniqueUID is an error for failure to generate alert rule UID ErrAlertRuleFailedGenerateUniqueUID = errors.New("failed to generate alert rule UID") // ErrCannotEditNamespace is an error returned if the user does not have permissions to edit the namespace ErrCannotEditNamespace = errors.New("user does not have permissions to edit the namespace") ErrRuleGroupNamespaceNotFound = errors.New("rule group not found under this namespace") ErrAlertRuleFailedValidation = errors.New("invalid alert rule") ErrAlertRuleUniqueConstraintViolation = errors.New("a conflicting alert rule is found: rule title under the same organisation and folder should be unique") )
var ErrNoQuery = errors.New("no `expr` property in the query model")
var InternalLabelNameSet = map[string]struct{}{
RuleUIDLabel: {},
NamespaceUIDLabel: {},
}
InternalLabelNameSet are labels that grafana automatically include as part of the labelset.
Functions ¶
func AlertRuleGen ¶
AlertRuleGen provides a factory function that generates a random AlertRule. The mutators arguments allows changing fields of the resulting structure
func PatchPartialAlertRule ¶
PatchPartialAlertRule patches `ruleToPatch` by `existingRule` following the rule that if a field of `ruleToPatch` is empty or has the default value, it is populated by the value of the corresponding field from `existingRule`. There are several exceptions: 1. Following fields are not patched and therefore will be ignored: AlertRule.ID, AlertRule.OrgID, AlertRule.Updated, AlertRule.Version, AlertRule.UID, AlertRule.DashboardUID, AlertRule.PanelID, AlertRule.Annotations and AlertRule.Labels 2. There are fields that are patched together:
- AlertRule.Condition and AlertRule.Data
If either of the pair is specified, neither is patched.
func ValidateAlertInstance ¶
func ValidateAlertInstance(alertInstance *AlertInstance) error
ValidateAlertInstance validates that the alert instance contains an alert rule id, and state.
Types ¶
type AdminConfiguration ¶
type AdminConfiguration struct { ID int64 `xorm:"pk autoincr 'id'"` OrgID int64 `xorm:"org_id"` // List of Alertmanager(s) URL to push alerts to. Alertmanagers []string // SendAlertsTo indicates which set of alertmanagers will handle the alert. SendAlertsTo AlertmanagersChoice `xorm:"send_alerts_to"` CreatedAt int64 `xorm:"created"` UpdatedAt int64 `xorm:"updated"` }
AdminConfiguration represents the ngalert administration configuration settings.
func (*AdminConfiguration) AsSHA256 ¶
func (ac *AdminConfiguration) AsSHA256() string
func (*AdminConfiguration) Validate ¶
func (ac *AdminConfiguration) Validate() error
type AlertConfiguration ¶
type AlertConfiguration struct { ID int64 `xorm:"pk autoincr 'id'"` AlertmanagerConfiguration string ConfigurationHash string ConfigurationVersion string CreatedAt int64 `xorm:"created"` Default bool OrgID int64 `xorm:"org_id"` }
AlertConfiguration represents a single version of the Alerting Engine Configuration.
type AlertInstance ¶
type AlertInstance struct { RuleOrgID int64 `xorm:"rule_org_id"` RuleUID string `xorm:"rule_uid"` Labels InstanceLabels LabelsHash string CurrentState InstanceStateType CurrentStateSince time.Time CurrentStateEnd time.Time LastEvalTime time.Time }
AlertInstance represents a single alert instance.
type AlertQuery ¶
type AlertQuery struct { // RefID is the unique identifier of the query, set by the frontend call. RefID string `json:"refId"` // QueryType is an optional identifier for the type of query. // It can be used to distinguish different types of queries. QueryType string `json:"queryType"` // RelativeTimeRange is the relative Start and End of the query as sent by the frontend. RelativeTimeRange RelativeTimeRange `json:"relativeTimeRange"` // Grafana data source unique identifier; it should be '-100' for a Server Side Expression operation. DatasourceUID string `json:"datasourceUid"` // JSON is the raw JSON query and includes the above properties as well as custom properties. Model json.RawMessage `json:"model"` // contains filtered or unexported fields }
AlertQuery represents a single query associated with an alert definition.
func GenerateAlertQuery ¶
func GenerateAlertQuery() AlertQuery
func (*AlertQuery) GetDatasource ¶
func (aq *AlertQuery) GetDatasource() (string, error)
GetDatasource returns the query datasource identifier.
func (*AlertQuery) GetIntervalDuration ¶
func (aq *AlertQuery) GetIntervalDuration() (time.Duration, error)
func (*AlertQuery) GetMaxDatapoints ¶
func (aq *AlertQuery) GetMaxDatapoints() (int64, error)
func (*AlertQuery) GetModel ¶
func (aq *AlertQuery) GetModel() ([]byte, error)
func (*AlertQuery) GetQuery ¶
func (aq *AlertQuery) GetQuery() (string, error)
GetQuery returns the query defined by `expr` within the model. Returns an ErrNoQuery if it is unable to find the query. Returns an error if it is not able to cast the query to a string.
func (*AlertQuery) IsExpression ¶
func (aq *AlertQuery) IsExpression() (bool, error)
IsExpression returns true if the alert query is an expression.
func (*AlertQuery) PreSave ¶
func (aq *AlertQuery) PreSave() error
PreSave sets query's properties. It should be called before being saved.
type AlertRule ¶
type AlertRule struct { ID int64 `xorm:"pk autoincr 'id'"` OrgID int64 `xorm:"org_id"` Title string Condition string Data []AlertQuery Updated time.Time IntervalSeconds int64 Version int64 UID string `xorm:"uid"` NamespaceUID string `xorm:"namespace_uid"` DashboardUID *string `xorm:"dashboard_uid"` PanelID *int64 `xorm:"panel_id"` RuleGroup string NoDataState NoDataState ExecErrState ExecutionErrorState // ideally this field should have been apimodels.ApiDuration // but this is currently not possible because of circular dependencies For time.Duration Annotations map[string]string Labels map[string]string }
AlertRule is the model for alert rules in unified alerting.
func GenerateAlertRules ¶
GenerateAlertRules generates many random alert rules. Does not guarantee that rules are unique (by UID)
func GenerateUniqueAlertRules ¶
GenerateUniqueAlertRules generates many random alert rules and makes sure that they have unique UID. It returns a tuple where first element is a map where keys are UID of alert rule and the second element is a slice of the same rules
func (*AlertRule) Diff ¶
func (alertRule *AlertRule) Diff(rule *AlertRule, ignore ...string) cmputil.DiffReport
Diff calculates diff between two alert rules. Returns nil if two rules are equal. Otherwise, returns cmputil.DiffReport
func (*AlertRule) GetKey ¶
func (alertRule *AlertRule) GetKey() AlertRuleKey
GetKey returns the alert definitions identifier
func (*AlertRule) GetLabels ¶
func (alertRule *AlertRule) GetLabels(opts ...LabelOption) map[string]string
GetLabels returns the labels specified as part of the alert rule.
func (*AlertRule) PreSave ¶
PreSave sets default values and loads the updated model for each alert query.
func (*AlertRule) ResourceID ¶
func (*AlertRule) ResourceOrgID ¶
func (*AlertRule) ResourceType ¶
type AlertRuleKey ¶
AlertRuleKey is the alert definition identifier
func (AlertRuleKey) String ¶
func (k AlertRuleKey) String() string
type AlertRuleVersion ¶
type AlertRuleVersion struct { ID int64 `xorm:"pk autoincr 'id'"` RuleOrgID int64 `xorm:"rule_org_id"` RuleUID string `xorm:"rule_uid"` RuleNamespaceUID string `xorm:"rule_namespace_uid"` RuleGroup string ParentVersion int64 RestoredFrom int64 Version int64 Created time.Time Title string Condition string Data []AlertQuery IntervalSeconds int64 NoDataState NoDataState ExecErrState ExecutionErrorState // ideally this field should have been apimodels.ApiDuration // but this is currently not possible because of circular dependencies For time.Duration Annotations map[string]string Labels map[string]string }
AlertRuleVersion is the model for alert rule versions in unified alerting.
type AlertmanagersChoice ¶
type AlertmanagersChoice int
const ( AllAlertmanagers AlertmanagersChoice = iota InternalAlertmanager ExternalAlertmanagers )
func StringToAlertmanagersChoice ¶
func StringToAlertmanagersChoice(str string) (AlertmanagersChoice, error)
func (AlertmanagersChoice) String ¶
func (amc AlertmanagersChoice) String() string
String implements the Stringer interface
type Condition ¶
type Condition struct { // Condition is the RefID of the query or expression from // the Data property to get the results for. Condition string `json:"condition"` OrgID int64 `json:"-"` // Data is an array of data source queries and/or server side expressions. Data []AlertQuery `json:"data"` }
Condition contains backend expressions and queries and the RefID of the query or expression that will be evaluated.
type Duration ¶
Duration is a type used for marshalling durations.
func (Duration) MarshalJSON ¶
func (*Duration) UnmarshalJSON ¶
type EvalAlertConditionCommand ¶
type EvalAlertConditionCommand struct { Condition string `json:"condition"` Data []AlertQuery `json:"data"` Now time.Time `json:"now"` }
EvalAlertConditionCommand is the command for evaluating a condition
func (*EvalAlertConditionCommand) UnmarshalJSON ¶
func (cmd *EvalAlertConditionCommand) UnmarshalJSON(b []byte) error
type ExecutionErrorState ¶
type ExecutionErrorState string
const ( AlertingErrState ExecutionErrorState = "Alerting" ErrorErrState ExecutionErrorState = "Error" OkErrState ExecutionErrorState = "OK" )
func ErrStateFromString ¶
func ErrStateFromString(opt string) (ExecutionErrorState, error)
func (ExecutionErrorState) String ¶
func (executionErrorState ExecutionErrorState) String() string
type GetAlertInstanceQuery ¶
type GetAlertInstanceQuery struct { RuleOrgID int64 RuleUID string Labels InstanceLabels Result *AlertInstance }
GetAlertInstanceQuery is the query for retrieving/deleting an alert definition by ID. nolint:unused
type GetAlertRuleByUIDQuery ¶
GetAlertRuleByUIDQuery is the query for retrieving/deleting an alert rule by UID and organisation ID.
type GetLatestAlertmanagerConfigurationQuery ¶
type GetLatestAlertmanagerConfigurationQuery struct { OrgID int64 Result *AlertConfiguration }
GetLatestAlertmanagerConfigurationQuery is the query to get the latest alertmanager configuration.
type InstanceLabels ¶
InstanceLabels is an extension to data.Labels with methods for database serialization.
func (*InstanceLabels) FromDB ¶
func (il *InstanceLabels) FromDB(b []byte) error
FromDB loads labels stored in the database as json tuples into InstanceLabels. FromDB is part of the xorm Conversion interface.
func (*InstanceLabels) StringAndHash ¶
func (il *InstanceLabels) StringAndHash() (string, string, error)
StringAndHash returns a the json representation of the labels as tuples sorted by key. It also returns the a hash of that representation.
func (*InstanceLabels) StringKey ¶
func (il *InstanceLabels) StringKey() (string, error)
func (*InstanceLabels) ToDB ¶
func (il *InstanceLabels) ToDB() ([]byte, error)
ToDB is not implemented as serialization is handled with manual SQL queries). ToDB is part of the xorm Conversion interface.
type InstanceStateType ¶
type InstanceStateType string
InstanceStateType is an enum for instance states.
const ( // InstanceStateFiring is for a firing alert. InstanceStateFiring InstanceStateType = "Alerting" // InstanceStateNormal is for a normal alert. InstanceStateNormal InstanceStateType = "Normal" // InstanceStatePending is for an alert that is firing but has not met the duration InstanceStatePending InstanceStateType = "Pending" // InstanceStateNoData is for an alert with no data. InstanceStateNoData InstanceStateType = "NoData" // InstanceStateError is for a erroring alert. InstanceStateError InstanceStateType = "Error" )
func (InstanceStateType) IsValid ¶
func (i InstanceStateType) IsValid() bool
IsValid checks that the value of InstanceStateType is a valid string.
type LabelOption ¶
func WithoutInternalLabels ¶
func WithoutInternalLabels() LabelOption
type ListAlertInstancesQuery ¶
type ListAlertInstancesQuery struct { RuleOrgID int64 `json:"-"` RuleUID string State InstanceStateType Result []*ListAlertInstancesQueryResult }
ListAlertInstancesQuery is the query list alert Instances.
type ListAlertInstancesQueryResult ¶
type ListAlertInstancesQueryResult struct { RuleOrgID int64 `xorm:"rule_org_id" json:"ruleOrgId"` RuleUID string `xorm:"rule_uid" json:"ruleUid"` Labels InstanceLabels `json:"labels"` LabelsHash string `json:"labeHash"` CurrentState InstanceStateType `json:"currentState"` CurrentStateSince time.Time `json:"currentStateSince"` CurrentStateEnd time.Time `json:"currentStateEnd"` LastEvalTime time.Time `json:"lastEvalTime"` }
ListAlertInstancesQueryResult represents the result of listAlertInstancesQuery.
type ListAlertRulesQuery ¶
type ListAlertRulesQuery struct { OrgID int64 NamespaceUIDs []string ExcludeOrgs []int64 RuleGroup string // DashboardUID and PanelID are optional and allow filtering rules // to return just those for a dashboard and panel. DashboardUID string PanelID int64 Result []*AlertRule }
ListAlertRulesQuery is the query for listing alert rules
type ListNamespaceAlertRulesQuery ¶
type ListNamespaceAlertRulesQuery struct { OrgID int64 // Namespace is the folder slug NamespaceUID string Result []*AlertRule }
ListNamespaceAlertRulesQuery is the query for listing namespace alert rules
type ListOrgRuleGroupsQuery ¶
type ListOrgRuleGroupsQuery struct { OrgID int64 NamespaceUIDs []string // DashboardUID and PanelID are optional and allow filtering rules // to return just those for a dashboard and panel. DashboardUID string PanelID int64 Result [][]string }
ListOrgRuleGroupsQuery is the query for listing unique rule groups for an organization
type ListRuleGroupsQuery ¶
type ListRuleGroupsQuery struct {
Result []string
}
ListRuleGroupsQuery is the query for listing unique rule groups across all organizations
type NoDataState ¶
type NoDataState string
const ( Alerting NoDataState = "Alerting" NoData NoDataState = "NoData" OK NoDataState = "OK" )
func NoDataStateFromString ¶
func NoDataStateFromString(state string) (NoDataState, error)
func (NoDataState) String ¶
func (noDataState NoDataState) String() string
type Provenance ¶
type Provenance string
const ( // ProvenanceNone reflects the provenance when no provenance is stored // for the requested object in the database. ProvenanceNone Provenance = "" ProvenanceAPI Provenance = "api" ProvenanceFile Provenance = "file" )
type Provisionable ¶
Provisionable represents a resource that can be created through a provisioning mechanism, such as Terraform or config file.
type RelativeTimeRange ¶
RelativeTimeRange is the per query start and end time for requests.
func (*RelativeTimeRange) ToTimeRange ¶
func (rtr *RelativeTimeRange) ToTimeRange(now time.Time) backend.TimeRange
type SaveAlertInstanceCommand ¶
type SaveAlertInstanceCommand struct { RuleOrgID int64 RuleUID string Labels InstanceLabels State InstanceStateType LastEvalTime time.Time CurrentStateSince time.Time CurrentStateEnd time.Time }
SaveAlertInstanceCommand is the query for saving a new alert instance.