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
4d108729
Commit
4d108729
authored
1 year ago
by
ensiouel
Browse files
Options
Downloads
Patches
Plain Diff
refactor: правки по метрикам кеша
parent
f84ac843
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_middleware.go
+12
-39
12 additions, 39 deletions
pkg/cache/metrics_middleware.go
pkg/metrics/cache.go
+38
-0
38 additions, 0 deletions
pkg/metrics/cache.go
pkg/metrics/request.go
+4
-4
4 additions, 4 deletions
pkg/metrics/request.go
with
54 additions
and
43 deletions
pkg/cache/metrics_middleware.go
+
12
−
39
View file @
4d108729
...
...
@@ -6,49 +6,21 @@ import (
)
type
metricsMiddleware
struct
{
cache
Cache
hitsTotal
prometheus
.
Counter
missesTotal
prometheus
.
Counter
invalidatesTotal
prometheus
.
Counter
cache
Cache
cacheMetrics
*
metrics
.
CacheMetrics
serviceName
string
}
// MetricsMiddleware возвращает обертку над кэшем, которая используется для отслеживания количества хитов и промахов в кэше.
//
// subsystem указывает подсистему, к которой принадлежат метрики.
// Значение должно быть уникальным, совпадение разрешено только при совпадении ключей labels. Пустое значение допустимо.
//
// labels - список меток, где каждый элемент метки соответствует парам ключ-значение. Отсутствие допустимо.
// Значения меток должны быть уникальными в рамках одной subsystem.
//
// Метрики записываются в prometheus.DefaultRegisterer
func
MetricsMiddleware
(
cache
Cache
,
subsystem
string
,
labels
...
string
)
Cache
{
func
MetricsMiddleware
(
cache
Cache
,
cacheMetrics
*
metrics
.
CacheMetrics
,
serviceName
string
)
Cache
{
if
cache
==
nil
{
panic
(
"cannot wrap metrics in cache, cache is nil"
)
}
middleware
:=
&
metricsMiddleware
{
cache
:
cache
,
hitsTotal
:
prometheus
.
NewCounter
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"cache_hits_total"
,
Help
:
"Количество попаданий в кэш."
,
}),
missesTotal
:
prometheus
.
NewCounter
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"cache_misses_total"
,
Help
:
"Количество пропусков в кэш."
,
}),
invalidatesTotal
:
prometheus
.
NewCounter
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"cache_invalidates_total"
,
Help
:
"Количество инвалидаций кэша."
,
}),
return
&
metricsMiddleware
{
cache
:
cache
,
cacheMetrics
:
cacheMetrics
,
serviceName
:
serviceName
,
}
prometheus
.
WrapRegistererWith
(
metrics
.
GetLabelsFromKV
(
labels
),
prometheus
.
DefaultRegisterer
)
.
MustRegister
(
middleware
.
hitsTotal
,
middleware
.
missesTotal
,
middleware
.
invalidatesTotal
,
)
return
middleware
}
func
(
c
*
metricsMiddleware
)
Set
(
key
,
value
any
)
error
{
...
...
@@ -56,16 +28,17 @@ func (c *metricsMiddleware) Set(key, value any) error {
}
func
(
c
*
metricsMiddleware
)
Get
(
key
any
)
(
any
,
error
)
{
labels
:=
prometheus
.
Labels
{
"service"
:
c
.
serviceName
}
value
,
err
:=
c
.
cache
.
Get
(
key
)
if
err
!=
nil
{
c
.
missesTotal
.
Inc
()
c
.
cacheMetrics
.
MissesTotal
.
With
(
labels
)
.
Inc
()
return
nil
,
err
}
c
.
hitsTotal
.
Inc
()
c
.
cacheMetrics
.
HitsTotal
.
With
(
labels
)
.
Inc
()
return
value
,
nil
}
func
(
c
*
metricsMiddleware
)
Remove
(
key
any
)
error
{
c
.
i
nvalidatesTotal
.
Inc
()
c
.
cacheMetrics
.
I
nvalidatesTotal
.
With
(
prometheus
.
Labels
{
"service"
:
c
.
serviceName
})
.
Inc
()
return
c
.
cache
.
Remove
(
key
)
}
This diff is collapsed.
Click to expand it.
pkg/metrics/cache.go
0 → 100644
+
38
−
0
View file @
4d108729
package
metrics
import
"github.com/prometheus/client_golang/prometheus"
type
CacheMetrics
struct
{
HitsTotal
*
prometheus
.
CounterVec
MissesTotal
*
prometheus
.
CounterVec
InvalidatesTotal
*
prometheus
.
CounterVec
}
func
NewCacheMetrics
(
subsystem
string
)
*
CacheMetrics
{
labelNames
:=
[]
string
{
"service"
,
}
metrics
:=
&
CacheMetrics
{
HitsTotal
:
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"cache_hits_total"
,
Help
:
"Количество попаданий в кэш."
,
},
labelNames
),
MissesTotal
:
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"cache_misses_total"
,
Help
:
"Количество пропусков в кэш."
,
},
labelNames
),
InvalidatesTotal
:
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"cache_invalidates_total"
,
Help
:
"Количество инвалидаций кэша."
,
},
labelNames
),
}
prometheus
.
MustRegister
(
metrics
.
HitsTotal
,
metrics
.
MissesTotal
,
metrics
.
InvalidatesTotal
,
)
return
metrics
}
This diff is collapsed.
Click to expand it.
pkg/metrics/request.go
+
4
−
4
View file @
4d108729
...
...
@@ -13,7 +13,7 @@ type RequestMetrics struct {
// NewRequestMetrics возвращает метрики для подсчета количества удачных/неудачных запросов, а так же длительности ответов.
// Метрики записываются в prometheus.DefaultRegisterer
func
NewRequestMetrics
(
subsystem
string
)
*
RequestMetrics
{
requestL
abelNames
:=
[]
string
{
l
abelNames
:=
[]
string
{
"service"
,
"method"
,
}
...
...
@@ -22,18 +22,18 @@ func NewRequestMetrics(subsystem string) *RequestMetrics {
Subsystem
:
subsystem
,
Name
:
"requests_total"
,
Help
:
"Количество запросов."
,
},
requestL
abelNames
),
},
l
abelNames
),
FailedTotal
:
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"requests_failed_total"
,
Help
:
"Количество запросов, вернувших ошибку."
,
},
requestL
abelNames
),
},
l
abelNames
),
DurationSeconds
:
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Subsystem
:
subsystem
,
Name
:
"request_duration_seconds"
,
Help
:
"Длительность обработки запроса."
,
Buckets
:
prometheus
.
DefBuckets
,
},
requestL
abelNames
),
},
l
abelNames
),
}
prometheus
.
MustRegister
(
metrics
.
Total
,
...
...
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