alert

package
v1.7.6 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2024 License: MIT Imports: 57 Imported by: 50

Documentation

Overview

Alert provides an implementation of the HTTP API for managing alert topics, handlers and events.

Responsibilities of this package include:

* Providing and HTTP API for the management of handlers * Storing handler definitions * Providing implementations of several simple handlers * Mapping external handler implementations to handler definitions

The last item needs some more clarification. Handlers can be implemented in external packages. In order for the HTTP API to consume those implementations a mapping needs to be defined from the HandlerAction definition to the expected configuration of the handler implementation. This package provides that mapping.

Index

Constants

View Source
const (
	TopicStoreVersionKey   = "topic_store_version"
	TopicStoreVersion2     = "2"
	TopicStoreBackupSuffix = ".v1.bak"
)
View Source
const (

	// The storage namespace V1 topic store and task data.
	// In V2, still stores handlers
	AlertNameSpace = "alert_store"
	// TopicStatesNameSpace - The storage namespace for the V2 topic store and nothing else
	TopicStatesNameSpace = "topic_states_store"
)
View Source
const (
	DefaultShutdownTimeout = toml.Duration(time.Second * 10)
)

Variables

View Source
var (
	ErrHandlerSpecExists   = errors.New("handler spec already exists")
	ErrNoHandlerSpecExists = errors.New("no handler spec exists")
)
View Source
var ErrHandlerDIsabled = errors.New("handler disabled")
View Source
var (
	ErrNoTopicStateExists = errors.New("no topic state exists")
)

Functions

func CopyFile added in v1.7.0

func CopyFile(src, dest string) (n int64, err error)

func DeleteV2TopicStore added in v1.7.0

func DeleteV2TopicStore(topicsStore storage.Interface) error

func LoadTopicBucket added in v1.7.0

func LoadTopicBucket(tx storage.ReadOnlyTx, topic []byte) (map[string]EventState, error)

func MigrateTopicStoreV2V1 added in v1.7.0

func MigrateTopicStoreV2V1(storageService StorageService) error

func NewTopicStateKV added in v1.7.0

func NewTopicStateKV(store storage.Interface) (*topicStateKV, error)

func WalkTopicBuckets added in v1.7.0

func WalkTopicBuckets(topicsStore storage.Interface, fn func(tx storage.ReadOnlyTx, topic string) error) error

Types

type AggregateHandlerConfig

type AggregateHandlerConfig struct {
	ID       string        `mapstructure:"id"`
	Interval time.Duration `mapstructure:"interval"`
	Topic    string        `mapstructure:"topic"`
	Message  string        `mapstructure:"message"`
	// contains filtered or unexported fields
}

type AnonHandlerRegistrar added in v1.3.0

type AnonHandlerRegistrar interface {
	// RegisterHandler registers the handler instance for the listed topics.
	RegisterAnonHandler(topic string, h alert.Handler)
	// DeregisterHandler removes the handler from the listed topics.
	DeregisterAnonHandler(topic string, h alert.Handler)
}

AnonHandlerRegistrar is responsible for directly registering handlers for anonymous topics. This is to be used only when the origin of the handler is not defined by a handler spec.

type Config

type Config struct {
	// Whether we persist the alert topics to BoltDB or not
	PersistTopics     bool `toml:"persist-topics"`
	TopicBufferLength int  `toml:"topic-buffer-length"`
}

func NewConfig

func NewConfig() Config

func (Config) Validate added in v1.5.2

func (c Config) Validate() error

type Diagnostic added in v1.4.0

type Diagnostic interface {
	WithHandlerContext(ctx ...keyvalue.T) HandlerDiagnostic

	MigratingHandlerSpecs()
	FoundHandlerRows(length int)
	FoundNewHandler(key string)
	CreatingNewHandlers(length int)
	MigratingOldHandlerSpec(id string)

	Error(msg string, err error, ctx ...keyvalue.T)
	Info(msg string, ctx ...keyvalue.T)
}

type EventCollector added in v1.3.0

type EventCollector interface {
	// Collect accepts a new event for processing.
	Collect(event alert.Event) error
}

type EventState

type EventState struct {
	Message  string        `json:"message,omitempty"`
	Details  string        `json:"details,omitempty"`
	Time     time.Time     `json:"time,omitempty"`
	Duration time.Duration `json:"duration,omitempty"`
	Level    alert.Level   `json:"level"`
}

func (*EventState) AlertEventState added in v1.7.0

func (e *EventState) AlertEventState(id string) *alert.EventState

func (EventState) MarshalEasyJSON added in v1.5.1

func (v EventState) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (EventState) MarshalJSON added in v1.5.1

func (v EventState) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (*EventState) Reset added in v1.7.0

func (e *EventState) Reset()

func (*EventState) UnmarshalEasyJSON added in v1.5.1

func (v *EventState) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*EventState) UnmarshalJSON added in v1.5.1

func (v *EventState) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type Events added in v1.3.0

type Events interface {
	EventCollector
	// UpdateEvent updates an existing event with a previously known state.
	UpdateEvent(topic string, event alert.EventState) error
	// EventState returns the current events state.
	EventState(topic, event string) (alert.EventState, bool, error)
}

Events is responsible for accepting events for processing and reporting on the state of events.

type ExecHandlerConfig

type ExecHandlerConfig struct {
	Prog      string            `mapstructure:"prog"`
	Args      []string          `mapstructure:"args"`
	Commander command.Commander `mapstructure:"-"`
}

type HandlerDiagnostic added in v1.4.0

type HandlerDiagnostic interface {
	Error(msg string, err error, ctx ...keyvalue.T)
}

type HandlerSpec

type HandlerSpec struct {
	ID      string                 `json:"id"`
	Topic   string                 `json:"topic"`
	Kind    string                 `json:"kind"`
	Options map[string]interface{} `json:"options"`
	Match   string                 `json:"match"`
}

HandlerSpec provides all the necessary information to create a handler.

func (HandlerSpec) MarshalBinary

func (h HandlerSpec) MarshalBinary() ([]byte, error)

func (HandlerSpec) ObjectID

func (h HandlerSpec) ObjectID() string

func (*HandlerSpec) UnmarshalBinary

func (h *HandlerSpec) UnmarshalBinary(data []byte) error

func (HandlerSpec) Validate

func (h HandlerSpec) Validate() error

type HandlerSpecDAO

type HandlerSpecDAO interface {
	// Retrieve a handler
	Get(topic, id string) (HandlerSpec, error)
	GetTx(tx storage.ReadOperator, topic, id string) (HandlerSpec, error)

	// Create a handler.
	// ErrHandlerSpecExists is returned if a handler already exists with the same ID.
	Create(h HandlerSpec) error
	CreateTx(tx storage.Tx, h HandlerSpec) error

	// Replace an existing handler.
	// ErrNoHandlerSpecExists is returned if the handler does not exist.
	Replace(h HandlerSpec) error
	ReplaceTx(tx storage.Tx, h HandlerSpec) error

	// Delete a handler.
	// It is not an error to delete an non-existent handler.
	Delete(topic, id string) error
	DeleteTx(tx storage.Tx, topic, id string) error

	// List handlers matching a pattern.
	// The pattern is shell/glob matching see https://golang.org/pkg/path/#Match
	// Offset and limit are pagination bounds. Offset is inclusive starting at index 0.
	// More results may exist while the number of returned items is equal to limit.
	List(topic, pattern string, offset, limit int) ([]HandlerSpec, error)
	ListTx(tx storage.ReadOperator, topic, pattern string, offset, limit int) ([]HandlerSpec, error)

	Rebuild() error
}

Data access object for HandlerSpec data.

type HandlerSpecRegistrar added in v1.3.0

type HandlerSpecRegistrar interface {
	// RegisterHandlerSpec saves the handler spec and registers a handler defined by the spec
	RegisterHandlerSpec(spec HandlerSpec) error
	// DeregisterHandlerSpec deletes the handler spec and deregisters the defined handler.
	DeregisterHandlerSpec(topic, id string) error
	// UpdateHandlerSpec updates the old spec with the new spec and takes care of registering new handlers based on the new spec.
	UpdateHandlerSpec(oldSpec, newSpec HandlerSpec) error
	// HandlerSpec returns a handler spec
	HandlerSpec(topic, id string) (HandlerSpec, bool, error)
	// Handlers returns a list of handler specs that match the pattern.
	HandlerSpecs(topic, pattern string) ([]HandlerSpec, error)
}

HandlerSpecRegistrar is responsible for registering and persisting handler spec definitions.

type InhibitorLookup added in v1.5.0

type InhibitorLookup interface {
	IsInhibited(name string, tags models.Tags) bool
	AddInhibitor(*alert.Inhibitor)
	RemoveInhibitor(*alert.Inhibitor)
}

InhibitorLookup provides lookup access to inhibitors

type LogHandlerConfig

type LogHandlerConfig struct {
	Path string      `mapstructure:"path"`
	Mode os.FileMode `mapstructure:"mode"`
}

func DefaultLogHandlerConfig

func DefaultLogHandlerConfig() LogHandlerConfig

func (LogHandlerConfig) Validate

func (c LogHandlerConfig) Validate() error

type PublishHandlerConfig

type PublishHandlerConfig struct {
	Topics []string `mapstructure:"topics"`
	// contains filtered or unexported fields
}

type Service

type Service struct {
	PersistTopics bool

	APIServer *apiServer

	EventCollector EventCollector

	HTTPDService interface {
		AddRoutes([]httpd.Route) error
		DelRoutes([]httpd.Route)
	}

	StorageService StorageService

	Commander command.Commander

	AlertaService interface {
		DefaultHandlerConfig() alerta.HandlerConfig
		Handler(alerta.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	BigPandaService interface {
		Handler(bigpanda.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	HipChatService interface {
		Handler(hipchat.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	KafkaService interface {
		Handler(kafka.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	MQTTService interface {
		Handler(mqtt.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	OpsGenieService interface {
		Handler(opsgenie.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	OpsGenie2Service interface {
		Handler(opsgenie2.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	PagerDutyService interface {
		Handler(pagerduty.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	PagerDuty2Service interface {
		Handler(pagerduty2.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	PushoverService interface {
		Handler(pushover.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	HTTPPostService interface {
		Handler(httppost.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	SensuService interface {
		Handler(sensu.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	SlackService interface {
		Handler(slack.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	DiscordService interface {
		Handler(discord.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	SMTPService interface {
		Handler(smtp.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	SNMPTrapService interface {
		Handler(snmptrap.HandlerConfig, ...keyvalue.T) (alert.Handler, error)
	}
	TalkService interface {
		Handler(...keyvalue.T) alert.Handler
	}
	TelegramService interface {
		Handler(telegram.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	VictorOpsService interface {
		Handler(victorops.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	TeamsService interface {
		Handler(teams.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	ServiceNowService interface {
		Handler(servicenow.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	ZenossService interface {
		Handler(zenoss.HandlerConfig, ...keyvalue.T) alert.Handler
	}
	// contains filtered or unexported fields
}

func NewService

func NewService(d Diagnostic, disabled map[string]struct{}, topicBufLen int) *Service

func (*Service) AddInhibitor added in v1.5.0

func (s *Service) AddInhibitor(in *alert.Inhibitor)

func (*Service) Close

func (s *Service) Close() error

func (*Service) CloseTopic

func (s *Service) CloseTopic(topic string) error

func (*Service) Collect

func (s *Service) Collect(event alert.Event) error

func (*Service) DeleteTopic

func (s *Service) DeleteTopic(topic string) error

func (*Service) DeregisterAnonHandler added in v1.3.0

func (s *Service) DeregisterAnonHandler(topic string, h alert.Handler)

func (*Service) DeregisterHandlerSpec

func (s *Service) DeregisterHandlerSpec(topic, handler string) error

func (*Service) EventState

func (s *Service) EventState(topic, event string) (alert.EventState, bool, error)

EventState returns the current state of the event.

func (*Service) EventStates added in v1.3.0

func (s *Service) EventStates(topic string, minLevel alert.Level) (map[string]alert.EventState, error)

EventStates returns the current state of events for the specified topic. Only events greater or equal to minLevel will be returned

func (*Service) HandlerSpec added in v1.3.0

func (s *Service) HandlerSpec(topic, handler string) (HandlerSpec, bool, error)

func (*Service) HandlerSpecs added in v1.3.0

func (s *Service) HandlerSpecs(topic, pattern string) ([]HandlerSpec, error)

func (*Service) IsInhibited added in v1.5.0

func (s *Service) IsInhibited(name string, tags models.Tags) bool

func (*Service) MigrateTopicStoreV1V2 added in v1.7.0

func (s *Service) MigrateTopicStoreV1V2() (rErr error)

MigrateTopicStoreV1V2 - Convert a V1 to a V2 topic store. Also ensures that a topic store has a V2 version number set.

func (*Service) Open

func (s *Service) Open() error

func (*Service) RegisterAnonHandler added in v1.3.0

func (s *Service) RegisterAnonHandler(topic string, h alert.Handler)

func (*Service) RegisterHandlerSpec

func (s *Service) RegisterHandlerSpec(spec HandlerSpec) error

func (*Service) RemoveInhibitor added in v1.5.0

func (s *Service) RemoveInhibitor(in *alert.Inhibitor)

func (*Service) RestoreTopic

func (s *Service) RestoreTopic(topic string) error

func (*Service) TopicState added in v1.3.0

func (s *Service) TopicState(topic string) (alert.TopicState, bool, error)

TopicState returns the state for the specified topic.

func (*Service) TopicStates added in v1.3.0

func (s *Service) TopicStates(pattern string, minLevel alert.Level) (map[string]alert.TopicState, error)

TopicStates returns the max alert level for each topic matching 'pattern', not returning any topics with max alert levels less severe than 'minLevel'

func (*Service) UpdateEvent

func (s *Service) UpdateEvent(topic string, event alert.EventState) error

func (*Service) UpdateHandlerSpec added in v1.3.0

func (s *Service) UpdateHandlerSpec(oldSpec, newSpec HandlerSpec) error

type StorageService added in v1.7.0

type StorageService interface {
	Store(namespace string) storage.Interface
	Register(name string, store storage.StoreActioner)
	Versions() storage.Versions
	Diagnostic() storage.Diagnostic
	Path() string
	CloseBolt() error
}

type TCPHandlerConfig

type TCPHandlerConfig struct {
	Address string `mapstructure:"address"`
}

type TopicPersister added in v1.3.0

type TopicPersister interface {
	// CloseTopic closes a topic but does not delete its state.
	CloseTopic(topic string) error
	// DeleteTopic closes a topic and deletes all state associated with the topic.
	DeleteTopic(topic string) error
	// RestoreTopic signals that a topic should be restored from persisted state.
	RestoreTopic(topic string) error
}

TopicPersister is responsible for controlling the persistence of topic state.

type TopicState

type TopicState struct {
	Topic       string                `json:"topic"`
	EventStates map[string]EventState `json:"event-states"`
}

func (TopicState) MarshalBinary

func (t TopicState) MarshalBinary() ([]byte, error)

func (TopicState) MarshalEasyJSON added in v1.5.1

func (v TopicState) MarshalEasyJSON(w *jwriter.Writer)

MarshalEasyJSON supports easyjson.Marshaler interface

func (TopicState) MarshalJSON added in v1.5.1

func (v TopicState) MarshalJSON() ([]byte, error)

MarshalJSON supports json.Marshaler interface

func (TopicState) ObjectID

func (t TopicState) ObjectID() string

func (*TopicState) UnmarshalBinary

func (t *TopicState) UnmarshalBinary(data []byte) error

func (*TopicState) UnmarshalEasyJSON added in v1.5.1

func (v *TopicState) UnmarshalEasyJSON(l *jlexer.Lexer)

UnmarshalEasyJSON supports easyjson.Unmarshaler interface

func (*TopicState) UnmarshalJSON added in v1.5.1

func (v *TopicState) UnmarshalJSON(data []byte) error

UnmarshalJSON supports json.Unmarshaler interface

type TopicStateDAO

type TopicStateDAO interface {
	Get(id string) (TopicState error)
	Put(t TopicState) error
	Replace(t TopicState) error
	Delete(id string) error
	List(pattern string, offset, limit int) ([]TopicState, error)
	Rebuild() error
	DeleteMultiple(keys []string) error
}

type Topics added in v1.3.0

type Topics interface {
	// TopicState returns the state of the specified topic,
	TopicState(topic string) (alert.TopicState, bool, error)
	// TopicStates returns the state of all topics that match the pattern and have at least minLevel.
	TopicStates(pattern string, minLevel alert.Level) (map[string]alert.TopicState, error)

	// EventState returns the current state of the event.
	EventState(topic, event string) (alert.EventState, bool, error)
	// EventStates returns the current state of events for the specified topic.
	// Only events greater or equal to minLevel will be returned
	EventStates(topic string, minLevel alert.Level) (map[string]alert.EventState, error)
}

Topics is responsible for querying the state of topics and their events.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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