monitors

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckIfMonitorRegistered

func CheckIfMonitorRegistered(name string) bool

CheckIfMonitorRegistered checks if a monitor is registered by name.

func GetRegisteredMonitorNames

func GetRegisteredMonitorNames() []string

GetRegisteredMonitorNames returns a slice of strings containing the names of all the registered monitors

func RegisterMonitor

func RegisterMonitor(monitorType types.MonitorType, initFunc func(configBody []byte, logger Logger, jitterFactor int, prometheusMetricsEnabled bool) (Monitor, error)) error

RegisterMonitor takes a monitor type and a function that returns a monitor and adds it to a map of monitors

Types

type BaseMonitor

type BaseMonitor struct {
	Name                     MonitorName
	Type                     types.MonitorType
	Description              string
	Group                    string
	Logger                   Logger
	Interval                 time.Duration
	TimeOut                  time.Duration
	JitterFactor             int
	Enabled                  bool
	StopChannel              chan bool
	Semaphore                *semaphore.Weighted
	InitOnce                 sync.Once
	NotifyRate               rate.Limit
	PrometheusMetricsEnabled bool
	PrometheusMetrics        *metrics.PrometheusMetrics
	NotifyLimiter            *rate.Limiter
	State                    *State
	MaxConcurrentRequests    int
	MaxRetries               int
	RetryCounter             int
	Process                  func() error
}

BaseMonitor is the base monitor struct that all monitors should be composed of

func (*BaseMonitor) GetDescription

func (m *BaseMonitor) GetDescription() string

GetDescription returns the description of the monitor

func (*BaseMonitor) GetGroup

func (m *BaseMonitor) GetGroup() string

GetGroup returns the group for the monitor

func (*BaseMonitor) GetInterval

func (m *BaseMonitor) GetInterval() time.Duration

GetInterval returns the interval for the monitor

func (*BaseMonitor) GetLogger

func (m *BaseMonitor) GetLogger() Logger

GetLogger returns the logger for the monitor

func (*BaseMonitor) GetName

func (m *BaseMonitor) GetName() MonitorName

GetName returns the name of the monitor

func (*BaseMonitor) GetState

func (m *BaseMonitor) GetState() *State

GetState returns the state of the monitor

func (*BaseMonitor) GetTimeOut

func (m *BaseMonitor) GetTimeOut() time.Duration

GetTimeOut returns the timeout for the monitor

func (*BaseMonitor) GetType

func (m *BaseMonitor) GetType() types.MonitorType

GetType returns the type of the monitor

func (*BaseMonitor) HandleFailure

func (m *BaseMonitor) HandleFailure(err error) error

HandleFailure handles a failure

func (*BaseMonitor) Initialize

func (m *BaseMonitor) Initialize() error

Initialize the base monitor Call this method in all Init() methods of all monitors that are composed of BaseMonitor

func (*BaseMonitor) IsEnabled

func (m *BaseMonitor) IsEnabled() bool

IsEnabled returns the enabled flag for the monitor

func (*BaseMonitor) ResetNotifyLimiter

func (m *BaseMonitor) ResetNotifyLimiter()

ResetNotifyLimiter resets the notify limiter for the monitor

func (*BaseMonitor) Run

func (m *BaseMonitor) Run(ctx context.Context) error

Run starts the monitor

func (*BaseMonitor) SetDescription

func (m *BaseMonitor) SetDescription(description string)

SetDescription sets the description of the monitor

func (*BaseMonitor) SetEnablePrometheusMetrics

func (m *BaseMonitor) SetEnablePrometheusMetrics(enableMetrics bool)

SetEnablePrometheusMetrics sets the enable metrics flag for the monitor

func (*BaseMonitor) SetEnabled

func (m *BaseMonitor) SetEnabled(enabled bool)

SetEnabled sets the enabled flag for the monitor

func (*BaseMonitor) SetGroup

func (m *BaseMonitor) SetGroup(group string)

SetGroup sets the group for the monitor

func (*BaseMonitor) SetInterval

func (m *BaseMonitor) SetInterval(interval time.Duration)

SetInterval sets the interval for the monitor

func (*BaseMonitor) SetJitterFactor

func (m *BaseMonitor) SetJitterFactor(jitterFactor int)

SetJitterFactor sets the jitter factor for the monitor

func (*BaseMonitor) SetLogger

func (m *BaseMonitor) SetLogger(logger Logger)

SetLogger sets the logger for the monitor

func (*BaseMonitor) SetMaxConcurrentRequests

func (m *BaseMonitor) SetMaxConcurrentRequests(maxConcurrentRequests int)

SetMaxConcurrentRequests sets the max concurrent requests for the monitor

func (*BaseMonitor) SetMaxRetries

func (m *BaseMonitor) SetMaxRetries(maxRetries int)

SetMaxRetries sets the max retries for the monitor

func (*BaseMonitor) SetName

func (m *BaseMonitor) SetName(name MonitorName)

SetName sets the name of the monitor

func (*BaseMonitor) SetNotifyRateLimit

func (m *BaseMonitor) SetNotifyRateLimit(notifyRateLimit time.Duration)

SetNotifyRateLimit sets the notify rate limit for the monitor

func (*BaseMonitor) SetProcess

func (m *BaseMonitor) SetProcess(p MonitorProcess)

SetProcess is the main process for the monitor

func (*BaseMonitor) SetTimeOut

func (m *BaseMonitor) SetTimeOut(timeOut time.Duration)

SetTimeOut sets the timeout for the monitor

func (*BaseMonitor) SetType

func (m *BaseMonitor) SetType(monitorType types.MonitorType)

SetType sets the type of the monitor

func (*BaseMonitor) Stop

func (m *BaseMonitor) Stop()

Stop stops the monitor

type JSONBaseConfig

type JSONBaseConfig struct {
	Name                  MonitorName    `json:"name"`
	Description           string         `json:"description"`
	Group                 string         `json:"group"`
	Enabled               bool           `json:"enabled"`
	Interval              utils.Duration `json:"interval"`
	Timeout               utils.Duration `json:"timeout"`
	MaxConcurrentRequests int            `json:"maxConcurrentRequests"`
	MaxRetries            int            `json:"maxRetries"`
	NotifyRateLimit       utils.Duration `json:"notifyRateLimit"`
}

JSONBaseConfig is the base JSON config for all monitors

func (*JSONBaseConfig) Validate

func (m *JSONBaseConfig) Validate() error

Validate validates the config for the Port monitor

type Logger

type Logger interface {
	// Info logs an info message
	Info(msg ...interface{})
	// Infof logs an info formatted string
	Infof(format string, a ...interface{})
	// Debug logs an debug message
	Debug(msg ...interface{})
	// Debugf logs an debug formatted string
	Debugf(format string, a ...interface{})
	// Warn logs a warn message
	Warn(msg ...interface{})
	// Warnf logs a warn message
	Warnf(format string, v ...interface{})
	// Error logs an error with the message
	Error(err error, msg ...interface{})
	// Errorf logs an error with the formatted string
	Errorf(err error, format string, a ...interface{})
	// GetLevel returns the current log level
	GetLevel() log.LogLevel
	// SetLevel sets the log level
	SetLevel(level log.LogLevel)
}

Logger is the interface of the logger for the monitor

type Monitor

type Monitor interface {
	// Init initializes the monitor
	Init() error
	// Run starts the monitor
	Run(ctx context.Context) error
	// SetProcess sets the process for the monitor
	// Have to use this method to pass Process function to embedded struct
	SetProcess(p MonitorProcess)
	// Stop stops the monitor
	Stop()
	// GetName returns the name of the monitor
	GetName() MonitorName
	// SetName sets the name of the monitor
	SetName(name MonitorName)
	// Group returns the group of the monitor
	GetGroup() string
	// SetGroup sets the group of the monitor
	SetGroup(group string)
	// GetDescription returns the description of the monitor
	GetDescription() string
	// SetDescription sets the description of the monitor
	SetDescription(description string)
	// SetType sets the type of the monitor
	SetType(monitorType types.MonitorType)
	// GetType returns the type of the monitor
	GetType() types.MonitorType
	// SetConfig sets the config for the monitor
	SetConfig(config interface{}) error
	// GetConfig returns the config for the monitor
	GetConfig() interface{}
	// SetLogger sets the logger for the monitor
	SetLogger(logger Logger)
	// GetLogger returns the logger for the monitor
	GetLogger() Logger
	// SetInterval sets the interval for the monitor
	SetInterval(interval time.Duration)
	// GetInterval returns the interval for the monitor
	GetInterval() time.Duration
	// SetEnabled sets the enabled flag for the monitor
	SetEnabled(enabled bool)
	// SetJitterFactor sets the jitter factor for the monitor
	SetJitterFactor(jitterFactor int)
	// IsEnabled returns the enabled flag for the monitor
	IsEnabled() bool
	// SetMaxConcurrentRequests sets the max concurrent requests for the monitor
	SetMaxConcurrentRequests(maxConcurrentRequests int)
	// SetTimeOut sets the timeout for the monitor
	SetTimeOut(timeOut time.Duration)
	// GetTimeOut returns the timeout for the monitor
	GetTimeOut() time.Duration
	// SetMaxRetries sets the max retries for the monitor
	SetMaxRetries(maxRetries int)
	// SetNotifyRateLimit sets the notify rate limit for the monitor
	SetNotifyRateLimit(notifyRateLimit time.Duration)
	// GetState returns the state of the monitor
	GetState() *State
	// SetEnablePrometheusMetrics sets the metrics enabled flag for the monitor
	SetEnablePrometheusMetrics(enablePrometheusMetrics bool)
	// GetNotificationBody returns the error notification body
	GetNotificationBody(state *State) *NotificationBody
}

Monitor is an interface that all monoitors must implement

func GetMonitor

func GetMonitor(name string, configBody []byte, logger Logger, jitterFactor int, prometheusMetricsEnabled bool) (Monitor, error)

GetMonitor takes a monitor name, a config body, a logger, and a boolean indicating whether metrics are enabled, and returns a monitor and an error

type MonitorName

type MonitorName string

MonitorName is a string that represents the name of the monitor

func (MonitorName) String

func (m MonitorName) String() string

type MonitorProcess

type MonitorProcess func() error

MonitorProcess is the process for the monitor

type NotificationBody

type NotificationBody struct {
	Name     MonitorName       `json:"name"`
	Type     types.MonitorType `json:"type"`
	EndPoint string            `json:"endPoint"`
	Time     time.Time         `json:"time"`
	Status   StateStatus       `json:"status"`
	Error    error             `json:"error,omitempty"`
}

NotificationBody is the body of the notification

func (*NotificationBody) GetEndPoint

func (n *NotificationBody) GetEndPoint() string

GetEndPoint returns the end point for the state from NotificationBody

func (*NotificationBody) GetError

func (n *NotificationBody) GetError() error

GetError returns the error for the state from NotificationBody

func (*NotificationBody) GetErrorString

func (n *NotificationBody) GetErrorString() string

GetErrorString returns the error string for the state from NotificationBody

func (*NotificationBody) GetName

func (n *NotificationBody) GetName() MonitorName

GetName returns the name of the monitor from NotificationBody

func (*NotificationBody) GetNameString

func (n *NotificationBody) GetNameString() string

GetNameString returns the name of the monitor from NotificationBody

func (*NotificationBody) GetStatus

func (n *NotificationBody) GetStatus() StateStatus

GetStatus returns the status of the state from NotificationBody

func (*NotificationBody) GetStatusString

func (n *NotificationBody) GetStatusString() string

GetStatusString returns the status string for the state from NotificationBody

func (*NotificationBody) GetStatusWithIcon

func (n *NotificationBody) GetStatusWithIcon() string

GetStatusWithIcon returns the current state with icon from NotificationBody

func (*NotificationBody) GetTime

func (n *NotificationBody) GetTime() time.Time

GetTime returns the time of the state change from NotificationBody

func (*NotificationBody) GetType

func (n *NotificationBody) GetType() types.MonitorType

GetType returns the type of the monitor from NotificationBody

func (*NotificationBody) GetTypeString

func (n *NotificationBody) GetTypeString() string

GetTypeString returns the type of the monitor from NotificationBody

type State

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

State is the state of the monitor

func (*State) Get

func (s *State) Get() *State

Get returns the current state of the monitor

func (*State) GetCurrent

func (s *State) GetCurrent() StateStatus

GetCurrent returns the current state

func (*State) GetError

func (s *State) GetError() error

GetError returns the error for the state

func (*State) GetErrorString

func (s *State) GetErrorString() string

GetErrorString returns the error string for the state

func (*State) GetPrevious

func (s *State) GetPrevious() StateStatus

GetPrevious returns the previous state

func (*State) GetStateChangeTime

func (s *State) GetStateChangeTime() time.Time

GetStateChangeTime returns the state change time

func (*State) Init

func (s *State) Init(current StateStatus, previous StateStatus, stateCHangeTime time.Time)

Init initializes the state

func (*State) IsCurrentStateAFinalState

func (s *State) IsCurrentStateAFinalState() bool

IsCurrentStateAFinalState returns true if the current state is a final state

func (*State) IsCurrentStatusDOWN

func (s *State) IsCurrentStatusDOWN() bool

IsCurrentStatusDOWN returns if the current status down

func (*State) IsCurrentStatusUP

func (s *State) IsCurrentStatusUP() bool

IsCurrentStatusUP returns if the current status up

func (*State) IsPreviousStateAFinalState

func (s *State) IsPreviousStateAFinalState() bool

IsPreviousStateAFinalState returns true if the previous state is a final state

func (*State) IsPreviousStatusDOWN

func (s *State) IsPreviousStatusDOWN() bool

IsPreviousStatusDOWN returns if the previous status error

func (*State) IsPreviousStatusUP

func (s *State) IsPreviousStatusUP() bool

IsPreviousStatusUP returns if the previous status ok

func (*State) Subscribe

func (s *State) Subscribe(id string, subscriber func(s *State))

Subscribe subscribes to state updates

func (*State) UnSubscribe

func (s *State) UnSubscribe(id string)

UnSubscribe unsubscribes the subscriber

func (*State) Update

func (s *State) Update(newState StateStatus, err error) error

Update updates the state of the monitor

type StateStatus

type StateStatus string

StateStatus is the state of the monitor

const (
	// StateStatusUP indicates that the monitor is in a healthy state
	StateStatusUP StateStatus = "UP"
	// StateStatusDOWN indicates that the monitor is in a down state
	StateStatusDOWN StateStatus = "DOWN"
	// StateStatusINIT indicates that the monitor is in a initializing state
	StateStatusINIT StateStatus = "INIT"
	// StateStatusSTARTING indicates that the monitor is in a initializing state
	StateStatusSTARTING StateStatus = "STARTING"
)

func (StateStatus) String

func (s StateStatus) String() string

String returns the string representation of the state

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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