rules

package
v0.0.0-...-18e089f Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: Apache-2.0 Imports: 30 Imported by: 1

Documentation

Overview

Copyright 2022 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2022 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2022 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2022 The KubeSphere Authors.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const (
	ErrGenRuleId = "error generating rule id"

	LabelKeyInternalRuleGroup    = "__rule_group__"
	LabelKeyInternalRuleName     = "__rule_name__"
	LabelKeyInternalRuleQuery    = "__rule_query__"
	LabelKeyInternalRuleDuration = "__rule_duration__"

	LabelKeyThanosRulerReplica = "thanos_ruler_replica"
	LabelKeyPrometheusReplica  = "prometheus_replica"

	LabelKeyRuleId = "rule_id"

	LabelKeyAlertType   = "alerttype"
	LabelValueAlertType = "metric"
)
View Source
const (
	CustomRuleResourceLabelKeyLevel = "custom-alerting-rule-level"
)

Variables

This section is empty.

Functions

func FormatDuration

func FormatDuration(for_ string) (string, error)

func FormatExpr

func FormatExpr(expr string) (string, error)

func GenEndpointRuleId

func GenEndpointRuleId(group string, epRule *alerting.AlertingRule,
	externalLabels func() map[string]string) (string, error)

func GenResourceRuleIdIgnoreFormat

func GenResourceRuleIdIgnoreFormat(group string, rule *promresourcesv1.Rule) string

func GetAlertingRuleStatus

func GetAlertingRuleStatus(ruleNamespace string, rule *ResourceRule, epRuleGroups []*alerting.RuleGroup,
	extLabels func() map[string]string) (*v2alpha1.GettableAlertingRule, error)

func GetAlertingRulesStatus

func GetAlertingRulesStatus(ruleNamespace string, ruleChunk *ResourceRuleChunk, epRuleGroups []*alerting.RuleGroup,
	extLabels func() map[string]string) ([]*v2alpha1.GettableAlertingRule, error)

GetAlertingRulesStatus mix rules from prometheusrule custom resources and rules from endpoints. Use rules from prometheusrule custom resources as the main reference.

func InjectExprNamespaceLabel

func InjectExprNamespaceLabel(expr, namespace string) (string, error)

InjectExprNamespaceLabel injects an label, whose key is "namespace" and whose value is the given namespace, into the prometheus query expression, which will limit the query scope.

func ParseAlertingRules

func ParseAlertingRules(epRuleGroups []*alerting.RuleGroup, custom bool, level v2alpha1.RuleLevel,
	filterFunc func(group, ruleId string, rule *alerting.AlertingRule) bool) ([]*v2alpha1.GettableAlertingRule, error)

Types

type PrometheusRuler

type PrometheusRuler struct {
	// contains filtered or unexported fields
}

func (*PrometheusRuler) AddAlertingRules

func (r *PrometheusRuler) AddAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector, ruleResourceLabels map[string]string,
	rules ...*RuleWithGroup) ([]*v2alpha1.BulkItemResponse, error)

func (*PrometheusRuler) DeleteAlertingRules

func (r *PrometheusRuler) DeleteAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
	ruleItems ...*ResourceRuleItem) ([]*v2alpha1.BulkItemResponse, error)

func (*PrometheusRuler) ExternalLabels

func (r *PrometheusRuler) ExternalLabels() func() map[string]string

func (*PrometheusRuler) ListRuleResources

func (r *PrometheusRuler) ListRuleResources(ruleNamespace *corev1.Namespace, extraRuleResourceSelector labels.Selector) (
	[]*promresourcesv1.PrometheusRule, error)

func (*PrometheusRuler) Namespace

func (r *PrometheusRuler) Namespace() string

func (*PrometheusRuler) RuleResourceNamespaceSelector

func (r *PrometheusRuler) RuleResourceNamespaceSelector() (labels.Selector, error)

func (*PrometheusRuler) RuleResourceSelector

func (r *PrometheusRuler) RuleResourceSelector(extraRuleResourceSelector labels.Selector) (labels.Selector, error)

func (*PrometheusRuler) UpdateAlertingRules

func (r *PrometheusRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector, ruleResourceLabels map[string]string,
	ruleItems ...*ResourceRuleItem) ([]*v2alpha1.BulkItemResponse, error)

type ResourceRule

type ResourceRule struct {
	Level  v2alpha1.RuleLevel
	Custom bool
	ResourceRuleItem
}

type ResourceRuleChunk

type ResourceRuleChunk struct {
	Level            v2alpha1.RuleLevel
	Custom           bool
	ResourceRulesMap map[string]*ResourceRuleCollection
}

type ResourceRuleCollection

type ResourceRuleCollection struct {
	GroupSet  map[string]struct{}
	IdRules   map[string]*ResourceRuleItem
	NameRules map[string][]*ResourceRuleItem
}

type ResourceRuleItem

type ResourceRuleItem struct {
	ResourceName string
	RuleWithGroup
}

type RuleCache

type RuleCache struct {
	// contains filtered or unexported fields
}

RuleCache caches all rules from the prometheusrule custom resources

func NewRuleCache

func NewRuleCache(ruleResourceInformer prominformersv1.PrometheusRuleInformer) *RuleCache

func (*RuleCache) GetRule

func (c *RuleCache) GetRule(ruler Ruler, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector, idOrName string) (*ResourceRuleItem, error)

func (*RuleCache) GetRuleByIdOrName

func (c *RuleCache) GetRuleByIdOrName(ruler Ruler, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector, idOrName string) ([]*ResourceRuleItem, error)

func (*RuleCache) ListRules

func (c *RuleCache) ListRules(ruler Ruler, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector) (map[string]*ResourceRuleCollection, error)

type RuleWithGroup

type RuleWithGroup struct {
	Group string
	Id    string
	promresourcesv1.Rule
}

type Ruler

type Ruler interface {
	Namespace() string
	RuleResourceNamespaceSelector() (labels.Selector, error)
	RuleResourceSelector(extraRuleResourceSelector labels.Selector) (labels.Selector, error)
	ExternalLabels() func() map[string]string

	ListRuleResources(ruleNamespace *corev1.Namespace, extraRuleResourceSelector labels.Selector) (
		[]*promresourcesv1.PrometheusRule, error)
	AddAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace, extraRuleResourceSelector labels.Selector,
		ruleResourceLabels map[string]string, rules ...*RuleWithGroup) ([]*v2alpha1.BulkItemResponse, error)
	UpdateAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace, extraRuleResourceSelector labels.Selector,
		ruleResourceLabels map[string]string, ruleItems ...*ResourceRuleItem) ([]*v2alpha1.BulkItemResponse, error)
	DeleteAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
		ruleItems ...*ResourceRuleItem) ([]*v2alpha1.BulkItemResponse, error)
}

type ThanosRuler

type ThanosRuler struct {
	// contains filtered or unexported fields
}

func (*ThanosRuler) AddAlertingRules

func (r *ThanosRuler) AddAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector, ruleResourceLabels map[string]string,
	rules ...*RuleWithGroup) ([]*v2alpha1.BulkItemResponse, error)

func (*ThanosRuler) DeleteAlertingRules

func (r *ThanosRuler) DeleteAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
	ruleItems ...*ResourceRuleItem) ([]*v2alpha1.BulkItemResponse, error)

func (*ThanosRuler) ExternalLabels

func (r *ThanosRuler) ExternalLabels() func() map[string]string

func (*ThanosRuler) ListRuleResources

func (r *ThanosRuler) ListRuleResources(ruleNamespace *corev1.Namespace, extraRuleSelector labels.Selector) (
	[]*promresourcesv1.PrometheusRule, error)

func (*ThanosRuler) Namespace

func (r *ThanosRuler) Namespace() string

func (*ThanosRuler) RuleResourceNamespaceSelector

func (r *ThanosRuler) RuleResourceNamespaceSelector() (labels.Selector, error)

func (*ThanosRuler) RuleResourceSelector

func (r *ThanosRuler) RuleResourceSelector(extraRuleSelector labels.Selector) (labels.Selector, error)

func (*ThanosRuler) UpdateAlertingRules

func (r *ThanosRuler) UpdateAlertingRules(ctx context.Context, ruleNamespace *corev1.Namespace,
	extraRuleResourceSelector labels.Selector, ruleResourceLabels map[string]string,
	ruleItems ...*ResourceRuleItem) ([]*v2alpha1.BulkItemResponse, error)

Jump to

Keyboard shortcuts

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