telemetry

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2020 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Overview

Example

Example is an example of a receiver and sender

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

var group errgroup.Group

receiver, err := telemetry.Listen("127.0.0.1:0")
if err != nil {
	log.Fatal(err)
}

// receiver
group.Go(func() (err error) {
	defer func() { err = errs.Combine(err, receiver.Close()) }()
	err = receiver.Serve(ctx, telemetry.HandlerFunc(
		func(application, instance string, key []byte, val float64) {
			fmt.Printf("receive %s %s %s %v\n", application, instance, string(key), val)
		},
	))
	if err == context.Canceled {
		err = nil
	}
	return err
})

// sender
group.Go(func() error {
	client, err := telemetry.NewClient(zap.L(), receiver.Addr(), telemetry.ClientOpts{
		Interval:      time.Second,
		Application:   "example",
		Instance:      telemetry.DefaultInstanceID(),
		Registry:      monkit.Default,
		FloatEncoding: admproto.Float32Encoding,
	})
	if err != nil {
		return err
	}

	client.Run(ctx)
	return nil
})

if err := group.Wait(); err != nil {
	fmt.Println(err)
}
Output:

Index

Examples

Constants

View Source
const (
	// DefaultInterval is the default amount of time between metric payload sends
	DefaultInterval = time.Minute

	// DefaultPacketSize sets the target packet size. MTUs are often 1500,
	// though a good argument could be made for 512
	DefaultPacketSize = 1000

	// DefaultApplication is the default values for application name. Should be used
	// when value in ClientOpts.Application is not set and len(os.Args) == 0
	DefaultApplication = "unknown"
)

Variables

View Source
var Error = errs.Class("telemetry error")

Error is the default telemetry errs class

Functions

func DefaultInstanceID

func DefaultInstanceID() string

DefaultInstanceID will return the first non-nil mac address if possible, unknown otherwise.

func ListenAndServe

func ListenAndServe(ctx context.Context, addr string, h Handler) error

ListenAndServe combines Listen and Serve

Types

type Client

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

Client is a telemetry client for sending UDP packets at a regular interval from a monkit.Registry

func NewClient

func NewClient(log *zap.Logger, remoteAddr string, opts ClientOpts) (rv *Client, err error)

NewClient constructs a telemetry client that sends packets to remoteAddr over UDP.

func (*Client) Report

func (c *Client) Report(ctx context.Context) (err error)

Report bundles up all the current stats and writes them out as UDP packets

func (*Client) Run

func (c *Client) Run(ctx context.Context)

Run calls Report roughly every Interval

type ClientOpts

type ClientOpts struct {
	// Interval is how frequently stats from the provided Registry will be
	// sent up. Note that this interval is "jittered", so the actual interval
	// is taken from a normal distribution with a mean of Interval and a
	// variance of Interval/4. Defaults to DefaultInterval
	Interval time.Duration

	// Application is the application name, usually prepended to metric names.
	// By default it will be os.Args[0]
	Application string

	// Instance is a string that identifies this particular server. Could be a
	// node id, but defaults to the result of DefaultInstanceId()
	Instance string

	// PacketSize controls how we fragment the data as it goes out in UDP
	// packets. Defaults to DefaultPacketSize
	PacketSize int

	// Registry is where to get stats from. Defaults to monkit.Default
	Registry *monkit.Registry

	// FloatEncoding is how floats should be encoded on the wire.
	// Default is float16.
	FloatEncoding admproto.FloatEncoding
}

ClientOpts allows you to set Client Options

type Handler

type Handler interface {
	Metric(application, instance string, key []byte, val float64)
}

Handler is called every time a new metric comes in

type HandlerFunc

type HandlerFunc func(application, instance string, key []byte, val float64)

HandlerFunc turns a func into a Handler

func (HandlerFunc) Metric

func (f HandlerFunc) Metric(a, i string, k []byte, v float64)

Metric implements the Handler interface

type Server

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

Server listens for incoming metrics

func Listen

func Listen(addr string) (*Server, error)

Listen will start listening on addr for metrics

func (*Server) Addr

func (s *Server) Addr() string

Addr returns the address the server is serving on

func (*Server) Close

func (s *Server) Close() error

Close will stop listening

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, h Handler) error

Serve will wait for metrics and call Handler h as they come in

Jump to

Keyboard shortcuts

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