checks

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidConfig = errors.New("invalid config")
View Source
var RegisteredChecks = map[string]func() Check{
	"health":  NewHealthCheck,
	"latency": NewLatencyCheck,
}

RegisteredChecks will be registered in this map The key is the name of the Check The name needs to map the configuration item key

Functions

func OpenapiFromPerfData

func OpenapiFromPerfData[T any](data T) (*openapi3.SchemaRef, error)

Takes in check perfdata and returns an openapi3.SchemaRef of a result wrapping the perfData this is a workaround, since the openapi3gen.NewSchemaRefForValue function does not work with any types

Types

type Check

type Check interface {
	// Run is called once per check interval
	// this should error if there is a problem running the check
	// Returns an error and a result. Returning a non nil error will cause a shutdown of the system
	Run(ctx context.Context) error
	// Startup is called once when the check is registered
	// In the Run() method, the check should send results to the cResult channel
	// this will cause sparrow to update its data store with the results
	Startup(ctx context.Context, cResult chan<- Result) error
	// Shutdown is called once when the check is unregistered or sparrow shuts down
	Shutdown(ctx context.Context) error
	// SetConfig is called once when the check is registered
	// This is also called while the check is running, if the remote config is updated
	// This should return an error if the config is invalid
	SetConfig(ctx context.Context, config any) error
	// SetClient sets an HTTP client for the check. This method is used to configure
	// the check with a specific HTTP client, which can be used for network requests
	// during the check's execution
	SetClient(c *http.Client)
	// Schema returns an openapi3.SchemaRef of the result type returned by the check
	Schema() (*openapi3.SchemaRef, error)
	// RegisterHandler Allows the check to register a handler on sparrows http server at runtime
	RegisterHandler(ctx context.Context, router *api.RoutingTree)
	// DeregisterHandler allows the check to deregister a handler on sparrows http server at runtime
	DeregisterHandler(ctx context.Context, router *api.RoutingTree)
	// GetMetricCollectors allows the check to provide prometheus metric collectors
	GetMetricCollectors() []prometheus.Collector
}

func NewHealthCheck

func NewHealthCheck() Check

NewHealthCheck creates a new HealthCheck

func NewLatencyCheck

func NewLatencyCheck() Check

type CheckMock added in v0.1.2

type CheckMock struct {
	// DeregisterHandlerFunc mocks the DeregisterHandler method.
	DeregisterHandlerFunc func(ctx context.Context, router *api.RoutingTree)

	// GetMetricCollectorsFunc mocks the GetMetricCollectors method.
	GetMetricCollectorsFunc func() []prometheus.Collector

	// RegisterHandlerFunc mocks the RegisterHandler method.
	RegisterHandlerFunc func(ctx context.Context, router *api.RoutingTree)

	// RunFunc mocks the Run method.
	RunFunc func(ctx context.Context) error

	// SchemaFunc mocks the Schema method.
	SchemaFunc func() (*openapi3.SchemaRef, error)

	// SetClientFunc mocks the SetClient method.
	SetClientFunc func(c *http.Client)

	// SetConfigFunc mocks the SetConfig method.
	SetConfigFunc func(ctx context.Context, config any) error

	// ShutdownFunc mocks the Shutdown method.
	ShutdownFunc func(ctx context.Context) error

	// StartupFunc mocks the Startup method.
	StartupFunc func(ctx context.Context, cResult chan<- Result) error
	// contains filtered or unexported fields
}

CheckMock is a mock implementation of Check.

func TestSomethingThatUsesCheck(t *testing.T) {

	// make and configure a mocked Check
	mockedCheck := &CheckMock{
		DeregisterHandlerFunc: func(ctx context.Context, router *api.RoutingTree)  {
			panic("mock out the DeregisterHandler method")
		},
		GetMetricCollectorsFunc: func() []prometheus.Collector {
			panic("mock out the GetMetricCollectors method")
		},
		RegisterHandlerFunc: func(ctx context.Context, router *api.RoutingTree)  {
			panic("mock out the RegisterHandler method")
		},
		RunFunc: func(ctx context.Context) error {
			panic("mock out the Run method")
		},
		SchemaFunc: func() (*openapi3.SchemaRef, error) {
			panic("mock out the Schema method")
		},
		SetClientFunc: func(c *http.Client)  {
			panic("mock out the SetClient method")
		},
		SetConfigFunc: func(ctx context.Context, config any) error {
			panic("mock out the SetConfig method")
		},
		ShutdownFunc: func(ctx context.Context) error {
			panic("mock out the Shutdown method")
		},
		StartupFunc: func(ctx context.Context, cResult chan<- Result) error {
			panic("mock out the Startup method")
		},
	}

	// use mockedCheck in code that requires Check
	// and then make assertions.

}

func (*CheckMock) DeregisterHandler added in v0.1.2

func (mock *CheckMock) DeregisterHandler(ctx context.Context, router *api.RoutingTree)

DeregisterHandler calls DeregisterHandlerFunc.

func (*CheckMock) DeregisterHandlerCalls added in v0.1.2

func (mock *CheckMock) DeregisterHandlerCalls() []struct {
	Ctx    context.Context
	Router *api.RoutingTree
}

DeregisterHandlerCalls gets all the calls that were made to DeregisterHandler. Check the length with:

len(mockedCheck.DeregisterHandlerCalls())

func (*CheckMock) GetMetricCollectors added in v0.1.2

func (mock *CheckMock) GetMetricCollectors() []prometheus.Collector

GetMetricCollectors calls GetMetricCollectorsFunc.

func (*CheckMock) GetMetricCollectorsCalls added in v0.1.2

func (mock *CheckMock) GetMetricCollectorsCalls() []struct {
}

GetMetricCollectorsCalls gets all the calls that were made to GetMetricCollectors. Check the length with:

len(mockedCheck.GetMetricCollectorsCalls())

func (*CheckMock) RegisterHandler added in v0.1.2

func (mock *CheckMock) RegisterHandler(ctx context.Context, router *api.RoutingTree)

RegisterHandler calls RegisterHandlerFunc.

func (*CheckMock) RegisterHandlerCalls added in v0.1.2

func (mock *CheckMock) RegisterHandlerCalls() []struct {
	Ctx    context.Context
	Router *api.RoutingTree
}

RegisterHandlerCalls gets all the calls that were made to RegisterHandler. Check the length with:

len(mockedCheck.RegisterHandlerCalls())

func (*CheckMock) Run added in v0.1.2

func (mock *CheckMock) Run(ctx context.Context) error

Run calls RunFunc.

func (*CheckMock) RunCalls added in v0.1.2

func (mock *CheckMock) RunCalls() []struct {
	Ctx context.Context
}

RunCalls gets all the calls that were made to Run. Check the length with:

len(mockedCheck.RunCalls())

func (*CheckMock) Schema added in v0.1.2

func (mock *CheckMock) Schema() (*openapi3.SchemaRef, error)

Schema calls SchemaFunc.

func (*CheckMock) SchemaCalls added in v0.1.2

func (mock *CheckMock) SchemaCalls() []struct {
}

SchemaCalls gets all the calls that were made to Schema. Check the length with:

len(mockedCheck.SchemaCalls())

func (*CheckMock) SetClient added in v0.1.2

func (mock *CheckMock) SetClient(c *http.Client)

SetClient calls SetClientFunc.

func (*CheckMock) SetClientCalls added in v0.1.2

func (mock *CheckMock) SetClientCalls() []struct {
	C *http.Client
}

SetClientCalls gets all the calls that were made to SetClient. Check the length with:

len(mockedCheck.SetClientCalls())

func (*CheckMock) SetConfig added in v0.1.2

func (mock *CheckMock) SetConfig(ctx context.Context, config any) error

SetConfig calls SetConfigFunc.

func (*CheckMock) SetConfigCalls added in v0.1.2

func (mock *CheckMock) SetConfigCalls() []struct {
	Ctx    context.Context
	Config any
}

SetConfigCalls gets all the calls that were made to SetConfig. Check the length with:

len(mockedCheck.SetConfigCalls())

func (*CheckMock) Shutdown added in v0.1.2

func (mock *CheckMock) Shutdown(ctx context.Context) error

Shutdown calls ShutdownFunc.

func (*CheckMock) ShutdownCalls added in v0.1.2

func (mock *CheckMock) ShutdownCalls() []struct {
	Ctx context.Context
}

ShutdownCalls gets all the calls that were made to Shutdown. Check the length with:

len(mockedCheck.ShutdownCalls())

func (*CheckMock) Startup added in v0.1.2

func (mock *CheckMock) Startup(ctx context.Context, cResult chan<- Result) error

Startup calls StartupFunc.

func (*CheckMock) StartupCalls added in v0.1.2

func (mock *CheckMock) StartupCalls() []struct {
	Ctx     context.Context
	CResult chan<- Result
}

StartupCalls gets all the calls that were made to Startup. Check the length with:

len(mockedCheck.StartupCalls())

type Health

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

Health is a check that measures the availability of an endpoint

func (*Health) DeregisterHandler

func (h *Health) DeregisterHandler(_ context.Context, router *api.RoutingTree)

DeregisterHandler dynamically deletes the server handler

func (*Health) GetMetricCollectors added in v0.1.2

func (h *Health) GetMetricCollectors() []prometheus.Collector

GetMetricCollectors returns all metric collectors of check

func (*Health) RegisterHandler

func (h *Health) RegisterHandler(ctx context.Context, router *api.RoutingTree)

RegisterHandler dynamically registers a server handler if it is enabled by the config

func (*Health) Run

func (h *Health) Run(ctx context.Context) error

Run starts the health check

func (*Health) Schema

func (h *Health) Schema() (*openapi3.SchemaRef, error)

Schema provides the schema of the data that will be provided by the heath check

func (*Health) SetClient added in v0.1.1

func (h *Health) SetClient(_ *http.Client)

SetClient sets the http client for the health check

func (*Health) SetConfig

func (h *Health) SetConfig(_ context.Context, config any) error

SetConfig sets the configuration for the health check

func (*Health) Shutdown

func (h *Health) Shutdown(_ context.Context) error

Shutdown is called once when the check is unregistered or sparrow shuts down

func (*Health) Startup

func (h *Health) Startup(_ context.Context, cResult chan<- Result) error

Startup is called once when the health check is registered

type HealthConfig

type HealthConfig struct {
	Enabled        bool     `json:"enabled,omitempty"`
	Targets        []string `json:"targets,omitempty"`
	HealthEndpoint bool     `json:"healthEndpoint,omitempty"`
}

HealthConfig contains the health check config

type Latency

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

func (*Latency) DeregisterHandler

func (l *Latency) DeregisterHandler(_ context.Context, router *api.RoutingTree)

func (*Latency) GetMetricCollectors added in v0.1.2

func (h *Latency) GetMetricCollectors() []prometheus.Collector

GetMetricCollectors returns all metric collectors of check

func (*Latency) Handler

func (l *Latency) Handler(w http.ResponseWriter, _ *http.Request)

func (*Latency) RegisterHandler

func (l *Latency) RegisterHandler(_ context.Context, router *api.RoutingTree)

func (*Latency) Run

func (l *Latency) Run(ctx context.Context) error

func (*Latency) Schema

func (l *Latency) Schema() (*openapi3.SchemaRef, error)

func (*Latency) SetClient added in v0.1.1

func (l *Latency) SetClient(c *http.Client)

SetClient sets the http client for the latency check

func (*Latency) SetConfig

func (l *Latency) SetConfig(_ context.Context, config any) error

func (*Latency) Shutdown

func (l *Latency) Shutdown(_ context.Context) error

func (*Latency) Startup

func (l *Latency) Startup(ctx context.Context, cResult chan<- Result) error

type LatencyConfig

type LatencyConfig struct {
	Targets  []string
	Interval time.Duration
	Timeout  time.Duration
	Retry    helper.RetryConfig
}

type LatencyResult

type LatencyResult struct {
	Code  int     `json:"code"`
	Error *string `json:"error"`
	Total float64 `json:"total"`
}

type Result

type Result struct {
	// data contains performance metrics about the check run
	Data any `json:"data"`
	// Timestamp is the UTC time the check was run
	Timestamp time.Time `json:"timestamp"`
	// Err should be nil if the check ran successfully indicating the check is "healthy"
	// if the check failed, this should be an error message that will be logged and returned to an API user
	Err string `json:"error"`
}

type ResultDTO

type ResultDTO struct {
	Name   string
	Result *Result
}

type Target

type Target struct {
	Target string `json:"target"`
	Status string `json:"status"`
}

Jump to

Keyboard shortcuts

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