health

package
v2.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package health provides a simple heartbeat strategy for WebPA/XMiDT services.

Deprecated: health is no longer planned to be used by future WebPA/XMiDT services.

This package is frozen and no new functionality will be added.

Index

Constants

View Source
const (
	// DefaultMemoryReaderLocation is the default location for meminfo
	// under Linux
	DefaultMemoryReaderLocation string = "/proc/meminfo"
)

Variables

View Source
var (

	// Invalid stat option error
	ErrorInvalidOption = errors.New("Invalid stat option")
)

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher interface {
	SendEvent(HealthFunc)
}

Dispatcher represents a sink for Health events

type Health

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

Health is the central type of this package. It defines and endpoint for tracking and updating various statistics. It also dispatches events to one or more StatsListeners at regular intervals.

func New

func New(interval time.Duration, logger *zap.Logger, options ...Option) *Health

New creates a Health object with the given statistics.

func (*Health) AddStatsListener

func (h *Health) AddStatsListener(listener StatsListener)

AddStatsListener adds a new listener to this Health. This method is asynchronous. The listener will eventually receive events, but callers should not assume events will be dispatched immediately after this method call.

func (*Health) RequestTracker

func (h *Health) RequestTracker(delegate http.Handler) http.Handler

RequestTracker is an Alice-style constructor that wraps the given delegate in request-tracking code.

func (*Health) Run

func (h *Health) Run(waitGroup *sync.WaitGroup, shutdown <-chan struct{}) error

Run executes this Health object. This method is idempotent: once a Health object is Run, it cannot be Run again.

func (*Health) SendEvent

func (h *Health) SendEvent(healthFunc HealthFunc)

SendEvent dispatches a HealthFunc to the internal event queue

func (*Health) ServeHTTP

func (h *Health) ServeHTTP(response http.ResponseWriter, request *http.Request)

type HealthFunc

type HealthFunc func(Stats)

HealthFunc functions are allowed to modify the passed-in stats.

func Ensure

func Ensure(stat Stat) HealthFunc

Ensure makes certain the given stat is defined. If it does not exist, it is initialized to 0. Otherwise, the existing stat value is left intact.

func Inc

func Inc(stat Stat, value int) HealthFunc

Inc increments the given stat by a certain amount

func Set

func Set(stat Stat, value int) HealthFunc

Set changes (or, initializes) the stat to the given value

func (HealthFunc) Set

func (f HealthFunc) Set(stats Stats)

type MemInfoReader

type MemInfoReader struct {
	Location string
}

MemInfoReader handles extracting the linux memory information from the enclosing environment.

func (*MemInfoReader) Read

func (reader *MemInfoReader) Read() (memInfo *linux.MemInfo, err error)

Read parses the configured Location as if it were a linux meminfo file.

type Monitor

type Monitor interface {
	Dispatcher

	// HACK HACK HACK
	// This should be moved to another package
	ServeHTTP(http.ResponseWriter, *http.Request)
}

Monitor is the basic interface implemented by health event sinks

type Option

type Option interface {
	Set(Stats)
}

Option describes an option that can be set on a Stats map. Various types implement this interface.

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	// contains filtered or unexported fields
}

ResponseWriter is a wrapper type for an http.ResponseWriter that exposes the status code.

func Wrap

func Wrap(delegate http.ResponseWriter) *ResponseWriter

Wrap returns a *health.ResponseWriter which wraps the given http.ResponseWriter

func (*ResponseWriter) CloseNotify

func (r *ResponseWriter) CloseNotify() <-chan bool

CloseNotify delegates to the wrapped ResponseWriter, panicking if the delegate does not implement http.CloseNotifier.

func (*ResponseWriter) Flush

func (r *ResponseWriter) Flush()

Flush delegates to the wrapped ResponseWriter. If the delegate ResponseWriter does not implement http.Flusher, this method does nothing.

func (*ResponseWriter) Hijack

func (r *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack delegates to the wrapped ResponseWriter, returning an error if the delegate does not implement http.Hijacker.

func (*ResponseWriter) Push

func (r *ResponseWriter) Push(target string, opts *http.PushOptions) error

Push delegates to the wrapper ResponseWriter, returning an error if the delegate does not implement http.Pusher.

func (*ResponseWriter) StatusCode

func (r *ResponseWriter) StatusCode() int

func (*ResponseWriter) WriteHeader

func (r *ResponseWriter) WriteHeader(statusCode int)

type Stat

type Stat string

Stat is a named piece of data to be tracked

const (
	// General memory stats
	CurrentMemoryUtilizationAlloc     Stat = "CurrentMemoryUtilizationAlloc"
	CurrentMemoryUtilizationHeapSys   Stat = "CurrentMemoryUtilizationHeapSys"
	CurrentMemoryUtilizationActive    Stat = "CurrentMemoryUtilizationActive"
	MaxMemoryUtilizationAlloc         Stat = "MaxMemoryUtilizationAlloc"
	MaxMemoryUtilizationHeapSys       Stat = "MaxMemoryUtilizationHeapSys"
	MaxMemoryUtilizationActive        Stat = "MaxMemoryUtilizationActive"
	TotalRequestsReceived             Stat = "TotalRequestsReceived"
	TotalRequestsSuccessfullyServiced Stat = "TotalRequestsSuccessfullyServiced"
	TotalRequestsDenied               Stat = "TotalRequestsDenied"
)

func (Stat) Set

func (s Stat) Set(stats Stats)

Create/Set the stat initially

type Stats

type Stats map[Stat]int

Stats is mapping of Stat to value

func NewStats

func NewStats(options []Option) (s Stats)

NewStats constructs a Stats object preinitialized with the internal default statistics plus the given options.

func (Stats) Apply

func (s Stats) Apply(options []Option)

Apply invokes each Option.Set() on this stats map.

func (Stats) Clone

func (s Stats) Clone() Stats

Clone returns a distinct copy of this Stats object

func (Stats) Set

func (s Stats) Set(stats Stats)

func (Stats) UpdateMemInfo

func (s Stats) UpdateMemInfo(memInfo *linux.MemInfo)

UpdateMemInfo takes memory information from a linux environment and sets the appropriate stats.

func (Stats) UpdateMemStats

func (s Stats) UpdateMemStats(memStats *runtime.MemStats)

UpdateMemStats takes a MemStats from the golang runtime and sets the appropriate stats.

func (Stats) UpdateMemory

func (s Stats) UpdateMemory(memInfoReader *MemInfoReader)

UpdateMemory updates all the memory statistics

type StatsListener

type StatsListener interface {
	// OnStats is called with a copy of the health's stats map
	// at regular intervals.
	OnStats(Stats)
}

StatsListener receives Stats on regular intervals.

type StatsListenerFunc

type StatsListenerFunc func(Stats)

StatsListenerFunc is a function type that implements StatsListener.

func (StatsListenerFunc) OnStats

func (f StatsListenerFunc) OnStats(stats Stats)

Jump to

Keyboard shortcuts

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