events

package
v0.0.0-...-dfd69a5 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2019 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	UntilRecoveryDeadline     = time.Date(2019, 2, 19, 18, 03, 31, 0, time.Local)
	UntilRecoveryDeadlineUnix = UntilRecoveryDeadline.Unix()
)

关闭通知 直到 告警恢复时, deadline 的取值

View Source
var (
	Channels map[string]ChannelSpec
)

Functions

func InitChannels

func InitChannels(settings []ChannelSpec) error

InitChannels 初始化 channel 数据

Types

type Alert

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

Alert represent a notice will be send

type ChannelReceiverProvider

type ChannelReceiverProvider interface {
	GetChannelReceiver(channel string, names []string) map[string]string
}

type ChannelSpec

type ChannelSpec struct {
	Key       string            `yaml:"key" json:"key"`
	Name      string            `yaml:"name" json:"name"`
	MaxLength int               `yaml:"max_length" json:"-"` // 允许最大消息长度,单位字节
	Append    string            `yaml:"message_addon" json:"-"`
	Prepend   string            `yaml:"message_header" json:"-"`
	Setting   map[string]string `yaml:"setting" json:"-"`
	// contains filtered or unexported fields
}

ChannelSpec 表示一类通道 (channel) 的配置项

func (ChannelSpec) AppendText

func (spec ChannelSpec) AppendText(data map[string]interface{}) string

AppendText 生成后缀缀内容

func (ChannelSpec) PrependText

func (spec ChannelSpec) PrependText(data map[string]interface{}) string

PrependText 生成前缀内容

type Emitter

type Emitter interface {
	Emit(Alert)
}

Emitter 代表告警器, event 根据策略调用 Notifier 发送消息

type Engine

type Engine struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Engine manage a event pool, process the events, including persistent and notify etc

func NewEngine

func NewEngine(ctx context.Context, storage *store.MySQL, emitter Emitter, g2u GroupProvider) (*Engine, error)

NewEngine create a new event engine

func (*Engine) AckEvent

func (engine *Engine) AckEvent(ctx context.Context, fingerprint, username string) error

AckEvent 将一个事件设置为待办, 待办状态的事件不会再发出通知

func (*Engine) AddEventReceiver

func (engine *Engine) AddEventReceiver(ctx context.Context, user string, eventID int64) error

AddEventReceiver 增加事件与接收人关联记录

func (*Engine) AddEventTag

func (engine *Engine) AddEventTag(ctx context.Context, id int64, tag string) error

AddEventTag 为 一个事件增加标签

func (*Engine) CloseEvent

func (engine *Engine) CloseEvent(ctx context.Context, name, metric string, settings ...EventSetting) error

CloseEvent 使事件恢复

func (*Engine) CloseEventByFingerprint

func (engine *Engine) CloseEventByFingerprint(ctx context.Context, fingerprint string, settings ...EventSetting) error

CloseEventByFingerprint 使事件恢复

func (*Engine) DeleteEventTag

func (engine *Engine) DeleteEventTag(ctx context.Context, id int64, tag string) error

DeleteEventTag 为 一个事件删除标签

func (*Engine) FireEvent

func (engine *Engine) FireEvent(ctx context.Context, name, metric string, settings ...EventSetting) (Event, error)

FireEvent 方法, 用来创建 Event, 方法发现如果 Event 已存在则返回已存在的, 如果不存在再调用 CreateEvent 创建. 同时也要负责更新 Expire 等

func (*Engine) GetEvent

func (engine *Engine) GetEvent(ctx context.Context, id int64) (Event, error)

GetEvent 获取一个事件, 包括关联人

func (*Engine) GetEventReceivers

func (engine *Engine) GetEventReceivers(ctx context.Context, eventID int64) ([]string, error)

GetEventReceivers 获取与事件相关的接收人

func (*Engine) GetEvents

func (engine *Engine) GetEvents(ctx context.Context, user string, name, metric string, closed, acked bool, levels []types.Level, tags []string, from, to time.Time, search string, page, pageSize int) ([]Event, error)

GetEvents 查询事件

type Event

type Event struct {
	ID             int64       `json:"id" db:"id"`
	Name           string      `json:"name" db:"name"`
	Fingerprint    string      `json:"fingerprint" db:"fingerprint"`
	Level          types.Level `json:"level" db:"level"`
	Metric         string      `json:"metric" db:"metric"`
	Message        string      `json:"message" db:"message"`
	Notified       int64       `json:"notified" db:"notified"`               // 记录事件累计通知人次
	RecoveryNotice bool        `json:"recovery_notice" db:"recovery_notice"` // 是否发送恢复通知

	ExpireTime  time.Time `json:"expire_time" db:"expire_time"`
	CreatedTime time.Time `json:"created_time" db:"created_time"`
	UpdatedTime time.Time `json:"updated_time" db:"updated_time"`

	Expired           bool   `json:"expired" db:"expired"` // 事件是否过期, 只有 Closed = true 时才为 true, 表示是因过期而关闭, 而不是恢复关闭
	Acked             bool   `json:"acked" db:"acked"`     // 事件是否被认领(确认)
	AckUser           string `json:"ackuser" db:"ackuser"`
	Recovery          bool   `json:"recovery" db:"recovery"` // 事件是否恢复, 只有 Closed = true 时这个值才有意义
	Closed            bool   `json:"closed" db:"closed"`     // 事件是否关闭, 即非活动
	CloseTimeUnixnano int64  `json:"close_time_unixnano" db:"close_time_unixnano"`

	Handlers []Handler `json:"handlers" db:"-"`

	Tags types.StringList `json:"tags" db:"tags"`
	// contains filtered or unexported fields
}

Event 代表一次异常事件 Event 的发送方面, 只要 AlertName 相同, 就会作为一个 Group 统一发送. 并不会像 Prometheus 那样根据 label 的 key-value 对匹配进行合并. 如果有这种情况, 要根据 key-value 对匹配, 那么就在外层做, 根据这些 event 的 key-value 对, 生成一个统一的 TaskName, 再交给 ecenter

func Clone

func Clone(e Event, settings ...EventSetting) Event

Clone generate a new event from a given event

func New

func New(ctx context.Context, name, metric string, settings ...EventSetting) Event

New generate a new event

func (Event) AlertMessage

func (e Event) AlertMessage() string

AlertMessage 生成该事件的告警消息内容

func (*Event) GetFingerprint

func (e *Event) GetFingerprint() string

GetFingerprint return the fingerprint value for event, it generate fingerprint if it not generated

type EventCommand

type EventCommand interface {
	Exec(*Event) error
}

EventCommand 表示一个对 event 进行变更处理的命令, 比如更新关闭等等

type EventSetting

type EventSetting func(event *Event)

EventSetting 代表配置选项

func WithEventACK

func WithEventACK() EventSetting

WithEventACK 设置事件确认

func WithEventEmitter

func WithEventEmitter(emitter Emitter) EventSetting

WithEventEmitter 设置事件确认

func WithEventExpire

func WithEventExpire() EventSetting

WithEventExpire 设置事件过期

func WithEventExpiretime

func WithEventExpiretime(t time.Time) EventSetting

WithEventExpiretime 修改 Event 的过期时间

func WithEventHandlers

func WithEventHandlers(handlers ...Handler) EventSetting

WithEventHandlers 设置 handlers

func WithEventLevel

func WithEventLevel(level types.Level) EventSetting

WithEventLevel 修改 Event 的 Level

func WithEventMessage

func WithEventMessage(msg string) EventSetting

WithEventMessage 修改 Event 的 Message

func WithEventName

func WithEventName(name string) EventSetting

WithEventName 设置 Event 的 TaskName, 任务名称更新时会用到

func WithEventRecovery

func WithEventRecovery() EventSetting

WithEventRecovery 设置事件恢复

func WithEventRecoveryNotice

func WithEventRecoveryNotice(notice bool) EventSetting

WithEventRecoveryNotice 设置事件确认

type GroupProvider

type GroupProvider interface {
	Groups2Users(types.Set) types.Set
}

GroupProvider convert group list to username list, if it is group name

type Handler

type Handler struct {
	ID        int64            `json:"id" db:"id"`
	EventID   int64            `json:"event_id" db:"event_id"`
	Receivers types.StringList `json:"receivers" db:"receivers"` // 接收人
	Channels  types.Chan       `json:"channels" db:"channels"`   // 发送渠道

	Everytime bool `json:"everytime" db:"everytime"` // 每次事件来了都通知, 如果为 true, delay 和 interval 会被忽略.

	Delay    time.Duration `json:"delay" db:"delay"`       // 延迟多久后通知
	Interval time.Duration `json:"interval" db:"interval"` // 到达 delay 的时间后, 每隔 interval 时间, 就通知一次

	CreatedTime time.Time `json:"created_time" db:"created_time"`
	UpdatedTime time.Time `json:"updated_time" db:"updated_time"`
}

Handler is Event handler policy mainly used for emitting

type Mute

type Mute struct {
	ID          int64     `json:"id" db:"id"`
	User        string    `json:"user" db:"user"`
	Name        string    `json:"name" db:"name"`
	Fingerprint string    `json:"fingerprint" db:"fingerprint"`
	Deadline    time.Time `json:"deadline" db:"deadline"`
	Description string    `json:"description" db:"description"`
	CreatedTime time.Time `json:"created_time" db:"created_time"`
	UpdatedTime time.Time `json:"updated_time" db:"updated_time"`
}

Mute 代表一个屏蔽规则

func (Mute) Mute

func (rule Mute) Mute(name, fingerprint, user string) bool

Mute 判断参数指定的规则是被这个 rule 屏蔽掉了, 参数指定的比不能为空 即, 参数 name, metric, user 肯定都是有值的, 否则它就不代表一个完整的事件

type Mutes

type Mutes []Mute

Mutes represents a group of TaskMute

type Pipeline

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

Pipeline is a kind of Emitter, and also the core Emitter

func NewPipeline

func NewPipeline(handlers ...Stage) Pipeline

NewPipeline create a new pipeline with handlers

func (Pipeline) Emit

func (pipe Pipeline) Emit(alert Alert)

Emit message in pipeline

type Receiver

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

Receiver 表示一个告警接收人, 一个人收到的所有事件都会通过 input 发送近来;Receiver 会对事件做自动合并调用 Sender 发送消息

func NewReceiver

func NewReceiver(ctx context.Context, channel, number string) (*Receiver, error)

NewReceiver 创建一个发送实体

type Stage

type Stage interface {
	Handle(Alert) []Alert
}

Stage will Handle the Alert

type StageMute

type StageMute struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewMuteStage

func NewMuteStage(ctx context.Context, storage *store.MySQL, crp ChannelReceiverProvider) (*StageMute, error)

NewMuteStage create the mute stage for pipeline

func (*StageMute) GetMutes

func (stage *StageMute) GetMutes(ctx context.Context, user ...string) Mutes

GetMutes 获取作用于某用户的所有拦截规则

func (*StageMute) Handle

func (stage *StageMute) Handle(alert Alert) []Alert

func (*StageMute) Mute

func (stage *StageMute) Mute(ctx context.Context, name string, fingerprint, user string, deadline time.Time, desc string) (Mute, error)

Mute 创建一条拦截规则, 对指定消息进行免通知

func (*StageMute) Muted

func (stage *StageMute) Muted(name, fingerprint, user string) bool

Muted 判断某个告警规则的某个事件,是否被某人屏蔽了

func (*StageMute) Unmute

func (stage *StageMute) Unmute(ctx context.Context, fingerprint string) error

Unmute 根据任务和 fingerprint 删除一个拦截规则 如果一个规则,要求的是屏蔽至异常恢复,那么这个规则就需要事件恢复时主动调用 Unmute 来清理 而不是根据 Deadline

func (*StageMute) UnmuteByID

func (stage *StageMute) UnmuteByID(ctx context.Context, id int64) error

UnmuteByID 删除一个屏蔽(拦截)规则

type StageSend

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

func (StageSend) Handle

func (stage StageSend) Handle(alert Alert) []Alert

Jump to

Keyboard shortcuts

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