Skip to content
Snippets Groups Projects
Commit 404bff1d authored by ensiouel's avatar ensiouel
Browse files

refactor: сгенерирован metrics middleware для clients

parent 777ffd48
Branches
Tags
No related merge requests found
......@@ -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
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment