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
404bff1d
Commit
404bff1d
authored
1 year ago
by
ensiouel
Browse files
Options
Downloads
Patches
Plain Diff
refactor: сгенерирован metrics middleware для clients
parent
777ffd48
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/clients/middleware/metrics_middleware.go
+73
-63
73 additions, 63 deletions
pkg/clients/middleware/metrics_middleware.go
with
73 additions
and
63 deletions
pkg/clients/middleware/metrics_middleware.go
+
73
−
63
View file @
404bff1d
...
...
@@ -8,6 +8,7 @@ package middleware
import
(
"context"
"strings"
"git.perx.ru/perxis/perxis-go/pkg/clients"
"git.perx.ru/perxis/perxis-go/pkg/metrics"
...
...
@@ -16,6 +17,7 @@ import (
// metricsMiddleware implements clients.Clients that is instrumented with metrics
type
metricsMiddleware
struct
{
serviceName
string
requestMetrics
*
metrics
.
RequestMetrics
next
clients
.
Clients
}
...
...
@@ -24,6 +26,7 @@ type metricsMiddleware struct {
func
MetricsMiddleware
(
requestMetrics
*
metrics
.
RequestMetrics
)
Middleware
{
return
func
(
next
clients
.
Clients
)
clients
.
Clients
{
return
&
metricsMiddleware
{
serviceName
:
strings
.
ToLower
(
"Clients"
),
requestMetrics
:
requestMetrics
,
next
:
next
,
}
...
...
@@ -32,91 +35,98 @@ func MetricsMiddleware(requestMetrics *metrics.RequestMetrics) Middleware {
// Create implements clients.Clients
func
(
m
metricsMiddleware
)
Create
(
ctx
context
.
Context
,
client
*
clients
.
Client
)
(
created
*
clients
.
Client
,
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"Create"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"Create"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"Create"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
created
,
err
=
m
.
next
.
Create
(
ctx
,
client
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"Create"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
Create
(
ctx
,
client
)
return
}
// Delete implements clients.Clients
func
(
m
metricsMiddleware
)
Delete
(
ctx
context
.
Context
,
spaceId
string
,
id
string
)
(
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"Delete"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"Delete"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"Delete"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
err
=
m
.
next
.
Delete
(
ctx
,
spaceId
,
id
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"Delete"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
Delete
(
ctx
,
spaceId
,
id
)
return
}
// Enable implements clients.Clients
func
(
m
metricsMiddleware
)
Enable
(
ctx
context
.
Context
,
spaceId
string
,
id
string
,
enable
bool
)
(
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"Enable"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"Enable"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"Enable"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
err
=
m
.
next
.
Enable
(
ctx
,
spaceId
,
id
,
enable
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"Enable"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
Enable
(
ctx
,
spaceId
,
id
,
enable
)
return
}
// Get implements clients.Clients
func
(
m
metricsMiddleware
)
Get
(
ctx
context
.
Context
,
spaceId
string
,
id
string
)
(
client
*
clients
.
Client
,
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"Get"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"Get"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"Get"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
client
,
err
=
m
.
next
.
Get
(
ctx
,
spaceId
,
id
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"Get"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
Get
(
ctx
,
spaceId
,
id
)
return
}
// GetBy implements clients.Clients
func
(
m
metricsMiddleware
)
GetBy
(
ctx
context
.
Context
,
spaceId
string
,
params
*
clients
.
GetByParams
)
(
client
*
clients
.
Client
,
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"GetBy"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"GetBy"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"GetBy"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
client
,
err
=
m
.
next
.
GetBy
(
ctx
,
spaceId
,
params
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"GetBy"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
GetBy
(
ctx
,
spaceId
,
params
)
return
}
// List implements clients.Clients
func
(
m
metricsMiddleware
)
List
(
ctx
context
.
Context
,
spaceId
string
)
(
clients
[]
*
clients
.
Client
,
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"List"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"List"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"List"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
clients
,
err
=
m
.
next
.
List
(
ctx
,
spaceId
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"List"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
List
(
ctx
,
spaceId
)
return
}
// Update implements clients.Clients
func
(
m
metricsMiddleware
)
Update
(
ctx
context
.
Context
,
client
*
clients
.
Client
)
(
err
error
)
{
m
.
requestMetrics
.
Total
.
WithLabelValues
(
"Update"
)
.
Inc
()
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
WithLabelValues
(
"Update"
))
defer
func
()
{
timer
.
ObserveDuration
()
labels
:=
prometheus
.
Labels
{
"service"
:
m
.
serviceName
,
"method"
:
"Update"
}
timer
:=
prometheus
.
NewTimer
(
m
.
requestMetrics
.
DurationSeconds
.
With
(
labels
))
defer
timer
.
ObserveDuration
()
m
.
requestMetrics
.
Total
.
With
(
labels
)
.
Inc
()
err
=
m
.
next
.
Update
(
ctx
,
client
)
if
err
!=
nil
{
m
.
requestMetrics
.
FailedTotal
.
WithLabelValues
(
"Update"
)
.
Inc
()
m
.
requestMetrics
.
FailedTotal
.
With
(
labels
)
.
Inc
()
return
}
}()
return
m
.
next
.
Update
(
ctx
,
client
)
return
}
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