strategy

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MetricName        = "__name__"
	MetricGroupName   = "__group_name__"
	MetricGroupId     = "__group_id__"
	MetricAlert       = "alertname"
	MetricAlertId     = "__alert_id__"
	MetricLevelId     = "__level_id__"
	MetricInstance    = "instance"
	MetricDescription = "description"
	MetricSummary     = "summary"
)

Variables

This section is empty.

Functions

func BuildDuration

func BuildDuration(duration string) int64

BuildDuration 字符串转为api时间

func Formatter

func Formatter(format string, data any) (s string)

Formatter 格式化告警文案

func NewAlarm

func NewAlarm(group *Group, rule *Rule, results []*Result) (*Alarm, *Alarm, bool)

func SetAlarmCache

func SetAlarmCache(cache AlarmCache)

SetAlarmCache 设置告警缓存

Types

type Alarm

type Alarm struct {
	// 接收者
	Receiver string `json:"receiver"`
	// 报警状态
	Status AlarmStatus `json:"status"`
	// 告警列表
	Alerts []*Alert `json:"alerts"`
	// 告警组标签
	GroupLabels Labels `json:"groupLabels"`
	// 公共标签
	CommonLabels map[string]string `json:"commonLabels"`
	// 公共注解
	CommonAnnotations map[string]string `json:"commonAnnotations"`
	// 外部链接
	ExternalURL string `json:"externalURL"`
	// 版本
	Version string `json:"version"`
	// 告警组key
	GroupKey string `json:"groupKey"`
	// 截断告警数
	TruncatedAlerts int32 `json:"truncate"`
}

func (*Alarm) Bytes

func (a *Alarm) Bytes() []byte

Bytes Alarm to bytes

type AlarmCache

type AlarmCache interface {
	// Get 获取当前规则下所有告警数据
	Get(ruleId uint32) (*Alarm, bool)
	// Set 当前规则下告警数据
	Set(ruleId uint32, alarm *Alarm) bool
	// Remove 删除告警
	Remove(ruleId uint32) bool
	// SetNotifyAlert 当前规则下告警数据
	SetNotifyAlert(alert *Alert) bool
	// RemoveNotifyAlert 删除告警
	RemoveNotifyAlert(alert *Alert) bool
	// GetNotifyAlert 获取告警数据
	GetNotifyAlert(alert *Alert) (*Alert, bool)
	// RangeNotifyAlerts range notify alerts
	RangeNotifyAlerts(f func(*Alert))
}

func NewAlarmCache

func NewAlarmCache(cache cache.GlobalCache) AlarmCache

func NewDefaultAlarmCache

func NewDefaultAlarmCache() AlarmCache

NewDefaultAlarmCache 创建默认的告警缓存

type AlarmStatus

type AlarmStatus string
const (
	// AlarmStatusFiring firing
	AlarmStatusFiring AlarmStatus = "firing"
	// AlarmStatusResolved resolved
	AlarmStatusResolved AlarmStatus = "resolved"
)

type Alarms

type Alarms struct {
	Alarms []*Alarm
	// contains filtered or unexported fields
}

func NewAlarmList

func NewAlarmList(alarms ...*Alarm) *Alarms

func (*Alarms) Append

func (l *Alarms) Append(alarm *Alarm)

Append append alarm

func (*Alarms) List

func (l *Alarms) List() []*Alarm

List alarm list

type Alert

type Alert struct {
	// 告警状态
	Status AlarmStatus `json:"status"`
	// 标签
	Labels Labels `json:"labels"`
	// 注解
	Annotations Annotations `json:"annotations"`
	// 开始时间
	StartsAt string `json:"startsAt"`
	// 结束时间, 如果为空, 则表示未结束
	EndsAt string `json:"endsAt"`
	// 告警生成链接
	GeneratorURL string `json:"generatorURL"`
	// 指纹
	Fingerprint string `json:"fingerprint"`
}

type Alerter

type Alerter interface {
	Eval(ctx context.Context) ([]*Alarm, error)
}

func NewAlerting

func NewAlerting(groups ...*Group) Alerter

NewAlerting 初始化策略告警实例

type Alerting

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

func (*Alerting) Eval

func (a *Alerting) Eval(ctx context.Context) ([]*Alarm, error)

type AlertingOption

type AlertingOption func(*Alerting)

func WithDatasourceName

func WithDatasourceName(datasourceName DatasourceName) AlertingOption

WithDatasourceName 设置数据源名称

type Annotation

type Annotation interface {
	Summary() string
	Description() string
	Get(key string) string
	Map() map[string]string
	String() string
	sql.Scanner
	driver.Valuer
}

type Annotations

type Annotations map[string]string

func MapToAnnotations

func MapToAnnotations(m map[string]string) *Annotations

MapToAnnotations 将map转换为注解

func (*Annotations) Description

func (l *Annotations) Description() string

func (*Annotations) Get

func (l *Annotations) Get(key string) string

func (*Annotations) Map

func (l *Annotations) Map() map[string]string

func (*Annotations) Scan

func (l *Annotations) Scan(src any) error

func (*Annotations) String

func (l *Annotations) String() string

func (*Annotations) Summary

func (l *Annotations) Summary() string

func (*Annotations) Value

func (l *Annotations) Value() (driver.Value, error)

type BasicAuth

type BasicAuth struct {
	Username string `json:"username"`
	Password string `json:"password"`
}

func NewBasicAuth

func NewBasicAuth(username, password string) *BasicAuth

func NewBasicAuthWithString

func NewBasicAuthWithString(str string) *BasicAuth

func (*BasicAuth) Bytes

func (b *BasicAuth) Bytes() []byte

func (*BasicAuth) Scan

func (b *BasicAuth) Scan(value interface{}) error

func (*BasicAuth) String

func (b *BasicAuth) String() string

func (*BasicAuth) Value

func (b *BasicAuth) Value() (driver.Value, error)

type Data

type Data struct {
	ResultType string    `json:"resultType"`
	Result     []*Result `json:"result"`
}

type Datasource

type Datasource interface {
	Query(ctx context.Context, expr string, duration int64) (*QueryResponse, error)
	GetCategory() string
	GetEndpoint() string
	GetBasicAuth() *BasicAuth
	WithBasicAuth(basicAuth *BasicAuth) Datasource
}

func NewDatasource

func NewDatasource(datasourceName DatasourceName, endpoint string) Datasource

type DatasourceName

type DatasourceName string
const (
	PrometheusDatasource DatasourceName = "prometheus"
)

type Group

type Group struct {
	Name  string  `json:"name"`
	Rules []*Rule `json:"rules"`
	Id    uint32  `json:"-"`
}

type Groups

type Groups struct {
	Groups []*Group `json:"groups"`
}

type Label

type Label interface {
	StrategyId() uint32
	LevelId() uint32
	Get(key string) string
	GetInstance() string
	Map() map[string]string
	String() string
	sql.Scanner
	driver.Valuer
}

type Labels

type Labels map[string]string

func MapToLabels

func MapToLabels(m map[string]string) *Labels

MapToLabels 将map转换为标签

func (*Labels) Append added in v0.0.16

func (l *Labels) Append(key, value string) *Labels

Append append labels

func (*Labels) AppendAll added in v0.0.16

func (l *Labels) AppendAll(m map[string]string) *Labels

AppendAll append labels

func (*Labels) Get

func (l *Labels) Get(key string) string

func (*Labels) GetInstance

func (l *Labels) GetInstance() string

func (*Labels) LevelId

func (l *Labels) LevelId() uint32

func (*Labels) Map

func (l *Labels) Map() map[string]string

func (*Labels) Scan

func (l *Labels) Scan(src any) error

func (*Labels) StrategyId

func (l *Labels) StrategyId() uint32

func (*Labels) String

func (l *Labels) String() string

func (*Labels) Value

func (l *Labels) Value() (driver.Value, error)

type Load

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

func NewStrategyLoad

func NewStrategyLoad(source config.Source) *Load

NewStrategyLoad 初始化规则加载器

func (*Load) Load

func (l *Load) Load() ([]*Group, error)

type Metric

type Metric map[string]string

func (Metric) Bytes

func (m Metric) Bytes() []byte

Bytes Metric to []byte

func (Metric) Get

func (m Metric) Get(key string) string

Get get tag value

func (Metric) MD5

func (m Metric) MD5() string

MD5 Metric to md5

func (Metric) Map

func (m Metric) Map() map[string]string

Map Metric to map[string]string

func (Metric) Name

func (m Metric) Name() string

Name Metric __name__

func (Metric) Set

func (m Metric) Set(key, value string)

Set Metric set tag value

func (Metric) String

func (m Metric) String() string

String Metric to string

type PromDatasource

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

func NewPrometheusDatasource

func NewPrometheusDatasource(domain string) *PromDatasource

NewPrometheusDatasource 实例化数据源对象

func (*PromDatasource) GetBasicAuth

func (d *PromDatasource) GetBasicAuth() *BasicAuth

func (*PromDatasource) GetCategory

func (d *PromDatasource) GetCategory() string

func (*PromDatasource) GetEndpoint

func (d *PromDatasource) GetEndpoint() string

func (*PromDatasource) Query

func (d *PromDatasource) Query(_ context.Context, expr string, duration int64) (*QueryResponse, error)

Query 调用数据源查询数据

curl 'https://<domain>/api/v1/query?query=go_memstats_sys_bytes&time=1704785907'

func (*PromDatasource) WithBasicAuth

func (d *PromDatasource) WithBasicAuth(basicAuth *BasicAuth) Datasource

type QueryResponse

type QueryResponse struct {
	Status    string `json:"status"`
	Data      *Data  `json:"data"`
	ErrorType string `json:"errorType"`
	Error     string `json:"error"`
}

func (*QueryResponse) Bytes

func (qr *QueryResponse) Bytes() []byte

Bytes QueryResponse to []byte

func (*QueryResponse) String

func (qr *QueryResponse) String() string

String QueryResponse to string

type Result

type Result struct {
	Metric Metric `json:"metric"`
	Value  []any  `json:"value"`
}

func (*Result) GetMetric

func (r *Result) GetMetric() Metric

GetMetric Result to Metric

type Rule

type Rule struct {
	Id          uint32      `json:"-"`
	Alert       string      `json:"alert"`
	Expr        string      `json:"expr"`
	For         string      `json:"for"`
	Labels      Labels      `json:"labels"`
	Annotations Annotations `json:"annotations"`
	// contains filtered or unexported fields
}

func (*Rule) Endpoint

func (r *Rule) Endpoint() string

Endpoint 获取数据源

func (*Rule) GetBasicAuth

func (r *Rule) GetBasicAuth() *BasicAuth

GetBasicAuth 获取基础认证

func (*Rule) MD5

func (r *Rule) MD5() string

MD5 Rule MD5

func (*Rule) SetBasicAuth

func (r *Rule) SetBasicAuth(auth *BasicAuth)

SetBasicAuth 设置基础认证

func (*Rule) SetEndpoint

func (r *Rule) SetEndpoint(endpoint string)

SetEndpoint 设置数据源

type Store

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

func NewStrategyStore

func NewStrategyStore(dir string) *Store

NewStrategyStore 初始化规则存储

func (*Store) Store

func (l *Store) Store(groups ...*Groups) error

Store 存储策略

Jump to

Keyboard shortcuts

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