Skip to content
Snippets Groups Projects
Commit 3a6a7a30 authored by ensiouel's avatar ensiouel
Browse files

feature: добавлена метрика для кэша

parent 5c9b98fa
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0
github.com/nats-io/nats.go v1.31.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.1
github.com/rs/xid v1.5.0
github.com/stretchr/testify v1.8.4
go.mongodb.org/mongo-driver v1.13.0
......@@ -32,6 +33,8 @@ require (
require (
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
......@@ -43,12 +46,16 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.3 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/nats-io/nkeys v0.4.6 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.30.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
......
This diff is collapsed.
package cache
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
cacheHitsTotal = promauto.With(prometheus.DefaultRegisterer).NewCounterVec(prometheus.CounterOpts{
Name: "cache_hits_total",
Help: "Количество попаданий в кэш.",
}, []string{"cache_id"})
cacheMissesTotal = promauto.With(prometheus.DefaultRegisterer).NewCounterVec(prometheus.CounterOpts{
Name: "cache_misses_total",
Help: "Количество пропусков в кэш.",
}, []string{"cache_id"})
)
var _ Cache = &MetricsCache{}
type MetricsCache struct {
cache Cache
cacheID string
}
// WithMetrics возвращает обертку над кэшем, которая используется для отслеживания количества хитов и промахов в кэше.
// Помимо этого, в функцию WithMetrics передается параметр cacheID, который используется в метке метрик для идентификации конкретного кэша.
//
// Метрики записываются в prometheus.DefaultRegisterer
func WithMetrics(cache Cache, cacheID string) *MetricsCache {
if cache == nil {
panic("cannot wrap metrics in cache, cache is nil")
}
return &MetricsCache{
cacheID: cacheID,
cache: cache,
}
}
func (c *MetricsCache) Set(key, value any) error {
return c.cache.Set(key, value)
}
func (c *MetricsCache) Get(key any) (any, error) {
value, err := c.cache.Get(key)
if err != nil {
cacheMissesTotal.WithLabelValues(c.cacheID).Inc()
return nil, err
}
cacheHitsTotal.WithLabelValues(c.cacheID).Inc()
return value, nil
}
func (c *MetricsCache) Remove(key any) error {
return c.cache.Remove(key)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment