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
340d55e1
Commit
340d55e1
authored
1 year ago
by
ensiouel
Browse files
Options
Downloads
Patches
Plain Diff
refactor(items): CacheMetrics перенесен в CachingMiddleware
parent
f3feb9ea
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
pkg/cache/metrics_cache.go
+0
-54
0 additions, 54 deletions
pkg/cache/metrics_cache.go
pkg/items/middleware/caching_middleware.go
+9
-1
9 additions, 1 deletion
pkg/items/middleware/caching_middleware.go
pkg/metrics/cache.go
+25
-0
25 additions, 0 deletions
pkg/metrics/cache.go
with
34 additions
and
55 deletions
pkg/cache/metrics_cache.go
deleted
100644 → 0
+
0
−
54
View file @
f3feb9ea
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"
})
)
type
metricsCache
struct
{
cache
Cache
cacheID
string
}
// WithMetrics возвращает обертку над кэшем, которая используется для отслеживания количества хитов и промахов в кэше.
// Помимо этого, в функцию WithMetrics передается параметр cacheID, который используется в метке метрик для идентификации конкретного кэша.
//
// Метрики записываются в prometheus.DefaultRegisterer
func
WithMetrics
(
cache
Cache
,
cacheID
string
)
Cache
{
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.
pkg/items/middleware/caching_middleware.go
+
9
−
1
View file @
340d55e1
...
...
@@ -7,17 +7,19 @@ import (
"git.perx.ru/perxis/perxis-go/pkg/cache"
envService
"git.perx.ru/perxis/perxis-go/pkg/environments"
service
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/metrics"
)
func
makeKey
(
ss
...
string
)
string
{
return
strings
.
Join
(
ss
,
"-"
)
}
func
CachingMiddleware
(
cache
,
cachePublished
cache
.
Cache
,
envs
envService
.
Environments
)
Middleware
{
func
CachingMiddleware
(
cache
,
cachePublished
cache
.
Cache
,
cacheMetrics
*
metrics
.
CacheMetrics
,
envs
envService
.
Environments
)
Middleware
{
return
func
(
next
service
.
Items
)
service
.
Items
{
return
&
cachingMiddleware
{
cache
:
cache
,
cachePublished
:
cachePublished
,
cacheMetrics
:
cacheMetrics
,
Items
:
next
,
envs
:
envs
,
}
...
...
@@ -27,6 +29,7 @@ func CachingMiddleware(cache, cachePublished cache.Cache, envs envService.Enviro
type
cachingMiddleware
struct
{
cache
cache
.
Cache
cachePublished
cache
.
Cache
cacheMetrics
*
metrics
.
CacheMetrics
envs
envService
.
Environments
service
.
Items
}
...
...
@@ -35,8 +38,11 @@ func (m cachingMiddleware) Get(ctx context.Context, spaceId, envId, collectionId
value
,
e
:=
m
.
cache
.
Get
(
makeKey
(
spaceId
,
envId
,
collectionId
,
itemId
))
if
e
==
nil
{
m
.
cacheMetrics
.
HitsTotal
.
WithLabelValues
(
"Items"
)
.
Inc
()
return
value
.
(
*
service
.
Item
),
err
}
m
.
cacheMetrics
.
MissesTotal
.
WithLabelValues
(
"Items"
)
.
Inc
()
itm
,
err
=
m
.
Items
.
Get
(
ctx
,
spaceId
,
envId
,
collectionId
,
itemId
,
options
...
)
if
err
==
nil
{
env
,
err
:=
m
.
envs
.
Get
(
ctx
,
itm
.
SpaceID
,
itm
.
EnvID
)
...
...
@@ -132,9 +138,11 @@ func (m cachingMiddleware) GetPublished(ctx context.Context, spaceId, envId, col
if
e
==
nil
{
value
:=
val
.
(
map
[
string
]
*
service
.
Item
)
if
i
,
ok
:=
value
[
opts
.
LocaleID
];
ok
{
m
.
cacheMetrics
.
HitsTotal
.
WithLabelValues
(
"Items"
)
.
Inc
()
return
i
,
nil
}
}
m
.
cacheMetrics
.
MissesTotal
.
WithLabelValues
(
"Items"
)
.
Inc
()
itm
,
err
=
m
.
Items
.
GetPublished
(
ctx
,
spaceId
,
envId
,
collectionId
,
itemId
,
opts
)
...
...
This diff is collapsed.
Click to expand it.
pkg/metrics/cache.go
0 → 100644
+
25
−
0
View file @
340d55e1
package
metrics
import
(
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
// CacheMetrics предоставляет метрики для отслеживания попаданий и промахов в кэш.
type
CacheMetrics
struct
{
HitsTotal
*
prometheus
.
CounterVec
MissesTotal
*
prometheus
.
CounterVec
}
func
NewCacheMetrics
(
registry
prometheus
.
Registerer
)
*
CacheMetrics
{
return
&
CacheMetrics
{
HitsTotal
:
promauto
.
With
(
registry
)
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
"cache_hits_total"
,
Help
:
"Количество попаданий в кэш."
,
},
[]
string
{
"cache_id"
}),
MissesTotal
:
promauto
.
With
(
registry
)
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Name
:
"cache_misses_total"
,
Help
:
"Количество пропусков в кэш."
,
},
[]
string
{
"cache_id"
}),
}
}
This diff is collapsed.
Click to expand it.
Semyon Krestyaninov
@krestyaninov
mentioned in commit
e8e363c0
·
1 year ago
mentioned in commit
e8e363c0
mentioned in commit e8e363c0b6c1c37b7283ca4da1097030e020428b
Toggle commit list
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