slidingwindow

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2022 License: MIT Imports: 3 Imported by: 0

README

CrowdStrike Go Metrics Sliding Window

Sliding Window Sampling for go-metrics

Overview

This project contains a simple implementation of a sliding window sampling approach implementing the rcrowley/go-metrics Sample interface. The user can define a reservoir size, much like the default sampling approach, but also define a window size that is used to phase out sample values once they fall outside the window.

Rationale

The problem with the default exponential decay sampling approach is that it only expires old data as new data comes in. While this approach works perfectly fine for high-volume metrics, it is problematic for low-volume metrics and can lead to misleading graphs where the data is stale, but looks like it is recent. This issue is particularly troublesome when measuring metrics with a histogram (e.g., endpoint latency).

Ideally this contribution would be made to the go-metrics project itself, but the author has made it clear he would rather have others implement this themselves and leverage the exported interfaces.

Examples

package main

import (
	"time"
	
	"github.com/crowdstrike/go-metrics-sliding-window"
	"github.com/rcrowley/go-metrics"
)

func main() {
	// Creates a histogram using the sliding window sampling approach with a reservoir size of 1024 and a sampling window of 2 minutes.
	sample := slidingwindow.NewSample(1024, time.Minute*2)
	histogram := metrics.GetOrRegisterHistogram("histogram.latency", metrics.DefaultRegistry, sample)
	histogram.Update(1)

	// Uses the histogram with the sliding window sampling to create a timer.
	timer := metrics.NewCustomTimer(histogram, metrics.NewMeter())
	timer.Time(myFunc())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSample

func NewSample(reservoirSize int, window time.Duration) metrics.Sample

NewSample constructs a new sliding-window sample over the given time window. At the end of each window the metrics outside the window are removed. This is an ideal sampling method for low-volume metrics where sample values would stick around indefinitely because there was not a steady stream of new metrics. Please note that the reservoir size is the size of two in-memory slices of int64s and time. Times used to hold the sample values and times. Therefore, if a large reservoir size is used it can be an extremely inefficient use of memory.

Types

type Sample

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

Sample is a sliding window sample with a maximum reservoir size in the window.

func (*Sample) Clear

func (s *Sample) Clear()

Clear deletes all the sample values.

func (*Sample) Count

func (s *Sample) Count() int64

Count returns the number of sample values.

func (*Sample) Max

func (s *Sample) Max() int64

Max returns the maximum sample v.

func (*Sample) Mean

func (s *Sample) Mean() float64

Mean returns the mean of the values in the sample.

func (*Sample) Min

func (s *Sample) Min() int64

Min returns the minimum of the values in the sample.

func (*Sample) Percentile

func (s *Sample) Percentile(p float64) float64

Percentile returns an arbitrary percentile of values in the sample.

func (*Sample) Percentiles

func (s *Sample) Percentiles(ps []float64) []float64

Percentiles returns a slice of arbitrary percentiles of values in the sample.

func (*Sample) Size

func (s *Sample) Size() int

Size returns the size of the sample, which will not exceed the reservoir size.

func (*Sample) Snapshot

func (s *Sample) Snapshot() metrics.Sample

Snapshot returns a copy of the samples that is flushed to graphite.

func (*Sample) StdDev

func (s *Sample) StdDev() float64

StdDev returns the standard deviation of the values in the sample.

func (*Sample) Sum

func (s *Sample) Sum() int64

Sum returns the sum of the values in the sample.

func (*Sample) Update

func (s *Sample) Update(v int64)

Update adds a new value v to the samples.

func (*Sample) Values

func (s *Sample) Values() []int64

Values returns a copy of the values in the sample.

func (*Sample) Variance

func (s *Sample) Variance() float64

Variance returns the variance of the values in the sample.

Jump to

Keyboard shortcuts

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