Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collect ristretto cache metrics helper #834

Merged
merged 6 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions cachex/ristretto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright © 2025 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package cachex

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

// RistrettoCollector collects Ristretto cache metrics.
type RistrettoCollector[K ristretto.Key, V any] struct {
prefix string
metricsFunc func() *ristretto.Metrics
}

// NewRistrettoCollector creates a new RistrettoCollector.
//
// To use this collector, you need to register it with a Prometheus registry:
//
// func main() {
// cache, _ := ristretto.NewCache(&ristretto.Config{
// NumCounters: 1e7,
// MaxCost: 1 << 30,
// BufferItems: 64,
// })
// collector := NewRistrettoCollector("prefix_", func() *ristretto.Metrics {
// return cache.Metrics
// })
// prometheus.MustRegister(collector)
// }
func NewRistrettoCollector[K ristretto.Key, V any](prefix string, metricsFunc func() *ristretto.Metrics) *RistrettoCollector[K, V] {
return &RistrettoCollector[K, V]{
prefix: prefix,
metricsFunc: metricsFunc,
}
}

// Describe sends the super-set of all possible descriptors of metrics
// collected by this Collector.
func (c *RistrettoCollector[K, V]) Describe(ch chan<- *prometheus.Desc) {
ch <- prometheus.NewDesc(c.prefix+"ristretto_hits", "Total number of cache hits", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_misses", "Total number of cache misses", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_ratio", "Cache hit ratio", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_keys_added", "Total number of keys added to the cache", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_cost_added", "Total cost of keys added to the cache", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_keys_evicted", "Total number of keys evicted from the cache", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_cost_evicted", "Total cost of keys evicted from the cache", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_sets_dropped", "Total number of sets dropped", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_sets_rejected", "Total number of sets rejected", nil, nil)
ch <- prometheus.NewDesc(c.prefix+"ristretto_gets_kept", "Total number of gets kept", nil, nil)
}

// Collect is called by the Prometheus registry when collecting metrics.
func (c *RistrettoCollector[K, V]) Collect(ch chan<- prometheus.Metric) {
metrics := c.metricsFunc()
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_hits", "Total number of cache hits", nil, nil), prometheus.GaugeValue, float64(metrics.Hits()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_misses", "Total number of cache misses", nil, nil), prometheus.GaugeValue, float64(metrics.Misses()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_ratio", "Cache hit ratio", nil, nil), prometheus.GaugeValue, metrics.Ratio())
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_keys_added", "Total number of keys added to the cache", nil, nil), prometheus.GaugeValue, float64(metrics.KeysAdded()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_cost_added", "Total cost of keys added to the cache", nil, nil), prometheus.GaugeValue, float64(metrics.CostAdded()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_keys_evicted", "Total number of keys evicted from the cache", nil, nil), prometheus.GaugeValue, float64(metrics.KeysEvicted()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_cost_evicted", "Total cost of keys evicted from the cache", nil, nil), prometheus.GaugeValue, float64(metrics.CostEvicted()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_sets_dropped", "Total number of sets dropped", nil, nil), prometheus.GaugeValue, float64(metrics.SetsDropped()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_sets_rejected", "Total number of sets rejected", nil, nil), prometheus.GaugeValue, float64(metrics.SetsRejected()))
ch <- prometheus.MustNewConstMetric(prometheus.NewDesc(c.prefix+"ristretto_gets_kept", "Total number of gets kept", nil, nil), prometheus.GaugeValue, float64(metrics.GetsKept()))
}
32 changes: 20 additions & 12 deletions configx/provider_watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func assertNoOpenFDs(t require.TestingT, dir, name string) {
}
var b, be bytes.Buffer
// we are only interested in the file descriptors, so we use the `-F f` option
c := exec.Command("lsof", "-n", "-F", "f", "--", filepath.Join(dir, name))
c := exec.Command("lsof", "-n", "-F", "fc", "--", filepath.Join(dir, name))
c.Stdout = &b
c.Stderr = &be
exitErr := new(exec.ExitError)
Expand All @@ -61,13 +61,22 @@ func TestReload(t *testing.T) {
setup := func(t *testing.T, dir, name string, c chan<- struct{}, modifiers ...OptionModifier) (*Provider, *logrusx.Logger) {
l := logrusx.New("configx", "test")
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
t.Cleanup(func() {
close(c)
cancel()
assertNoOpenFDs(t, dir, name)
})
modifiers = append(modifiers,
WithLogrusWatcher(l),
WithLogger(l),
AttachWatcher(func(event watcherx.Event, err error) {
fmt.Printf("Received event: %+v error: %+v\n", event, err)
c <- struct{}{}
fmt.Printf("Received event: %+v %T error: %+v\n", event, event, err)
select {
case <-ctx.Done():
return
case c <- struct{}{}:
return
}
}),
WithContext(ctx),
)
Expand All @@ -79,12 +88,12 @@ func TestReload(t *testing.T) {
t.Run("case=rejects not validating changes", func(t *testing.T) {
t.Parallel()
dir, name := tmpConfigFile(t, "memory", "bar")
assertNoOpenFDs(t, dir, name)

c := make(chan struct{})
p, l := setup(t, dir, name, c)
hook := test.NewLocal(l.Entry.Logger)

assertNoOpenFDs(t, dir, name)

assert.Equal(t, []*logrus.Entry{}, hook.AllEntries())
assert.Equal(t, "memory", p.String("dsn"))
assert.Equal(t, "bar", p.String("foo"))
Expand Down Expand Up @@ -112,13 +121,12 @@ func TestReload(t *testing.T) {
t.Run("case=rejects to update immutable", func(t *testing.T) {
t.Parallel()
dir, name := tmpConfigFile(t, "memory", "bar")

c := make(chan struct{})
p, l := setup(t, dir, name, c,
WithImmutables("dsn"))
hook := test.NewLocal(l.Entry.Logger)

assertNoOpenFDs(t, dir, name)

assert.Equal(t, []*logrus.Entry{}, hook.AllEntries())
assert.Equal(t, "memory", p.String("dsn"))
assert.Equal(t, "bar", p.String("foo"))
Expand All @@ -133,10 +141,10 @@ func TestReload(t *testing.T) {
assert.Equal(t, "bar", p.String("foo"))

// but it is still watching the files
updateConfigFile(t, c, dir, name, "memory", "bar", "baz")
assert.Equal(t, "baz", p.String("bar"))

assertNoOpenFDs(t, dir, name)
updateConfigFile(t, c, dir, name, "memory", "foo", "bar")
assert.Equal(t, "bar", p.String("bar"))
assert.Equal(t, "foo", p.String("bar"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes sense to assert two different values for the same key here. Just revert the assertions here? If you run this test locally on a mac, it's also possible that it's a bit different from linux on the CI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derp accidental commit

assert.Equal(t, "memory", p.String("dsn"))
})

t.Run("case=allows to update excepted immutable", func(t *testing.T) {
Expand Down
Loading