ristretto_prometheus

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 3 Imported by: 0

README

ristretto-prometheus

Go Doc

Prometheus Collector for Ristretto Cache metrics

Usage

Example
package main

import (
	"github.com/dgraph-io/ristretto/v2"
	"github.com/prometheus/client_golang/prometheus"

	ristretto_prometheus "github.com/wolfmetr/ristretto-prometheus"
)

func main() {
	cache, err := ristretto.NewCache[string, string](&ristretto.Config[string, string]{
		NumCounters: 1e3,
		MaxCost:     1 << 30,
		BufferItems: 64,
		Metrics:     true, // enable ristretto metrics
	})
	if err != nil {
		panic(err)
	}
	defer cache.Close()

	ristrettoCollector, err := ristretto_prometheus.NewCollector(
		cache.Metrics,
		ristretto_prometheus.WithNamespace("appname"),
		ristretto_prometheus.WithSubsystem("subsystemname"),
		ristretto_prometheus.WithConstLabels(prometheus.Labels{"app_version": "v1.2.3"}),
		ristretto_prometheus.WithHitsCounterMetric(),
		ristretto_prometheus.WithMissesCounterMetric(),
		ristretto_prometheus.WithMetric(ristretto_prometheus.Desc{
			Name:      "ristretto_keys_added_total",
			Help:      "The number of added keys in the cache.",
			ValueType: prometheus.CounterValue,
			Extractor: func(m *ristretto.Metrics) float64 { return float64(m.KeysAdded()) },
		}),
		ristretto_prometheus.WithHitsRatioGaugeMetric(),
	)
	if err != nil {
		panic(err)
	}
	prometheus.MustRegister(ristrettoCollector)
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrDuplicateMetricName = errors.New("duplicate metric name")

Functions

This section is empty.

Types

type Collector

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

func NewMetricsCollector added in v0.2.0

func NewMetricsCollector(source *ristretto.Metrics, opts ...Option) (*Collector, error)

NewMetricsCollector returns a Prometheus metrics collector using metrics from the given provider.

Example
cache, err := ristretto.NewCache[string, string](&ristretto.Config[string, string]{
	NumCounters: 1e3,
	MaxCost:     1 << 30,
	BufferItems: 64,
	Metrics:     true, // enable ristretto metrics
})
if err != nil {
	panic(err)
}
defer cache.Close()

ristrettoCollector, err := ristretto_prometheus.NewMetricsCollector(
	cache.Metrics,
	ristretto_prometheus.WithNamespace("appname"),
	ristretto_prometheus.WithSubsystem("subsystemname"),
	ristretto_prometheus.WithConstLabels(prometheus.Labels{"app_version": "v1.2.3"}),
	ristretto_prometheus.WithHitsCounterMetric(),
	ristretto_prometheus.WithMissesCounterMetric(),
	ristretto_prometheus.WithMetric(ristretto_prometheus.Desc{
		Name:      "ristretto_keys_added_total",
		Help:      "The number of added keys in the cache.",
		ValueType: prometheus.CounterValue,
		Extractor: func(m *ristretto.Metrics) float64 { return float64(m.KeysAdded()) },
	}),
	ristretto_prometheus.WithHitsRatioGaugeMetric(),
)
if err != nil {
	panic(err)
}
prometheus.MustRegister(ristrettoCollector)

// fill the cache
for i := 0; i < 123; i++ {
	_ = cache.Set(
		fmt.Sprintf("key%d", i),
		fmt.Sprintf("val%d", i),
		1,
	)
}

// wait for value to pass through buffers
cache.Wait()

// generate hits
for i := 50; i < 99; i++ {
	key := fmt.Sprintf("key%d", i)
	if _, ok := cache.Get(key); !ok {
		panic("expected key:" + key)
	}
}

// generate misses
for i := 150; i < 170; i++ {
	key := fmt.Sprintf("key%d", i)
	if _, ok := cache.Get(key); ok {
		panic("unexpected key:" + key)
	}
}

expected := `
		# HELP appname_subsystemname_ristretto_hits_ratio The percentage of successful Get calls (hits).
		# TYPE appname_subsystemname_ristretto_hits_ratio gauge
		appname_subsystemname_ristretto_hits_ratio{app_version="v1.2.3"} 0.7101449275362319
		# HELP appname_subsystemname_ristretto_hits_total The number of Get calls where a value was found for the corresponding key.
		# TYPE appname_subsystemname_ristretto_hits_total counter
		appname_subsystemname_ristretto_hits_total{app_version="v1.2.3"} 49
		# HELP appname_subsystemname_ristretto_keys_added_total The number of added keys in the cache.
		# TYPE appname_subsystemname_ristretto_keys_added_total counter
		appname_subsystemname_ristretto_keys_added_total{app_version="v1.2.3"} 123
		# HELP appname_subsystemname_ristretto_misses_total The number of Get calls where a value was not found for the corresponding key.
		# TYPE appname_subsystemname_ristretto_misses_total counter
		appname_subsystemname_ristretto_misses_total{app_version="v1.2.3"} 20
	`

if err := testutil.CollectAndCompare(ristrettoCollector, strings.NewReader(expected)); err != nil {
	panic(fmt.Sprintf("unexpected collecting result:\n%s", err))
}
Output:

func (Collector) Collect

func (c Collector) Collect(ch chan<- prometheus.Metric)

func (Collector) Describe

func (c Collector) Describe(ch chan<- *prometheus.Desc)

type Desc

type Desc struct {
	Name      string
	Help      string
	ValueType prometheus.ValueType

	// Extractor is a function that can extract metric from ristretto.Metrics
	Extractor MetricValueExtractor
}

Desc describes metric and function for extract value for it

type MetricValueExtractor

type MetricValueExtractor func(m *ristretto.Metrics) float64

type Option

type Option func(*config)

func WithConstLabels

func WithConstLabels(constLabels prometheus.Labels) Option

func WithCostAddedMetric added in v0.1.1

func WithCostAddedMetric() Option

func WithCostEvictedMetric added in v0.1.1

func WithCostEvictedMetric() Option

func WithGetsDroppedMetric added in v0.1.1

func WithGetsDroppedMetric() Option

func WithGetsKeptMetric added in v0.1.1

func WithGetsKeptMetric() Option

func WithHitsCounterMetric

func WithHitsCounterMetric() Option

func WithHitsRatioGaugeMetric

func WithHitsRatioGaugeMetric() Option

func WithKeysAddedMetric added in v0.1.1

func WithKeysAddedMetric() Option

func WithKeysEvictedMetric added in v0.1.1

func WithKeysEvictedMetric() Option

func WithKeysUpdatedMetric added in v0.1.1

func WithKeysUpdatedMetric() Option

func WithMetric

func WithMetric(d Desc) Option

func WithMissesCounterMetric

func WithMissesCounterMetric() Option

func WithNamespace

func WithNamespace(namespace string) Option

func WithSetsDroppedMetric added in v0.1.1

func WithSetsDroppedMetric() Option

func WithSetsRejectedMetric added in v0.1.1

func WithSetsRejectedMetric() Option

func WithSubsystem

func WithSubsystem(subsystem string) Option

Jump to

Keyboard shortcuts

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