latency

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2024 License: Apache-2.0 Imports: 3 Imported by: 3

Documentation

Overview

Package latency supports exporting latency stats (avg/max/min) of a set of time windows as metadata.

Index

Constants

View Source
const (
	// ElemLatency is the container of all latency metadata about a target.
	ElemLatency = "latency"
	// ElemWindow contains latency metadatas (avg, max, min) of a particular
	// window size.
	ElemWindow = "window"
	// ElemAvg is the average latency during a time window.
	ElemAvg = "avg"
	// ElemMax is the maximum latency during a time window.
	ElemMax = "max"
	// ElemMin is the minimum latency during a time window.
	ElemMin = "min"
)
View Source
const (
	// Avg is the average latency of a time window.
	Avg = StatType(iota)
	// Max is the maximum latency of a time window.
	Max
	// Min is the minimum latency of a time window.
	Min
)

Variables

View Source
var Now = time.Now

Now is a stub for testing.

Functions

func CompactDurationString

func CompactDurationString(d time.Duration) string

CompactDurationString returns a compact string for a time window d. It removes unnecessary suffixes like "0m0s" and "0s" from the Golang fmt.Sprint generated string of a time.Duration.

func MetadataName

func MetadataName(w time.Duration, typ StatType) string

MetadataName returns the metadata name for the latency statistics of window w and type typ.

func ParseWindows

func ParseWindows(tds []string, metaUpdatePeriod time.Duration) ([]time.Duration, error)

ParseWindows parses the time durations of latency windows and verify they are multiples of the metadata update period.

func Path

func Path(w time.Duration, typ StatType, prefix []string) []string

Path returns the metadata path for the latency statistics of window w and type typ.

Types

type Latency

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

Latency supports calculating and exporting latency stats for a specified set of time windows.

func New

func New(windowSizes []time.Duration, opts *Options) *Latency

New returns a Latency object supporting latency stats for time windows specified in windowSizes.

func (*Latency) Compute

func (l *Latency) Compute(ts time.Time)

Compute calculates the time difference between now and ts (the timestamp of an update) and updates the latency stats saved in Latency.

func (*Latency) UpdateLast

func (l *Latency) UpdateLast(m Metadata)

UpdateLast is the same as UpdateReset except that it doesn't require a window to meet the initial window size requirement to update its stats. UpdateLast is expected to be called at most once at the end of the life of Latency to force updating the windows that haven't received latency stats long enough to generate even a single update. For a long lasting Latency, there is no need to call UpdateLast.

func (*Latency) UpdateReset

func (l *Latency) UpdateReset(m Metadata)

UpdateReset use the latencies saved during the last interval to update the latency stats of all the supported time windows. And then it updates the corresponding stats in Metadata m. UpdateReset is expected to be called periodically at a fixed interval (e.g. 2s) of which the time windows should be multiples of this interval.

type Metadata

type Metadata interface {
	SetInt(name string, value int64) error
}

Metadata defines the interface required by latency metadata.

type Options

type Options struct {
	// Precision for the avg stats. If unspecified, the precision is nanoseconds.
	// The exported latency stats are always in nanoseconds no matter what
	// precision is set here. Setting precision at a more coarse time duration
	// than nanosecond is to avoid overflowing of int64 for the accumulated time
	// durations needed to calculate averages. The precision of the Max and Min
	// stats are not affected by this setting.
	AvgPrecision time.Duration
	// ComputeFunc defines how to calculate latency for the timestamp provided
	// to Compute. If unspecified, the latency is calculated as now.Sub(ts).
	ComputeFunc func(ts, now time.Time) time.Duration
}

Options contains the options for creating a Latency.

type StatType

type StatType int

StatType is the type of latency statistics supported for a time window.

func (StatType) String

func (st StatType) String() string

String returns the string representation of the StatType.

Jump to

Keyboard shortcuts

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