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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
1290c552
Commit
1290c552
authored
1 year ago
by
Alena Petraki
Browse files
Options
Downloads
Patches
Plain Diff
Предложения по рефакторингу метрик
parent
ff64e0fd
Branches
feature/PRXS-1219-PrometheusMetrics-Suggestion
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
pkg/items/middleware/metrics_middleware.go
+5
-1
5 additions, 1 deletion
pkg/items/middleware/metrics_middleware.go
pkg/metrics/request.go
+20
-9
20 additions, 9 deletions
pkg/metrics/request.go
with
25 additions
and
10 deletions
pkg/items/middleware/metrics_middleware.go
+
5
−
1
View file @
1290c552
...
...
@@ -8,6 +8,7 @@ package middleware
import
(
"context"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/items"
"git.perx.ru/perxis/perxis-go/pkg/metrics"
...
...
@@ -17,6 +18,7 @@ import (
// metricsMiddleware implements items.Items that is instrumented with metrics
type
metricsMiddleware
struct
{
serviceName
string
requestMetrics
*
metrics
.
RequestMetrics
next
items
.
Items
}
...
...
@@ -25,6 +27,7 @@ type metricsMiddleware struct {
func
MetricsMiddleware
(
requestMetrics
*
metrics
.
RequestMetrics
)
Middleware
{
return
func
(
next
items
.
Items
)
items
.
Items
{
return
&
metricsMiddleware
{
serviceName
:
strings
.
ToLower
(
"Items"
),
// эта строка для генерации по шаблону для всех сервисов, там ServiceName с заглавной буквы
requestMetrics
:
requestMetrics
,
next
:
next
,
}
...
...
@@ -35,7 +38,8 @@ func MetricsMiddleware(requestMetrics *metrics.RequestMetrics) Middleware {
func
(
m
metricsMiddleware
)
Aggregate
(
ctx
context
.
Context
,
spaceId
string
,
envId
string
,
collectionId
string
,
filter
*
items
.
Filter
,
options
...*
items
.
AggregateOptions
)
(
result
map
[
string
]
interface
{},
err
error
)
{
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"Aggregate"
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"Aggregate"
)
.
Inc
()
m
.
requestMetrics
.
Total
.
With
(
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"Aggregate"
})
.
Inc
()
// тут как больше нравится. Мне нравится более явно
// m.requestMetrics.Total.WithLabelValues(m.serviceName, "Aggregate").Inc()
result
,
err
=
m
.
next
.
Aggregate
(
ctx
,
spaceId
,
envId
,
collectionId
,
filter
,
options
...
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"Aggregate"
)
.
Inc
()
...
...
This diff is collapsed.
Click to expand it.
pkg/metrics/request.go
+
20
−
9
View file @
1290c552
...
...
@@ -4,12 +4,26 @@ import (
"github.com/prometheus/client_golang/prometheus"
)
var
(
RequestLabels
=
[]
string
{
"service"
,
"method"
,
}
)
type
RequestMetrics
struct
{
Total
*
prometheus
.
CounterVec
FailedTotal
*
prometheus
.
CounterVec
DurationSeconds
*
prometheus
.
HistogramVec
}
// Пример использования:
// ...
// requestMetrics := metrics.NewRequestMetrics("content")
// s.Items = itemsMiddleware.MetricsMiddleware(requestMetrics)
// s.Collections = collectionsMiddleware.MetricsMiddleware(requestMetrics)
// ...
// NewRequestMetrics возвращает метрики для подсчета количества удачных/неудачных запросов, а так же длительности ответов.
//
// subsystem указывает подсистему, к которой принадлежат метрики.
...
...
@@ -19,29 +33,26 @@ type RequestMetrics struct {
// Значения меток должны быть уникальными в рамках одной subsystem.
//
// Метрики записываются в prometheus.DefaultRegisterer
func
NewRequestMetrics
(
subsystem
string
,
durationBuckets
[]
float64
,
labels
...
string
)
*
RequestMetrics
{
if
len
(
durationBuckets
)
==
0
{
durationBuckets
=
prometheus
.
DefBuckets
}
func
NewRequestMetrics
(
subsystem
string
)
*
RequestMetrics
{
metrics
:=
&
RequestMetrics
{
Total
:
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"service_requests_total"
,
Help
:
"Количество запросов."
,
},
[]
string
{
"method"
}
),
},
RequestLabels
),
FailedTotal
:
prometheus
.
NewCounterVec
(
prometheus
.
CounterOpts
{
Subsystem
:
subsystem
,
Name
:
"service_requests_failed_total"
,
Help
:
"Количество запросов, вернувших ошибку."
,
},
[]
string
{
"method"
}
),
},
RequestLabels
),
DurationSeconds
:
prometheus
.
NewHistogramVec
(
prometheus
.
HistogramOpts
{
Subsystem
:
subsystem
,
Name
:
"service_request_duration_seconds"
,
Help
:
"Длительность обработки запроса."
,
Buckets
:
duration
Buckets
,
},
[]
string
{
"method"
}
),
Buckets
:
prometheus
.
Def
Buckets
,
},
RequestLabels
),
}
prometheus
.
WrapRegistererWith
(
GetLabelsFromKV
(
labels
),
prometheus
.
DefaultRegisterer
)
.
MustRegister
(
prometheus
.
MustRegister
(
metrics
.
Total
,
metrics
.
FailedTotal
,
metrics
.
DurationSeconds
,
...
...
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