Documentation
¶
Index ¶
- Constants
- Variables
- func CheckQueryExprNamespace(expr, namespace string) error
- func EmailSecretKey(receverName, from string) string
- func GetBaseAlertmanagerConfig(namespace, name string) *v1alpha1.AlertmanagerConfig
- func GetBasePrometheusRule(namespace, name string) *monitoringv1.PrometheusRule
- func ModifyReceiver(ctx context.Context, aconfig *v1alpha1.AlertmanagerConfig, ...) error
- func RealTimeAlertKey(namespace, name string) string
- func SplitQueryExpr(logql string) (query, op, value string, hasOp bool)
- func TestEmail(e EmailConfig, cluster, namespace string) error
- func ToAlertmanagerReceiver(rec ReceiverConfig) v1alpha1.Receiver
- type Action
- type AlertLevel
- type AlertReceiver
- type AlertRule
- type AlertRuleList
- type BaseAlertResource
- func (base *BaseAlertResource) AddNullReceivers()
- func (base *BaseAlertResource) GetInhibitRuleMap() map[string]v1alpha1.InhibitRule
- func (base *BaseAlertResource) GetReceiverMap() (map[string][]AlertReceiver, error)
- func (base *BaseAlertResource) GetSilenceMap() map[string]alertmanagertypes.Silence
- func (base *BaseAlertResource) UpdateInhibitRules(alertrules AlertRuleList[AlertRule]) error
- func (base *BaseAlertResource) UpdateRoutes(alertrules AlertRuleList[AlertRule])
- type BaseAlertRule
- type EmailConfig
- type ExporterOptions
- type LoggingAlertRule
- type LogqlGenerator
- type MonitorAlertRule
- type MonitorOptions
- type PromqlGenerator
- type RawLoggingAlertRule
- type RawMonitorAlertResource
- type RealTimeAlertRule
- type ReceiverConfig
- type ResourceDetail
- type RuleContext
- type RuleDetail
- type UnitValue
- type WebhookConfig
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 = "kubegems-loki-rules"
LokiRecordingRulesKey = "kubegems-loki-recording-rules.yaml"
)
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 ToAlertmanagerReceiver ¶
func ToAlertmanagerReceiver(rec ReceiverConfig) v1alpha1.Receiver
Types ¶
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) 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"` // 标签键值对
}
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)
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)
查询规则上下文
type RawLoggingAlertRule ¶
type RawLoggingAlertRule struct {
Base *BaseAlertResource
*corev1.ConfigMap
*rulefmt.RuleGroups
}
func (*RawLoggingAlertRule) ModifyLoggingAlertRule ¶
func (raw *RawLoggingAlertRule) ModifyLoggingAlertRule(r LoggingAlertRule, act Action) 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
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"`
}
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
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 WebhookConfig ¶
type WebhookConfig struct {
URL string `json:"url"`
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.