healthcheck

package
v2.7.4 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultMinRequests is a default minimal threshold of total requests. If Counter has less than
	// this amount of requests total, it will be skipped because it can trigger false alerts otherwise.
	DefaultMinRequests = 10

	// DefaultFailureThreshold is a default value of successful requests that should be passed in order to suppress any
	// error notifications. If less than that percentage of requests are successful, the notification will be sent.
	DefaultFailureThreshold = 0.8
)
View Source
const DefaultResetPeriod = time.Minute * 15

DefaultResetPeriod is a default period for AtomicCounter after which internal request counters will be reset.

Variables

This section is empty.

Functions

func DefaultNotifyFunc

func DefaultNotifyFunc(apiURL, apiKey, msg string) error

Types

type AtomicCounter

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

AtomicCounter is a default Counter implementation. It uses atomics under the hood (hence the name) and can be configured with custom reset timeout and

func (*AtomicCounter) ClearCountersProcessed

func (a *AtomicCounter) ClearCountersProcessed()

func (*AtomicCounter) CountersProcessed

func (a *AtomicCounter) CountersProcessed()

func (*AtomicCounter) Failed

func (a *AtomicCounter) Failed(message string)

func (*AtomicCounter) FailureProcessed

func (a *AtomicCounter) FailureProcessed()

func (*AtomicCounter) FlushCounters

func (a *AtomicCounter) FlushCounters()

func (*AtomicCounter) HitFailure

func (a *AtomicCounter) HitFailure()

func (*AtomicCounter) HitSuccess

func (a *AtomicCounter) HitSuccess()

func (*AtomicCounter) IsCountersProcessed

func (a *AtomicCounter) IsCountersProcessed() bool

func (*AtomicCounter) IsFailed

func (a *AtomicCounter) IsFailed() bool

func (*AtomicCounter) IsFailureProcessed

func (a *AtomicCounter) IsFailureProcessed() bool

func (*AtomicCounter) Message

func (a *AtomicCounter) Message() string

func (*AtomicCounter) Name

func (a *AtomicCounter) Name() string

func (*AtomicCounter) SetName

func (a *AtomicCounter) SetName(name string)

func (*AtomicCounter) TotalFailed

func (a *AtomicCounter) TotalFailed() uint32

func (*AtomicCounter) TotalSucceeded

func (a *AtomicCounter) TotalSucceeded() uint32

type ConnectionDataProvider

type ConnectionDataProvider func(id int) (apiURL, apiKey, lang string, exists bool)

ConnectionDataProvider should return the connection credentials and language by counter ID. It's best to use account ID as a counter ID to be able to retrieve the necessary data as easy as possible.

type Counter

type Counter interface {
	// Name can be used as a more friendly identifier for the counter.
	Name() string
	// SetName of the counter.
	SetName(name string)
	// HitSuccess registers successful request. It should automatically clear error state because that state should be
	// used only if error is totally unrecoverable.
	HitSuccess()
	// HitFailure registers failed request.
	HitFailure()
	// TotalSucceeded returns how many requests were successful.
	TotalSucceeded() uint32
	// TotalFailed returns how many requests have failed.
	TotalFailed() uint32
	// Failed will put Counter into failed state with specific error message.
	Failed(message string)
	// IsFailed returns true if Counter is in failed state.
	IsFailed() bool
	// Message will return error message if Counter is in failed state.
	Message() string
	// IsFailureProcessed will return true if current error inside counter has been processed already.
	IsFailureProcessed() bool
	// FailureProcessed will mark current error inside Counter as processed.
	FailureProcessed()
	// IsCountersProcessed returns true if counters value has been processed by the checker.
	// This can be used if you want to process counter values only once.
	IsCountersProcessed() bool
	// CountersProcessed will mark current counters value as processed.
	CountersProcessed()
	// ClearCountersProcessed will set IsCountersProcessed to false.
	ClearCountersProcessed()
	// FlushCounters will reset request counters if deemed necessary (for example, AtomicCounter will clear counters
	// only if their contents are older than provided time period).
	// This won't clear IsCountersProcessed flag!
	FlushCounters()
}

Counter will count successful and failed requests. Its contents can be used to judge if specific entity (e.g. Connection / Account) is not working properly (invalid credentials, too many failed requests, etc) and take further action based on the result. Implementation should be goroutine-safe.

func NewAtomicCounter

func NewAtomicCounter(name string) Counter

NewAtomicCounter returns AtomicCounter with DefaultResetPeriod.

func NewAtomicCounterWithPeriod

func NewAtomicCounterWithPeriod(name string, resetPeriod time.Duration) Counter

NewAtomicCounterWithPeriod returns AtomicCounter configured with provided period.

type CounterConstructor

type CounterConstructor func(name string) Counter

CounterConstructor is used to create counters. This way you can implement your own counter and still use default CounterStorage.

type CounterProcessor

type CounterProcessor struct {
	Localizer              NotifyMessageLocalizer
	Logger                 logger.Logger
	Notifier               NotifyFunc
	ConnectionDataProvider ConnectionDataProvider
	Error                  string
	FailureThreshold       float64
	MinRequests            uint32
	Debug                  bool
}

CounterProcessor is a default implementation of Processor. It will try to localize the message in case of error.

func (CounterProcessor) Process

func (c CounterProcessor) Process(id int, counter Counter) bool

type NotifyFunc

type NotifyFunc func(apiURL, apiKey, msg string) error

NotifyFunc will send notification about error to the system with provided credentials. It will send the notification to system admins.

type NotifyMessageLocalizer

type NotifyMessageLocalizer interface {
	SetLocale(locale string)
	GetLocalizedTemplateMessage(messageID string, templateData map[string]interface{}) string
}

NotifyMessageLocalizer is the smallest subset of core.Localizer used in the Processor implementation.

type Processor

type Processor interface {
	// Process counter data. This method is not goroutine-safe!
	Process(id int, counter Counter) bool
}

Processor is used to check if Counter is in error state and act accordingly.

type Storage

type Storage interface {
	// Get counter by its ID. The counter will be instantiated automatically if necessary.
	// Name here is not used to identify the counter in the storage.
	Get(id int, name string) Counter
	// Remove counter if it exists.
	Remove(id int)
	// Process will iterate over counters and call Processor on each of them.
	// This method is used to collect counters data & send notifications.
	Process(processor Processor)
}

Storage stores different instances of Counter. Implementation should be goroutine-safe.

func NewSyncMapStorage

func NewSyncMapStorage(constructor CounterConstructor) Storage

NewSyncMapStorage is a SyncMapStorage constructor.

type SyncMapStorage

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

SyncMapStorage is a default Storage implementation. It uses sync.Map under the hood because deletions should be rare for the storage. If your business logic calls Remove often, it would be better to use your own implementation with map[int]Counter and sync.RWMutex.

func (*SyncMapStorage) Get

func (s *SyncMapStorage) Get(id int, name string) Counter

func (*SyncMapStorage) Process

func (s *SyncMapStorage) Process(proc Processor)

func (*SyncMapStorage) Remove

func (s *SyncMapStorage) Remove(id int)

Jump to

Keyboard shortcuts

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