cache

package
v0.0.0-...-05f6ae7 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Cache consists of an LRU cache and the evicted items from the LRU cache. This data structure makes sure all the cached items can be retrieved either from the LRU cache or the evictedItems map. In redmetricsconnector's use case, we need to hold all the items during the current processing step for building the metrics. The evicted items can/should be safely removed once the metrics are built from the current batch of spans.

Important: This implementation is non-thread safe.

func NewCache

func NewCache[K comparable, V any](size int) (*Cache[K, V], error)

NewCache creates a Cache.

func (*Cache[K, V]) Add

func (c *Cache[K, V]) Add(key K, value V) bool

Add a value to the cache, returns true if an eviction occurred and updates the "recently used"-ness of the key.

func (*Cache[K, V]) Contains

func (c *Cache[K, V]) Contains(key K) bool

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

Get an item from the LRU cache or evicted items.

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

Len returns the number of items in the cache.

func (*Cache[K, V]) RemoveEvictedItems

func (c *Cache[K, V]) RemoveEvictedItems()

RemoveEvictedItems cleans all the evicted items.

type Histogram

type Histogram interface {
	Update(value float64)
	Read() HistogramValues
}

func NewHistogram

func NewHistogram(metricType MetricsType, latencyBounds []float64) Histogram

type HistogramValues

type HistogramValues interface {
	GetBuckets() map[string]uint64
	GetBucketCounts() []uint64
	GetKey() string
	GetCount() uint64
	GetSum() float64
}

type MetricsType

type MetricsType string
const (
	MetricsProm MetricsType = "prom"
	MetricsVm   MetricsType = "vm"
)

func GetMetricsType

func GetMetricsType(metricsType string) MetricsType

type PromHistogram

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

func NewPromHistogram

func NewPromHistogram(latencyBounds []float64) *PromHistogram

func (*PromHistogram) GetBucketCounts

func (values *PromHistogram) GetBucketCounts() []uint64

func (*PromHistogram) GetBuckets

func (values *PromHistogram) GetBuckets() map[string]uint64

func (*PromHistogram) GetCount

func (values *PromHistogram) GetCount() uint64

func (*PromHistogram) GetKey

func (values *PromHistogram) GetKey() string

func (*PromHistogram) GetSum

func (values *PromHistogram) GetSum() float64

func (*PromHistogram) Read

func (h *PromHistogram) Read() HistogramValues

func (*PromHistogram) Update

func (h *PromHistogram) Update(v float64)

type ReusedKeyValue

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

func NewReusedKeyValue

func NewReusedKeyValue(maxAttribute int) *ReusedKeyValue

func (*ReusedKeyValue) Add

func (kv *ReusedKeyValue) Add(key string, value string) *ReusedKeyValue

func (*ReusedKeyValue) GetMap

func (kv *ReusedKeyValue) GetMap() pcommon.Map

func (*ReusedKeyValue) GetValue

func (kv *ReusedKeyValue) GetValue() string

func (*ReusedKeyValue) Reset

func (kv *ReusedKeyValue) Reset()

type VmHistogram

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

Histogram is a histogram for non-negative values with automatically created buckets.

See https://medium.com/@valyala/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350

Each bucket contains a counter for values in the given range. Each non-empty bucket is exposed via the following metric:

<metric_name>_bucket{<optional_tags>,vmrange="<start>...<end>"} <counter>

Where:

  • <metric_name> is the metric name passed to NewHistogram
  • <optional_tags> is optional tags for the <metric_name>, which are passed to NewHistogram
  • <start> and <end> - start and end values for the given bucket
  • <counter> - the number of hits to the given bucket during Update* calls

Histogram buckets can be converted to Prometheus-like buckets with `le` labels with `prometheus_buckets(<metric_name>_bucket)` function from PromQL extensions in VictoriaMetrics. (see https://docs.victoriametrics.com/metricsql/ ):

prometheus_buckets(request_duration_bucket)

Time series produced by the Histogram have better compression ratio comparing to Prometheus histogram buckets with `le` labels, since they don't include counters for all the previous buckets.

Zero histogram is usable.

func NewVmHistogram

func NewVmHistogram() *VmHistogram

NewHistogram creates and returns new histogram with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

func (*VmHistogram) GetSum

func (h *VmHistogram) GetSum() float64

func (*VmHistogram) Merge

func (h *VmHistogram) Merge(src *VmHistogram)

Merge merges src to h

func (*VmHistogram) Read

func (h *VmHistogram) Read() HistogramValues

func (*VmHistogram) Reset

func (h *VmHistogram) Reset()

Reset resets the given histogram.

func (*VmHistogram) Update

func (h *VmHistogram) Update(v float64)

Update updates h with v.

Negative values and NaNs are ignored.

func (*VmHistogram) UpdateDuration

func (h *VmHistogram) UpdateDuration(startTime time.Time)

UpdateDuration updates request duration based on the given startTime.

func (*VmHistogram) VisitNonZeroBuckets

func (h *VmHistogram) VisitNonZeroBuckets(f func(vmrange string, count uint64))

VisitNonZeroBuckets calls f for all buckets with non-zero counters.

vmrange contains "<start>...<end>" string with bucket bounds. The lower bound isn't included in the bucket, while the upper bound is included. This is required to be compatible with Prometheus-style histogram buckets with `le` (less or equal) labels.

Jump to

Keyboard shortcuts

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