health

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2020 License: MIT Imports: 19 Imported by: 6

Documentation

Index

Constants

View Source
const (
	Blue   = State("blue")   // init or shut-down
	Green  = State("green")  // A-OK
	Yellow = State("yellow") // may need assistance
	Red    = State("red")    // HELP!
	Gray   = State("gray")   // maintenance mode
)
View Source
const DefaultFlushTimeout = 1000 // ms
View Source
const EventType = "health"

EventType value is "health"

View Source
const MaxKafkaHealthCheckInterval = time.Minute * 5
View Source
const SignalUrl = "https://signal.atsu.io/"
View Source
const StateDefault = Blue

StateDefault health is Blue

Variables

This section is empty.

Functions

func GetHostHealthBytes

func GetHostHealthBytes(now time.Time) []byte

Types

type Event

type Event struct {
	Hostname  string `json:"hostname"`  // Emitting host
	Timestamp int64  `json:"timestamp"` // Timestamp of event
	Type      string `json:"etype"`     // Type of event ( "health")
	Name      string `json:"event"`     // Name of event (e.g. status, startup, shutdown)
	Service   string `json:"service"`   // Operating service (e.g. gather)
	Version   string `json:"version"`   // Service version
	State     State  `json:"state"`     // Enumeration of health
	Message   string `json:"msg"`       // User actionable message

	Data interface{} `json:"data,omitempty"` // Service-specific data
}

Event is the basic format for all JSON-encoded Health Example of a json encoded health event

{"hostname":"host123", "timestamp":1559761560, "etype":"health", "event":"status",
 "service":"gather", "version":"v0.0.1", "state":"green", "msg":"a-ok",
 "data": { "testmode": "off", "kernel": true }}

type HealthMsg

type HealthMsg struct {
	Hostname  string `json:"hostname"`
	Timestamp int64  `json:"timestamp"`
	Etype     string `json:"etype"`
	Event     string `json:"event"`

	Build string `json:"build,omitempty"`
}

type HttpSignal

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

HttpSignal implements against our canonical endpoint SignalUrl

func NewHttpSignal

func NewHttpSignal(url string) *HttpSignal

func (*HttpSignal) RawReport

func (r *HttpSignal) RawReport(jsonPayload []byte) error

RawReport will POST the provided jsonPayload to SignalUrl

func (*HttpSignal) Report

func (r *HttpSignal) Report(e Event, i interface{}) error

Report will Marshal and send, or error

type IReporter

type IReporter interface {
	Initialize() (bool, error)
	Health() Event
	SetStdOutFallback(b bool)
	SetErrFn(efn func(err error))
	SetHealth(state State, message string)
	SetTopic(topic string)
	PersistStat(key string, val interface{})
	RegisterStatFn(name string, fn func(reporter IReporter))
	AddStat(key string, val interface{})
	GetStat(key string) interface{}
	ClearStat(key string)
	ClearStats()
	ClearStatFns()
	ClearAll()
	GetKafkaLogWriter() io.Writer
	KafkaHealthy() (bool, error)
	ReportHealth()
	AddHostnameSuffix(string)
	StartIntervalReporting(interval time.Duration)
	HealthHandler(w http.ResponseWriter, req *http.Request)
	Stop() error
	StopWithFinalState(final State, msg string) error
}

type Reporter

type Reporter struct {
	// Errfn is the callback function that fires whenever there is an error.
	Errfn func(err error)
	// contains filtered or unexported fields
}

Reporter is the front door for reporting

func NewReporter

func NewReporter(service, prefix, brokers string, errfn func(err error)) *Reporter

NewReporter create a new reporter

func (*Reporter) AddHostnameSuffix

func (r *Reporter) AddHostnameSuffix(suffix string)

func (*Reporter) AddStat

func (r *Reporter) AddStat(key string, val interface{})

AddStat adds a keyed entry to be included in reporting

func (*Reporter) ClearAll

func (r *Reporter) ClearAll()

ClearAll clears all stats and persistent stats

func (*Reporter) ClearStat

func (r *Reporter) ClearStat(key string)

ClearStat clears an individual stat by key, does not clear persistent stats

func (*Reporter) ClearStatFns

func (r *Reporter) ClearStatFns()

ClearStatFns clears the reporter functions

func (*Reporter) ClearStats

func (r *Reporter) ClearStats()

ClearStats clears all non persistent stats

func (*Reporter) GetKafkaLogWriter

func (r *Reporter) GetKafkaLogWriter() io.Writer

GetKafkaLogWriter returns an io.Writer which can be used to produce to the service log stream, *Note* if you use pass this io.Writer to log.SetOutput(), be careful not to call any log functions

in the error callback, otherwise you may cause a deadlock.

this stream is always `<prefix>.service.log`

func (*Reporter) GetStat

func (r *Reporter) GetStat(key string) interface{}

GetStat retrieve a previously entered stat or persistent stat. persistent stats will act as default stats.

func (*Reporter) Health

func (r *Reporter) Health() Event

Health returns the current health as a populated Event object

func (*Reporter) HealthHandler

func (r *Reporter) HealthHandler(w http.ResponseWriter, req *http.Request)

HealthHandler return the current health on demand

func (*Reporter) Initialize

func (r *Reporter) Initialize() (bool, error)

Initialize must be called to create a new producer If Initialize fails with a fatal, non-recoverable error, it returns false and the non-nil error. when false is returned, kafka monitoring is disabled because no producer could be created or found Otherwise it returns true and and a non-nil error only if kafka is unhealthy

func (*Reporter) JsonStats

func (r *Reporter) JsonStats() []byte

JsonStats returns the current set of stats as a json

func (*Reporter) KafkaHealthy

func (r *Reporter) KafkaHealthy() (bool, error)

KafkaHealthy returns true if kafka is healthy, false if it isn't, and the last error associated with the unhealthy state

func (*Reporter) PersistStat

func (r *Reporter) PersistStat(key string, val interface{})

PersistStat is akin to storing a stat, but a persistent stat is a stat that does not clear when Clear is called.

func (*Reporter) RegisterStatFn

func (r *Reporter) RegisterStatFn(name string, fn func(reporter IReporter))

RegisterStatFn registers a function to be called before each call of ReportHealth.

func (*Reporter) ReportHealth

func (r *Reporter) ReportHealth()

ReportHealth sends the current health to kafka

func (*Reporter) SetErrFn

func (r *Reporter) SetErrFn(efn func(err error))

func (*Reporter) SetHealth

func (r *Reporter) SetHealth(state State, message string)

SetHealth sets the current state and message

func (*Reporter) SetStdOutFallback

func (r *Reporter) SetStdOutFallback(b bool)

SetStdOutFallback defaults to false. When enabled, health messages will be written to std out if kafka is unhealthy or if no producer is set

func (*Reporter) SetTopic

func (r *Reporter) SetTopic(topic string)

SetTopic provides the ability to override the default topic. if never called, Initialize() will default the topic to `health.<service name>`

func (*Reporter) StartIntervalReporting

func (r *Reporter) StartIntervalReporting(interval time.Duration)

StartIntervalReporting starts automatic reporting over the given interval.

func (*Reporter) Stop

func (r *Reporter) Stop() error

Stop interval reporting and close the kafka producer, after calling stop, the health reporter should no longer be used. To re-start, you should create a new reporter calling this function will result in emitting a default shutdown state of Gray with an empty message if you want to set the final state or message, use StopWithFinalState

func (*Reporter) StopWithFinalState

func (r *Reporter) StopWithFinalState(final State, msg string) error

StopWithFinalState is the same as calling Stop() but takes in a final state and message, that will be emitted before shutdown.

type Signal

type Signal interface {
	Report(Event, interface{}) error

	RawReport([]byte) error
}

Signal is a mechanism for sending a health.Event and Data (interface{})

type State

type State string

State represents a visual indicator of the health

func (*State) Set

func (s *State) Set(h string) error

Set compiles with the Flag.Value interface

func (State) String

func (s State) String() string

Set compiles with the Flag.Value interface (and Stringer)

type VersionMsg

type VersionMsg struct {
	Version            string `json:"version"`
	Build              string `json:"build"`
	Hostname           string `json:"hostname"`
	Kernel             string `json:"kernel"`
	ModuleLoaded       bool   `json:"loaded"`
	Timestamp          int64  `json:"timestamp"`
	LastEventTimestamp int64  `json:"last"`
	Count              uint64 `json:"count"`
	EventsSkipped      int    `json:"skipped"`
	Health             State  `json:"health"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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