prometheus

package
v1.21.0-rc.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: Apache-2.0 Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AlertNamespaceLabel = "gems_namespace"
	AlertNameLabel      = "gems_alertname"
	// 用于从告警中获取告警资源
	AlertResourceLabel = "gems_alert_resource"
	AlertRuleLabel     = "gems_alert_rule"
	AlertFromLabel     = "gems_alert_from" // 告警来自哪里,logging/monitor

	AlertTypeMonitor = "monitor"
	AlertTypeLogging = "logging"

	SeverityLabel    = "severity"
	SeverityError    = "error"    // 错误
	SeverityCritical = "critical" // 严重

	AlertClusterKey = "cluster"

	// 告警消息发送范围
	AlertScopeLabel  = "gems_alert_scope"
	ScopeSystemAdmin = "system-admin" // 系统管理员
	ScopeSystemUser  = "system-user"  // 所有用户
	ScopeNormal      = "normal"       // 普通租户用户

	SilenceCommentForBlackListPrefix = "fingerprint-"
	SilenceCommentForAlertrulePrefix = "silence for"
)
View Source
const (
	// 全局告警命名空间,非此命名空间强制加上namespace筛选
	GlobalAlertNamespace = gemlabels.NamespaceMonitor
	// namespace
	PromqlNamespaceKey = "namespace"
)
View Source
const (
	LoggingAlertRuleCMName  = "loki-alerting-rules"
	LoggingRecordingRuleKey = "loki-alerting-rules.yaml" // loki helm 部署 recording rule就是这个名字。。
)
View Source
const (
	// prometheusrule and alertmanagerconfigname
	MonitorAlertCRDName = "kubegems-default-monitor-alert-rule"
	LoggingAlertCRDName = "kubegems-default-logging-alert-rule"
)

Variables

View Source
var (
	DefaultReceiverName = "gemcloud-default-webhook"
	DefaultReceiverURL  = fmt.Sprintf("https://kubegems-local-agent.%s:8041/alert", gems.NamespaceLocal)
	DefaultReceiver     = v1alpha1.Receiver{
		Name: DefaultReceiverName,
		WebhookConfigs: []v1alpha1.WebhookConfig{
			{
				URL: &DefaultReceiverURL,
				HTTPConfig: &v1alpha1.HTTPConfig{
					TLSConfig: &v1.SafeTLSConfig{
						InsecureSkipVerify: true,
					},
				},
			},
		},
	}

	NullReceiverName = "null"
	NullReceiver     = v1alpha1.Receiver{Name: NullReceiverName}

	EmailSecretName                    = "gemscloud-email-password"
	EmailSecretLabel map[string]string = map[string]string{
		"gemcloud": "email-secret",
	}
)
View Source
var (
	// 单位表
	UnitValueMap = map[string]UnitValue{
		"B":  defaultUnitValue,
		"KB": {Op: "/", Value: "1024"},
		"MB": {Op: "/", Value: "(1024 * 1024)"},
		"GB": {Op: "/", Value: "(1024 * 1024 * 1024)"},
		"TB": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024)"},
		"PB": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024 * 1024)"},

		"B/s":  defaultUnitValue,
		"KB/s": {Op: "/", Value: "1024"},
		"MB/s": {Op: "/", Value: "(1024 * 1024)"},
		"GB/s": {Op: "/", Value: "(1024 * 1024 * 1024)"},
		"TB/s": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024)"},
		"PB/s": {Op: "/", Value: "(1024 * 1024 * 1024 * 1024 * 1024)"},

		"us": {Op: "*", Value: "(1000 * 1000)"},
		"ms": {Op: "*", Value: "1000"},
		"s":  defaultUnitValue,
		"m":  {Op: "/", Value: "60"},
		"h":  {Op: "/", Value: "(60 * 60)"},
		"d":  {Op: "/", Value: "(24 * 60 * 60)"},
		"w":  {Op: "/", Value: "(7 * 24 * 60 * 60)"},

		"0.0-1.0": {Op: "*", Value: "100", Show: "%"},
		"0-100":   {Show: "%"},
	}
)

Functions

func CheckQueryExprNamespace

func CheckQueryExprNamespace(expr, namespace string) error

func EmailSecretKey

func EmailSecretKey(receverName, from string) string

func GetBaseAlertmanagerConfig

func GetBaseAlertmanagerConfig(namespace, name string) *v1alpha1.AlertmanagerConfig

func GetBasePrometheusRule

func GetBasePrometheusRule(namespace, name string) *monitoringv1.PrometheusRule

func ModifyReceiver

func ModifyReceiver(ctx context.Context, aconfig *v1alpha1.AlertmanagerConfig, receiver *v1alpha1.Receiver, act Action) error

func RealTimeAlertKey

func RealTimeAlertKey(namespace, name string) string

func SplitQueryExpr

func SplitQueryExpr(logql string) (query, op, value string, hasOp bool)

func TestEmail

func TestEmail(e EmailConfig, cluster, namespace string) error

func ToAlertmanagerReceiver

func ToAlertmanagerReceiver(rec ReceiverConfig) v1alpha1.Receiver

Types

type Action

type Action int
const (
	Add Action = iota
	Update
	Delete
)

type AlertLevel

type AlertLevel struct {
	CompareOp    string `json:"compareOp"`
	CompareValue string `json:"compareValue"` // 支持表达式, eg. 24 * 60
	Severity     string `json:"severity"`     // error, critical
}

type AlertReceiver

type AlertReceiver struct {
	Name     string `json:"name"`
	Interval string `json:"interval"` // 分组间隔
}

type AlertRule

type AlertRule interface {
	GetNamespace() string
	GetName() string
	GetInhibitLabels() []string
	GetAlertLevels() []AlertLevel
	GetReceivers() []AlertReceiver
}

type AlertRuleList

type AlertRuleList[T AlertRule] []T

func (AlertRuleList[T]) ToAlertRuleList

func (l AlertRuleList[T]) ToAlertRuleList() []AlertRule

type BaseAlertResource

type BaseAlertResource struct {
	AMConfig *v1alpha1.AlertmanagerConfig
	Silences []alertmanagertypes.Silence
}

func (*BaseAlertResource) AddNullReceivers

func (base *BaseAlertResource) AddNullReceivers()

func (*BaseAlertResource) GetInhibitRuleMap

func (base *BaseAlertResource) GetInhibitRuleMap() map[string]v1alpha1.InhibitRule

func (*BaseAlertResource) GetReceiverMap

func (base *BaseAlertResource) GetReceiverMap() (map[string][]AlertReceiver, error)

func (*BaseAlertResource) GetSilenceMap

func (base *BaseAlertResource) GetSilenceMap() map[string]alertmanagertypes.Silence

func (*BaseAlertResource) UpdateInhibitRules

func (base *BaseAlertResource) UpdateInhibitRules(alertrules AlertRuleList[AlertRule]) error

func (*BaseAlertResource) UpdateRoutes

func (base *BaseAlertResource) UpdateRoutes(alertrules AlertRuleList[AlertRule])

type BaseAlertRule

type BaseAlertRule struct {
	Namespace string `json:"namespace"`
	Name      string `json:"name"`

	Expr    string `json:"expr"`    // promql/logql表达式,不能包含比较运算符(<, <=, >, >=, ==)
	For     string `json:"for"`     // 持续时间, eg. 10s, 1m, 1h
	Message string `json:"message"` // 告警消息,若为空后端自动填充

	InhibitLabels []string        `json:"inhibitLabels"` // 如果有多个告警级别,需要配置告警抑制的labels
	AlertLevels   []AlertLevel    `json:"alertLevels"`   // 告警级别
	Receivers     []AlertReceiver `json:"receivers"`     // 接收器

	IsOpen bool   `json:"isOpen"` // 是否启用
	State  string `json:"state"`  // 状态
}

func (BaseAlertRule) GetAlertLevels

func (r BaseAlertRule) GetAlertLevels() []AlertLevel

func (BaseAlertRule) GetInhibitLabels

func (r BaseAlertRule) GetInhibitLabels() []string

func (BaseAlertRule) GetName

func (r BaseAlertRule) GetName() string

func (BaseAlertRule) GetNamespace

func (r BaseAlertRule) GetNamespace() string

func (BaseAlertRule) GetReceivers

func (r BaseAlertRule) GetReceivers() []AlertReceiver

type EmailConfig

type EmailConfig struct {
	SMTPServer   string `json:"smtpServer"`
	RequireTLS   bool   `json:"requireTLS"`
	From         string `json:"from"`
	To           string `json:"to"`
	AuthPassword string `json:"authPassword"`
}

type ExporterOptions

type ExporterOptions struct {
	Listen string `json:"listen,omitempty" description:"listen address"`
}

func DefaultExporterOptions

func DefaultExporterOptions() *ExporterOptions

type LoggingAlertRule

type LoggingAlertRule struct {
	LogqlGenerator *LogqlGenerator `json:"logqlGenerator"`
	BaseAlertRule  `json:",inline"`
	RealTimeAlerts []*promv1.Alert `json:"realTimeAlerts,omitempty"` // 实时告警
	Origin         string          `json:"origin,omitempty"`         // 原始的prometheusrule
}

func (*LoggingAlertRule) CheckAndModify

func (r *LoggingAlertRule) CheckAndModify(opts *MonitorOptions) error

type LogqlGenerator

type LogqlGenerator struct {
	Duration   string            `json:"duration"`             // 时间范围
	Match      string            `json:"match"`                // 正则匹配的字符串
	LabelPairs map[string]string `json:"labelpairs,omitempty"` // 标签键值对
}

func (*LogqlGenerator) IsEmpty

func (g *LogqlGenerator) IsEmpty() bool

func (*LogqlGenerator) ToLogql

func (g *LogqlGenerator) ToLogql(namespace string) string

type MonitorAlertRule

type MonitorAlertRule struct {
	PromqlGenerator *PromqlGenerator `json:"promqlGenerator"`

	BaseAlertRule  `json:",inline"`
	RealTimeAlerts []*promv1.Alert `json:"realTimeAlerts,omitempty"` // 实时告警
	Origin         string          `json:"origin,omitempty"`         // 原始的prometheusrule
	Source         string          `json:"source"`                   // 来自哪个prometheusrule
}

func (*MonitorAlertRule) CheckAndModify

func (r *MonitorAlertRule) CheckAndModify(opts *MonitorOptions) error

TODO: unit test

type MonitorOptions

type MonitorOptions struct {
	Severity  map[string]string `json:"severity"`  // 告警级别
	Operators []string          `json:"operators"` // 运算符

	Resources map[string]ResourceDetail `json:"resources"` // 告警列表
}

func DefaultMonitorOptions

func DefaultMonitorOptions() *MonitorOptions

func (*MonitorOptions) FindRuleContext

func (cfg *MonitorOptions) FindRuleContext(resName, ruleName string) (RuleContext, error)

func (*MonitorOptions) JSON

func (opts *MonitorOptions) JSON() []byte

func (*MonitorOptions) Name

func (opts *MonitorOptions) Name() string

func (*MonitorOptions) Validate

func (opts *MonitorOptions) Validate() error

type PromqlGenerator

type PromqlGenerator struct {
	Resource   string            `json:"resource"`             // 告警资源, eg. node、pod
	Rule       string            `json:"rule"`                 // 告警规则名, eg. cpuUsage、memoryUsagePercent
	Unit       string            `json:"unit"`                 // 单位
	LabelPairs map[string]string `json:"labelpairs,omitempty"` // 标签键值对

	// 相关配置
	RuleContext `json:"-"`
}

func (*PromqlGenerator) FindRuleContext

func (g *PromqlGenerator) FindRuleContext(cfg *MonitorOptions) (RuleContext, error)

查询规则上下文

func (*PromqlGenerator) IsEmpty

func (g *PromqlGenerator) IsEmpty() bool

func (*PromqlGenerator) ToPromql

func (g *PromqlGenerator) ToPromql(namespace string, opts *MonitorOptions) (string, error)

type RawLoggingAlertRule

type RawLoggingAlertRule struct {
	Base *BaseAlertResource
	*corev1.ConfigMap
	*rulefmt.RuleGroups
}

func (*RawLoggingAlertRule) ModifyLoggingAlertRule

func (raw *RawLoggingAlertRule) ModifyLoggingAlertRule(r LoggingAlertRule, act Action) error

func (*RawLoggingAlertRule) ToAlerts

func (raw *RawLoggingAlertRule) ToAlerts(hasDetail bool) (AlertRuleList[LoggingAlertRule], error)

type RawMonitorAlertResource

type RawMonitorAlertResource struct {
	Base *BaseAlertResource
	*monitoringv1.PrometheusRule
	*MonitorOptions
}

里面资源的namespace必须相同

func (*RawMonitorAlertResource) ModifyAlertRule

func (raw *RawMonitorAlertResource) ModifyAlertRule(newAlertRule MonitorAlertRule, act Action) error

所有alertrule都是一个namespace

func (*RawMonitorAlertResource) ToAlerts

func (raw *RawMonitorAlertResource) ToAlerts(hasDetail bool) (AlertRuleList[MonitorAlertRule], error)

默认认为namespace全部一致

type RealTimeAlertRule

type RealTimeAlertRule struct {
	Name string `json:"name"`
	// Query          string         `json:"query"`
	// Duration       float64        `json:"duration"`
	// Labels         model.LabelSet `json:"labels"`
	// Annotations    model.LabelSet `json:"annotations"`
	Alerts []*v1.Alert `json:"alerts"`
	// Health         v1.RuleHealth  `json:"health"`
	// LastError      string         `json:"lastError,omitempty"`
	// EvaluationTime float64        `json:"evaluationTime"`
	// LastEvaluation time.Time      `json:"lastEvaluation"`
	State string `json:"state"`
}

func (*RealTimeAlertRule) Len

func (r *RealTimeAlertRule) Len() int

func (*RealTimeAlertRule) Less

func (r *RealTimeAlertRule) Less(i, j int) bool

func (*RealTimeAlertRule) Swap

func (r *RealTimeAlertRule) Swap(i, j int)

type ReceiverConfig

type ReceiverConfig struct {
	Name           string          `json:"name"`
	Namespace      string          `json:"namespace"`
	EmailConfigs   []EmailConfig   `json:"emailConfigs"`
	WebhookConfigs []WebhookConfig `json:"webhookConfigs"`
	Source         string          `json:"source"` // 来自哪个alertmanagerconfig
}

func ToGemsReceiver

func ToGemsReceiver(rec v1alpha1.Receiver, namespace, source string, sec *corev1.Secret) ReceiverConfig

func (*ReceiverConfig) Precheck

func (rec *ReceiverConfig) Precheck() error

type ResourceDetail

type ResourceDetail struct {
	Namespaced bool                  `json:"namespaced"` // 是否带有namespace
	ShowName   string                `json:"showName"`
	Rules      map[string]RuleDetail `json:"rules"`
}

type RuleContext

type RuleContext struct {
	ResourceDetail ResourceDetail
	RuleDetail     RuleDetail
}

type RuleDetail

type RuleDetail struct {
	Expr     string   `json:"expr"`     // 原生表达式
	ShowName string   `json:"showName"` // 前端展示
	Labels   []string `json:"labels"`   // 支持的标签
	Unit     string   `json:"unit"`     // 使用的单位
}

type UnitValue

type UnitValue struct {
	Show string

	Op    string
	Value string
}

func ParseUnit

func ParseUnit(unit string) (UnitValue, error)

type WebhookConfig

type WebhookConfig struct {
	URL string `json:"url"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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