monitor

package
v2.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2021 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventCountKey

func EventCountKey() interface{}

EventCountKey returns the contextual logging key for the event count

func NopFilter

func NopFilter(i []string) []string

NopFilter does nothing. It returns the slice of instances as is.

Types

type DelayedListener

type DelayedListener struct {
	Listener  Listener
	Capacitor capacitor.Interface
}

DelayedListener is a decorator for Listener that uses a capacitor to implement a grace period between service discovery events.

func (DelayedListener) MonitorEvent

func (dl DelayedListener) MonitorEvent(e Event)

type Event

type Event struct {
	// Key is the in-process unique identifier for the sd.Instancer that produced this event.
	// For consul, this value does not equal the name of the service (as it includes tags, datacenters, etc.).
	// For that purpose, use the Service field.
	Key string

	// Service, unlike Key, specifically identifies the service of the sd.Instancer that produced this event.
	// This value is used by listeners to update metric labels.
	Service string

	// Instancer is the go-kit sd.Instancer which sent this event.  This instance can be used to enrich
	// logging via logging.Enrich.
	Instancer sd.Instancer

	// EventCount is the postive, ascending integer identifying this event's sequence, e.g. 1 refers to the first
	// service discovery event.  Useful for logging and certain types of logic, such as ignoring the initial instances from a monitor.
	EventCount int

	// Instances are the filtered instances that came from the sd.Instancer.  If this is set,
	// Err will be nil.
	Instances []string

	// Err is any service discovery error that occurred.  If this is set, Instances will be empty.
	Err error

	// Stopped is set to true if and only if this event is being sent to indicate the monitoring goroutine
	// has exited, either because of being explicitly stopped or because the environment was closed.
	Stopped bool
}

Event carries the same information as go-kit's sd.Event, but with the extra Key that identifies which filtered service or path was updated.

type Filter

type Filter func([]string) []string

Filter represents a preprocessing strategy for discovered service instances

func DefaultFilter

func DefaultFilter() Filter

DefaultFilter returns the global default Filter instance.

func NewNormalizeFilter

func NewNormalizeFilter(defaultScheme string) Filter

NewNormalizeFilter returns a Filter that uses service.NormalizeInstance to ensure that each instance is a valid URI with scheme and port (where applicable). The defaultScheme is used if an instance has no scheme, e.g. "localhost:8080".

type Interface

type Interface interface {
	// Stopped returns a channel that is closed when this Monitor is stopped.
	// Semantics are equivalent to context.Context.Done().
	Stopped() <-chan struct{}

	// Stop halts all goroutines that are dispatching events, but does not stop
	// or close the service discovery backend.  This method is idempotent.  Once stopped,
	// a Monitor cannot be reused.
	Stop()
}

Interface represents an active monitor for one or more sd.Instancer objects.

func New

func New(options ...Option) (Interface, error)

New begins monitoring one or more sd.Instancer objects, dispatching events to any Listeners that are configured. This function returns an error if i is empty or nil.

type Listener

type Listener interface {
	MonitorEvent(Event)
}

func NewAccessorListener

func NewAccessorListener(f service.AccessorFactory, next func(service.Accessor, error)) Listener

NewAccessorListener creates a service discovery Listener that dispatches accessor instances to a nested closure. Any error received from the event results in a nil Accessor together with that error being passed to the next closure. If the AccessorFactory is nil, DefaultAccessorFactory is used. If the next closure is nil, this function panics.

An UpdatableAccessor may directly be used to receive events by passing Update as the next closure:

ua := new(UpdatableAccessor)
l := NewAccessorListener(f, ua.Update)

func NewKeyAccessorListener

func NewKeyAccessorListener(f service.AccessorFactory, key string, next func(string, service.Accessor, error)) Listener

func NewMetricsListener

func NewMetricsListener(p provider.Provider) Listener

NewMetricsListener produces a monitor Listener that gathers metrics related to service discovery.

func NewRegistrarListener

func NewRegistrarListener(logger log.Logger, r sd.Registrar, initiallyRegistered bool) Listener

NewRegistrarListener binds service registration to the lifecycle of a service discovery watch. Upon the first successful update, or on any successful update following one or more errors, the given registrar is registered. Any error that follows a successful update, or on the first error, results in deregistration.

type ListenerFunc

type ListenerFunc func(Event)

func (ListenerFunc) MonitorEvent

func (lf ListenerFunc) MonitorEvent(e Event)

type Listeners

type Listeners []Listener

func (Listeners) MonitorEvent

func (ls Listeners) MonitorEvent(e Event)

type Option

type Option func(*monitor)

Option represents a configuration option for a monitor

func WithClosed

func WithClosed(c <-chan struct{}) Option

WithClosed sets an external channel that, when closed, will cause all goroutines spawned by this monitor to exit. This is useful when orchestrating multiple monitors, or when restarting service discovery clients.

func WithEnvironment

func WithEnvironment(e service.Environment) Option

func WithFilter

func WithFilter(f Filter) Option

WithFilter establishes the filtering strategy for discovered service instances. By default, TrimAndSortFilter is used. If the filter is nil, filtering is disabled and every Listener will receive the raw, unfiltered instances from the service discovery backend.

func WithInstancers

func WithInstancers(i service.Instancers) Option

WithInstancers establishes the set of sd.Instancer objects to be monitored. The given Instancers is copied to maintain the monitor's immutability.

func WithListeners

func WithListeners(l ...Listener) Option

WithListeners configures the monitor to dispatch to zero or more Listeners. It is legal to start a Monitor with no Listeners, as this is equivalent to just logging messages for the service discovery backend.

func WithLogger

func WithLogger(l log.Logger) Option

WithLogger sets a go-kit Logger for this monitor. This logger will be enriched with information about each instancer, if available. If nil, the default logger is used instead.

Jump to

Keyboard shortcuts

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