timings

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: BSD-3-Clause Imports: 14 Imported by: 27

README

go-timings

Go package implementing interface and methods for background timing monitors

Documentation

Go Reference

Exmaple

import (
       "context"
       "github.com/sfomuseum/go-timings"
       "os"       
)

func main() {

	ctx := context.Background()
	
	monitor, _ := timings.NewMonitor(ctx, "counter://PT60S")

	monitor.Start(ctx, os.Stderr)
	defer monitor.Stop(ctx)

	monitor.Signal(ctx)	// increments by 1
}

Documentation

Overview

package timings provides an interface and methods for background timing monitors

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterMonitor added in v1.0.0

func RegisterMonitor(ctx context.Context, scheme string, init_func MonitorInitializationFunc) error

RegisterMonitor registers 'scheme' as a key pointing to 'init_func' in an internal lookup table used to create new `Monitor` instances by the `NewMonitor` method.

func Schemes added in v1.0.0

func Schemes() []string

Schemes returns the list of schemes that have been registered.

Types

type CounterMonitor

type CounterMonitor struct {
	Monitor
	// contains filtered or unexported fields
}

type CounterMonitor implements the `Monitor` interface providing a background timings mechanism that tracks incrementing events.

func (*CounterMonitor) Signal

func (t *CounterMonitor) Signal(ctx context.Context, args ...interface{}) error

Signal will cause the background monitors counter to be incremented by one.

func (*CounterMonitor) Start

func (t *CounterMonitor) Start(ctx context.Context, wr io.Writer) error

Start() will cause background monitoring to begin, dispatching notifications to wr.

func (*CounterMonitor) Stop

func (t *CounterMonitor) Stop(ctx context.Context) error

Stop() will cause background monitoring to be halted.

type Monitor

type Monitor interface {
	// The Start method starts the monitor dispatching notifications to an io.Writer instance
	Start(context.Context, io.Writer) error
	// The Stop method will stop monitoring
	Stop(context.Context) error
	// The Signal method will dispatch messages to the monitoring process
	Signal(context.Context, ...interface{}) error
}

type Monitor provides a common interface for timings-based monitors

func NewCounterMonitor

func NewCounterMonitor(ctx context.Context, uri string) (Monitor, error)

NewCounterMonitor creates a new `CounterMonitor` instance that will dispatch notifications using a time.Ticker configured by 'uri' which is expected to take the form of:

counter://?duration={ISO8601_DURATION}

Where {ISO8601_DURATION} is a valid ISO8601 duration string.

func NewMonitor added in v1.0.0

func NewMonitor(ctx context.Context, uri string) (Monitor, error)

NewMonitor returns a new `Monitor` instance configured by 'uri'. The value of 'uri' is parsed as a `url.URL` and its scheme is used as the key for a corresponding `MonitorInitializationFunc` function used to instantiate the new `Monitor`. It is assumed that the scheme (and initialization function) have been registered by the `RegisterMonitor` method.

func NewNullMonitor added in v1.0.0

func NewNullMonitor(ctx context.Context, uri string) (Monitor, error)

NewNullMonitor() creates a new `NullMonitor` which does nothing.

null://

func NewSinceMonitor added in v1.1.0

func NewSinceMonitor(ctx context.Context, uri string) (Monitor, error)

NewSinceMonitor creates a new `SinceMonitor` instance that will dispatch notifications using a time.Ticker configured by 'uri' which is expected to take the form of:

since://

type MonitorInitializationFunc added in v1.0.0

type MonitorInitializationFunc func(ctx context.Context, uri string) (Monitor, error)

MonitorInitializationFunc is a function defined by individual monitor package and used to create an instance of that monitor

type NullMonitor added in v1.0.0

type NullMonitor struct {
	Monitor
}

type NullMonitor implements the `Monitor` interface but does nothing.

func (*NullMonitor) Signal added in v1.0.0

func (nm *NullMonitor) Signal(ctx context.Context, args ...interface{}) error

Signal() is a no-op and nothing will happen.

func (*NullMonitor) Start added in v1.0.0

func (nm *NullMonitor) Start(ctx context.Context, wr io.Writer) error

Start() will cause monitoring to begin, which in this means nothing will happen and no events will be written to 'wr'.

func (*NullMonitor) Stop added in v1.0.0

func (nm *NullMonitor) Stop(ctx context.Context) error

Stop() will cause monitoring to be halted.

type SinceEvent added in v1.2.0

type SinceEvent uint8
const (
	SinceStart SinceEvent = iota
	SinceStop
)

type SinceMonitor added in v1.1.0

type SinceMonitor struct {
	Monitor
	// contains filtered or unexported fields
}

type SinceMonitor implements the `Monitor` interface providing a background timings mechanism that tracks the duration of time between events.

func (*SinceMonitor) Signal added in v1.1.0

func (t *SinceMonitor) Signal(ctx context.Context, args ...interface{}) error

Signal will cause the background monitors since to be incremented by one.

func (*SinceMonitor) Start added in v1.1.0

func (t *SinceMonitor) Start(ctx context.Context, wr io.Writer) error

Start() will cause background monitoring to begin, dispatching notifications to wr in the form of JSON-encoded `SinceResponse` values.

func (*SinceMonitor) Stop added in v1.1.0

func (t *SinceMonitor) Stop(ctx context.Context) error

Stop() will cause background monitoring to be halted.

type SinceResponse added in v1.1.0

type SinceResponse struct {
	// Label is a string that was included with a `Signal` event
	Label string `json:"message"`
	// Duration is the string representation of a `time.Duuration` which is the amount of time that elapsed between `Signal` events
	Duration string `json:"duration"`
	// Timestamp is the Unix timestamp when the `SinceResponse` was created
	Timestamp int64 `json:"timestamp"`
}

SinceResponse is a struct containing information related to a "since" timing event.

func (SinceResponse) String added in v1.2.1

func (s SinceResponse) String() string

String() returns a string representation of the response.

Jump to

Keyboard shortcuts

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