Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
3a6a7a30
Commit
3a6a7a30
authored
1 year ago
by
ensiouel
Browse files
Options
Downloads
Patches
Plain Diff
feature: добавлена метрика для кэша
parent
5c9b98fa
No related branches found
No related tags found
No related merge requests found
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
go.mod
+7
-0
7 additions, 0 deletions
go.mod
go.sum
+419
-0
419 additions, 0 deletions
go.sum
pkg/cache/metrics_cache.go
+56
-0
56 additions, 0 deletions
pkg/cache/metrics_cache.go
with
482 additions
and
0 deletions
go.mod
+
7
−
0
View file @
3a6a7a30
...
...
@@ -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.
Click to expand it.
go.sum
+
419
−
0
View file @
3a6a7a30
This diff is collapsed.
Click to expand it.
pkg/cache/metrics_cache.go
0 → 100644
+
56
−
0
View file @
3a6a7a30
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
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment