From a381442291368d9c224fd2de192f247342dec51a Mon Sep 17 00:00:00 2001 From: Trim21 Date: Mon, 25 Nov 2024 23:02:51 +0800 Subject: [PATCH] fix --- internal/metrics/metrics.go | 1 + internal/tag/cache_repo.go | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index 9756532f..4d9d7d76 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -23,6 +23,7 @@ func init() { prometheus.MustRegister(RequestCount) prometheus.MustRegister(RequestHistogram) prometheus.MustRegister(SQLHistogram) + } var RequestCount = prometheus.NewCounter(prometheus.CounterOpts{ diff --git a/internal/tag/cache_repo.go b/internal/tag/cache_repo.go index 74c6ab70..fd6a64c2 100644 --- a/internal/tag/cache_repo.go +++ b/internal/tag/cache_repo.go @@ -18,6 +18,7 @@ import ( "context" "time" + "github.com/prometheus/client_golang/prometheus" "github.com/samber/lo" "github.com/trim21/errgo" "go.uber.org/zap" @@ -44,6 +45,25 @@ type cachedTags struct { Tags []Tag } +var CacheCount = prometheus.NewCounter(prometheus.CounterOpts{ + Subsystem: "chii", + Name: "request_cached_count", + Help: "", + ConstLabels: map[string]string{"repo": "meta_tags"}, +}) + +var NotCacheCount = prometheus.NewCounter(prometheus.CounterOpts{ + Subsystem: "chii", + Name: "request_not_cached_count", + Help: "", + ConstLabels: map[string]string{"repo": "meta_tags"}, +}) + +func init() { + prometheus.MustRegister(CacheCount) + prometheus.MustRegister(NotCacheCount) +} + func (r cacheRepo) Get(ctx context.Context, id model.SubjectID) ([]Tag, error) { var key = cachekey.SubjectMetaTag(id) @@ -54,8 +74,10 @@ func (r cacheRepo) Get(ctx context.Context, id model.SubjectID) ([]Tag, error) { } if ok { + CacheCount.Add(1) return s.Tags, nil } + NotCacheCount.Add(1) tags, err := r.repo.Get(ctx, id) if err != nil { @@ -86,6 +108,7 @@ func (r cacheRepo) GetByIDs(ctx context.Context, ids []model.SubjectID) (map[mod return nil, errgo.Wrap(err, "cache.MGet") } + CacheCount.Add(float64(len(tags))) for _, tag := range tags { result[tag.ID] = tag.Tags } @@ -97,6 +120,7 @@ func (r cacheRepo) GetByIDs(ctx context.Context, ids []model.SubjectID) (map[mod } } + NotCacheCount.Add(float64(len(missing))) missingFromCache, err := r.repo.GetByIDs(ctx, missing) if err != nil { return nil, err