ristretto_prometheus

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 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"
	"github.com/prometheus/client_golang/prometheus"

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

func main() {
    cache, err := ristretto.NewCache(&ristretto.Config{
		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,
		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 CacheMetricsProvider added in v0.2.0

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

CacheMetricsProvider is a metrics provider using a ristretto.Cache instance as source.

func NewCacheMetricsProvider added in v0.2.0

func NewCacheMetricsProvider(cache *ristretto.Cache) *CacheMetricsProvider

NewCacheMetricsProvider creates a metrics provider using the provided cache instance as source.

func (*CacheMetricsProvider) Provide added in v0.2.0

func (p *CacheMetricsProvider) Provide() *ristretto.Metrics

Provide is a MetricsProvider implementation. It simply returns the Metrics field from the intenal cache value.

type Collector

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

func NewCollector

func NewCollector(cache *ristretto.Cache, opts ...Option) (*Collector, error)

NewCollector returns a Prometheus metrics collector using metrics from the provided cache instance.

Example
package main

import (
	"fmt"
	"strings"

	"github.com/dgraph-io/ristretto"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/testutil"

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

func main() {
	cache, err := ristretto.NewCache(&ristretto.Config{
		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,
		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 NewMetricsCollector added in v0.2.0

func NewMetricsCollector(provider MetricsProvider, opts ...Option) (*Collector, error)

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

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 MetricsProvider added in v0.2.0

type MetricsProvider func() *ristretto.Metrics

MetricsProvider is a functor contract for cache metrics

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